Search in sources :

Example 6 with Diagnostic

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

the class BuildWorkspaceHandler method publishDiagnostics.

private void publishDiagnostics(List<IMarker> markers) {
    Map<IResource, List<IMarker>> map = markers.stream().collect(Collectors.groupingBy(IMarker::getResource));
    for (Map.Entry<IResource, List<IMarker>> entry : map.entrySet()) {
        IResource resource = entry.getKey();
        // ignore problems caused by standalone files
        if (JavaLanguageServerPlugin.getProjectsManager().getDefaultProject().equals(resource.getProject())) {
            continue;
        }
        IFile file = resource.getAdapter(IFile.class);
        if (file == null) {
            continue;
        }
        IDocument document = null;
        String uri = JDTUtils.getFileURI(resource);
        if (JavaCore.isJavaLikeFileName(file.getName())) {
            ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
            try {
                document = JsonRpcHelpers.toDocument(cu.getBuffer());
            } catch (JavaModelException e) {
                logException("Failed to publish diagnostics.", e);
            }
        } else if (projectsManager.isBuildFile(file)) {
            document = JsonRpcHelpers.toDocument(file);
        }
        if (document != null) {
            List<Diagnostic> diagnostics = WorkspaceDiagnosticsHandler.toDiagnosticsArray(document, entry.getValue().toArray(new IMarker[0]));
            connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics));
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) Diagnostic(org.eclipse.lsp4j.Diagnostic) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) ArrayList(java.util.ArrayList) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker) Map(java.util.Map) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 7 with Diagnostic

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

the class WorkspaceDiagnosticsHandler method toDiagnostic.

private static Diagnostic toDiagnostic(IDocument document, IMarker marker) {
    if (marker == null || !marker.exists()) {
        return null;
    }
    Diagnostic d = new Diagnostic();
    d.setSource(JavaLanguageServerPlugin.SERVER_SOURCE_ID);
    d.setMessage(marker.getAttribute(IMarker.MESSAGE, ""));
    d.setCode(String.valueOf(marker.getAttribute(IJavaModelMarker.ID, 0)));
    d.setSeverity(convertSeverity(marker.getAttribute(IMarker.SEVERITY, -1)));
    d.setRange(convertRange(document, marker));
    return d;
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 8 with Diagnostic

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

the class CodeActionHandlerTest method getDiagnostic.

private Diagnostic getDiagnostic(String code, Range range) {
    Diagnostic $ = new Diagnostic();
    $.setCode(code);
    $.setRange(range);
    $.setSeverity(DiagnosticSeverity.Error);
    $.setMessage("Test Diagnostic");
    return $;
}
Also used : Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 9 with Diagnostic

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

the class DiagnosticHandlerTest method testMultipleLineRange.

@Test
public void testMultipleLineRange() throws Exception {
    IJavaProject javaProject = newEmptyProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public boolean foo(boolean b1) {\n");
    buf.append("        if (false) {\n");
    buf.append("            return true;\n");
    buf.append("        }\n");
    buf.append("        return false;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, monitor);
    IProblem[] problems = astRoot.getProblems();
    List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems));
    assertEquals(diagnostics.size(), 1);
    Range range = diagnostics.get(0).getRange();
    assertNotEquals(range.getStart().getLine(), range.getEnd().getLine());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) IProblem(org.eclipse.jdt.core.compiler.IProblem) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)

Example 10 with Diagnostic

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

the class DocumentLifeCycleHandlerTest method testDidOpenStandaloneFileWithNonSyntaxErrors.

@Test
public void testDidOpenStandaloneFileWithNonSyntaxErrors() throws Exception {
    IJavaProject javaProject = newDefaultProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);
    // @formatter:off
    String standaloneFileContent = "package java;\n" + "public class Foo {\n" + "	public static void notThis(){\n" + "		System.out.println(this);\n" + "	}\n" + "	public void method1(){\n" + "	}\n" + "	public void method1(){\n" + "	}\n" + "}";
    // @formatter:on
    ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);
    openDocument(cu1, cu1.getSource(), 1);
    List<PublishDiagnosticsParams> diagnosticReports = getClientRequests("publishDiagnostics");
    assertEquals(1, diagnosticReports.size());
    PublishDiagnosticsParams diagParam = diagnosticReports.get(0);
    assertEquals("Unexpected number of errors " + diagParam.getDiagnostics(), 3, diagParam.getDiagnostics().size());
    Diagnostic d = diagParam.getDiagnostics().get(0);
    assertEquals("Cannot use this in a static context", d.getMessage());
    assertRange(3, 21, 25, d.getRange());
    d = diagParam.getDiagnostics().get(1);
    assertEquals("Duplicate method method1() in type Foo", d.getMessage());
    assertRange(5, 13, 22, d.getRange());
    d = diagParam.getDiagnostics().get(2);
    assertEquals("Duplicate method method1() in type Foo", d.getMessage());
    assertRange(7, 13, 22, d.getRange());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) Diagnostic(org.eclipse.lsp4j.Diagnostic) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

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