Commits

Jan Lahoda committed ee299d2

Fixing tree position in the index for MemberSelectTree, adding 'declarationSignature' for declarations in navigable index

Comments (0)

Files changed (2)

remoting/server/indexer/usages/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImpl.java

 import com.sun.source.tree.MemberSelectTree;
 import com.sun.source.tree.MethodTree;
 import com.sun.source.tree.NewClassTree;
+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 com.sun.source.util.Trees;
 import java.io.IOException;
         this.root = root;
     }
 
+    static long treePosition(Trees trees, TreePath tree) {
+        switch (tree.getLeaf().getKind()) {
+            case MEMBER_SELECT:
+                return trees.getSourcePositions().getEndPosition(tree.getCompilationUnit(), tree.getLeaf()) - ((MemberSelectTree) tree.getLeaf()).getIdentifier().length();
+        }
+
+        return trees.getSourcePositions().getStartPosition(tree.getCompilationUnit(), tree.getLeaf());
+    }
+
     @Override
     public void process(CompilationUnitTree toProcess, Indexable indexable, Lookup services) {
         if (!IndexAccessor.getCurrent().isAcceptable(indexable.getURL())) return;
                             usages.add(new Field(KEY_SIGNATURES, serialized, Store.YES, Index.NOT_ANALYZED));
                         }
 
-                        long pos = trees.getSourcePositions().getStartPosition(getCurrentPath().getCompilationUnit(), getCurrentPath().getLeaf());
+                        long pos = treePosition(trees, getCurrentPath());
 
                         if (NAVIGABLE) {
                             attributedSignatures.append(Long.toString(pos));
                                 currentClassDocument.add(new Field("file", file, Store.YES, Index.NOT_ANALYZED));
                                 currentClassDocument.add(new Field(KEY_MARKER, "true", Store.NO, Index.NOT_ANALYZED));
 
+                                if (NAVIGABLE) {
+                                    currentClassDocument.add(new Field("declarationSignature", Common.serialize(ElementHandle.create(el)), Store.YES, Index.NOT_ANALYZED));
+                                }
+
                                 IndexAccessor.getCurrent().getIndexWriter().addDocument(currentClassDocument);
                             } catch (CorruptIndexException ex) {
                                 Exceptions.printStackTrace(ex);
                                 }
                             }
 
+                            if (NAVIGABLE) {
+                                currentFeatureDocument.add(new Field("declarationSignature", Common.serialize(ElementHandle.create(el)), Store.YES, Index.NOT_ANALYZED));
+                            }
+
                             IndexAccessor.getCurrent().getIndexWriter().addDocument(currentFeatureDocument);
                         } catch (CorruptIndexException ex) {
                             Exceptions.printStackTrace(ex);

remoting/server/indexer/usages/test/unit/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImplTest.java

         assertTrue(foundA);
     }
 
+    public void testTreePositions() throws IOException {
+        doPositionTests("package test; public class Test { private Test() { ^Sy|stem.err.println(1); } }");
+        doPositionTests("package test; public class Test { private Test() { System.^e|rr.println(1); } }");
+        doPositionTests("package test; public class Test { private Test() { System.err.^p|rintln(1); } }");
+    }
+
+    private void doPositionTests(String code) throws IOException {
+        final int caret = code.replace("^", "").indexOf('|');
+
+        assertTrue("" + caret, caret != (-1));
+
+        code = code.replace("|", "");
+
+        final int expected = code.indexOf('^');
+
+        assertTrue("" + expected, expected != (-1));
+
+        FileObject testFile = FileUtil.createData(new File(getWorkDir(), "Test.java"));
+
+        copyToFile(testFile, code.replace("^", ""));
+
+        final boolean[] invoked = new boolean[1];
+
+        JavaSource.forFileObject(testFile).runUserActionTask(new Task<CompilationController>() {
+            @Override public void run(CompilationController parameter) throws Exception {
+                parameter.toPhase(JavaSource.Phase.RESOLVED);
+
+                TreePath tp = parameter.getTreeUtilities().pathFor(caret);
+
+                assertEquals(expected, IndexerImpl.treePosition(parameter.getTrees(), tp));
+
+                invoked[0] = true;
+            }
+        }, true);
+
+        assertTrue(invoked[0]);
+    }
+
     private void copyToFile(FileObject testFile, String code) throws IOException {
         OutputStream out = testFile.getOutputStream();
         
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.