AbstractSyntaxHighlighter.st
changeset 3599 a999663bd83f
parent 3561 f04a8db1590f
child 3675 6a75a2fe7a83
--- a/AbstractSyntaxHighlighter.st	Fri Feb 27 14:22:32 2015 +0100
+++ b/AbstractSyntaxHighlighter.st	Fri Feb 27 14:22:47 2015 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2006 by eXept Software AG
 	      All Rights Reserved
@@ -39,6 +41,14 @@
 documentation
 "
     common superclass for (smalltalk-) syntax highlighting.
+
+    This will parse the source code and update the local copy of the
+    sourceText (in that instvar) by changing emphasis and color,
+    as syntax elements are encountered.
+    This is done by redefining appropriate markXXX methods, which are
+    called by the parser whenever an interesting syntactic construct is encountered.
+    These methods are no-ops in the Parser class, but redefined here to update
+    the colorization of sourceText.
 "
 ! !
 
@@ -64,7 +74,7 @@
     "format (recolor) an expression in a given class.
      Return the text containing font changes and color information."
 
-    |parser tree text endPos|
+    |parser tree text|
 
     aString isNil ifTrue:[^ nil].
 
@@ -81,32 +91,14 @@
     text emphasisCollection:(text emphasis asRunArray).
 
     tree == #Error ifTrue:[
-	"/ mhmh - which is better ...
-	"/ alternative1: color rest after error in red
-"/        text
-"/            emphasizeFrom:(parser sourceStream position)
-"/            to:text size
-"/            with:(#color->Color red).
-
-
-	"/ alternative2: take original emphasis for rest
-
-	endPos := parser sourceStream position + 1.
-	endPos >= text size ifTrue:[
-	    ^ text
-	].
-	^ (text copyTo:endPos) , (aString copyFrom:(endPos+1))
-
-	"/ alternative3: no emphasis for rest.
-
-"/        ^ text "/ aString
+        ^ self colorize:text forErrorAtPosition:parser sourceStream position withOriginal:aString.
     ].
     ^ text
 
     "
      self
-	formatExpression:'(1 + 2) max:5'
-	in:UndefinedObject
+        formatExpression:'(1 + 2) max:5'
+        in:UndefinedObject
     "
 
     "Modified: / 7.4.1998 / 09:57:19 / cg"
@@ -168,68 +160,63 @@
     "format (recolor) a method in a given class.
      Return the text containing font changes and color information."
 
