Skip to content

Commit ef4e04f

Browse files
committed
Merged PR 443751: Support to dump diagnostic information
Support to dump diagnostic information
2 parents 72594b4 + b5f2dea commit ef4e04f

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
target/
44
bin/
55
**/lib/
6-
maven-wrapper.jar
6+
maven-wrapper.jar
7+
cmd/repository/
8+
cmd/workspace/

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/indexer/EdgeBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,8 @@ public Edge documentSymbols(Vertex from, Vertex to) {
6363
public Edge refersTo(Vertex from, Vertex to) {
6464
return new Edge(generator.next(), Edge.REFERSTO, from.getId(), to.getId());
6565
}
66+
67+
public Edge diagnostic(Vertex from, Vertex to) {
68+
return new Edge(generator.next(), Edge.T_DIAGNOSTIC, from.getId(), to.getId());
69+
}
6670
}

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/indexer/Indexer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.microsoft.java.lsif.core.internal.protocol.Document;
3939
import com.microsoft.java.lsif.core.internal.protocol.Project;
4040
import com.microsoft.java.lsif.core.internal.visitors.DefinitionVisitor;
41+
import com.microsoft.java.lsif.core.internal.visitors.DiagnosticVisitor;
4142
import com.microsoft.java.lsif.core.internal.visitors.DocumentVisitor;
4243
import com.microsoft.java.lsif.core.internal.visitors.HoverVisitor;
4344
import com.microsoft.java.lsif.core.internal.visitors.ImplementationsVisitor;
@@ -136,6 +137,10 @@ private void buildIndex(IPath path, IProgressMonitor monitor, Emitter emitter) {
136137
vis.setContext(currentContext);
137138
cu.accept(vis);
138139
}
140+
141+
// Dump diagnostic information
142+
DiagnosticVisitor diagnosticVisitor = new DiagnosticVisitor(currentContext, cu);
143+
diagnosticVisitor.enlist();
139144
}
140145
}
141146
}

com.microsoft.java.lsif.core/src/com/microsoft/java/lsif/core/internal/indexer/VertexBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77

88
import java.util.List;
99

10+
import org.eclipse.lsp4j.Diagnostic;
1011
import org.eclipse.lsp4j.DocumentSymbol;
1112
import org.eclipse.lsp4j.Hover;
1213
import org.eclipse.lsp4j.Location;
1314
import org.eclipse.lsp4j.jsonrpc.messages.Either;
1415

