tools/JavaSourceHighlighter.st
branchdevelopment
changeset 2788 b25c8b9e886a
parent 2787 d4914ffbb0cf
child 2789 e6109bd7dfd2
--- a/tools/JavaSourceHighlighter.st	Thu Oct 03 20:07:05 2013 +0100
+++ b/tools/JavaSourceHighlighter.st	Fri Oct 04 08:59:25 2013 +0100
@@ -211,9 +211,74 @@
 
 formatClassDefinition:source in:class
 
-    ^ self format: source kind: #start in: class
+
+    | marker cacheIt document sourceUnit parser tree |
+
+    "Optimization - if full class source is to be formatted,
+     consult cache - when browsing the code or debugging, very 
+     often same same source is to be highlighted"
+
+    preferences isNil ifTrue:[
+        preferences := UserPreferences current.
+    ].
+    cacheIt := class notNil and:[class isJavaClass].
+    cacheIt ifTrue:[
+        document := JavaSourceDocument cachedDocumentFor: class theNonMetaclass.
+        document notNil ifTrue:[
+            (document sourceText notNil and:[document sourceText string = source]) ifTrue:[
+                (sourceIndex notNil and:[document sourceTreeIndex notNil]) ifTrue:[
+                     sourceIndex addAll: document sourceTreeIndex.
+                ].
+                ^ document sourceText copy.
+            ].
+        ] ifFalse:[
+            document := JavaSourceDocument for: class theNonMetaclass.
+            (sourceIndex isNil and:[SmallSense::ParseTreeIndex notNil]) ifTrue:[
+                 sourceIndex := SmallSense::ParseTreeIndex new.
+            ].
+            JavaSourceDocument cachedDocumentFor: class theNonMetaclass put: document.  
+        ].
+    ].
+    marker := Marker new.
+    marker highlighter: self.
+
+    sourceText := source isText 
+                    ifTrue:[source copy] 
+                    ifFalse:[source asText].
+
+    self doLexicalHighlightingOnly ifTrue:[          
+        sourceText := self format: source string.
+    ] ifFalse:[
+        sourceUnit := (Java classForName:'stx.libjava.tools.Source') new.
+        sourceUnit setContents: source string.
+        JavaCompiler synchronized:[
+            parser := (Java classForName:'stx.libjava.tools.text.Highlighter') new.
+        ].
+        parser setMarker: marker.
+        (sourceIndex notNil and:[sourceIndex isKindOf: SmallSense::ParseTreeIndex]) ifTrue:[
+            | indexer |
+
+            indexer := Indexer new.
+            indexer index: sourceIndex.
+            parser setIndexer: indexer.
+
+        ].
+        tree := parser parse: sourceUnit diet: false resolve: true.
+        (sourceIndex notNil and:[sourceIndex isKindOf: SmallSense::ParseTreeIndex]) ifTrue:[
+            sourceIndex tree: tree. 
+        ]
+    ].
+
+    ^ cacheIt ifTrue:[
+        document sourceText: sourceText.
+        document sourceTreeIndex: sourceIndex.
+        sourceText copy
+    ] ifFalse:[
+        sourceText
+    ]
 
     "Created: / 04-08-2011 / 23:44:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 20:12:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 formatClassDefinition:source in:class elementsInto: els
@@ -225,12 +290,10 @@
 !
 
 formatExpression:source in:class
-
-"/    self breakPoint:#jv.
-    ^source.
-    ^ self format: source kind: #'K_EXPRESSION' in: class
+    ^ self format: source
 
     "Created: / 04-08-2011 / 23:45:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 20:19:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 formatExpression:source in:class elementsInto: els
@@ -242,8 +305,6 @@
 !
 
 formatMethod:mth source:source in:class using: prefs
