Fixed CodeGenerator to preserve original source code formatting.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Wed, 27 Aug 2014 00:58:13 +0200
changeset 14717 7fd0faf76e90
parent 14716 ef075895beeb
child 14718 21ebe048c4a6
Fixed CodeGenerator to preserve original source code formatting.
CodeGenerator.st
--- a/CodeGenerator.st	Tue Aug 26 23:45:46 2014 +0200
+++ b/CodeGenerator.st	Wed Aug 27 00:58:13 2014 +0200
@@ -26,7 +26,8 @@
 "{ Package: 'stx:libtool' }"
 
 RBProgramNodeVisitor subclass:#CodeGenerator
-	instanceVariableNames:'class protocol source change replacements'
+	instanceVariableNames:'class protocol source change replacements
+		recordedReplacementsInSource'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Interface-Browsers'
@@ -151,19 +152,39 @@
 
     | parser method |
     parser := RBParser new.
+    recordedReplacementsInSource := OrderedCollection new.
     parser errorBlock:[ :str :pos | self error: ('Error: %1: %2' bindWith: pos with: str). ^ self ].
     parser initializeParserWith: source type: #rewriteSavingCommentsOn:errorBlock:.
     method := parser parseMethod: source.    
 
     method source: nil.
     method acceptVisitor: self.
+    self replaceInSourceCode.
     (change := InteractiveAddMethodChange new)
         class: class
         protocol: protocol
-        source: method formattedCode.
+        source: (source notNil ifTrue:[source] ifFalse:[method formattedCode]).
 
     "Created: / 07-07-2009 / 18:44:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
-    "Modified: / 24-05-2014 / 01:02:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+recordReplaceInSourceFrom:start to:stop by:code
+    recordedReplacementsInSource add: { start. stop . code }.
+
+    "Modified: / 26-08-2014 / 23:50:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+replaceInSourceCode
+    "Perform recorded replacements in source"
+
+    recordedReplacementsInSource sort: [ :a :b | a second < b first ].
+    recordedReplacementsInSource reverseDo:[ :replacement |
+        source := 
+            (source copyTo: replacement first - 1) , replacement third , (source copyFrom: replacement second + 1)  
+    ].
+
+    "Created: / 26-08-2014 / 23:51:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 replacePlaceholdersInSelectorPartsOf:aMessageNode 
@@ -175,9 +196,17 @@
             (replacement isSymbol or:[ replacement isVariable ]) ifFalse:[
                 self error:'Replacement for selector parts must be a single selector'
             ].
-            part value:replacement formattedCode
+            source notNil ifTrue:[
+                self 
+                      recordReplaceInSourceFrom:part start
+                      to:part stop
+                      by:replacement formattedCode.
+            ].
+            part value:replacement formattedCode.                 
         ]
     ]
+
+    "Modified: / 26-08-2014 / 23:37:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CodeGenerator methodsFor:'visitor-double dispatching'!
@@ -199,21 +228,28 @@
 
 acceptVariableNode: aVariableNode
 
-    aVariableNode isPatternNode ifTrue:
-        [aVariableNode replaceWith:
-            (self replacementFor:aVariableNode name)]
+    aVariableNode isPatternNode ifTrue:[            
+        source notNil ifTrue:[ 
+            self 
+                  recordReplaceInSourceFrom:aVariableNode start
+                  to:aVariableNode stop
+                  by:(self replacementFor:aVariableNode name) formattedCode
+        ].
+        aVariableNode replaceWith: (self replacementFor:aVariableNode name).
+    ]
 
     "Created: / 30-12-2008 / 17:13:16 / Jan Vrany <vranyj1@fel.cvut.cz>"
     "Modified: / 07-07-2009 / 19:13:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
+    "Modified: / 26-08-2014 / 23:37:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CodeGenerator class methodsFor:'documentation'!
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.3 2014-05-24 00:10:56 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $'
 !
 
 version_SVN
-    ^ '$Id: CodeGenerator.st,v 1.3 2014-05-24 00:10:56 vrany Exp $'
+    ^ '$Id: CodeGenerator.st,v 1.4 2014-08-26 22:58:13 vrany Exp $'
 ! !