better comment in CustomNamespace >> execute
authorJakub Nesveda <jakubnesveda@seznam.cz>
Sun, 19 Oct 2014 14:55:14 +0200
changeset 704 340954b48789
parent 703 300ac48af954
child 705 1eb0878dc85c
better comment in CustomNamespace >> execute fix replace on wrong code selection ( replace only if found match ) in CustomRefactoryBuilder
CustomCodeSelectionToResourceTranslationTests.st
CustomNamespace.st
CustomRefactoryBuilder.st
CustomReplaceIfNilWithIfTrueRefactoringTests.st
patches/patches.rc
refactoring_custom.rc
--- a/CustomCodeSelectionToResourceTranslationTests.st	Sun Oct 19 14:08:55 2014 +0200
+++ b/CustomCodeSelectionToResourceTranslationTests.st	Sun Oct 19 14:55:14 2014 +0200
@@ -118,6 +118,77 @@
     "Created: / 15-10-2014 / 09:45:08 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
+test_code_selection_replaced_by_resource_translation_with_keep_package
+    | expectedSource originalSource codeSelection class method compiledMethod |
+
+    originalSource := 'selector
+    self information: self theInformation.
+    ^ self.'.
+
+    class := model createClassImmediate: 'DummyClassForTestCase01' instanceVariableNames: 'resources'.
+    method := model createMethodImmediate: class source: originalSource.
+    method package: #some_package.
+
+    codeSelection := CustomSourceCodeSelection new.
+    codeSelection
+        currentSourceCode: originalSource;
+        selectedClass: class;
+        selectedInterval: (32 to: 51);
+        selectedMethod: (class compiledMethodAt: #selector);
+        selectedSelector: #selector.
+
+    context selectedCodes: (Array with: codeSelection).  
+    generatorOrRefactoring executeInContext: context.  
+
+    expectedSource := 'selector
+    self information:(resources string:self theInformation).
+    ^ self.'.
+
+    self assertMethodSource:expectedSource atSelector:#selector forClass:class.
+
+    compiledMethod := class compiledMethodAt: #selector.
+
+    self assert: #some_package = (compiledMethod package).
+
+    "Created: / 16-10-2014 / 21:46:23 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+test_code_selection_replaced_by_resource_translation_with_wrong_selection
+    | expectedSource originalSource codeSelection class |
+
+    originalSource := 'method: arg
+
+    arg ifNil: [ 
+        self warn: ''nil''.
+    ]
+    ifNotNil: [ self information: ''info'' ]'.
+
+    class := model createClassImmediate: 'DummyClassForTestCase01' instanceVariableNames: 'resources'.
+    model createMethodImmediate: class source: originalSource.
+
+    codeSelection := CustomSourceCodeSelection new.
+    codeSelection
+        currentSourceCode: originalSource;
+        selectedClass: class;
+        selectedInterval: (8 to: 10);
+        selectedMethod: (class compiledMethodAt: #method:);
+        selectedSelector: #selector.
+
+    context selectedCodes: (Array with: codeSelection).  
+    generatorOrRefactoring executeInContext: context.  
+
+    expectedSource := 'method: arg
+
+    arg ifNil: [ 
+        self warn: ''nil''.
+    ]
+    ifNotNil: [ self information: ''info'' ]'.
+
+    self assertMethodSource:expectedSource atSelector:#method: forClass:class
+
+    "Created: / 17-10-2014 / 22:31:16 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
 test_not_available_in_class_perspective
     | perspective |
 
--- a/CustomNamespace.st	Sun Oct 19 14:08:55 2014 +0200
+++ b/CustomNamespace.st	Sun Oct 19 14:55:14 2014 +0200
@@ -229,16 +229,15 @@
 !CustomNamespace methodsFor:'compiling'!
 
 execute
-    "
-    Most likely as compile method, but without undo change
-    in global RefactoryChangeManager
-    (true for CustomLocalChangeManager changeManager implementation)
-    "
+    "Performs code changes ( add method, add class, rename class... )
+    so they take in effect ( method is added, class is renamed, ... )
+    with respect to current change manager implementatin - see CustomChangeManager subclasses."
 
     changeManager performChange: changes
 
     "Created: / 27-04-2014 / 16:30:05 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
     "Modified: / 21-09-2014 / 22:34:58 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified (comment): / 19-10-2014 / 14:30:45 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 redoChanges
--- a/CustomRefactoryBuilder.st	Sun Oct 19 14:08:55 2014 +0200
+++ b/CustomRefactoryBuilder.st	Sun Oct 19 14:55:14 2014 +0200
@@ -78,15 +78,14 @@
 !CustomRefactoryBuilder methodsFor:'compiling'!
 
 execute
-    "
-    Most likely as compile method, but without undo change
-    in global RefactoryChangeManager
-    (true for CustomLocalChangeManager changeManager implementation)
-    "
+    "Performs code changes ( add method, add class, rename class... )
+    so they take in effect ( method is added, class is renamed, ... )
+    with respect to current change manager implementatin - see CustomChangeManager subclasses."
 
     changeManager performChange: self change
 
     "Created: / 15-08-2014 / 00:45:10 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified (comment): / 19-10-2014 / 14:30:56 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 redoChanges
@@ -138,36 +137,40 @@
 
 executeReplace: searchPattern with: rewritePattern inCodeSelection: aCodeSelection
 
-    | newTree change parseTree selectedParseTree newSelectedParseTree |
+    | newTree change parseTree selectedParseTree newSelectedParseTree foundMatch rewriter |
 
     selectedParseTree := self parseExpression: aCodeSelection selectedSourceCode.
 
-    "/ Transcript showCR: 'SS: ', selectedParseTree asString.  
-    "/ Transcript showCR: 'SC: ', aCodeSelection selectedSourceCode.  
-
     newSelectedParseTree := (CustomParseTreeRewriter new)
         replace: searchPattern with: rewritePattern;
         executeTree: selectedParseTree;
         tree.
 
-    "/ Transcript showCR: 'NSS: ', newSelectedParseTree asString.
+    parseTree := self parseMethod: aCodeSelection currentSourceCode.
 
-    parseTree := self parseMethod: aCodeSelection currentSourceCode.
-    newTree := CustomParseTreeRewriter 
+    rewriter := CustomParseTreeRewriter new.
+
+    foundMatch := rewriter
         replace: aCodeSelection selectedSourceCode
-        with: (formatter formatParseTree: newSelectedParseTree)
-        in: parseTree
-        onInterval: aCodeSelection selectedInterval.
+            with: (formatter formatParseTree: newSelectedParseTree)
+            when: [:aNode | aNode intersectsInterval: aCodeSelection selectedInterval];
+        executeTree: parseTree.
+
+    foundMatch ifTrue: [ 
+        newTree := rewriter tree.
 
-    change := InteractiveAddMethodChange
-        compile: (formatter formatParseTree: (self parseMethod: newTree newSource))
-        in: aCodeSelection selectedMethod mclass 
-        classified: aCodeSelection selectedMethod category.
+        change := InteractiveAddMethodChange
+            compile: (formatter formatParseTree: (self parseMethod: newTree newSource))
+            in: aCodeSelection selectedMethod mclass 
+            classified: aCodeSelection selectedMethod category.
 
-    changes add: change
+        change package: aCodeSelection selectedMethod package.  
+
+        changes add: change
+    ].
 
     "Created: / 24-08-2014 / 10:24:52 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
-    "Modified: / 31-08-2014 / 17:17:01 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 17-10-2014 / 23:14:16 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 executeReplace: searchPattern with: rewritePattern inMethod: aMethod
--- a/CustomReplaceIfNilWithIfTrueRefactoringTests.st	Sun Oct 19 14:08:55 2014 +0200
+++ b/CustomReplaceIfNilWithIfTrueRefactoringTests.st	Sun Oct 19 14:55:14 2014 +0200
@@ -15,6 +15,22 @@
 
 !CustomReplaceIfNilWithIfTrueRefactoringTests methodsFor:'tests'!
 
+test_available_in_context
+    
+    "Available for every context"
+    self assert: (generatorOrRefactoring class availableInContext: context).
+
+    "Modified: / 16-10-2014 / 21:36:15 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+test_available_in_perspective
+
+    "Available for every perspective"
+    self assert: (generatorOrRefactoring class availableInContext: CustomPerspective instance).
+
+    "Modified: / 16-10-2014 / 21:38:11 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
 test_if_nil_replaced_with_is_nil_if_true
     | expectedSource class method |
 
--- a/patches/patches.rc	Sun Oct 19 14:08:55 2014 +0200
+++ b/patches/patches.rc	Sun Oct 19 14:55:14 2014 +0200
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "LibraryName\0"
       VALUE "ProductVersion", "6.2.4.1378\0"
-      VALUE "ProductDate", "Sun, 19 Oct 2014 11:27:33 GMT\0"
+      VALUE "ProductDate", "Sun, 19 Oct 2014 12:45:03 GMT\0"
     END
 
   END
--- a/refactoring_custom.rc	Sun Oct 19 14:08:55 2014 +0200
+++ b/refactoring_custom.rc	Sun Oct 19 14:55:14 2014 +0200
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "ProductName\0"
       VALUE "ProductVersion", "6.2.4.1378\0"
-      VALUE "ProductDate", "Sun, 19 Oct 2014 11:27:29 GMT\0"
+      VALUE "ProductDate", "Sun, 19 Oct 2014 12:45:01 GMT\0"
     END
 
   END