working refactoring CustomCodeSelectionToResourceTranslation - wraps code selection in code editor (codeView) to translation call, finished code selection search/replace API in CustomRefactoryBuilder
authorJakub Nesveda <jakubnesveda@seznam.cz>
Wed, 27 Aug 2014 21:55:25 +0200
changeset 650 7514c13d6dd8
parent 645 38a38c28ddb1
child 651 78bb28086b93
working refactoring CustomCodeSelectionToResourceTranslation - wraps code selection in code editor (codeView) to translation call, finished code selection search/replace API in CustomRefactoryBuilder
CustomCodeSelectionToResourceTranslation.st
CustomCodeSelectionToResourceTranslationTests.st
CustomRefactoryBuilder.st
CustomRefactoryBuilderTests.st
refactoring_custom.rc
--- a/CustomCodeSelectionToResourceTranslation.st	Mon Aug 25 23:45:19 2014 +0200
+++ b/CustomCodeSelectionToResourceTranslation.st	Wed Aug 27 21:55:25 2014 +0200
@@ -73,11 +73,12 @@
     "Performs a refactoring within given context scope"
 
     refactoryBuilder 
-          replace:'``@expression'
-          with:'(resources string: (``@expression))'
+          replace:'`@expression'
+          with:'(resources string: (`@expression))'
           inCodeSelections:aCustomContext selectedCodes
 
     "Created: / 23-08-2014 / 00:16:58 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:11:20 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 ! !
 
 !CustomCodeSelectionToResourceTranslation class methodsFor:'documentation'!
--- a/CustomCodeSelectionToResourceTranslationTests.st	Mon Aug 25 23:45:19 2014 +0200
+++ b/CustomCodeSelectionToResourceTranslationTests.st	Wed Aug 27 21:55:25 2014 +0200
@@ -37,12 +37,12 @@
     generatorOrRefactoring executeInContext: context.  
 
     expectedSource := 'selector
-    self information: (resources string: (''Translate this'')).
+    self information:(resources string:''Translate this'').
     ^ self.'.
 
-    self assertMethodSource:expectedSource atSelector:#selector
+    self assertMethodSource:expectedSource atSelector:#selector forClass:class
 
     "Created: / 23-08-2014 / 20:09:06 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
-    "Modified: / 24-08-2014 / 09:52:39 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:13:22 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 ! !
 
--- a/CustomRefactoryBuilder.st	Mon Aug 25 23:45:19 2014 +0200
+++ b/CustomRefactoryBuilder.st	Wed Aug 27 21:55:25 2014 +0200
@@ -128,13 +128,18 @@
 
     | newTree change parseTree selectedParseTree newSelectedParseTree |
 
-    selectedParseTree := (self parseExpression: aCodeSelection selectedSourceCode) formattedCode.
+    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.
     newTree := CustomParseTreeRewriter 
         replace: aCodeSelection selectedSourceCode
@@ -143,14 +148,14 @@
         onInterval: aCodeSelection selectedInterval.
 
     change := InteractiveAddMethodChange
-        compile: newTree newSource 
+        compile: (self parseMethod: newTree newSource) formattedCode  
         in: aCodeSelection selectedMethod mclass 
         classified: aCodeSelection selectedMethod category.
 
     changes add: change
 
     "Created: / 24-08-2014 / 10:24:52 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
-    "Modified: / 25-08-2014 / 23:33:15 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:03:51 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 executeReplace: searchPattern with: rewritePattern inMethod: aMethod
@@ -173,12 +178,12 @@
     "Created: / 17-08-2014 / 18:45:20 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
-replace:searchPattern with:rewritePattern inCodeSelections:aCustomContext 
+replace:searchPattern with:rewritePattern inCodeSelections:aCodeSelectionCollection 
     "Searches for given pattern in methods source code and if source code matches then executes replacement"
     
     self 
         search:searchPattern
-        inCodeSelections:aCustomContext selectedCodes
+        inCodeSelections:aCodeSelectionCollection
         withResultDo:[:codeSelection | 
             self 
                 executeReplace:searchPattern
@@ -187,6 +192,7 @@
         ]
 
     "Created: / 24-08-2014 / 10:10:40 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:09:26 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 replace: searchPattern with: rewritePattern inContext: aCustomContext