-    | scanner |
-
     preferences := prefs.
     preferences isNil ifTrue:[
         preferences := UserPreferences current.
@@ -256,13 +317,7 @@
     sourceText := source asText.
 
     self doLexicalHighlightingOnly ifTrue:[
-        scanner := Scanner for: source string.
-        scanner highlighter: self.
-        [
-            [ scanner nextToken ~~ #EOF ] whileTrue.
-        ] on: Error do:[:ex|
-            ex
-        ].
+        sourceText := self format: source
     ] ifFalse:[
         | document type parser marker nodes debug |
 
@@ -291,7 +346,7 @@
     ^ sourceText
 
     "Created: / 04-08-2011 / 23:45:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 25-09-2013 / 22:32:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 03-10-2013 / 20:20:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 formatMethod:mth source:source in:class using: prefs elementsInto: els
@@ -325,86 +380,37 @@
     "Created: / 04-08-2013 / 00:26:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
-!JavaSourceHighlighter methodsFor:'formatting - private'!
-
-format: source kind: kind in: class
-
-    | scanner marker cacheIt document sourceUnit parser tree |
-
-    "Optimization - if full class source is to be formatted,
-     consult cache - when browsing the code or debugging, very 
-     often same same source is to be highlighted"
+!JavaSourceHighlighter methodsFor:'formatting-private'!
 
-    preferences isNil ifTrue:[
-        preferences := UserPreferences current.
-    ].
-    cacheIt := kind == #start and: [class notNil and:[class isJavaClass]].
-    cacheIt ifTrue:[
-        document := JavaSourceDocument cachedDocumentFor: class theNonMetaclass.
-        document notNil ifTrue:[
-            (document sourceText notNil and:[document sourceText string = source]) ifTrue:[
-                (sourceIndex notNil and:[document sourceTreeIndex notNil]) ifTrue:[
-                     sourceIndex addAll: document sourceTreeIndex.
-                ].
-                ^ document sourceText copy.
-            ].
-        ] ifFalse:[
-            document := JavaSourceDocument for: class theNonMetaclass.
-            (sourceIndex isNil and:[SmallSense::ParseTreeIndex notNil]) ifTrue:[
-                 sourceIndex := SmallSense::ParseTreeIndex new.
-            ].
-            JavaSourceDocument cachedDocumentFor: class theNonMetaclass put: document.  
-        ].
-    ].
-    marker := Marker new.
-    marker highlighter: self.
-
-    sourceText := source isText 
-                    ifTrue:[source copy] 
-                    ifFalse:[source asText].
+format: source
+    "Simple formatting based on lexical structure only"
 
-    self doLexicalHighlightingOnly ifTrue:[          
-        scanner := Scanner for: source string.
-        scanner highlighter: self.
-        [
-            [ scanner nextToken ~~ #EOF ] whileTrue.
-        ] on: Error do:[:ex|
-        ].  
-    ] ifFalse:[
-        sourceUnit := (Java classForName:'stx.libjava.tools.Source') new.
-        sourceUnit setContents: source string.
-        JavaCompiler synchronized:[
-            parser := (Java classForName:'stx.libjava.tools.text.Highlighter') new.
-        ].
-        parser setMarker: marker.
-        (sourceIndex notNil and:[sourceIndex isKindOf: SmallSense::ParseTreeIndex]) ifTrue:[
-            | indexer |
-
-            indexer := Indexer new.
-            indexer index: sourceIndex.
-            parser setIndexer: indexer.
+    | scanner token lastToken0 lastToken1 lastValue0 lastValue1 lastPosition0 lastPosition1 |
+    sourceText := source asText.
+    scanner := Scanner for: source string.
+    scanner highlighter: self.
+    [
+        [ (token := scanner nextToken) ~~ #EOF ] whileTrue:[
+            "/ Here, try to guess what's selector...
+            token == $( ifTrue:[
+                lastPosition0 == #Identifier ifTrue:[
+                    self markSelectorFrom: lastPosition0  to: lastPosition0 + lastValue0 size - 1.
+                ].
+            ].
 
-        ].
-        tree := parser parse: sourceUnit diet: false resolve: true.
-        (sourceIndex notNil and:[sourceIndex isKindOf: SmallSense::ParseTreeIndex]) ifTrue:[
-            sourceIndex tree: tree. 
-        ]
-    ].
-    
-
-
-
+            lastToken1 := lastToken0.
+            lastValue1 := lastValue0.
+            lastPosition1  := lastPosition0.
 
-    ^ cacheIt ifTrue:[
-        document sourceText: sourceText.
-        document sourceTreeIndex: sourceIndex.
-        sourceText copy
-    ] ifFalse:[
-        sourceText
-    ]
+            lastToken0 := token.
+            lastValue0 := scanner tokenValue.
+            lastPosition0 := scanner tokenStartPosition.
+        ].
+    ] on: Error do:[:ex|
+    ].   
+    ^ sourceText
 
-    "Created: / 17-03-2012 / 14:02:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 03-10-2013 / 18:36:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 03-10-2013 / 20:19:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaSourceHighlighter methodsFor:'queries'!
@@ -1024,15 +1030,15 @@
     t == #Integer ifTrue:[
         highlighter markConstantFrom:tokenStartPosition + 1 to: tokenEndPosition + 1.
     ]].
-    bufferLast := (bufferLast \\ buffer size) + 1.
-    bufferLast == bufferFirst ifTrue:[
-        bufferFirst := (bufferFirst \\ buffer size) + 1.
-    ].
-    buffer at: bufferLast put: self token.
-    "/ Now, do a quick check for some common token sequences...not a full parsing,
-    "/ but helps a bit
-    ((bufferLast - bufferFirst) \\ 10) > 2 ifTrue:[
-        "/ Quick check for method call sequence...
+"/    bufferLast := (bufferLast \\ buffer size) + 1.
+"/    bufferLast == bufferFirst ifTrue:[
+"/        bufferFirst := (bufferFirst \\ buffer size) + 1.
+"/    ].
+"/    buffer at: bufferLast put: self token.
+"/    "/ Now, do a quick check for some common token sequences...not a full parsing,
+"/    "/ but helps a bit
+"/    ((bufferLast - bufferFirst) \\ 10) > 2 ifTrue:[
+"/        "/ Quick check for method call sequence...
 "/        t == $( ifTrue:[
 "/
 "/            ((buffer at:(bufferLast - 1) \\ buffer size) type == #Identifier
@@ -1046,15 +1052,15 @@
 "/                    ^ t
 "/                ].
 "/        ].
-        "/ Add more patterns here
-    ].
+"/        "/ Add more patterns here
+"/    ].
 
     ^ t
 
     "Created: / 14-05-1998 / 15:48:04 / cg"
     "Modified: / 16-05-1998 / 19:12:29 / cg"
     "Created: / 17-03-2012 / 19:15:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 02-09-2013 / 00:04:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 03-10-2013 / 20:25:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 skipComment