SyntaxHighlighter2.st
changeset 15446 d4cda7a1df4e
parent 14489 1b8f51cfd187
child 15456 44e323d6539f
--- a/SyntaxHighlighter2.st	Fri Feb 27 02:02:30 2015 +0100
+++ b/SyntaxHighlighter2.st	Fri Feb 27 14:22:39 2015 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
               All Rights Reserved
@@ -25,6 +27,8 @@
 "
 "{ Package: 'stx:libtool' }"
 
+"{ NameSpace: Smalltalk }"
+
 SyntaxHighlighter subclass:#SyntaxHighlighter2
 	instanceVariableNames:'elements lastVariableElements lastSelectorElement
 		ignoreBadIdentifier'
@@ -61,6 +65,25 @@
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 "
+!
+
+documentation
+"
+    A slightly improved syntax highlighter.
+
+    In addition to the inherited colorization, this one also remembers
+    so called 'syntax elements' (variable tokens and selectors) and remembers
+    them in a list.
+    This can be later used by the codeView to highlight uses of a clicked-on
+    variable or a clicked-on selector.
+    Also this list could (but is not, at the moment) be used to forward/backward
+    search for the next use of some variable.
+
+    [caveat:]
+        This code has a smell: there is a lot of code duplication,
+        and most can be moved to the superclass. Actually, there is propably no
+        reason to have both classes around, so why not integrate all into the superclass.
+"
 ! !
 
 !SyntaxHighlighter2 class methodsFor:'highlighting'!
@@ -72,7 +95,9 @@
      As a side effect, put syntax elements into the passed in elements container
      (for element-highlighting in codeView2)"
 
-    |parser tree text endPos|
+    "/ cg: I smell a wumpus: I don't think, all of this must be redefined - most is already there in the superclass.
+
+    |parser tree text|
 
     aString isNil ifTrue:[^ nil].
 
@@ -90,25 +115,7 @@
     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
 
@@ -133,7 +140,9 @@
     "format (recolor) an expression in a given class.
      Return the text containing font changes and color information."
 
-    |parser tree text endPos|
+    "/ cg: I smell a wumpus: I don't think, all of this must be redefined - most is already there in the superclass.
+
+    |parser tree text|
 
     aString isNil ifTrue:[^ nil].
 
@@ -151,25 +160,7 @@
     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
 
@@ -202,59 +193,55 @@
     "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 redefined - most is already there in the superclass.
+
+    |highlighter tree newText|
 
     aString isNil ifTrue:[^ nil].
 
     Error handle:[:ex |
-        ex creator isHandled ifTrue:[
-            ex reject.
-        ].
+        "/ Transcript showCR:ex description.
         (self parseErrorSignal 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.
+        |sourceString|
+
+        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:sourceString) in:aClass.
         highlighter elements: elements.
         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).
+        highlighter sourceText:newText.
 
         tree := highlighter parseMethod.
+        newText := highlighter sourceText.  "/ might have changed identity
         "/ now, convert the emphasis-array to a runArray
-        text emphasisCollection:(text emphasis asRunArray).
+        newText emphasisCollection:(newText emphasis asRunArray).
 
         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
+            ^ self colorize:newText forErrorAtPosition:highlighter sourceStream position withOriginal:aString
+        ].
+        ^ newText
+    ]
 
-                endPos := highlighter sourceStream position + 1.
-                endPos >= text size ifTrue:[
-                    ^ Array with: text with: highlighter elements
-                ].
-                ^ ((text copyTo:endPos) , (aString copyFrom:(endPos+1)))
-            ].
-            "/ alternative3: no emphasis for rest.
-        ].
-        ^text
-    ]
     "
-     self
-        formatMethod:'foo
+     self formatMethod:'foo
     ^ self bar:''hello''.
 
     ' , (Character doubleQuote asString) , 'some comment' , (Character doubleQuote asString) , '
@@ -1065,14 +1052,14 @@
 !SyntaxHighlighter2 class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libtool/SyntaxHighlighter2.st,v 1.23 2014-06-13 10:25:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/SyntaxHighlighter2.st,v 1.24 2015-02-27 13:22:39 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libtool/SyntaxHighlighter2.st,v 1.23 2014-06-13 10:25:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libtool/SyntaxHighlighter2.st,v 1.24 2015-02-27 13:22:39 cg Exp $'
 !
 
 version_SVN
-    ^ '$Id: SyntaxHighlighter2.st,v 1.23 2014-06-13 10:25:17 cg Exp $'
+    ^ '$Id: SyntaxHighlighter2.st,v 1.24 2015-02-27 13:22:39 cg Exp $'
 ! !