Fixes for syntax highlighting. development
authorJan Vrany <jan.vrany@fit.cvut.cz>
Mon, 16 Sep 2013 01:04:57 +0100
branchdevelopment
changeset 2733 3d97124aebf5
parent 2732 7d1a1fb5b01a
child 2734 f56049613ff3
Fixes for syntax highlighting. Even full class source is fully parser and indexed as the parser seems to be fast enough (at least at i5 CPU).
JavaClass.st
tools/JavaSourceHighlighter.st
tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java
tools/java/src/stx/libjava/tools/environment/Resolver.java
tools/java/src/stx/libjava/tools/text/Highlighter.java
tools/java/src/stx/libjava/tools/text/Indexer.java
--- a/JavaClass.st	Sun Sep 15 02:16:13 2013 +0100
+++ b/JavaClass.st	Mon Sep 16 01:04:57 2013 +0100
@@ -668,10 +668,25 @@
     | enclosingMethodAttr |
 
     enclosingMethodAttr := self getAttribute: #EnclosingMethod.
-    enclosingMethodAttr isNil ifTrue:[ ^ nil ].
+    enclosingMethodAttr isNil ifTrue:[ 
+        "/ Funny, for some inner classes the enclosing method attributes is not generated...
+        | dollar |
+        ^ ((dollar := name lastIndexOf: $$) == 0) ifTrue:[
+            nil
+        ] ifFalse:[
+            | enclosingClassName |
+
+            enclosingClassName := name copyTo: dollar - 1.
+            enclosingClassName notEmptyOrNil ifTrue:[
+                JavaVM classForName: enclosingClassName definedBy: classLoader
+            ]
+        ]
+    ].
     ^ enclosingMethodAttr first resolve: false.
 
     "Created: / 13-09-2013 / 01:26:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 16-09-2013 / 00:33:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+
 !
 
 enclosingMethod
--- a/tools/JavaSourceHighlighter.st	Sun Sep 15 02:16:13 2013 +0100
+++ b/tools/JavaSourceHighlighter.st	Mon Sep 16 01:04:57 2013 +0100
@@ -338,7 +338,7 @@
         sourceUnit setContents: source string.
         parser := (Java classForName:'stx.libjava.tools.text.Highlighter') new.
         parser setMarker: marker.
-        parser parse: sourceUnit diet: false.
+        parser parse: sourceUnit diet: false resolve: true.
     ].
     
 
