Search in sources :

Example 6 with PublishDiagnosticsParams

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

the class DiagnosticsHandler method endReporting.

@Override
public void endReporting() {
    JavaLanguageServerPlugin.logInfo(problems.size() + " problems reported for " + this.uri.substring(this.uri.lastIndexOf('/')));
    PublishDiagnosticsParams $ = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(this.cu, problems));
    this.connection.publishDiagnostics($);
}
Also used : PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams)

Example 7 with PublishDiagnosticsParams

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

the class WorkspaceDiagnosticsHandler method visit.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.
	 * resources.IResourceDelta)
	 */
@Override
public boolean visit(IResourceDelta delta) throws CoreException {
    IResource resource = delta.getResource();
    // WorkspaceEventsHandler removes the diagnostics for deleted resources.
    if (resource == null || !resource.isAccessible()) {
        return false;
    }
    if (resource.getType() == IResource.FOLDER || resource.getType() == IResource.ROOT) {
        return true;
    }
    // ignore problems caused by standalone files
    if (resource.getType() == IResource.PROJECT) {
        return !JavaLanguageServerPlugin.getProjectsManager().getDefaultProject().equals(resource.getProject());
    }
    // No marker changes continue to visit
    if ((delta.getFlags() & IResourceDelta.MARKERS) == 0) {
        return false;
    }
    IFile file = (IFile) resource;
    String uri = JDTUtils.getFileURI(resource);
    IDocument document = null;
    IMarker[] markers = null;
    // Check if it is a Java ...
    if (JavaCore.isJavaLikeFileName(file.getName())) {
        markers = resource.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_ONE);
        ICompilationUnit cu = (ICompilationUnit) JavaCore.create(file);
        document = JsonRpcHelpers.toDocument(cu.getBuffer());
    } else // or a build file
    if (projectsManager.isBuildFile(file)) {
        // all errors on that build file should be relevant
        markers = file.findMarkers(null, true, 1);
        document = JsonRpcHelpers.toDocument(file);
    }
    if (document != null) {
        this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(document, markers)));
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) IMarker(org.eclipse.core.resources.IMarker) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument)

Example 8 with PublishDiagnosticsParams

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

the class DocumentLifeCycleHandlerTest method testDidOpenStandaloneFile.

@Test
public void testDidOpenStandaloneFile() 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 extends UnknownType {" + "	public void method1(){\n" + "		super.whatever();" + "	}\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(0, diagParam.getDiagnostics().size());
}
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) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 9 with PublishDiagnosticsParams

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

Example 10 with PublishDiagnosticsParams

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

Aggregations

PublishDiagnosticsParams (org.eclipse.lsp4j.PublishDiagnosticsParams)22 Diagnostic (org.eclipse.lsp4j.Diagnostic)8 URI (java.net.URI)7 ArrayList (java.util.ArrayList)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 HashMap (java.util.HashMap)4 ICompilerProblem (org.apache.flex.compiler.problems.ICompilerProblem)4 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 Test (org.junit.Test)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 Path (java.nio.file.Path)2 List (java.util.List)2 Map (java.util.Map)2 Workspace (org.apache.flex.compiler.internal.workspaces.Workspace)2 IWorkspace (org.apache.flex.compiler.workspaces.IWorkspace)2