1516
import com.microsoft.java.lsif.core.internal.JdtlsUtils;
1617
import com.microsoft.java.lsif.core.internal.protocol.DefinitionResult;
18+
import com.microsoft.java.lsif.core.internal.protocol.DiagnosticResult;
1719
import com.microsoft.java.lsif.core.internal.protocol.Document;
1820
import com.microsoft.java.lsif.core.internal.protocol.DocumentSymbolResult;
1921
import com.microsoft.java.lsif.core.internal.protocol.HoverResult;
@@ -83,4 +85,8 @@ public ImplementationResult implementationResult(List<Either<String, Location>>
8385
public DocumentSymbolResult documentSymbolResult(List<DocumentSymbol> symbols) {
8486
return new DocumentSymbolResult(generator.next(), symbols);
8587
}
88+
89+
public DiagnosticResult diagnosticResult(List<Diagnostic> diagnostics) {
90+
return new DiagnosticResult(generator.next(), diagnostics);
91+
}
8692
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
7+
package com.microsoft.java.lsif.core.internal.protocol;
8+
9+
import java.util.List;
10+
11+
import org.eclipse.lsp4j.Diagnostic;
12+
13+
public class DiagnosticResult extends Vertex {
14+
15+
private List<Diagnostic> result;
16+
17+
public DiagnosticResult(String id, List<Diagnostic> result) {
18+
super(id, Vertex.DIAGNOSTICRESULT);
19+
this.result = result;
20+
}
21+
22+
public List<Diagnostic> getResult() {
23+
return this.result;
24+
}
25+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
7+
package com.microsoft.java.lsif.core.internal.visitors;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
import org.eclipse.core.resources.IMarker;
15+
import org.eclipse.core.resources.IResource;
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.jdt.core.IJavaModelMarker;
18+
import org.eclipse.jdt.core.dom.CompilationUnit;
19+
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
20+
import org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers;
21+
import org.eclipse.jface.text.BadLocationException;
22+
import org.eclipse.jface.text.IDocument;
23+
import org.eclipse.lsp4j.Diagnostic;
24+
import org.eclipse.lsp4j.DiagnosticSeverity;
25+
import org.eclipse.lsp4j.Position;
26+
import org.eclipse.lsp4j.Range;
27+
import org.eclipse.m2e.core.internal.IMavenConstants;
28+
29+
import com.microsoft.java.lsif.core.internal.LanguageServerIndexerPlugin;
30+
import com.microsoft.java.lsif.core.internal.emitter.Emitter;
31+
import com.microsoft.java.lsif.core.internal.indexer.IndexerContext;
32+
import com.microsoft.java.lsif.core.internal.indexer.LsifService;
33+
import com.microsoft.java.lsif.core.internal.protocol.DiagnosticResult;
34+
import com.microsoft.java.lsif.core.internal.protocol.Document;
35+
36+
public class DiagnosticVisitor extends ProtocolVisitor {
37+
38+
private CompilationUnit cu;
39+
40+
public DiagnosticVisitor(IndexerContext context, CompilationUnit cu) {
41+
this.setContext(context);
42+
this.cu = cu;
43+
}
44+
45+
public void enlist() {
46+
Emitter emitter = this.getContext().getEmitter();
47+
LsifService lsif = this.getContext().getLsif();
48+
Document docVertex = this.getContext().getDocVertex();
49+
IResource resource = cu.getJavaElement().getResource();
50+
if (resource == null || !resource.exists()) {
51+
LanguageServerIndexerPlugin.logError("Cannot find resource for: " + cu.getJavaElement().getElementName());
52+
return;
53+
}
54+
IMarker[] markers = null;
55+
IDocument document = null;
56+
try {
57+
IMarker[] javaMarkers = resource.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false,
58+
IResource.DEPTH_ONE);
59+
IMarker[] taskMarkers = resource.findMarkers(IJavaModelMarker.TASK_MARKER, false, IResource.DEPTH_ONE);
60+
int totalLength = javaMarkers.length + taskMarkers.length;
61+
if (totalLength == 0) {
62+
return;
63+
}
64+
markers = Arrays.copyOf(javaMarkers, javaMarkers.length + taskMarkers.length);
65+
System.arraycopy(taskMarkers, 0, markers, javaMarkers.length, taskMarkers.length);
66+
document = JsonRpcHelpers.toDocument(cu.getJavaElement().getOpenable().getBuffer());
67+
} catch (CoreException ex) {
68+
LanguageServerIndexerPlugin.logException("Exception when dumping diagnostics ", ex);
69+
return;
70+
}
71+
72+
if (document == null) {
73+
LanguageServerIndexerPlugin
74+
.logError("Cannot parse the document for: " + cu.getJavaElement().getElementName());
75+
return;
76+
}
77+
78+
List<Diagnostic> diagnostics = toDiagnosticsArray(document, markers);
79+
DiagnosticResult diagnosticResult = lsif.getVertexBuilder().diagnosticResult(diagnostics);
80+
emitter.emit(diagnosticResult);
81+
emitter.emit(lsif.getEdgeBuilder().diagnostic(docVertex, diagnosticResult));
82+
}
83+
84+
private List<Diagnostic> toDiagnosticsArray(IDocument document, IMarker[] markers) {
85+
List<Diagnostic> diagnostics = Stream.of(markers).map(m -> toDiagnostic(document, m)).filter(d -> d != null)
86+
.collect(Collectors.toList());
87+
return diagnostics;
88+
}
89+
90+
private static Diagnostic toDiagnostic(IDocument document, IMarker marker) {
91+
if (marker == null || !marker.exists()) {
92+
return null;
93+
}
94+
Diagnostic d = new Diagnostic();
95+
d.setSource(JavaLanguageServerPlugin.SERVER_SOURCE_ID);
96+
d.setMessage(marker.getAttribute(IMarker.MESSAGE, ""));
97+
d.setCode(String.valueOf(marker.getAttribute(IJavaModelMarker.ID, 0)));
98+
d.setSeverity(convertSeverity(marker.getAttribute(IMarker.SEVERITY, -1)));
99+
d.setRange(convertRange(document, marker));
100+
return d;
101+
}
102+
103+
private static Range convertRange(IDocument document, IMarker marker) {
104+
int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
105+
int cStart = 0;
106+
int cEnd = 0;
107+
try {
108+
// Buildship doesn't provide markers for gradle files, Maven does
109+
if (marker.isSubtypeOf(IMavenConstants.MARKER_ID)) {
110+
cStart = marker.getAttribute(IMavenConstants.MARKER_COLUMN_START, -1);
111+
cEnd = marker.getAttribute(IMavenConstants.MARKER_COLUMN_END, -1);
112+
} else {
113+
int lineOffset = 0;
114+
try {
115+
lineOffset = document.getLineOffset(line);
116+
} catch (BadLocationException unlikelyException) {
117+
JavaLanguageServerPlugin.logException(unlikelyException.getMessage(), unlikelyException);
118+
return new Range(new Position(line, 0), new Position(line, 0));
119+
}
120+
cEnd = marker.getAttribute(IMarker.CHAR_END, -1) - lineOffset;
121+
cStart = marker.getAttribute(IMarker.CHAR_START, -1) - lineOffset;
122+
}
123+
} catch (CoreException e) {
124+
LanguageServerIndexerPlugin.logException(e.getMessage(), e);
125+
}
126+
cStart = Math.max(0, cStart);
127+
cEnd = Math.max(0, cEnd);
128+
129+
return new Range(new Position(line, cStart), new Position(line, cEnd));
130+
}
131+
132+
private static DiagnosticSeverity convertSeverity(int severity) {
133+
if (severity == IMarker.SEVERITY_ERROR) {
134+
return DiagnosticSeverity.Error;
135+
}
136+
if (severity == IMarker.SEVERITY_WARNING) {
137+
return DiagnosticSeverity.Warning;
138+
}
139+
return DiagnosticSeverity.Information;
140+
}
141+
}

0 commit comments

Comments
 (0)