Search in sources :

Example 6 with TextDocument

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

the class SpringIndexer method scanFile.

private void scanFile(String docURI, String content, String[] classpathEntries) throws Exception {
    ASTParser parser = ASTParser.newParser(AST.JLS10);
    Map<String, String> options = JavaCore.getOptions();
    JavaCore.setComplianceOptions(JavaCore.VERSION_10, options);
    parser.setCompilerOptions(options);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setStatementsRecovery(true);
    parser.setBindingsRecovery(true);
    parser.setResolveBindings(true);
    parser.setIgnoreMethodBodies(false);
    String[] sourceEntries = new String[] {};
    parser.setEnvironment(classpathEntries, sourceEntries, null, true);
    String unitName = docURI.substring(docURI.lastIndexOf("/"));
    parser.setUnitName(unitName);
    parser.setSource(content.toCharArray());
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    if (cu != null) {
        List<SymbolInformation> oldSymbols = symbolsByDoc.remove(docURI);
        if (oldSymbols != null) {
            symbols.removeAll(oldSymbols);
        }
        List<SymbolAddOnInformation> oldAddOnInformation = addonInformationByDoc.remove(docURI);
        if (oldAddOnInformation != null) {
            addonInformation.removeAll(oldAddOnInformation);
        }
        AtomicReference<TextDocument> docRef = new AtomicReference<>();
        scanAST(cu, docURI, docRef, content);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) AtomicReference(java.util.concurrent.atomic.AtomicReference) ASTParser(org.eclipse.jdt.core.dom.ASTParser) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) SymbolAddOnInformation(org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation)

Example 7 with TextDocument

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

the class SpringIndexer method extractSymbolInformation.

private void extractSymbolInformation(MethodDeclaration methodDeclaration, String docURI, AtomicReference<TextDocument> docRef, String content) throws Exception {
    Collection<SymbolProvider> providers = symbolProviders.getAll();
    if (!providers.isEmpty()) {
        TextDocument doc = getTempTextDocument(docURI, docRef, content);
        for (SymbolProvider provider : providers) {
            Collection<EnhancedSymbolInformation> sbls = provider.getSymbols(methodDeclaration, doc);
            if (sbls != null) {
                sbls.forEach(enhancedSymbol -> {
                    symbols.add(enhancedSymbol.getSymbol());
                    symbolsByDoc.computeIfAbsent(docURI, s -> new ArrayList<SymbolInformation>()).add(enhancedSymbol.getSymbol());
                    if (enhancedSymbol.getAdditionalInformation() != null) {
                        addonInformation.addAll(Arrays.asList(enhancedSymbol.getAdditionalInformation()));
                        addonInformationByDoc.computeIfAbsent(docURI, s -> new ArrayList<SymbolAddOnInformation>()).addAll(Arrays.asList(enhancedSymbol.getAdditionalInformation()));
                    }
                });
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SymbolProvider(org.springframework.ide.vscode.boot.java.handlers.SymbolProvider) LoggerFactory(org.slf4j.LoggerFactory) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FileASTRequestor(org.eclipse.jdt.core.dom.FileASTRequestor) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) AnnotationHierarchyAwareLookup(org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchyAwareLookup) URI(java.net.URI) Path(java.nio.file.Path) ExceptionUtil(org.springframework.ide.vscode.commons.util.ExceptionUtil) Predicate(java.util.function.Predicate) SymbolAddOnInformation(org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) List(java.util.List) Stream(java.util.stream.Stream) ASTParser(org.eclipse.jdt.core.dom.ASTParser) WorkspaceFolder(org.eclipse.lsp4j.WorkspaceFolder) AST(org.eclipse.jdt.core.dom.AST) Optional(java.util.Optional) SimpleWorkspaceService(org.springframework.ide.vscode.commons.languageserver.util.SimpleWorkspaceService) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleLanguageServer(org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) JavaProjectFinder(org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder) SymbolKind(org.eclipse.lsp4j.SymbolKind) BootLanguageServerParams(org.springframework.ide.vscode.boot.BootLanguageServerParams) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) ProjectObserver(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) ImmutableList(com.google.common.collect.ImmutableList) Annotation(org.eclipse.jdt.core.dom.Annotation) AnnotationHierarchies(org.springframework.ide.vscode.boot.java.annotations.AnnotationHierarchies) UriUtil(org.springframework.ide.vscode.commons.util.UriUtil) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation) IClasspath(org.springframework.ide.vscode.commons.java.IClasspath) LanguageId(org.springframework.ide.vscode.commons.util.text.LanguageId) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Logger(org.slf4j.Logger) Files(java.nio.file.Files) JavaCore(org.eclipse.jdt.core.JavaCore) Futures(org.springframework.ide.vscode.commons.util.Futures) FileUtils(org.apache.commons.io.FileUtils) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) File(java.io.File) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Listener(org.springframework.ide.vscode.commons.languageserver.java.ProjectObserver.Listener) Paths(java.nio.file.Paths) Collections(java.util.Collections) SymbolProvider(org.springframework.ide.vscode.boot.java.handlers.SymbolProvider) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ArrayList(java.util.ArrayList) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation)

