Search in sources :

Example 26 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class CompilationUnitCacheTest method cu_cache_invalidated_by_doc_change.

@Test
public void cu_cache_invalidated_by_doc_change() throws Exception {
    harness = BootJavaLanguageServerHarness.builder().mockDefaults().build();
    harness.useProject(new IJavaProject() {

        @Override
        public IClasspath getClasspath() {
            return new DelegatingCachedClasspath<>(() -> null, null);
        }
    });
    harness.intialize(null);
    TextDocument doc = new TextDocument(harness.createTempUri(), LanguageId.JAVA, 0, "package my.package\n" + "\n" + "public class SomeClass {\n" + "\n" + "}\n");
    harness.newEditorFromFileUri(doc.getUri(), doc.getLanguageId());
    CompilationUnit cu = getCompilationUnit(doc);
    assertNotNull(cu);
    harness.changeDocument(doc.getUri(), 0, 0, "     ");
    CompilationUnit cuAnother = getCompilationUnit(doc);
    assertNotNull(cuAnother);
    assertFalse(cu == cuAnother);
    CompilationUnit cuYetAnother = getCompilationUnit(doc);
    assertTrue(cuAnother == cuYetAnother);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) IClasspath(org.springframework.ide.vscode.commons.java.IClasspath) Test(org.junit.Test)

Example 27 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class SpringIndexerHarness method getSymbolsInFile.

