newly created documents are now taken into account for indexing, makes rename refactoring work fine spring indexer

This commit is contained in:
Martin Lippert
2018-01-17 12:05:03 +01:00
parent 02831fc569
commit 851f5b29e5
2 changed files with 92 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
@@ -145,6 +146,9 @@ public class SpringIndexer {
server.getWorkspaceService().getFileObserver().onFileDeleted(globPattern, (file) -> {
deleteDocument(new TextDocumentIdentifier(file).getUri());
});
server.getWorkspaceService().getFileObserver().onFileCreated(globPattern, (file) -> {
createDocument(new TextDocumentIdentifier(file).getUri());
});
}
public CompletableFuture<Void> initialize(Collection<WorkspaceFolder> workspaceRoots) {
@@ -246,6 +250,29 @@ public class SpringIndexer {
return null;
}
public CompletableFuture<Void> createDocument(String docURI) {
synchronized(this) {
if (docURI.endsWith(".java") && lastInitializeItem != null) {
try {
Optional<IJavaProject> maybeProject = projectFinder.find(new TextDocumentIdentifier(docURI));
if (maybeProject.isPresent()) {
String[] classpathEntries = getClasspathEntries(maybeProject.get());
String content = FileUtils.readFileToString(new File(new URI(docURI)));
UpdateItem updateItem = new UpdateItem(docURI, content, classpathEntries);
updateQueue.put(updateItem);
return updateItem.getFuture();
}
}
catch (Exception e) {
Log.log(e);
}
}
}
return null;
}
public List<SymbolInformation> getAllSymbols(String query) {
waitForInitializeTask();

View File

@@ -154,6 +154,71 @@ public class SpringIndexerTest {
assertTrue(containsSymbol(allSymbols, "@/classlevel/mapping-subpackage", uriPrefix + "/src/main/java/org/test/sub/MappingClassSubpackage.java", 7, 1, 7, 38));
}
@Test
public void testNewDocumentCreated() throws Exception {
harness.intialize(new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-indexing-parent/test-annotation-indexing/").toURI()));
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-indexing-parent/test-annotation-indexing/").toURI());
String createdDocURI = "file://" + directory.getAbsolutePath() + "/src/main/java/org/test/CreatedClass.java";
// check for document to not be created yet
List<? extends SymbolInformation> symbols = indexer().getSymbols(createdDocURI);
assertNull(symbols);
List<? extends SymbolInformation> allSymbols = indexer().getAllSymbols("");
assertEquals(6, allSymbols.size());
try {
// create document and update index
String content = "package org.test;\n" +
"\n" +
"import org.springframework.web.bind.annotation.RequestMapping;\n" +
"\n" +
"public class SimpleMappingClass {\n" +
" \n" +
" @RequestMapping(\"created-mapping1\")\n" +
" public String hello1() {\n" +
" return \"hello1\";\n" +
" }\n" +
"\n" +
" @RequestMapping(\"created-mapping2\")\n" +
" public String hello2() {\n" +
" return \"hello2\";\n" +
" }\n" +
"\n" +
"}\n" +
"";
FileUtils.write(new File(new URI(createdDocURI)), content);
CompletableFuture<Void> createFuture = indexer().createDocument(createdDocURI);
createFuture.get(5, TimeUnit.SECONDS);
// check for updated index per document
symbols = indexer().getSymbols(createdDocURI);
assertEquals(2, symbols.size());
assertTrue(containsSymbol(symbols, "@/created-mapping1", createdDocURI, 6, 1, 6, 36));
assertTrue(containsSymbol(symbols, "@/created-mapping2", createdDocURI, 11, 1, 11, 36));
// check for updated index in all symbols
allSymbols = indexer().getAllSymbols("");
assertEquals(8, allSymbols.size());
String uriPrefix = "file://" + directory.getAbsolutePath();
assertTrue(containsSymbol(allSymbols, "@+ 'mainClass' (@SpringBootApplication <: @SpringBootConfiguration, @Configuration, @Component) MainClass", uriPrefix + "/src/main/java/org/test/MainClass.java", 6, 0, 6, 22));
assertTrue(containsSymbol(allSymbols, "@/embedded-foo-mapping", uriPrefix + "/src/main/java/org/test/MainClass.java", 17, 1, 17, 41));
assertTrue(containsSymbol(allSymbols, "@/foo-root-mapping/embedded-foo-mapping-with-root", uriPrefix + "/src/main/java/org/test/MainClass.java", 27, 1, 27, 51));
assertTrue(containsSymbol(allSymbols, "@/mapping1", uriPrefix + "/src/main/java/org/test/SimpleMappingClass.java", 6, 1, 6, 28));
assertTrue(containsSymbol(allSymbols, "@/mapping2", uriPrefix + "/src/main/java/org/test/SimpleMappingClass.java", 11, 1, 11, 28));
assertTrue(containsSymbol(allSymbols, "@/classlevel/mapping-subpackage", uriPrefix + "/src/main/java/org/test/sub/MappingClassSubpackage.java", 7, 1, 7, 38));
assertTrue(containsSymbol(allSymbols, "@/created-mapping1", createdDocURI, 6, 1, 6, 36));
assertTrue(containsSymbol(allSymbols, "@/created-mapping2", createdDocURI, 11, 1, 11, 36));
}
finally {
FileUtils.deleteQuietly(new File(new URI(createdDocURI)));
}
}
@Test
public void testRemoveSymbolsFromDeletedDocument() throws Exception {
harness.intialize(new File(ProjectsHarness.class.getResource("/test-projects/test-annotation-indexing-parent/test-annotation-indexing/").toURI()));