Commits

Jan Lahoda committed 77ce946

Handle name spans for anotation types and enums

Comments (0)

Files changed (3)

remoting/server/web/resolve.web.api/src/org/netbeans/modules/jackpot30/resolve/api/CompilationInfo.java

 import com.sun.source.util.Trees;
 import javax.lang.model.util.Elements;
 import javax.lang.model.util.Types;
+import org.netbeans.api.java.lexer.JavaTokenId;
+import org.netbeans.api.lexer.TokenHierarchy;
 
 /**
  *
 
     private final Javac javac;
     private final CompilationUnitTree cut;
+    private final String text;
+    private final TokenHierarchy<?> th;
 
-    public CompilationInfo(Javac javac, CompilationUnitTree cut) {
+    public CompilationInfo(Javac javac, CompilationUnitTree cut, String text) {
         this.javac = javac;
         this.cut = cut;
+        this.text = text;
+        this.th = TokenHierarchy.create(text, JavaTokenId.language());
     }
 
     public /*@NonNull*/ Trees getTrees() {
     public CompilationUnitTree getCompilationUnit() {
         return cut;
     }
+
+    public /*@NonNull*/ String getText() {
+        return text;
+    }
+
+    public /*@NonNull*/ TokenHierarchy<?> getTokenHierarchy() {
+        return th;
+    }
 }

remoting/server/web/resolve.web.api/src/org/netbeans/modules/jackpot30/resolve/api/Javac.java

 
             javacTask.analyze(javacTask.enter(Collections.singletonList(cut)));
 
-            path2CUT.put(relativePath, result = new CompilationInfo(this, cut));
+            path2CUT.put(relativePath, result = new CompilationInfo(this, cut, content));
         }
 
         return result;

remoting/server/web/resolve.web.api/src/org/netbeans/modules/jackpot30/resolve/api/ResolveService.java

 import com.sun.source.tree.IdentifierTree;
 import com.sun.source.tree.MemberSelectTree;
 import com.sun.source.tree.MethodTree;
-import com.sun.source.tree.Tree;
 import com.sun.source.tree.VariableTree;
 import com.sun.source.util.TreePath;
 import com.sun.source.util.TreePathScanner;
 import java.util.Map;
 import java.util.Properties;
 import javax.lang.model.element.Element;
+import javax.lang.model.element.Name;
+import org.netbeans.api.java.lexer.JavaTokenId;
 import org.netbeans.api.java.source.ElementHandle;
+import org.netbeans.api.lexer.TokenSequence;
 import org.netbeans.modules.jackpot30.backend.base.CategoryStorage;
 import org.netbeans.modules.jackpot30.backend.base.SourceRoot;
 import org.openide.filesystems.FileObject;
 
     public static long[] nameSpan(CompilationInfo info, TreePath forTree) {
         JCTree jcTree = (JCTree) forTree.getLeaf(); //XXX
-        CharSequence name = null;
+        Name name = null;
+        int pos = jcTree.pos;
 
         switch (forTree.getLeaf().getKind()) {
             case IDENTIFIER: name = ((IdentifierTree) forTree.getLeaf()).getName(); break;
             case MEMBER_SELECT: name = ((MemberSelectTree) forTree.getLeaf()).getIdentifier(); break;
-            case ANNOTATION_TYPE: case CLASS:
-            case ENUM: case INTERFACE: name = ((ClassTree) forTree.getLeaf()).getSimpleName(); break;
+            case ANNOTATION_TYPE: case ENUM:
+                name = ((ClassTree) forTree.getLeaf()).getSimpleName();
+                
+                TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());
+
+                ts.move(pos);
+
+                while (ts.moveNext()) {
+                    if (ts.token().id() == JavaTokenId.IDENTIFIER) {
+                        if (name.contentEquals(ts.token().text())) {
+                            pos = ts.offset();
+                        }
+                        break;
+                    }
+                }
+
+                break;
+            case CLASS:
+            case INTERFACE: name = ((ClassTree) forTree.getLeaf()).getSimpleName(); break;
             case METHOD:
                 if ((((JCMethodDecl) forTree.getLeaf()).getModifiers().flags & Flags.GENERATEDCONSTR) != 0) {
                     //no positions for generated constructors:
             return new long[] {
                 info.getTrees().getSourcePositions().getStartPosition(forTree.getCompilationUnit(), forTree.getLeaf()),
                 info.getTrees().getSourcePositions().getEndPosition(forTree.getCompilationUnit(), forTree.getLeaf()),
-                jcTree.pos,
-                jcTree.pos + name.length()
+                pos,
+                pos + name.length()
             };
         }