Fixing tree position in the index for MemberSelectTree, adding 'declarationSignature' for declarations in navigable index
authorJan Lahoda <jlahoda@netbeans.org>
Tue, 21 Aug 2012 13:33:42 +0200
changeset 8538d169fc210ea
parent 852 14b2dae1089d
child 854 2d6591b8b42c
Fixing tree position in the index for MemberSelectTree, adding 'declarationSignature' for declarations in navigable index
remoting/server/indexer/usages/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImpl.java
remoting/server/indexer/usages/test/unit/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImplTest.java
     1.1 --- a/remoting/server/indexer/usages/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImpl.java	Fri Aug 17 14:53:41 2012 +0200
     1.2 +++ b/remoting/server/indexer/usages/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImpl.java	Tue Aug 21 13:33:42 2012 +0200
     1.3 @@ -47,7 +47,9 @@
     1.4  import com.sun.source.tree.MemberSelectTree;
     1.5  import com.sun.source.tree.MethodTree;
     1.6  import com.sun.source.tree.NewClassTree;
     1.7 +import com.sun.source.tree.Tree;
     1.8  import com.sun.source.tree.VariableTree;
     1.9 +import com.sun.source.util.TreePath;
    1.10  import com.sun.source.util.TreePathScanner;
    1.11  import com.sun.source.util.Trees;
    1.12  import java.io.IOException;
    1.13 @@ -113,6 +115,15 @@
    1.14          this.root = root;
    1.15      }
    1.16  
    1.17 +    static long treePosition(Trees trees, TreePath tree) {
    1.18 +        switch (tree.getLeaf().getKind()) {
    1.19 +            case MEMBER_SELECT:
    1.20 +                return trees.getSourcePositions().getEndPosition(tree.getCompilationUnit(), tree.getLeaf()) - ((MemberSelectTree) tree.getLeaf()).getIdentifier().length();
    1.21 +        }
    1.22 +
    1.23 +        return trees.getSourcePositions().getStartPosition(tree.getCompilationUnit(), tree.getLeaf());
    1.24 +    }
    1.25 +
    1.26      @Override
    1.27      public void process(CompilationUnitTree toProcess, Indexable indexable, Lookup services) {
    1.28          if (!IndexAccessor.getCurrent().isAcceptable(indexable.getURL())) return;
    1.29 @@ -155,7 +166,7 @@
    1.30                              usages.add(new Field(KEY_SIGNATURES, serialized, Store.YES, Index.NOT_ANALYZED));
    1.31                          }
    1.32  
    1.33 -                        long pos = trees.getSourcePositions().getStartPosition(getCurrentPath().getCompilationUnit(), getCurrentPath().getLeaf());
    1.34 +                        long pos = treePosition(trees, getCurrentPath());
    1.35  
    1.36                          if (NAVIGABLE) {
    1.37                              attributedSignatures.append(Long.toString(pos));
    1.38 @@ -214,6 +225,10 @@
    1.39                                  currentClassDocument.add(new Field("file", file, Store.YES, Index.NOT_ANALYZED));
    1.40                                  currentClassDocument.add(new Field(KEY_MARKER, "true", Store.NO, Index.NOT_ANALYZED));
    1.41  
    1.42 +                                if (NAVIGABLE) {
    1.43 +                                    currentClassDocument.add(new Field("declarationSignature", Common.serialize(ElementHandle.create(el)), Store.YES, Index.NOT_ANALYZED));
    1.44 +                                }
    1.45 +
    1.46                                  IndexAccessor.getCurrent().getIndexWriter().addDocument(currentClassDocument);
    1.47                              } catch (CorruptIndexException ex) {
    1.48                                  Exceptions.printStackTrace(ex);
    1.49 @@ -278,6 +293,10 @@
    1.50                                  }
    1.51                              }
    1.52  
    1.53 +                            if (NAVIGABLE) {
    1.54 +                                currentFeatureDocument.add(new Field("declarationSignature", Common.serialize(ElementHandle.create(el)), Store.YES, Index.NOT_ANALYZED));
    1.55 +                            }
    1.56 +
    1.57                              IndexAccessor.getCurrent().getIndexWriter().addDocument(currentFeatureDocument);
    1.58                          } catch (CorruptIndexException ex) {
    1.59                              Exceptions.printStackTrace(ex);
     2.1 --- a/remoting/server/indexer/usages/test/unit/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImplTest.java	Fri Aug 17 14:53:41 2012 +0200
     2.2 +++ b/remoting/server/indexer/usages/test/unit/src/org/netbeans/modules/jackpot30/indexer/usages/IndexerImplTest.java	Tue Aug 21 13:33:42 2012 +0200
     2.3 @@ -248,6 +248,44 @@
     2.4          assertTrue(foundA);
     2.5      }
     2.6  
     2.7 +    public void testTreePositions() throws IOException {
     2.8 +        doPositionTests("package test; public class Test { private Test() { ^Sy|stem.err.println(1); } }");
     2.9 +        doPositionTests("package test; public class Test { private Test() { System.^e|rr.println(1); } }");
    2.10 +        doPositionTests("package test; public class Test { private Test() { System.err.^p|rintln(1); } }");
    2.11 +    }
    2.12 +
    2.13 +    private void doPositionTests(String code) throws IOException {
    2.14 +        final int caret = code.replace("^", "").indexOf('|');
    2.15 +
    2.16 +        assertTrue("" + caret, caret != (-1));
    2.17 +
    2.18 +        code = code.replace("|", "");
    2.19 +
    2.20 +        final int expected = code.indexOf('^');
    2.21 +
    2.22 +        assertTrue("" + expected, expected != (-1));
    2.23 +
    2.24 +        FileObject testFile = FileUtil.createData(new File(getWorkDir(), "Test.java"));
    2.25 +
    2.26 +        copyToFile(testFile, code.replace("^", ""));
    2.27 +
    2.28 +        final boolean[] invoked = new boolean[1];
    2.29 +
    2.30 +        JavaSource.forFileObject(testFile).runUserActionTask(new Task<CompilationController>() {
    2.31 +            @Override public void run(CompilationController parameter) throws Exception {
    2.32 +                parameter.toPhase(JavaSource.Phase.RESOLVED);
    2.33 +
    2.34 +                TreePath tp = parameter.getTreeUtilities().pathFor(caret);
    2.35 +
    2.36 +                assertEquals(expected, IndexerImpl.treePosition(parameter.getTrees(), tp));
    2.37 +
    2.38 +                invoked[0] = true;
    2.39 +            }
    2.40 +        }, true);
    2.41 +
    2.42 +        assertTrue(invoked[0]);
    2.43 +    }
    2.44 +
    2.45      private void copyToFile(FileObject testFile, String code) throws IOException {
    2.46          OutputStream out = testFile.getOutputStream();
    2.47