-    |highlighter tree text endPos eColor|
+    "/ cg: I smell a wumpus: I don't think, all of this must be copied to subclasses
+
+    |highlighter tree newText|
 
     aString isNil ifTrue:[^ nil].
 
     Error handle:[:ex |
-	Transcript showCR:ex description.
-	self breakPoint:#cg.
+        "/ Transcript showCR:ex description.
+        self breakPoint:#cg.
 
-	ex creator isHandled ifTrue:[
-	    ex reject.
-	].
-	(ParseError handles:ex) ifFalse:[
-	    "Parse error may happen when re-formatting incomplete code while editing"
-	    ('SyntaxHighlighter [info]: error during highlight: ' , ex description) infoPrintCR.
-	    "/ ex suspendedContext fullPrintAll.
-	].
-	^ aString
+        (ParseError handles:ex) ifFalse:[
+            ex creator isHandled ifTrue:[
+                "/ Transcript showCR:'handled' .
+                ex reject.
+            ].
+            "Parse error may happen when re-formatting incomplete code while editing"
+            ('SyntaxHighlighter [info]: error during highlight: ' , ex description) infoPrintCR.
+            "/ ex suspendedContext fullPrintAll.
+        ].
+        highlighter notNil ifTrue:[
+            ^ self colorize:(newText ? aString) forErrorAtPosition:highlighter sourceStream position withOriginal:aString
+        ].
+        ^ aString
     ] do:[
-	highlighter := self for:(ReadStream on:aString string) in:aClass.
-	highlighter method:methodOrNil.
-	preferencesOrNil notNil ifTrue:[highlighter preferences:preferencesOrNil].
-	"/ highlighter ignoreErrors:true.
-	highlighter ignoreWarnings:true.
-	highlighter sourceText:(text := aString string asText).
-
-	"/ use an array here - this can be changed much faster using #at:put:
-	text emphasisCollection:(Array new:aString size).
+        |sourceString|
 
-	tree := highlighter parseMethod.
-	"/ now, convert the emphasis-array to a runArray
-	text emphasisCollection:(text emphasis asRunArray).
+        sourceString := aString string.
+        newText := sourceString asUnicode16String asText.
+        "/ use an array here (instead of the RunArray) - this can be changed much faster using #at:put:
+        newText emphasisCollection:(Array new:sourceString size).
+
+        highlighter := self for:(ReadStream on:aString string) in:aClass.
+        highlighter method:methodOrNil.
+        preferencesOrNil notNil ifTrue:[highlighter preferences:preferencesOrNil].
+        "/ highlighter ignoreErrors:true.
+        highlighter ignoreWarnings:true.
+        highlighter sourceText:newText.
 
-	tree == #Error ifTrue:[
-	    eColor := UserPreferences current errorColor.
-	    eColor notNil ifTrue:[
-		"/ mhmh - which is better ...
-		"/ alternative1: color rest after error in red
-		text
-		    emphasizeFrom:(highlighter sourceStream position + 1)
-		    to:text size
-		    with:(#color->eColor).
-	    ] ifFalse:[
-		"/ alternative2: take original emphasis for rest
+        tree := highlighter parseMethod.
+        newText := highlighter sourceText.  "/ might have changed identity
+        "/ now, convert the emphasis-array to a runArray
+        newText emphasisCollection:(newText emphasis asRunArray).
 
-		endPos := highlighter sourceStream position + 1.
-		endPos >= text size ifTrue:[
-		    ^ text
-		].
-		^ (text copyTo:endPos) , (aString copyFrom:(endPos+1))
-	    ].
-	    "/ alternative3: no emphasis for rest.
-	].
-	^ text
+        tree == #Error ifTrue:[
+            ^ self colorize:newText forErrorAtPosition:highlighter sourceStream position withOriginal:aString.
+        ].
+        ^ newText
     ]
+
     "
      self
-	formatMethod:'foo
+        formatMethod:'foo
     ^ self bar:''hello''.
 
     ' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , '
 '
-	in:UndefinedObject
+        in:UndefinedObject
     "
 
     "Created: / 28-04-2010 / 13:01:42 / cg"
@@ -272,6 +259,36 @@
     "Modified: / 28-04-2010 / 13:02:11 / cg"
 ! !
 
+!AbstractSyntaxHighlighter class methodsFor:'highlighting'!
+
+colorize:text forErrorAtPosition:pos withOriginal:originalString
+    |eColor endPos|
+
+    endPos := pos + 1.
+
+    "/ mhmh - which is better ...
+
+    "/ alternative1: colorize the rest after the error in red
+
+    eColor := UserPreferences current errorColor.
+    eColor notNil ifTrue:[
+        ^ text
+            emphasizeFrom:endPos 
+            to:text size 
+            with:(#color->eColor).
+    ].
+
+    "/ alternative2: take the original emphasis for the rest
+    endPos >= text size ifTrue:[
+        ^ text
+    ] ifFalse:[
+        ^ (text copyTo:endPos) , (originalString copyFrom:(endPos+1))
+    ].
+
+    "/ alternative3: no emphasis for rest.
+    ^ text
+! !
+
 !AbstractSyntaxHighlighter class methodsFor:'misc'!
 
 collectionEnumerationSelectors
@@ -492,12 +509,16 @@
 !
 
 sourceText
+    "retrieve the updated source text after the highlighting process"
+
     ^ sourceText
 
     "Created: / 31.3.1998 / 11:49:05 / cg"
 !
 
 sourceText:aString
+    "this text will be updated by the highlighting process"
+
     sourceText := aString.
 
     "Created: / 31-03-1998 / 11:49:05 / cg"
@@ -648,11 +669,11 @@
 !AbstractSyntaxHighlighter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libcomp/AbstractSyntaxHighlighter.st,v 1.33 2015-02-01 21:53:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libcomp/AbstractSyntaxHighlighter.st,v 1.34 2015-02-27 13:22:47 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libcomp/AbstractSyntaxHighlighter.st,v 1.33 2015-02-01 21:53:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libcomp/AbstractSyntaxHighlighter.st,v 1.34 2015-02-27 13:22:47 cg Exp $'
 !
 
 version_SVN