Search in sources :

Example 16 with Range

use of org.eclipse.lsp4j.Range in project sts4 by spring-projects.

the class Editor method apply.

public void apply(CompletionItem completion) throws Exception {
    completion = harness.resolveCompletionItem(completion);
    TextEdit edit = completion.getTextEdit();
    String docText = doc.getText();
    if (edit != null) {
        String replaceWith = edit.getNewText();
        int cursorReplaceOffset = 0;
        if (!Boolean.getBoolean("lsp.completions.indentation.enable")) {
            // Apply indentfix, this is magic vscode seems to apply to edits returned by language server. So our harness has to
            // mimick that behavior. See https://github.com/Microsoft/language-server-protocol/issues/83
            int referenceLine = edit.getRange().getStart().getLine();
            int cursorOffset = edit.getRange().getStart().getCharacter();
            String referenceIndent = doc.getLineIndentString(referenceLine);
            if (cursorOffset < referenceIndent.length()) {
                referenceIndent = referenceIndent.substring(0, cursorOffset);
            }
            replaceWith = replaceWith.replaceAll("\\n", "\n" + referenceIndent);
        }
        // Replace the cursor string
        cursorReplaceOffset = replaceWith.indexOf(VS_CODE_CURSOR_MARKER);
        if (cursorReplaceOffset >= 0) {
            replaceWith = replaceWith.substring(0, cursorReplaceOffset) + replaceWith.substring(cursorReplaceOffset + VS_CODE_CURSOR_MARKER.length());
        } else {
            cursorReplaceOffset = replaceWith.length();
        }
        Range rng = edit.getRange();
        int start = doc.toOffset(rng.getStart());
        int end = doc.toOffset(rng.getEnd());
        replaceText(start, end, replaceWith);
        selectionStart = selectionEnd = start + cursorReplaceOffset;
    } else {
        String insertText = getInsertText(completion);
        String newText = docText.substring(0, selectionStart) + insertText + docText.substring(selectionStart);
        selectionStart += insertText.length();
        selectionEnd += insertText.length();
        setRawText(newText);
    }
}
Also used : TextEdit(org.eclipse.lsp4j.TextEdit) MarkedString(org.eclipse.lsp4j.MarkedString) Range(org.eclipse.lsp4j.Range)

Example 17 with Range

use of org.eclipse.lsp4j.Range 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 18 with Range

use of org.eclipse.lsp4j.Range in project sts4 by spring-projects.

the class ValueHoverProvider method provideHover.

private Hover provideHover(String value, int offset, int nodeStartOffset, TextDocument doc, SpringBootApp[] runningApps) {
    try {
        LocalRange range = getPropertyRange(value, offset);
        if (range != null) {
            String propertyKey = value.substring(range.getStart(), range.getEnd());
            if (propertyKey != null) {
                Map<SpringBootApp, JSONObject> allProperties = getPropertiesFromProcesses(runningApps);
                StringBuilder hover = new StringBuilder();
                for (SpringBootApp app : allProperties.keySet()) {
                    JSONObject properties = allProperties.get(app);
                    Iterator<?> keys = properties.keys();
                    while (keys.hasNext()) {
                        String key = (String) keys.next();
                        if (properties.get(key) instanceof JSONObject) {
                            JSONObject props = properties.getJSONObject(key);
                            if (props.has(propertyKey)) {
                                String propertyValue = props.getString(propertyKey);
                                hover.append(propertyKey + " : " + propertyValue);
                                hover.append(" (from: " + key + ")\n\n");
                                hover.append(LiveHoverUtils.niceAppName(app));
                                hover.append("\n\n");
                            }
                        }
                    }
                }
                if (hover.length() > 0) {
                    Range hoverRange = doc.toRange(nodeStartOffset + range.getStart(), range.getEnd() - range.getStart());
                    Hover result = new Hover(ImmutableList.of(Either.forLeft(hover.toString())));
                    result.setRange(hoverRange);
                    return result;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SpringBootApp(org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp) JSONObject(org.json.JSONObject) Hover(org.eclipse.lsp4j.Hover) Range(org.eclipse.lsp4j.Range)

Example 19 with Range

use of org.eclipse.lsp4j.Range 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 20 with Range

use of org.eclipse.lsp4j.Range in project sts4 by spring-projects.

the class ValuePropertyReferencesProvider method findReferencesInYMLFile.

private List<Location> findReferencesInYMLFile(String filePath, String propertyKey) {
    List<Location> foundLocations = new ArrayList<>();
    try {
        String fileContent = FileUtils.readFileToString(new File(filePath));
        Yaml yaml = new Yaml();
        YamlASTProvider parser = new YamlParser(yaml);
        URI docURI = Paths.get(filePath).toUri();
        TextDocument doc = new TextDocument(docURI.toString(), null);
        doc.setText(fileContent);
        YamlFileAST ast = parser.getAST(doc);
        List<Node> nodes = ast.getNodes();
        if (nodes != null && !nodes.isEmpty()) {
            for (Node node : nodes) {
                Node foundNode = findNode(node, "", propertyKey);
                if (foundNode != null) {
                    Position start = new Position();
                    start.setLine(foundNode.getStartMark().getLine());
                    start.setCharacter(foundNode.getStartMark().getColumn());
                    Position end = new Position();
                    end.setLine(foundNode.getEndMark().getLine());
                    end.setCharacter(foundNode.getEndMark().getColumn());
                    Range range = new Range();
                    range.setStart(start);
                    range.setEnd(end);
                    Location location = new Location(docURI.toString(), range);
                    foundLocations.add(location);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return foundLocations;
}
Also used : YamlParser(org.springframework.ide.vscode.commons.yaml.ast.YamlParser) TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) YamlFileAST(org.springframework.ide.vscode.commons.yaml.ast.YamlFileAST) Position(org.eclipse.lsp4j.Position) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) Node(org.yaml.snakeyaml.nodes.Node) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) YamlASTProvider(org.springframework.ide.vscode.commons.yaml.ast.YamlASTProvider) Range(org.eclipse.lsp4j.Range) URI(java.net.URI) Yaml(org.yaml.snakeyaml.Yaml) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) File(java.io.File) Location(org.eclipse.lsp4j.Location)

Aggregations

Range (org.eclipse.lsp4j.Range)293 Test (org.junit.Test)178 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)153 Position (org.eclipse.lsp4j.Position)111 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)105 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)54 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)47 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)47 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)41 ArrayList (java.util.ArrayList)39 Command (org.eclipse.lsp4j.Command)34 List (java.util.List)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)31 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)30 Diagnostic (org.eclipse.lsp4j.Diagnostic)29 TextEdit (org.eclipse.lsp4j.TextEdit)29 CodeAction (org.eclipse.lsp4j.CodeAction)28 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)27 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)25 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21