# HG changeset patch # User Jan Vrany # Date 1409093893 -7200 # Node ID 7fd0faf76e9077e848ca5937c4403a8358a66972 # Parent ef075895beeb6485357d744c16f6d06b0b4925ae Fixed CodeGenerator to preserve original source code formatting. diff -r ef075895beeb -r 7fd0faf76e90 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 " - "Modified: / 24-05-2014 / 01:02:46 / Jan Vrany " + "Modified: / 26-08-2014 / 23:51:54 / Jan Vrany " +! + +recordReplaceInSourceFrom:start to:stop by:code + recordedReplacementsInSource add: { start. stop . code }. + + "Modified: / 26-08-2014 / 23:50:10 / Jan Vrany " +! + +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 " ! 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 " ! ! !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 " "Modified: / 07-07-2009 / 19:13:55 / Jan Vrany " + "Modified: / 26-08-2014 / 23:37:48 / Jan Vrany " ! ! !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 $' ! !