home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / browser.st < prev    next >
Text File  |  1992-02-16  |  12KB  |  420 lines

  1. "======================================================================
  2. |
  3. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  4. | Written by Steve Byrne.
  5. |
  6. | This file is part of GNU Smalltalk.
  7. |
  8. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  9. | under the terms of the GNU General Public License as published by the Free
  10. | Software Foundation; either version 1, or (at your option) any later version.
  11. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  12. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. | details.
  15. | You should have received a copy of the GNU General Public License along with
  16. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  17. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  18. |
  19.  ======================================================================"
  20.  
  21. "
  22. |     Change Log
  23. | ============================================================================
  24. | Author       Date       Change 
  25. | sbb         16 Feb 92      created a while ago
  26. |
  27. "
  28.  
  29. Object subclass: #Browser
  30.        instanceVariableNames: ''
  31.        classVariableNames: ''
  32.        poolDictionaries: ''
  33.        category: 'Class browser'
  34. !
  35.  
  36. !Browser class methodsFor: 'browsing'!
  37.  
  38. startEmacsMessage
  39.     stdout nextPut: (Character value: 2)
  40. !
  41.  
  42. finishEmacsMessage
  43.     nil                "does nothing for now"
  44. !
  45.  
  46. withGcOff: aBlock
  47.     | oldFlag |
  48.     oldFlag _ Smalltalk gcMessage: false.
  49.     aBlock value.
  50.     Smalltalk gcMessage: oldFlag
  51. !
  52.  
  53. emacsFunction: funcName on: aBlock
  54.     self withGcOff:
  55.     [ self startEmacsMessage.
  56.       stdout nextPut: $(;
  57.           nextPutAll: funcName; nl.
  58.       aBlock value.
  59.       stdout nextPut: $); nl.    
  60.           self finishEmacsMessage ]
  61. !
  62.  
  63. emacsListFunction: funcName on: aBlock
  64.     self emacsFunction: funcName on:
  65.     [ stdout nextPutAll: '''('; nl.
  66.       aBlock value.
  67.       stdout nextPut: $) ]
  68. !
  69.  
  70. oldShowInstanceMethods: class
  71.     | methods |
  72.     methods _ self getMethods: class.
  73.     self withGcOff:
  74.     [ self startEmacsMessage.
  75.       stdout nextPutAll: '(smalltalk-browse "instance methods"'; nl;
  76.           nextPutAll: '''test-func'; nl;
  77.           nextPutAll: 't'; nl;
  78.           nextPutAll: '''('; nl.
  79.       class allSelectors do:
  80.           [ :sel | stdout nextPutAll: '("';
  81.                            print: sel;
  82.                            nextPutAll: '" . "';
  83.                    print: sel;
  84.                       nextPutAll: '")'; nl ].
  85.           stdout nextPutAll: ')'; nl.    
  86.           self finishEmacsMessage ]
  87. !
  88.  
  89. showMethods: class for: methodType 
  90.     | methods |
  91.     "Experimental version"
  92.     methods _ class newGetMethods.    
  93.     self emacsFunction: 'smalltalk-browse' on: 
  94.     [ stdout nextPut: $";
  95.           nextPutAll: methodType;
  96.           nextPutAll: ' methods"'; nl;
  97.           nextPutAll: '''test-func'; nl;
  98.           nextPutAll: 't'; nl;
  99.           nextPutAll: '''('; nl.
  100.       methods associationsDo:
  101.           [ :sel | sel value value methodSourceFile notNil 
  102.                ifTrue: 
  103.                    [ stdout nextPutAll: '("'; 
  104.                      print: sel key; tab; tab;
  105.                      print: sel value key;
  106.                      nextPutAll: '" . ("'; 
  107.                      nextPutAll: sel value value methodSourceFile;
  108. nextPutAll: '" . ';
  109.                      nextPutAll: sel value value methodSourcePos printString;
  110.                      nextPut: $).
  111.                  stdout nextPut: $); nl ] 
  112.                ifFalse:
  113.                    [ stdout nextPutAll: '("'; 
  114.                      print: sel key; tab; tab;
  115.                      print: sel value key;
  116.                      nextPutAll: '" . ("'; 
  117.                      nextPutAll: sel value value methodSourceString
  118.                      nextPut: $).
  119.                  stdout nextPut: $); nl ]
  120.                ].
  121.           stdout nextPut: $) ]
  122. !
  123.     
  124. showDirectMethods: class inBuffer: bufferName
  125.     | methods |
  126.     "Experimental version"
  127.     methods _ class getDirectMethods.
  128.     self browseMethods: methods forClass: class inBuffer: bufferName .
  129. !
  130.  
  131. showAllMethods: class inBuffer: bufferName
  132.     | methods |
  133.     "Experimental version"
  134.     methods _ class getAllMethods.
  135.     self browseMethods: methods forClass: class inBuffer: bufferName .
  136. !
  137.  
  138. showIndirectMethods: class inBuffer: bufferName
  139.     | methods |
  140.     "Experimental version"
  141.     methods _ class getIndirectMethods.
  142.     self browseMethods: methods forClass: class inBuffer: bufferName.
  143. !
  144.  
  145. getAllSelectors: selector inBuffer: bufferName
  146.     | methods |
  147.     methods _ self getMethodsFor: selector.
  148.     self browseMethods: methods forClass: Object inBuffer: bufferName.
  149. !    
  150.  
  151. browseMethods: methods forClass: class inBuffer: bufferName
  152.     self emacsFunction: 'smalltalk-method-browse' on:
  153.     [ stdout nextPut: $";
  154.           nextPutAll: bufferName;
  155.           nextPutAll: '" ''('; nl.
  156.       methods associationsDo:
  157.           [ :sel | sel value value methodSourceFile notNil 
  158.                ifTrue:
  159.                    [ stdout nextPutAll: '("'; 
  160.                      print: sel key; 
  161.                      nextPutAll: '" . ("'; 
  162.                      nextPutAll: sel value value methodSourceFile;
  163.                      nextPutAll: '" . ';
  164.                      nextPutAll: sel value value methodSourcePos printString;
  165.                      nextPutAll: '))'; nl ] 
  166.                ifFalse:
  167.                    [ stdout nextPutAll: '("'; 
  168.                      print: sel key; 
  169.                      nextPutAll: '" . ("'; 
  170.                      print: class;
  171.                      nextPutAll: '" "';
  172.                      nextPutAll: sel value value methodCategory;
  173.                      nextPut: $"; nl;
  174.                      nextPut: $";
  175.                      nextPutAll: sel value value methodSourceString;
  176.                      nextPutAll: '")'.
  177.                  stdout nextPut: $); nl ]
  178.                ].
  179.       stdout nextPutAll: ')'; nl ]
  180. !
  181.  
  182. oldShowMethods: class for: methodType
  183.     | methods |
  184.     methods _ class getMethods.    
  185.     self withGcOff:
  186.     [ self startEmacsMessage.
  187.       stdout nextPutAll: '(smalltalk-browse "';
  188.           nextPutAll: methodType;
  189.           nextPutAll: ' methods"'; nl;
  190.           nextPutAll: '''test-func'; nl;
  191.           nextPutAll: 't'; nl;
  192.           nextPutAll: '''('; nl.
  193.       methods associationsDo:
  194.           [ :sel | sel value methodSourceFile notNil ifTrue:
  195.                [ stdout nextPutAll: '("'; 
  196.                      print: sel key;
  197.                                  nextPutAll: '" . ("'; 
  198.                  nextPutAll: sel value methodSourceFile;
  199.                  nextPutAll: '" . ';
  200.                  nextPutAll: sel value methodSourcePos printString;
  201.                  nextPutAll: ')'.
  202.                  stdout nextPutAll: ')'; nl ] ].
  203.           stdout nextPutAll: '))'; nl.    
  204.           self finishEmacsMessage ]
  205. !
  206.  
  207. oldloadClassNames
  208.     self withGcOff:
  209.         [ self startEmacsMessage. 
  210.           stdout nextPutAll: '(smalltalk-set-class-names ''('; nl. 
  211.       Object withAllSubclasses do: 
  212.           [ :class | class name == nil
  213.                  ifFalse: [ stdout nextPutAll: class name; nl. ]
  214.                  ].
  215.       stdout nextPutAll: '))'.
  216.           self finishEmacsMessage ]
  217. !
  218.  
  219. loadClassNames
  220.     self emacsListFunction: 'smalltalk-set-class-names' on:
  221.     [ Smalltalk associationsDo:
  222.           [ :assoc | (assoc value isKindOf: Behavior)
  223.                  ifTrue: [ stdout nextPutAll: assoc key; nl ]
  224.                  ] 
  225.           ]
  226. !
  227.  
  228. selectors
  229.     | md |
  230.     self emacsListFunction: 'smalltalk-set-all-methods' on:
  231.     [ Smalltalk associationsDo:
  232.           [ :assoc | (assoc value isKindOf: Behavior)
  233.                  ifTrue: 
  234.                  [ (md _ assoc value methodDictionary)
  235.                        isNil ifFalse:
  236.                        [ md keysDo:
  237.                          " also spit out class methods"
  238.                          [ :key | stdout nextPut: $";
  239.                                   print: key;
  240.                                   nextPut: $"; nl 
  241.                                   ]
  242.                          ]
  243.                        ]
  244.                  ]
  245.           ]
  246. !
  247.  
  248. browseHierarchy
  249.     self emacsListFunction: 'smalltalk-hier-browser' on: 
  250.     [ Object printHierarchy ]
  251. !
  252.  
  253. testMethods: aClass for: methodType
  254.     | classes methods md |
  255.     classes _ (aClass allSuperclasses). 
  256.     classes addFirst: aClass.
  257.     self withGcOff:
  258.     [ self startEmacsMessage.
  259.       stdout nextPutAll: '(smalltalk-fast-browse "';
  260.           nextPutAll: methodType;
  261.           nextPutAll: ' methods"'; nl;
  262.           nextPutAll: '''test-func'; nl;
  263.           nextPutAll: '''('; nl.
  264.       classes do:
  265.           [ :cl | md _ cl methodDictionary.
  266.               md notNil ifTrue:
  267.               [ md associationsDo: 
  268.                 [ :meth | stdout nextPutAll: '("';
  269.                           nextPutAll: meth key;
  270.                           nextPutAll: '" . ("'; 
  271.                           nextPutAll: meth value methodSourceFile;
  272.                           nextPutAll: '" . ';
  273.                           nextPutAll: meth value methodSourcePos printString;
  274.                           nextPutAll: '))';
  275.                           nl ]
  276.                 ]
  277.               ].
  278.       stdout nextPutAll: '))'.
  279.       self finishEmacsMessage ].
  280. !!
  281.  
  282. !Behavior methodsFor: 'browsing'!
  283.  
  284. methodDictionary
  285.     ^methodDictionary
  286. !
  287.  
  288. getMethods
  289.     | classes methods md |
  290.     methods _ Dictionary new.
  291.     self allSuperclasses reverseDo:
  292.     [ :superclass | md _ superclass methodDictionary.
  293.             md notNil ifTrue: 
  294.                 [ md associationsDo:
  295.                   [ :assoc | methods add: assoc ] ] ].
  296.     methodDictionary notNil ifTrue: 
  297.     [ methodDictionary associationsDo:
  298.           [ :assoc | methods add: assoc ] ].
  299.     ^methods
  300. !
  301.  
  302. newGetMethods
  303.     | classes methods md b |
  304.     methods _ Dictionary new.
  305.     b _ [ :md :class | md associationsDo:
  306.           [ :assoc | methods 
  307.                  add: (Association key: assoc key 
  308.                            value: (Association 
  309.                                    key: class 
  310.                                    value: assoc value)) ] ].
  311.     self allSuperclasses reverseDo:
  312.     [ :superclass | md _ superclass methodDictionary.
  313.             md notNil ifTrue: 
  314.                 [ b value: md value: superclass ] ].
  315.     methodDictionary notNil ifTrue: 
  316.     [ b value: methodDictionary value: self ].
  317.     ^methods
  318. !
  319.  
  320. getIndirectMethods
  321.     | classes methods md b |
  322.     methods _ Dictionary new.
  323.     b _ [ :md :class | md associationsDo:
  324.           [ :assoc | methods 
  325.                  add: (assoc key ->
  326.                        (class -> assoc value)) ] ].
  327.     self allSuperclasses reverseDo:
  328.     [ :superclass | md _ superclass methodDictionary.
  329.             md notNil ifTrue: 
  330.                 [ b value: md value: superclass ] ].
  331.     ^methods
  332. !
  333.  
  334. getAllMethods
  335.     | classes methods md b |
  336.     methods _ Dictionary new.
  337.     b _ [ :md :class | md associationsDo:
  338.           [ :assoc | methods 
  339.                  add: (assoc key ->
  340.                         (class -> assoc value)) ] ].
  341.     classes _ self allSuperclasses.
  342.     classes addFirst: self.
  343.     classes reverseDo:
  344.     [ :superclass | md _ superclass methodDictionary.
  345.             md notNil ifTrue: 
  346.                 [ b value: md value: superclass ] ].
  347.     ^methods
  348. !
  349.  
  350. getDirectMethods
  351.     | classes methods md b |
  352.     methods _ Dictionary new.
  353.     b _ [ :md :class | md associationsDo:
  354.           [ :assoc | methods 
  355.                  add: (Association key: assoc key 
  356.                            value: (Association 
  357.                                    key: class 
  358.                                    value: assoc value)) ] ].
  359.     methodDictionary notNil ifTrue: 
  360.     [ b value: methodDictionary value: self ].
  361.     ^methods
  362. !
  363.  
  364. getMethodsFor: aSelector
  365.     | methods dict elt b |
  366.     methods _ Dictionary new.
  367.     b _ [ :subclass | dict _ subclass methodDictionary.
  368.               dict notNil ifTrue:
  369.               [ elt _ dict at: aSelector
  370.                        ifAbsent: [ nil ].
  371.                 elt notNil ifTrue:
  372.                 [ methods add: 
  373.                       (subclass -> 
  374.                        (subclass -> elt)) ]
  375.                 ]
  376.               ].
  377.  
  378.     Object allSubclassesDo: b.
  379.     b value: Object.
  380.     ^methods
  381. ! !
  382.  
  383. !Behavior methodsFor: 'hierarchy browsing'!
  384.  
  385. printHierarchy
  386.     "I my entire subclassclass hierarchy on the terminal."
  387.     self printSubclasses: 0    
  388. !!
  389.  
  390. !Behavior methodsFor: 'private'!
  391.  
  392. printSubclasses: level
  393.     "I print my name, and then all my subclasses, each indented according
  394.      to its position in the hierarchy."
  395.     | mySubclasses |
  396.     stdout nextPutAll: '("'.
  397.     stdout print: self name;
  398.     nextPutAll: '" . ';
  399.     print: level;
  400.     nextPutAll: ')'; nl.
  401.     mySubclasses _ self subclasses asSortedCollection:
  402.                         [ :a :b | (a name isNil or: [ b name isNil ])
  403.                                       ifTrue: [ true ]
  404.                               ifFalse: [ a name <= b name ] ].
  405.     mySubclasses do:
  406.         [ :subclass | subclass class ~~ Metaclass
  407.                     ifTrue: [ subclass printSubclasses: level + 1 ] ]
  408. !
  409.  
  410. indentToLevel: level
  411.     level timesRepeat:
  412.         [ stdout next: (self hierarchyIndent) put: Character space ]
  413. !
  414.  
  415. hierarchyIndent
  416.     ^4
  417. !!
  418.