Search in sources :

Example 11 with Range

use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method publishDiagnostics.

private void publishDiagnostics(List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics, Path path) {
    Map<String, List<Diagnostic>> diagnosticsMap = new HashMap<>();
    balDiagnostics.forEach(diagnostic -> {
        Diagnostic d = new Diagnostic();
        d.setSeverity(DiagnosticSeverity.Error);
        d.setMessage(diagnostic.getMessage());
        Range r = new Range();
        // LSP diagnostics range is 0 based
        int startLine = diagnostic.getPosition().getStartLine() - 1;
        int startChar = diagnostic.getPosition().getStartColumn() - 1;
        int endLine = diagnostic.getPosition().getEndLine() - 1;
        int endChar = diagnostic.getPosition().getEndColumn() - 1;
        if (endLine <= 0) {
            endLine = startLine;
        }
        if (endChar <= 0) {
            endChar = startChar + 1;
        }
        r.setStart(new Position(startLine, startChar));
        r.setEnd(new Position(endLine, endChar));
        d.setRange(r);
        String fileName = diagnostic.getPosition().getSource().getCompilationUnitName();
        Path filePath = Paths.get(path.getParent().toString(), fileName);
        String fileURI = filePath.toUri().toString();
        if (!diagnosticsMap.containsKey(fileURI)) {
            diagnosticsMap.put(fileURI, new ArrayList<Diagnostic>());
        }
        List<Diagnostic> clientDiagnostics = diagnosticsMap.get(fileURI);
        clientDiagnostics.add(d);
    });
    // clear previous diagnostics
    List<Diagnostic> empty = new ArrayList<Diagnostic>(0);
    for (Map.Entry<String, List<Diagnostic>> entry : lastDiagnosticMap.entrySet()) {
        if (diagnosticsMap.containsKey(entry.getKey())) {
            continue;
        }
        PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
        diagnostics.setUri(entry.getKey());
        diagnostics.setDiagnostics(empty);
        this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
    }
    for (Map.Entry<String, List<Diagnostic>> entry : diagnosticsMap.entrySet()) {
        PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
        diagnostics.setUri(entry.getKey());
        diagnostics.setDiagnostics(entry.getValue());
        this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
    }
    lastDiagnosticMap = diagnosticsMap;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) MarkedString(org.eclipse.lsp4j.MarkedString) Range(org.eclipse.lsp4j.Range) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) List(java.util.List) ArrayList(java.util.ArrayList) CompletionList(org.eclipse.lsp4j.CompletionList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with Range

use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method formatting.

@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
    return CompletableFuture.supplyAsync(() -> {
        String textEditContent = null;
        TextDocumentServiceContext formatContext = new TextDocumentServiceContext();
        formatContext.put(DocumentServiceKeys.FILE_URI_KEY, params.getTextDocument().getUri());
        LSDocument document = new LSDocument(params.getTextDocument().getUri());
        String fileContent = documentManager.getFileContent(CommonUtil.getPath(document));
        String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
        int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
        int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
        int totalLines = contentComponents.length;
        Range range = new Range(new Position(0, 0), new Position(totalLines, lastCharCol));
        try {
            // Source generation for given ast.
            JsonObject ast = TextDocumentFormatUtil.getAST(params, documentManager, formatContext);
            SourceGen sourceGen = new SourceGen(0);
            sourceGen.build(ast.getAsJsonObject("model"), null, "CompilationUnit");
            textEditContent = sourceGen.getSourceOf(ast.getAsJsonObject("model"), true, false);
        } catch (Exception e) {
        // Ignore
        }
        TextEdit textEdit = textEditContent != null ? new TextEdit(range, textEditContent) : null;
        return Collections.singletonList(textEdit);
    });
}
Also used : Position(org.eclipse.lsp4j.Position) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextEdit(org.eclipse.lsp4j.TextEdit) JsonObject(com.google.gson.JsonObject) MarkedString(org.eclipse.lsp4j.MarkedString) Range(org.eclipse.lsp4j.Range)

Example 13 with Range

use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.

the class RenameUtil method getRenameTextEdits.

/**
 * Get the list of rename related TextEdits.
 *
 * @param locationList      List of locations of occurrences
 * @param documentManager   {@link WorkspaceDocumentManager} instance
 * @param newName           New name to be replaced with
 * @param replaceSymbolName Symbol name being replaced
 * @return {@link List}         List of TextEdits
 */