--- a/tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java	Sun Sep 15 02:16:13 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/compiler/CompilerAdapter.java	Mon Sep 16 01:04:57 2013 +0100
@@ -46,6 +46,19 @@
 	 * @return true, if compilation succeeded, false otherwise.
 	 */
 	public boolean compile(String source) {
+	    return compile(source, true);
+	}
+	
+	/**
+     * Compiles classes in given source. The resulting .class files are added
+     * to an internal list which can be later retrieved by getClassFiles() - 
+     * but only if @param generate is true. 
+     * 
+     * @param source source code of the class as String.
+     * @param generate if false, .class files are not generated (used to check source for errors)   
+     * @return true, if compilation succeeded, false otherwise.
+     */
+	public boolean compile(String source, boolean generate) {
 		ICompilerRequestor requestor = this;
 		IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitAfterAllProblems();
 	    IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
--- a/tools/java/src/stx/libjava/tools/environment/Resolver.java	Sun Sep 15 02:16:13 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/environment/Resolver.java	Mon Sep 16 01:04:57 2013 +0100
@@ -40,8 +40,10 @@
     }
     
     protected void completeTypeBindings2(CompilationUnitDeclaration cud) {
-        for (int i = 0; i < cud.types.length; i++) {
-            this.completeTypeBindings2(cud.types[i]);
+        if (cud.types != null) {
+            for (int i = 0; i < cud.types.length; i++) {
+                this.completeTypeBindings2(cud.types[i]);
+            }
         }
     }
     
--- a/tools/java/src/stx/libjava/tools/text/Highlighter.java	Sun Sep 15 02:16:13 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/text/Highlighter.java	Mon Sep 16 01:04:57 2013 +0100
@@ -1,5 +1,7 @@
 package stx.libjava.tools.text;
 
+import net.sf.jasperreports.components.sort.FieldFilter;
+
 import org.eclipse.jdt.core.compiler.InvalidInputException;
 import org.eclipse.jdt.internal.compiler.ASTVisitor;
 import org.eclipse.jdt.internal.compiler.CompilationResult;
@@ -7,15 +9,20 @@
 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.FieldReference;
 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
+import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
+import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
 
+import stx.libjava.tools.environment.Resolver;
 import stx.libjava.tools.parser.Parser;
 
 public class Highlighter extends Parser {
@@ -27,26 +34,32 @@
         this(new Marker() {            
             @Override
             public void mark(int kind, int from, int to) {
-                // TODO Auto-generated method stub
-                
+            }
+        },
+        new Indexer() {
+            
+            @Override
+            public void index(ASTNode node, int from, int to) {                
             }
         });
     }
     
-    public Highlighter(Marker marker) {
+    public Highlighter(Marker marker, Indexer indexer) {
         this(
                 new ProblemReporter(
                         DefaultErrorHandlingPolicies.proceedWithAllProblems(), 
                         new CompilerOptions(), 
                         new DefaultProblemFactory()),
                 true, 
-                marker);
+                marker,
+                indexer);
     }
 
     public Highlighter(ProblemReporter problemReporter,
-            boolean optimizeStringLiterals, Marker marker) {
+            boolean optimizeStringLiterals, Marker marker, Indexer indexer) {
         super(problemReporter, optimizeStringLiterals);
         setMarker(marker);
+        setIndexer(indexer);
     }
     
     public Marker getMarker() {
@@ -55,7 +68,7 @@
     
     public void setMarker(Marker marker) {
         this.marker = marker;        
-        ((Scanner)this.scanner).marker = marker;
+        ((HighlightingScanner)this.scanner).marker = marker;
     }
 
     public Indexer getIndexer() {
@@ -68,7 +81,7 @@
 
 
     public void initializeScanner(){
-        this.scanner = new Highlighter.Scanner(
+        this.scanner = new Highlighter.HighlightingScanner(
             false /*comment*/,
             false /*whitespace*/,
             false, /* will be set in initialize(boolean) */
@@ -80,13 +93,24 @@
     }
     
     
+    public CompilationUnitDeclaration parse(ICompilationUnit cu, boolean diet, boolean resolve) {
+        CompilationUnitDeclaration cud = super.parse(cu, diet, resolve);
+        HighlightingAndIndexingVisitor v = new HighlightingAndIndexingVisitor();
+        cud.ignoreFurtherInvestigation = false;
+        v.setMarker(marker);
+        v.setIndexer(indexer);
+        cud.traverse(v, (CompilationUnitScope)null);                            
+        return cud;
+    }
+    
+    
     public ASTNode[] parseClassBodyDeclarations(char[] source, int offset, int length, CompilationUnitDeclaration unit) {
         ASTNode[] nodes = super.parseClassBodyDeclarations(source, offset, length, unit);
-        Visitor v = new Visitor();
+        HighlightingAndIndexingVisitor v = new HighlightingAndIndexingVisitor();
         v.setMarker(marker);
         v.setIndexer(indexer);
         for (int i = 0; i < nodes.length; i++) {
-            ASTNode n = nodes[i];
+            ASTNode n = nodes[i];            
             if (n instanceof MethodDeclaration) {
                 ((MethodDeclaration)n).traverse(v, (ClassScope)null);
             } else {
@@ -97,7 +121,7 @@
     }
 
     
-    public static class Visitor extends ASTVisitor {
+    public static class HighlightingAndIndexingVisitor extends ASTVisitor {
         protected Marker marker;        
         protected Indexer indexer;
 
@@ -116,40 +140,58 @@
         public void setIndexer(Indexer indexer) {
             this.indexer = indexer;
         }
+                     
+        public void endVisit(FieldDeclaration fieldDeclaration, MethodScope scope) {
+            marker.mark(Marker.MARK_FIELD, fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd);
+            indexer.index(fieldDeclaration, fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd);
+        }
         
-        public boolean visit(MessageSend messageSend, BlockScope scope) {
+        public void endVisit(FieldReference fieldReference, BlockScope scope) {
+            int start = (int)(fieldReference.nameSourcePosition >>> 32);
+            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFF);
+            marker.mark(Marker.MARK_FIELD, start, stop);
+            indexer.index(fieldReference, start, stop);
+        }
+        public void endVisit(FieldReference fieldReference, ClassScope scope) {
+            int start = (int)(fieldReference.nameSourcePosition >>> 32);
+            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFF);
+            marker.mark(Marker.MARK_FIELD, start, stop);
+            indexer.index(fieldReference, start, stop);
+        }
+        
+                
+        public void endVisit(MessageSend messageSend, BlockScope scope) {
             int start = (int)(messageSend.nameSourcePosition >>> 32);
             int stop  = (int)(messageSend.nameSourcePosition & 0x0000FFFF);
-            marker.mark(Marker.MARK_SELECTOR, start, stop);                                                            
-            return true; // continue visiting
+            marker.mark(Marker.MARK_SELECTOR, start, stop);
+            indexer.index(messageSend, start, stop);                                                                        
         }
         
-        public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
+        public void endVisit(MethodDeclaration methodDeclaration, ClassScope scope) {
             int start = methodDeclaration.sourceStart;
             int stop  = start + methodDeclaration.selector.length - 1;
             marker.mark(Marker.MARK_SELECTOR, start, stop);
-            return true; // do nothing by default, keep traversing
+            
         }
         
-        public boolean visit(
+        public void endVisit(
                 ConstructorDeclaration constructorDeclaration,
                 ClassScope scope) {
             int start = constructorDeclaration.sourceStart;
             int stop  = start + constructorDeclaration.selector.length - 1;
             marker.mark(Marker.MARK_SELECTOR, start, stop);
-
-            return true; // do nothing by default, keep traversing
+            
         }
 
         
     }
         
-    public static class Scanner extends org.eclipse.jdt.internal.compiler.parser.Scanner {
+    public static class HighlightingScanner extends org.eclipse.jdt.internal.compiler.parser.Scanner {
         
         public Marker marker;
         protected int lastCommentPtr = -1;
 
-        public Scanner(boolean b, boolean c, boolean d, long sourceLevel,
+        public HighlightingScanner(boolean b, boolean c, boolean d, long sourceLevel,
                 long complianceLevel, char[][] taskTags,
                 char[][] taskPriorities, boolean isTaskCaseSensitive) {
             super(b,c,d,sourceLevel, complianceLevel, taskTags, taskPriorities, isTaskCaseSensitive);
--- a/tools/java/src/stx/libjava/tools/text/Indexer.java	Sun Sep 15 02:16:13 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/text/Indexer.java	Mon Sep 16 01:04:57 2013 +0100
@@ -9,6 +9,6 @@
      *  
      * @param node node to add to the index.
      */
-    public abstract void add(ASTNode node);
+    public abstract void index(ASTNode node, int from, int to);
 
 }