--- a/CustomRefactoryBuilderTests.st	Mon Aug 25 23:45:19 2014 +0200
+++ b/CustomRefactoryBuilderTests.st	Wed Aug 27 21:55:25 2014 +0200
@@ -27,7 +27,7 @@
     ^ self.'.
 
     expectedSource := 'selector
-    self information: (resources string: ''Translate this'').
+    self information:(resources string:''Translate this'').
     ^ self.'.
 
     codeSelection := CustomSourceCodeSelection new.
@@ -45,6 +45,101 @@
     self assertSource: expectedSource sameAs: actualSource.
 
     "Created: / 25-08-2014 / 21:53:39 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 22:35:26 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+test_execute_replace_with_in_code_selection_02
+    
+    |originalSource expectedSource actualSource codeSelection|
+
+    originalSource := 'selector
+    self information: ''Translate this''.
+    ^ self.'.
+
+    expectedSource := 'selector
+    resources string:(self information:''Translate this'').
+    ^ self.'.
+
+    codeSelection := CustomSourceCodeSelection new.
+    codeSelection
+        currentSourceCode: originalSource;
+        selectedInterval: (14 to: 47);
+        selectedMethod: (self class compiledMethodAt: #generatorOrRefactoring). "/ just some method
+
+    refactoryBuilder executeReplace: '`@code' 
+        with: '(resources string: `@code)'
+        inCodeSelection: codeSelection.
+
+    actualSource := refactoryBuilder changes last source.
+
+    self assertSource: expectedSource sameAs: actualSource.
+
+    "Created: / 26-08-2014 / 22:37:59 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+test_execute_replace_with_in_code_selection_03
+
+    |originalSource expectedSource actualSource codeSelection|
+
+    originalSource := 'selector
+    self information: ''Translate this''.
+    ^ self.'.
+
+    expectedSource := 'selector
+    self information:''Translate this''.
+    ^ resources string:self.'.
+
+    codeSelection := CustomSourceCodeSelection new.
+    codeSelection
+        currentSourceCode: originalSource;
+        selectedInterval: (55 to: 59);
+        selectedMethod: (self class compiledMethodAt: #generatorOrRefactoring). "/ just some method
+
+    refactoryBuilder executeReplace: '`@code' 
+        with: '(resources string: `@code)'
+        inCodeSelection: codeSelection.
+
+    actualSource := refactoryBuilder changes last source.
+
+    self assertSource: expectedSource sameAs: actualSource.
+
+    "Created: / 26-08-2014 / 22:41:35 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
+!
+
+test_execute_replace_with_in_code_selection_04
+
+    |originalSource expectedSource actualSource codeSelection|
+
+    originalSource := 'selector
+    | temp |
+
+    temp := ''Translate this''.
+
+    self information: temp.
+    ^ self.'.
+
+    expectedSource := 'selector
+    |temp|
+
+    temp := ''Translate this''.
+    self information:(resources string:temp).
+    ^ self.'.
+
+    codeSelection := CustomSourceCodeSelection new.
+    codeSelection
+        currentSourceCode: originalSource;
+        selectedInterval: (77 to: 81);
+        selectedMethod: (self class compiledMethodAt: #generatorOrRefactoring). "/ just some method
+
+    refactoryBuilder executeReplace: '`@code' 
+        with: '(resources string: `@code)'
+        inCodeSelection: codeSelection.
+
+    actualSource := refactoryBuilder changes last source.
+
+    self assertSource: expectedSource sameAs: actualSource.
+
+    "Created: / 26-08-2014 / 22:52:47 / Jakub Nesveda <nesvejak@fit.cvut.cz>"
 !
 
 test_search_in_code_selections_with_result_do_01
--- a/refactoring_custom.rc	Mon Aug 25 23:45:19 2014 +0200
+++ b/refactoring_custom.rc	Wed Aug 27 21:55:25 2014 +0200
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "ProductName\0"
       VALUE "ProductVersion", "6.2.4.1326\0"
-      VALUE "ProductDate", "Mon, 25 Aug 2014 21:39:49 GMT\0"
+      VALUE "ProductDate", "Wed, 27 Aug 2014 19:26:05 GMT\0"
     END
 
   END