Example 8 with TextDocument

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

the class SpringIndexer method provideDefaultSymbol.

private SymbolInformation provideDefaultSymbol(Annotation node, String docURI, AtomicReference<TextDocument> docRef, String content) {
    try {
        ITypeBinding type = node.resolveTypeBinding();
        if (type != null) {
            String qualifiedName = type.getQualifiedName();
            if (qualifiedName != null && qualifiedName.startsWith("org.springframework")) {
                TextDocument doc = getTempTextDocument(docURI, docRef, content);
                SymbolInformation symbol = new SymbolInformation(node.toString(), SymbolKind.Interface, new Location(doc.getUri(), doc.toRange(node.getStartPosition(), node.getLength())));
                return symbol;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) EnhancedSymbolInformation(org.springframework.ide.vscode.boot.java.handlers.EnhancedSymbolInformation) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Location(org.eclipse.lsp4j.Location)

Example 9 with TextDocument

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

the class SpringLiveHoverWatchdog method update.

public void update(String docURI, SpringBootApp[] runningBootApps) {
    if (highlightsEnabled) {
        try {
            if (runningBootApps == null) {
                runningBootApps = runningAppProvider.getAllRunningSpringApps().toArray(new SpringBootApp[0]);
            }
            if (runningBootApps != null && runningBootApps.length > 0) {
                TextDocument doc = this.server.getTextDocumentService().get(docURI);
                if (doc != null) {
                    Range[] ranges = this.hoverProvider.getLiveHoverHints(doc, runningBootApps);
                    publishLiveHints(docURI, ranges);
                }
            } else {
                cleanupLiveHints(docURI);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : SpringBootApp(org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) Range(org.eclipse.lsp4j.Range)

Example 10 with TextDocument

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

the class ValuePropertyReferencesProvider method findReferencesInPropertiesFile.

private List<Location> findReferencesInPropertiesFile(String filePath, String propertyKey) {
    List<Location> foundLocations = new ArrayList<>();
    try {
        String fileContent = FileUtils.readFileToString(new File(filePath));
        Parser parser = new AntlrParser();
        ParseResults parseResults = parser.parse(fileContent);
        if (parseResults != null && parseResults.ast != null) {
            parseResults.ast.getNodes(KeyValuePair.class).forEach(pair -> {
                if (pair.getKey() != null && pair.getKey().decode().equals(propertyKey)) {
                    URI docURI = Paths.get(filePath).toUri();
                    TextDocument doc = new TextDocument(docURI.toString(), null);
                    doc.setText(fileContent);
                    try {
                        int line = doc.getLineOfOffset(pair.getKey().getOffset());
                        int startInLine = pair.getKey().getOffset() - doc.getLineOffset(line);
                        int endInLine = startInLine + (pair.getKey().getLength());
                        Position start = new Position();
                        start.setLine(line);
                        start.setCharacter(startInLine);
                        Position end = new Position();
                        end.setLine(line);
                        end.setCharacter(endInLine);
                        Range range = new Range();
                        range.setStart(start);
                        range.setEnd(end);
                        Location location = new Location(docURI.toString(), range);
                        foundLocations.add(location);
                    } catch (BadLocationException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return foundLocations;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) KeyValuePair(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.KeyValuePair) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) YamlParser(org.springframework.ide.vscode.commons.yaml.ast.YamlParser) Parser(org.springframework.ide.vscode.java.properties.parser.Parser) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) File(java.io.File) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Location(org.eclipse.lsp4j.Location)

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