Tools__MethodRewriter.st
changeset 17004 04522250f6ce
parent 16514 0a071f68ea56
child 17136 cb908d2ba02e
child 17883 88acb0d3c7b1
equal deleted inserted replaced
17003:bec342932427 17004:04522250f6ce
   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'.
  1134 
  1182 
  1135     "Created: / 12-12-2007 / 11:14:25 / janfrog"
  1183     "Created: / 12-12-2007 / 11:14:25 / janfrog"
  1136 !
  1184 !
  1137 
  1185 
  1138 removeSeletedClassesAction
  1186 removeSeletedClassesAction
  1139 
  1187     |toRemove|
  1140     self classes removeAll: self selectedClasses.
  1188 
  1141     classes changed:#content.
  1189     toRemove := self selectedClasses.
  1142     self classesChanged.
  1190     toRemove notEmptyOrNil ifTrue:[
  1143 
  1191         self classes removeAll: self selectedClasses.
       
  1192         classes changed:#content.
       
  1193         self classesChanged.
       
  1194     ].
       
  1195     
  1144     "Created: / 12-12-2007 / 12:05:15 / janfrog"
  1196     "Created: / 12-12-2007 / 12:05:15 / janfrog"
  1145 !
  1197 !
  1146 
  1198 
  1147 useAsRuleAction
  1199 useAsRuleAction
  1148     self adHocFindAndReplaceVisibleHolder value:false.
  1200     self adHocFindAndReplaceVisibleHolder value:false.
  1343 classesChanged
  1395 classesChanged
  1344     cachedMethodsFromClasses := nil. "/ flush
  1396     cachedMethodsFromClasses := nil. "/ flush
  1345     methods := nil.
  1397     methods := nil.
  1346 
  1398 
  1347     self classesInfoTextHolder
  1399     self classesInfoTextHolder
  1348 	value:(((classes size == 1) ifTrue:['%1 class / %2 method(s)'] ifFalse:['%1 classes / %2 methods'])
  1400         value:(((classes size == 1) 
  1349 		    bindWith:classes size
  1401                     ifTrue:['%1 class / %2 method(s)'] 
  1350 		    with:self methods size)
  1402                     ifFalse:['%1 classes / %2 methods'])
       
  1403                         bindWith:classes size
       
  1404                         with:self methods size)
  1351 !
  1405 !
  1352 
  1406 
  1353 selectedRuleIndexChanged
  1407 selectedRuleIndexChanged
  1354     |selectedRuleSpec|
  1408     |selectedRuleSpec|
  1355 
  1409 
  1504 doSearchRule: rule withResultDo: block
  1558 doSearchRule: rule withResultDo: block
  1505 
  1559 
  1506     | matchingMethods searcher currentMethod|
  1560     | matchingMethods searcher currentMethod|
  1507 
  1561 
  1508     rule isNil ifTrue:[
  1562     rule isNil ifTrue:[
  1509 	Dialog warn:'No rule selected.'.
  1563         Dialog warn:'No rule selected.'.
  1510 	AbortSignal raise
  1564         AbortSignal raise
  1511     ].
  1565     ].
  1512 
  1566 
  1513     searcher := ParseTreeSearcher new.
  1567     searcher := ParseTreeSearcher new.
  1514     rule rewriteRule searches do:[:eachSearch |
  1568     rule rewriteRule searches do:[:eachSearch |
  1515 	searcher
  1569         searcher
  1516 	    matchesTree:eachSearch searchTree
  1570             matchesTree:eachSearch searchTree
  1517 	    do:[:aNode :answer |
  1571             do:[:aNode :answer |
  1518 		(eachSearch canMatch: aNode) ifTrue:[
  1572                 (eachSearch canMatch: aNode) ifTrue:[
  1519 		    matchingMethods add:currentMethod
  1573                     matchingMethods add:currentMethod
  1520 		]
  1574                 ]
  1521 	    ]
  1575             ]
  1522     ].
  1576     ].
       
  1577     searcher computeQuickSearchStrings.
  1523 
  1578 
  1524     matchingMethods := Set new.
  1579     matchingMethods := Set new.
  1525     self
  1580     self
  1526 	withMethodsDo:[:mthd|
  1581         withMethodsDo:[:mthd|
  1527 	    | tree |
  1582             | tree |
  1528 
  1583 
  1529 	    tree := mthd parseTree.
  1584             (searcher canQuicklyReject:mthd source) ifFalse:[
  1530 	    tree
  1585                 tree := mthd parseTree.
  1531 		ifNil:[
  1586                 tree
  1532 		    Transcript showCR:'MethodRewriter: parse tree error in ',mthd whoString.
  1587                     ifNil:[
  1533 		    self breakPoint: #jv
  1588                         Transcript showCR:'MethodRewriter: parse tree error in ',mthd whoString.
  1534 		]
  1589                         self breakPoint: #jv
  1535 		ifNotNil:[
  1590                     ]
  1536 		    currentMethod := mthd.
  1591                     ifNotNil:[
  1537 		    searcher executeTree: tree
  1592                         currentMethod := mthd.
  1538 		]
  1593                         searcher executeTree: tree
  1539 	]
  1594                     ]
  1540 	finallyDo:[
  1595             ]
  1541 	    block value: matchingMethods
  1596         ]
  1542 	]
  1597         finallyDo:[
       
  1598             block value: matchingMethods
       
  1599         ]
  1543 
  1600 
  1544     "Created: / 12-12-2007 / 10:34:50 / janfrog"
  1601     "Created: / 12-12-2007 / 10:34:50 / janfrog"
  1545     "Modified: / 07-04-2011 / 22:02:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1602     "Modified: / 07-04-2011 / 22:02:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  1546 !
  1603 !
  1547 
  1604 
  1644 !
  1701 !
  1645 
  1702 
  1646 withMethods: methods do: methodBlock finallyDo: finallyBlock
  1703 withMethods: methods do: methodBlock finallyDo: finallyBlock
  1647 
  1704 
  1648     [
  1705     [
  1649 	|numMethods|
  1706         |numMethods|
  1650 
  1707 
  1651 	numMethods := methods size.
  1708         numMethods := methods size.
  1652 
  1709 
  1653 	self actionInProgress: true.
  1710         self actionInProgress: true.
  1654 	self progress: 0.
  1711         self progress: 0.
  1655 
  1712 
  1656 	methods asArray keysAndValuesDo:[:idx :mth|
  1713         methods asArray keysAndValuesDo:[:idx :mth|
  1657 	    numMethods > 100 ifTrue:[
  1714             |mclass|
  1658 		self info: mth mclass name.
  1715 
  1659 	    ] ifFalse:[
  1716             mclass := mth mclass.
  1660 		self info: mth selector storeString.
  1717             mclass isNil ifTrue:[
  1661 	    ].
  1718                 Transcript showCR:'method no longer valid in ',mth getMclass printString.
  1662 	    methodBlock value: mth.
  1719             ] ifFalse:[    
  1663 	    self progress: ((100 / methods size) * idx) rounded
  1720                 numMethods > 100 ifTrue:[
  1664 	].
  1721                     self info: mclass name.
  1665 	finallyBlock value
  1722                 ] ifFalse:[
       
  1723                     self info: mth selector storeString.
       
  1724                 ].
       
  1725                 methodBlock value: mth.
       
  1726                 self progress: ((100 / methods size) * idx) rounded
       
  1727             ].
       
  1728         ].
       
  1729         finallyBlock value
  1666     ] ensure:[
  1730     ] ensure:[
  1667 	"/self actionInProgress: false.
  1731         "/self actionInProgress: false.
  1668 	self progress: 0.
  1732         self progress: 0.
  1669 	self info: ''
  1733         self info: ''
  1670     ]
  1734     ]
  1671 
  1735 
  1672     "Created: / 12-12-2007 / 10:32:16 / janfrog"
  1736     "Created: / 12-12-2007 / 10:32:16 / janfrog"
  1673 !
  1737 !
  1674 
  1738