Search in sources :

Example 6 with BadLocationException

use of org.springframework.ide.vscode.commons.util.BadLocationException 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)

Example 7 with BadLocationException

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

the class WebfluxPathFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    boolean visitChildren = true;
    if (node != this.root) {
        IMethodBinding methodBinding = node.resolveMethodBinding();
        try {
            if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
                String name = methodBinding.getName();
                if (name != null && WebfluxUtils.REQUEST_PREDICATE_ALL_PATH_METHODS.contains(name)) {
                    StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(node);
                    if (stringLiteral != null) {
                        Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
                        path.add(new WebfluxRouteElement(stringLiteral.getLiteralValue(), range));
                    }
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        if (WebfluxUtils.isRouteMethodInvocation(methodBinding)) {
            visitChildren = false;
        }
    }
    return visitChildren;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 8 with BadLocationException

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

the class WebfluxRouterSymbolProvider method extractContentTypes.

private WebfluxRouteElement[] extractContentTypes(MethodInvocation routerInvocation, TextDocument doc) {
    WebfluxContentTypeFinder contentTypeFinder = new WebfluxContentTypeFinder(doc);
    List<?> arguments = routerInvocation.arguments();
    for (Object argument : arguments) {
        if (argument != null && argument instanceof ASTNode) {
            ((ASTNode) argument).accept(contentTypeFinder);
        }
    }
    final List<WebfluxRouteElement> contentTypes = contentTypeFinder.getContentTypes();
    extractNestedValue(routerInvocation, contentTypes, (methodInvocation) -> {
        IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
        String methodName = methodBinding.getName();
        try {
            if (WebfluxUtils.REQUEST_PREDICATE_CONTENT_TYPE_METHOD.equals(methodName)) {
                SimpleName nameArgument = WebfluxUtils.extractSimpleNameArgument(methodInvocation);
                if (nameArgument != null && nameArgument.getFullyQualifiedName() != null) {
                    Range range = doc.toRange(nameArgument.getStartPosition(), nameArgument.getLength());
                    return new WebfluxRouteElement(nameArgument.getFullyQualifiedName().toString(), range);
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        return null;
    });
    return (WebfluxRouteElement[]) contentTypes.toArray(new WebfluxRouteElement[contentTypes.size()]);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 9 with BadLocationException

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

the class WebfluxRouterSymbolProvider method extractMappingSymbol.

protected void extractMappingSymbol(MethodInvocation node, TextDocument doc, List<EnhancedSymbolInformation> result) {
    WebfluxRouteElement[] pathElements = extractPath(node, doc);
    WebfluxRouteElement[] httpMethods = extractMethods(node, doc);
    WebfluxRouteElement[] contentTypes = extractContentTypes(node, doc);
    WebfluxRouteElement[] acceptTypes = extractAcceptTypes(node, doc);
    int methodNameStart = node.getName().getStartPosition();
    int invocationStart = node.getStartPosition();
    StringBuilder pathBuilder = new StringBuilder();
    for (WebfluxRouteElement pathElement : pathElements) {
        pathBuilder.insert(0, pathElement.getElement());
    }
    String path = pathBuilder.toString();
    if (path.length() > 0) {
        try {
            Location location = new Location(doc.getUri(), doc.toRange(methodNameStart, node.getLength() - (methodNameStart - invocationStart)));
            WebfluxHandlerInformation handler = extractHandlerInformation(node, path, httpMethods, contentTypes, acceptTypes);
            WebfluxElementsInformation elements = extractElementsInformation(pathElements, httpMethods, contentTypes, acceptTypes);
            result.add(RouteUtils.createRouteSymbol(location, path, getElementStrings(httpMethods), getElementStrings(contentTypes), getElementStrings(acceptTypes), new SymbolAddOnInformation[] { handler, elements }));
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
Also used : SymbolAddOnInformation(org.springframework.ide.vscode.boot.java.handlers.SymbolAddOnInformation) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) Location(org.eclipse.lsp4j.Location)

Example 10 with BadLocationException

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

the class WebfluxContentTypeFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    IMethodBinding methodBinding = node.resolveMethodBinding();
    try {
        if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
            String name = methodBinding.getName();
            if (name != null && WebfluxUtils.REQUEST_PREDICATE_CONTENT_TYPE_METHOD.equals(name)) {
                SimpleName nameArgument = WebfluxUtils.extractSimpleNameArgument(node);
                if (nameArgument != null && nameArgument.getFullyQualifiedName() != null) {
                    Range range = doc.toRange(nameArgument.getStartPosition(), nameArgument.getLength());
                    contentTypes.add(new WebfluxRouteElement(nameArgument.getFullyQualifiedName().toString(), range));
                }
            }
        }
    } catch (BadLocationException e) {
    // ignore
    }
    return !WebfluxUtils.isRouteMethodInvocation(methodBinding);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)23 Range (org.eclipse.lsp4j.Range)10 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)9 Location (org.eclipse.lsp4j.Location)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)3 ImmutableList (com.google.common.collect.ImmutableList)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)2 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1