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));
}
}
}
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;
}
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 $;
}
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());
}
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());
}
Aggregations