Search in sources :

Example 11 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic in project eclipse.jdt.ls by eclipse.

the class WorkspaceDiagnosticsHandlerTest method testMavenMarkers.

@Test
public void testMavenMarkers() throws Exception {
    String msg1 = "Some dependency is missing";
    IMarker m1 = createMavenMarker(IMarker.SEVERITY_ERROR, msg1, 2, 95, 100);
    IDocument d = mock(IDocument.class);
    when(d.getLineOffset(1)).thenReturn(90);
    List<Diagnostic> diags = handler.toDiagnosticsArray(d, new IMarker[] { m1, null });
    assertEquals(1, diags.size());
    Range r;
    Diagnostic d1 = diags.get(0);
    assertEquals(msg1, d1.getMessage());
    assertEquals(DiagnosticSeverity.Error, d1.getSeverity());
    r = d1.getRange();
    assertEquals(1, r.getStart().getLine());
    assertEquals(95, r.getStart().getCharacter());
    assertEquals(1, r.getEnd().getLine());
    assertEquals(100, r.getEnd().getCharacter());
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic) IMarker(org.eclipse.core.resources.IMarker) Range(org.eclipse.lsp4j.Range) IDocument(org.eclipse.jface.text.IDocument) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 12 with Diagnostic

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

the class CommandUtil method getCommandsByDiagnostic.

/**
 * Get the command instances for a given diagnostic.
 * @param diagnostic        Diagnostic to get the command against
 * @param params            Code Action parameters
 * @param lsPackageCache    Lang Server Package cache
 * @return  {@link List}    List of commands related to the given diagnostic
 */
public static List<Command> getCommandsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, LSPackageCache lsPackageCache) {
    String diagnosticMessage = diagnostic.getMessage();
    List<Command> commands = new ArrayList<>();
    if (isUndefinedPackage(diagnosticMessage)) {
        String packageAlias = diagnosticMessage.substring(diagnosticMessage.indexOf("'") + 1, diagnosticMessage.lastIndexOf("'"));
        LSDocument sourceDocument = new LSDocument(params.getTextDocument().getUri());
        Path openedPath = CommonUtil.getPath(sourceDocument);
        String sourceRoot = TextDocumentServiceUtil.getSourceRoot(openedPath);
        sourceDocument.setSourceRoot(sourceRoot);
        lsPackageCache.getPackageMap().entrySet().stream().filter(pkgEntry -> {
            String fullPkgName = pkgEntry.getValue().packageID.orgName.getValue() + "/" + pkgEntry.getValue().packageID.getName().getValue();
            return fullPkgName.endsWith("." + packageAlias) || fullPkgName.endsWith("/" + packageAlias);
        }).forEach(pkgEntry -> {
            PackageID packageID = pkgEntry.getValue().packageID;
            String commandTitle = CommandConstants.IMPORT_PKG_TITLE + " " + packageID.getName().toString();
            String fullPkgName = packageID.getOrgName() + "/" + packageID.getName().getValue();
            CommandArgument pkgArgument = new CommandArgument(CommandConstants.ARG_KEY_PKG_NAME, fullPkgName);
            CommandArgument docUriArgument = new CommandArgument(CommandConstants.ARG_KEY_DOC_URI, params.getTextDocument().getUri());
            commands.add(new Command(commandTitle, CommandConstants.CMD_IMPORT_PACKAGE, new ArrayList<>(Arrays.asList(pkgArgument, docUriArgument))));
        });
    }
    return commands;
}
Also used : Path(java.nio.file.Path) CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) CommandConstants(org.ballerinalang.langserver.common.constants.CommandConstants) Diagnostic(org.eclipse.lsp4j.Diagnostic) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) LSDocument(org.ballerinalang.langserver.common.LSDocument) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) DocTag(org.ballerinalang.model.elements.DocTag) Locale(java.util.Locale) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) Path(java.nio.file.Path) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) PackageID(org.ballerinalang.model.elements.PackageID) FunctionNode(org.ballerinalang.model.tree.FunctionNode) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) TextDocumentServiceUtil(org.ballerinalang.langserver.TextDocumentServiceUtil) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) Command(org.eclipse.lsp4j.Command) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Collections(java.util.Collections) Command(org.eclipse.lsp4j.Command) LSDocument(org.ballerinalang.langserver.common.LSDocument) ArrayList(java.util.ArrayList) PackageID(org.ballerinalang.model.elements.PackageID)

Example 13 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic 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 14 with Diagnostic

use of org.eclipse.lsp4j.Diagnostic 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 Diagnostic

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

the class FreemarkerTextDocumentService method triggerValidation.

private void triggerValidation(TextDocumentItem document) {
    String uri = document.getUri();
    List<Diagnostic> diagnostics = validateFMDocument(uri, document.getText());
    fmLanguageServer.getLanguageClient().publishDiagnostics(new PublishDiagnosticsParams(uri, diagnostics));
}
Also used : PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Aggregations

Diagnostic (org.eclipse.lsp4j.Diagnostic)56 Test (org.junit.Test)31 Editor (org.springframework.ide.vscode.languageserver.testharness.Editor)22 Range (org.eclipse.lsp4j.Range)13 ArrayList (java.util.ArrayList)12 PublishDiagnosticsParams (org.eclipse.lsp4j.PublishDiagnosticsParams)11 List (java.util.List)8 Position (org.eclipse.lsp4j.Position)8 DiagnosticSeverity (org.eclipse.lsp4j.DiagnosticSeverity)7 URI (java.net.URI)5 Path (java.nio.file.Path)5 Map (java.util.Map)5 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)5 Command (org.eclipse.lsp4j.Command)5 MarkedString (org.eclipse.lsp4j.MarkedString)5 CodeAction (org.springframework.ide.vscode.languageserver.testharness.CodeAction)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 ImmutableList (com.google.common.collect.ImmutableList)3