public static List<TextDocumentEdit> getRenameTextEdits(List<Location> locationList, WorkspaceDocumentManager documentManager, String newName, String replaceSymbolName) {
    Map<String, ArrayList<Location>> documentLocationMap = new HashMap<>();
    List<TextDocumentEdit> documentEdits = new ArrayList<>();
    Comparator<Location> locationComparator = (location1, location2) -> location1.getRange().getStart().getCharacter() - location2.getRange().getStart().getCharacter();
    locationList.forEach(location -> {
        if (documentLocationMap.containsKey(location.getUri())) {
            documentLocationMap.get(location.getUri()).add(location);
        } else {
            documentLocationMap.put(location.getUri(), (ArrayList<Location>) Lists.of(location));
        }
    });
    documentLocationMap.forEach((uri, locations) -> {
        Collections.sort(locations, locationComparator);
        String fileContent = documentManager.getFileContent(CommonUtil.getPath(new LSDocument(uri)));
        String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
        int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
        int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
        for (Location location : locations) {
            int line = location.getRange().getStart().getLine();
            StringBuilder lineComponent = new StringBuilder(contentComponents[line]);
            int index = lineComponent.indexOf(replaceSymbolName);
            while (index >= 0) {
                char previousChar = lineComponent.charAt(index - 1);
                if (Character.isLetterOrDigit(previousChar) || String.valueOf(previousChar).equals("_")) {
                    index = lineComponent.indexOf(replaceSymbolName, index + replaceSymbolName.length());
                } else {
                    lineComponent.replace(index, index + replaceSymbolName.length(), newName);
                    index = lineComponent.indexOf(replaceSymbolName, index + newName.length());
                }
            }
            contentComponents[line] = lineComponent.toString();
        }
        Range range = new Range(new Position(0, 0), new Position(contentComponents.length, lastCharCol));
        TextEdit textEdit = new TextEdit(range, String.join("\r\n", Arrays.asList(contentComponents)));
        VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
        textDocumentIdentifier.setUri(uri);
        TextDocumentEdit textDocumentEdit = new TextDocumentEdit(textDocumentIdentifier, Collections.singletonList(textEdit));
        documentEdits.add(textDocumentEdit);
    });
    return documentEdits;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) Range(org.eclipse.lsp4j.Range) HashMap(java.util.HashMap) Lists(org.wso2.ballerinalang.util.Lists) ArrayList(java.util.ArrayList) LSDocument(org.ballerinalang.langserver.common.LSDocument) List(java.util.List) TextEdit(org.eclipse.lsp4j.TextEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) Position(org.eclipse.lsp4j.Position) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) WorkspaceDocumentManager(org.ballerinalang.langserver.workspace.WorkspaceDocumentManager) RenameParams(org.eclipse.lsp4j.RenameParams) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Range(org.eclipse.lsp4j.Range) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextEdit(org.eclipse.lsp4j.TextEdit) Location(org.eclipse.lsp4j.Location)

Example 14 with Range

use of org.eclipse.lsp4j.Range in project freemarker-languageserver by angelozerr.

the class FreemarkerTextDocumentService method validateFMDocument.

private List<Diagnostic> validateFMDocument(String xmlDocumentUri, String xmlDocumentContent) {
    List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
    if (fmConfiguration == null) {
        fmConfiguration = new Configuration(Configuration.getVersion());
        fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
        fmConfiguration.setTabSize(1);
    }
    try {
        @SuppressWarnings("unused") Template dummy = new Template(xmlDocumentUri, xmlDocumentContent, fmConfiguration);
    } catch (ParseException e) {
        Position start = new Position(e.getLineNumber() - 1, e.getColumnNumber());
        Position end = new Position(e.getEndLineNumber() - 1, e.getEndColumnNumber());
        Diagnostic diagnostic = new Diagnostic(new Range(start, end), e.getEditorMessage());
        diagnostics.add(diagnostic);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return diagnostics;
}
Also used : Configuration(freemarker.template.Configuration) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) ParseException(freemarker.core.ParseException) IOException(java.io.IOException) Range(org.eclipse.lsp4j.Range) Template(freemarker.template.Template)

Example 15 with Range

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

the class Editor method assertHighlights.

public List<Range> assertHighlights(String... expectedHighlights) throws Exception {
    HighlightParams highlights = harness.getHighlights(doc);
    List<Range> ranges = new ArrayList<>(highlights.getRanges());
    Collections.sort(ranges, RANGE_COMPARATOR);
    List<String> actualHighlights = ranges.stream().map(this::getText).collect(Collectors.toList());
    assertEquals(ImmutableList.copyOf(expectedHighlights), actualHighlights);
    return ranges;
}
Also used : ArrayList(java.util.ArrayList) HighlightParams(org.springframework.ide.vscode.commons.languageserver.HighlightParams) MarkedString(org.eclipse.lsp4j.MarkedString) Range(org.eclipse.lsp4j.Range)

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