public List<TestSymbolInfo> getSymbolsInFile(String docURI) throws Exception {
    List<? extends SymbolInformation> symbols = indexer.getSymbols(docURI);
    if (symbols != null) {
        symbols = new ArrayList<>(symbols);
        Collections.sort(symbols, SYMBOL_COMPARATOR);
        TextDocument doc = new TextDocument(docURI, LanguageId.JAVA, 1, IOUtils.toString(new URI(docURI)));
        ImmutableList.Builder<TestSymbolInfo> symbolInfos = ImmutableList.builder();
        for (SymbolInformation s : symbols) {
            int start = doc.toOffset(s.getLocation().getRange().getStart());
            int end = doc.toOffset(s.getLocation().getRange().getEnd());
            symbolInfos.add(new TestSymbolInfo(doc.textBetween(start, end), s.getName()));
        }
        return symbolInfos.build();
    }
    return ImmutableList.of();
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ImmutableList(com.google.common.collect.ImmutableList) URI(java.net.URI) SymbolInformation(org.eclipse.lsp4j.SymbolInformation)

Example 28 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class SpringPropertiesIndexTest method testPropertiesIndexRefreshOnProjectChange.

@Test
public void testPropertiesIndexRefreshOnProjectChange() throws Exception {
    harness.intialize(new File(ProjectsHarness.class.getResource("/test-projects/boot-1.2.0-properties-live-metadta/").toURI()));
    propertyIndexProvider = (DefaultSpringPropertyIndexProvider) harness.getServerWrapper().getComponents().get(BootPropertiesLanguageServerComponents.class).getPropertiesIndexProvider();
    File directory = new File(ProjectsHarness.class.getResource("/test-projects/boot-1.2.0-properties-live-metadta/").toURI());
    File javaFile = new File(directory, "/src/main/java/demo/Application.java");
    TextDocument doc = new TextDocument(javaFile.toURI().toString(), LanguageId.JAVA);
    // Not cached yet, hence progress service invoked
    ProgressService progressService = mock(ProgressService.class);
    propertyIndexProvider.setProgressService(progressService);
    propertyIndexProvider.getIndex(doc);
    verify(progressService, atLeastOnce()).progressEvent(anyObject(), anyObject());
    // Should be cached now, so progress service should not be touched
    progressService = mock(ProgressService.class);
    propertyIndexProvider.setProgressService(progressService);
    propertyIndexProvider.getIndex(doc);
    verify(progressService, never()).progressEvent(anyObject(), anyObject());
    // Change POM file for the project
    harness.changeFile(new File(directory, MavenCore.POM_XML).toURI().toString());
    // POM has changed, hence project needs to be reloaded, cached value is cleared
    progressService = mock(ProgressService.class);
    propertyIndexProvider.setProgressService(progressService);
    propertyIndexProvider.getIndex(doc);
    verify(progressService, atLeastOnce()).progressEvent(anyObject(), anyObject());
}
Also used : BootPropertiesLanguageServerComponents(org.springframework.ide.vscode.boot.properties.BootPropertiesLanguageServerComponents) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ProjectsHarness(org.springframework.ide.vscode.project.harness.ProjectsHarness) ProgressService(org.springframework.ide.vscode.commons.languageserver.ProgressService) File(java.io.File) Test(org.junit.Test)

Example 29 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class RequestMappingSymbolProvider method getSymbols.

@Override
public Collection<EnhancedSymbolInformation> getSymbols(Annotation node, ITypeBinding annotationType, Collection<ITypeBinding> metaAnnotations, TextDocument doc) {
    if (node.getParent() instanceof MethodDeclaration) {
        try {
            Location location = new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength()));
            String[] path = getPath(node);
            String[] parentPath = getParentPath(node);
            String[] methods = getMethod(node);
            String[] contentTypes = getContentTypes(node);
            String[] acceptTypes = getAcceptTypes(node);
            return (parentPath == null ? Stream.of("") : Arrays.stream(parentPath)).filter(Objects::nonNull).flatMap(parent -> (path == null ? Stream.<String>empty() : Arrays.stream(path)).filter(Objects::nonNull).map(p -> {
                String separator = !parent.endsWith("/") && !p.startsWith("/") ? "/" : "";
                String resultPath = parent + separator + p;
                if (resultPath.endsWith("/")) {
                    resultPath = resultPath.substring(0, resultPath.length() - 1);
                }
                return resultPath.startsWith("/") ? resultPath : "/" + resultPath;
            })).map(p -> RouteUtils.createRouteSymbol(location, p, methods, contentTypes, acceptTypes, null)).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Arrays(java.util.Arrays) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Iterator(java.util.Iterator) Collection(java.util.Collection) SymbolProvider(org.springframework.ide.vscode.boot.java.handlers.SymbolProvider) Annotations(org.springframework.ide.vscode.boot.java.Annotations) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) Collectors(java.util.stream.Collectors) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTUtils(org.springframework.ide.vscode.boot.java.utils.ASTUtils) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Expression(org.eclipse.jdt.core.dom.Expression) Annotation(org.eclipse.jdt.core.dom.Annotation) Location(org.eclipse.lsp4j.Location) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Objects(java.util.Objects) Location(org.eclipse.lsp4j.Location)

Example 30 with TextDocument

use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.

the class BootJavaDocumentHighlightEngine method handle.

@Override
public List<DocumentHighlight> handle(TextDocumentPositionParams params) {
    SimpleTextDocumentService documents = server.getTextDocumentService();
    String docURI = params.getTextDocument().getUri();
    if (documents.get(docURI) != null) {
        TextDocument doc = documents.get(docURI).copy();
        try {
            return provideDocumentHighlights(doc, params.getPosition());
        } catch (Exception e) {
            log.error("", e);
        }
    }
    return SimpleTextDocumentService.NO_HIGHLIGHTS;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) SimpleTextDocumentService(org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService)

Aggregations

TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)43 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)12 URI (java.net.URI)11 File (java.io.File)10 Location (org.eclipse.lsp4j.Location)8 Test (org.junit.Test)8 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)7 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)7 IClasspath (org.springframework.ide.vscode.commons.java.IClasspath)7 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 Range (org.eclipse.lsp4j.Range)6 Collection (java.util.Collection)5 Map (java.util.Map)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Stream (java.util.stream.Stream)5 ASTParser (org.eclipse.jdt.core.dom.ASTParser)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5