Tools__MethodRewriter.st
branchjv
changeset 17136 cb908d2ba02e
parent 16571 cf319f2e56d0
parent 17004 04522250f6ce
child 18226 346376844040
equal deleted inserted replaced
17135:81b78926f09a 17136:cb908d2ba02e
   214      Tools::MethodRewriter new openInterface:#classesSpec
   214      Tools::MethodRewriter new openInterface:#classesSpec
   215     "
   215     "
   216 
   216 
   217     <resource: #canvas>
   217     <resource: #canvas>
   218 
   218 
   219     ^
   219     ^ 
   220     #(FullSpec
   220     #(FullSpec
   221        name: classesSpec
   221        name: classesSpec
   222        window:
   222        window: 
   223       (WindowSpec
   223       (WindowSpec
   224 	 label: 'Classes'
   224          label: 'Classes'
   225 	 name: 'Classes'
   225          name: 'Classes'
   226 	 min: (Point 10 10)
   226          min: (Point 10 10)
   227 	 bounds: (Rectangle 0 0 300 300)
   227          bounds: (Rectangle 0 0 300 300)
   228        )
   228        )
   229        component:
   229        component: 
   230       (SpecCollection
   230       (SpecCollection
   231 	 collection: (
   231          collection: (
   232 	  (LabelSpec
   232           (LabelSpec
   233 	     label: 'Hint: drag''n''drop classes, methods or packages from browser'
   233              label: 'Hint: drag''n''drop classes, methods or packages from browser'
   234 	     name: 'HintLabel'
   234              name: 'HintLabel'
   235 	     layout: (LayoutFrame 0 0 0 0 0 1 30 0)
   235              layout: (LayoutFrame 0 0 0 0 0 1 30 0)
   236 	     translateLabel: true
   236              translateLabel: true
   237 	     adjust: left
   237              adjust: left
   238 	   )
   238            )
   239 	  (SelectionInListModelViewSpec
   239           (SelectionInListModelViewSpec
   240 	     name: 'ClassesList'
   240              name: 'ClassesList'
   241 	     layout: (LayoutFrame 0 0 30 0 0 1 -60 1)
   241              layout: (LayoutFrame 0 0 30 0 0 1 -60 1)
   242 	     model: selectedClassesHolder
   242              model: selectedClassesHolder
   243 	     hasHorizontalScrollBar: true
   243              hasHorizontalScrollBar: true
   244 	     hasVerticalScrollBar: true
   244              hasVerticalScrollBar: true
   245 	     listModel: classes
   245              listModel: classes
   246 	     multipleSelectOk: true
   246              multipleSelectOk: true
   247 	     useIndex: false
   247              useIndex: false
   248 	     highlightMode: line
   248              highlightMode: line
   249 	     properties:
   249              properties: 
   250 	    (PropertyListDictionary
   250             (PropertyListDictionary
   251 	       canDropSelector: canDropClasses:
   251                canDropSelector: canDropClasses:
   252 	       dropArgument: nil
   252                dropArgument: nil
   253 	       dropSelector: doDropClasses:
   253                dropSelector: doDropClasses:
   254 	       dragArgument: nil
   254                dragArgument: nil
   255 	     )
   255              )
   256 	   )
   256            )
   257 	  (LabelSpec
   257           (LabelSpec
   258 	     name: 'ClassInfoLabel'
   258              name: 'ClassInfoLabel'
   259 	     layout: (LayoutFrame 0 0 -60 1 0 1 -30 1)
   259              layout: (LayoutFrame 0 0 -60 1 0 1 -34 1)
   260 	     translateLabel: true
   260              translateLabel: true
   261 	     labelChannel: classesInfoTextHolder
   261              labelChannel: classesInfoTextHolder
   262 	     adjust: left
   262              adjust: left
   263 	   )
   263            )
   264 	  (HorizontalPanelViewSpec
   264           (HorizontalPanelViewSpec
   265 	     name: 'HorizontalPanel1'
   265              name: 'HorizontalPanel1'
   266 	     layout: (LayoutFrame 0 0 -30 1 -16 1 0 1)
   266              layout: (LayoutFrame 0 0 -34 1 0 1 0 1)
   267 	     horizontalLayout: center
   267              horizontalLayout: center
   268 	     verticalLayout: center
   268              verticalLayout: center
   269 	     horizontalSpace: 3
   269              horizontalSpace: 3
   270 	     verticalSpace: 3
   270              verticalSpace: 3
   271 	     component:
   271              component: 
   272 	    (SpecCollection
   272             (SpecCollection
   273 	       collection: (
   273                collection: (
   274 		(ActionButtonSpec
   274                 (ActionButtonSpec
   275 		   label: 'Remove Selected Classes'
   275                    label: 'Add All Your Packages'
   276 		   name: 'RemoveClassesButton'
   276                    name: 'AddAllYourPackagesButton'
   277 		   translateLabel: true
   277                    translateLabel: true
   278 		   model: removeSeletedClassesAction
   278                    model: addAllYourPackagesAction
   279 		   useDefaultExtent: true
   279                    useDefaultExtent: true
   280 		 )
   280                  )
   281 		)
   281                 (ActionButtonSpec
   282 
   282                    label: 'Add All Packages'
   283 	     )
   283                    name: 'AddAllPackagesButton'
   284 	     keepSpaceForOSXResizeHandleH: true
   284                    translateLabel: true
   285 	   )
   285                    model: addAllPackagesAction
   286 	  )
   286                    useDefaultExtent: true
   287 
   287                  )
       
   288                 (ActionButtonSpec
       
   289                    label: 'Remove Selected Classes'
       
   290                    name: 'RemoveSelectedClassesButton'
       
   291                    translateLabel: true
       
   292                    model: removeSeletedClassesAction
       
   293                    useDefaultExtent: true
       
   294                  )
       
   295                 )
       
   296               
       
   297              )
       
   298              keepSpaceForOSXResizeHandleH: true
       
   299            )
       
   300           )
       
   301         
   288        )
   302        )
   289      )
   303      )
   290 !
   304 !
   291 
   305 
   292 howToUseSpec
   306 howToUseSpec
   887     ^ classes ifNil:[classes := List new]
   901     ^ classes ifNil:[classes := List new]
   888 
   902 
   889     "Created: / 20-07-2007 / 16:31:18 / janfrog"
   903     "Created: / 20-07-2007 / 16:31:18 / janfrog"
   890 !
   904 !
   891 
   905 
   892 classes: aCollection
   906 classes:aCollectionOfClasses
   893     self classes contents:((aCollection collect:[:cls|cls theNonMetaclass])
   907     |answer classesToAdd unloadedClasses|
   894 			    sortBySelector:#name).
   908 
       
   909     classesToAdd := aCollectionOfClasses.
       
   910     
       
   911     unloadedClasses := classesToAdd reject:[:cls | cls isLoaded].
       
   912     unloadedClasses notEmpty ifTrue:[
       
   913         answer := Dialog confirmWithCancel:(resources stringWithCRs:'Some classes are unloaded.\\Load them now?').
       
   914         answer isNil ifTrue:[ AbortSignal raise. ].
       
   915         answer == true ifTrue:[
       
   916             unloadedClasses do:[:each | each autoload].
       
   917         ] ifFalse:[
       
   918             classesToAdd := classesToAdd select:[:cls | cls isLoaded].
       
   919         ].    
       
   920     ].
       
   921     classesToAdd := (classesToAdd collect:[:cls|cls theNonMetaclass])
       
   922                         asOrderedCollection sortBySelector:#name.
       
   923     
       
   924     self classes contents:classesToAdd.
   895     self classesChanged
   925     self classesChanged
   896 
   926 
   897 
   927 
   898     "Created: / 20-07-2007 / 16:31:18 / janfrog"
   928     "Created: / 20-07-2007 / 16:31:18 / janfrog"
   899     "Modified: / 12-12-2007 / 09:47:01 / janfrog"
   929     "Modified: / 12-12-2007 / 09:47:01 / janfrog"
  1030 
  1060 
  1031     "Created: / 12-12-2007 / 11:17:55 / janfrog"
  1061     "Created: / 12-12-2007 / 11:17:55 / janfrog"
  1032 ! !
  1062 ! !
  1033 
  1063 
  1034 !MethodRewriter methodsFor:'actions'!
  1064 !MethodRewriter methodsFor:'actions'!
       
  1065 
       
  1066 addAllPackagesAction
       
  1067     |classes|
       
  1068 
       
  1069     classes := Smalltalk allClasses.
       
  1070     self classes: classes
       
  1071 !
       
  1072 
       
  1073 addAllYourPackagesAction
       
  1074     |classes|
       
  1075 
       
  1076     classes := Smalltalk 
       
  1077                     allClassesForWhich:[:cls | 
       
  1078                         cls isNameSpace not
       
  1079                         and:[ ((cls package ?'') startsWith:'stx:') not]
       
  1080                     ].
       
  1081     self classes: classes
       
  1082 !
  1035 
  1083 
  1036 doRewrite: methodsMatching
  1084 doRewrite: methodsMatching
  1037     | changes compositeChangeCollector |
  1085     | changes compositeChangeCollector |
  1038 
  1086 
  1039     changes := ChangeSet new name:'Rewrite Changes'.
  1087     changes := ChangeSet new name:'Rewrite Changes'.
  1135 
  1183 
  1136     "Created: / 12-12-2007 / 11:14:25 / janfrog"
  1184     "Created: / 12-12-2007 / 11:14:25 / janfrog"
  1137 !
  1185 !
  1138 
  1186 
  1139 removeSeletedClassesAction
  1187 removeSeletedClassesAction
  1140 
  1188     |toRemove|
  1141     self classes removeAll: self selectedClasses.
  1189 
  1142     classes changed:#content.
  1190     toRemove := self selectedClasses.
  1143     self classesChanged.
  1191     toRemove notEmptyOrNil ifTrue:[
  1144 
  1192         self classes removeAll: self selectedClasses.
       
  1193         classes changed:#content.
       
  1194         self classesChanged.
       
  1195     ].
       
  1196     
  1145     "Created: / 12-12-2007 / 12:05:15 / janfrog"
  1197     "Created: / 12-12-2007 / 12:05:15 / janfrog"
  1146 !
  1198 !
  1147 
  1199 
  1148 useAsRuleAction
  1200 useAsRuleAction
  1149     self adHocFindAndReplaceVisibleHolder value:false.
  1201     self adHocFindAndReplaceVisibleHolder value:false.
  1344 classesChanged
  1396 classesChanged
  1345     cachedMethodsFromClasses := nil. "/ flush
  1397     cachedMethodsFromClasses := nil. "/ flush
  1346     methods := nil.
  1398     methods := nil.
  1347 
  1399 
  1348     self classesInfoTextHolder
  1400     self classesInfoTextHolder
  1349 	value:(((classes size == 1) ifTrue:['%1 class / %2 method(s)'] ifFalse:['%1 classes / %2 methods'])
  1401         value:(((classes size == 1) 
  1350 		    bindWith:classes size
  1402                     ifTrue:['%1 class / %2 method(s)'] 
  1351 		    with:self methods size)
  1403                     ifFalse:['%1 classes / %2 methods'])
       
  1404                         bindWith:classes size
       
  1405                         with:self methods size)
  1352 !
  1406 !
  1353 
  1407 
  1354 selectedRuleIndexChanged
  1408 selectedRuleIndexChanged
  1355     |selectedRuleSpec|
  1409     |selectedRuleSpec|
  1356 
  1410 
  1505 doSearchRule: rule withResultDo: block
  1559 doSearchRule: rule withResultDo: block
  1506 
  1560 
  1507     | matchingMethods searcher currentMethod|
  1561     | matchingMethods searcher currentMethod|
  1508 
  1562 
  1509     rule isNil ifTrue:[
  1563     rule isNil ifTrue:[
  1510 	Dialog warn:'No rule selected.'.
  1564         Dialog warn:'No rule selected.'.
  1511 	AbortSignal raise
  1565         AbortSignal raise
  1512     ].
  1566     ].
  1513 
  1567 
  1514     searcher := ParseTreeSearcher new.
  1568     searcher := ParseTreeSearcher new.
  1515     rule rewriteRule searches do:[:eachSearch |
  1569     rule rewriteRule searches do:[:eachSearch |
  1516 	searcher
  1570         searcher
  1517 	    matchesTree:eachSearch searchTree
  1571             matchesTree:eachSearch searchTree
  1518 	    do:[:aNode :answer |
  1572             do:[:aNode :answer |
  1519 		(eachSearch canMatch: aNode) ifTrue:[
  1573                 (eachSearch canMatch: aNode) ifTrue:[
  1520 		    matchingMethods add:currentMethod
  1574                     matchingMethods add:currentMethod
  1521 		]
  1575                 ]
  1522 	    ]
  1576             ]
  1523     ].
  1577     ].
       
  1578     searcher computeQuickSearchStrings.
  1524 
  1579 
  1525     matchingMethods := Set new.
  1580     matchingMethods := Set new.
  1526     self
  1581     self
  1527 	withMethodsDo:[:mthd|
  1582         withMethodsDo:[:mthd|
  1528 	    | tree |
  1583             | tree |
  1529 
  1584 
  1530 	    tree := mthd parseTree.
  1585             (searcher canQuicklyReject:mthd source) ifFalse:[
  1531 	    tree
  1586                 tree := mthd parseTree.
  1532 		ifNil:[
  1587                 tree
  1533 		    Transcript showCR:'MethodRewriter: parse tree error in ',mthd whoString.
  1588                     ifNil:[
  1534 		    self breakPoint: #jv
  1589                         Transcript showCR:'MethodRewriter: parse tree error in ',mthd whoString.
  1535 		]
  1590                         self breakPoint: #jv
  1536 		ifNotNil:[
  1591                     ]
  1537 		    currentMethod := mthd.
  1592                     ifNotNil:[
  1538 		    searcher executeTree: tree
  1593                         currentMethod := mthd.
  1539 		]
  1594                         searcher executeTree: tree
  1540 	]
  1595                     ]
  1541 	finallyDo:[
  1596             ]
  1542 	    block value: matchingMethods
  1597         ]
  1543 	]
  1598         finallyDo:[
       
  1599             block value: matchingMethods
       
  1600         ]
  1544 
  1601 
  1545     "Created: / 12-12-2007 / 10:34:50 / janfrog"
  1602     "Created: / 12-12-2007 / 10:34:50 / janfrog"
  1546     "Modified: / 07-04-2011 / 22:02:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1603     "Modified: / 07-04-2011 / 22:02:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1547 !
  1604 !
  1548 
  1605 
  1645 !
  1702 !
  1646 
  1703 
  1647 withMethods: methods do: methodBlock finallyDo: finallyBlock
  1704 withMethods: methods do: methodBlock finallyDo: finallyBlock
  1648 
  1705 
  1649     [
  1706     [
  1650 	|numMethods|
  1707         |numMethods|
  1651 
  1708 
  1652 	numMethods := methods size.
  1709         numMethods := methods size.
  1653 
  1710 
  1654 	self actionInProgress: true.
  1711         self actionInProgress: true.
  1655 	self progress: 0.
  1712         self progress: 0.
  1656 
  1713 
  1657 	methods asArray keysAndValuesDo:[:idx :mth|
  1714         methods asArray keysAndValuesDo:[:idx :mth|
  1658 	    numMethods > 100 ifTrue:[
  1715             |mclass|
  1659 		self info: mth mclass name.
  1716 
  1660 	    ] ifFalse:[
  1717             mclass := mth mclass.
  1661 		self info: mth selector storeString.
  1718             mclass isNil ifTrue:[
  1662 	    ].
  1719                 Transcript showCR:'method no longer valid in ',mth getMclass printString.
  1663 	    methodBlock value: mth.
  1720             ] ifFalse:[    
  1664 	    self progress: ((100 / methods size) * idx) rounded
  1721                 numMethods > 100 ifTrue:[
  1665 	].
  1722                     self info: mclass name.
  1666 	finallyBlock value
  1723                 ] ifFalse:[
       
  1724                     self info: mth selector storeString.
       
  1725                 ].
       
  1726                 methodBlock value: mth.
       
  1727                 self progress: ((100 / methods size) * idx) rounded
       
  1728             ].
       
  1729         ].
       
  1730         finallyBlock value
  1667     ] ensure:[
  1731     ] ensure:[
  1668 	"/self actionInProgress: false.
  1732         "/self actionInProgress: false.
  1669 	self progress: 0.
  1733         self progress: 0.
  1670 	self info: ''
  1734         self info: ''
  1671     ]
  1735     ]
  1672 
  1736 
  1673     "Created: / 12-12-2007 / 10:32:16 / janfrog"
  1737     "Created: / 12-12-2007 / 10:32:16 / janfrog"
  1674     "Modified: / 26-08-2015 / 16:43:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1738     "Modified: / 26-08-2015 / 16:43:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1675 !
  1739 !