Skip to content

Commit

Permalink
Repsect Diagnostic Tag Client Capability
Browse files Browse the repository at this point in the history
Signed-off-by: Rome Li <rome.li@microsoft.com>
  • Loading branch information
akaroml committed Sep 20, 2019
1 parent 1ef6dfa commit 6eb9f78
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void beginReporting() {
@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));
boolean isDiagnosticTagSupported = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isDiagnosticTagSupported();
PublishDiagnosticsParams $ = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(this.cu, problems, isDiagnosticTagSupported));
this.connection.publishDiagnostics($);
}

Expand All @@ -127,7 +128,12 @@ public boolean isActive() {
return true;
}

@Deprecated
public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProblem> problems) {
return toDiagnosticsArray(openable, problems, false);
}

public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProblem> problems, boolean isDiagnosticTagSupported) {
List<Diagnostic> array = new ArrayList<>(problems.size());
for (IProblem problem : problems) {
Diagnostic diag = new Diagnostic();
Expand All @@ -136,7 +142,9 @@ public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProb
diag.setCode(Integer.toString(problem.getID()));
diag.setSeverity(convertSeverity(problem));
diag.setRange(convertRange(openable, problem));
diag.setTags(getDiagnosticTag(problem.getID()));
if (isDiagnosticTagSupported) {
diag.setTags(getDiagnosticTag(problem.getID()));
}
array.add(diag);
}
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,15 @@ public void initialized(InitializedParams params) {
// we do not have the user setting initialized yet at this point but we should
// still call to enable defaults in case client does not support configuration changes
syncCapabilitiesToSettings();
boolean isDiagnosticTagSupported = preferenceManager.getClientPreferences().isDiagnosticTagSupported();
Job initializeWorkspace = new Job("Initialize workspace") {

@Override
public IStatus run(IProgressMonitor monitor) {
try {
JobHelpers.waitForBuildJobs(60 * 60 * 1000); // 1 hour
logInfo(">> build jobs finished");
workspaceDiagnosticsHandler = new WorkspaceDiagnosticsHandler(JDTLanguageServer.this.client, pm);
workspaceDiagnosticsHandler = new WorkspaceDiagnosticsHandler(JDTLanguageServer.this.client, pm, isDiagnosticTagSupported);
workspaceDiagnosticsHandler.publishDiagnostics(monitor);
workspaceDiagnosticsHandler.addResourceChangeListener();
pm.registerWatchers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ public final class WorkspaceDiagnosticsHandler implements IResourceChangeListene
public static final String PROJECT_CONFIGURATION_IS_NOT_UP_TO_DATE_WITH_POM_XML = "Project configuration is not up-to-date with pom.xml, requires an update.";
private final JavaClientConnection connection;
private final ProjectsManager projectsManager;
private final boolean isDiagnosticTagSupported;

@Deprecated
public WorkspaceDiagnosticsHandler(JavaClientConnection connection, ProjectsManager projectsManager) {
this(connection, projectsManager, false);
}

public WorkspaceDiagnosticsHandler(JavaClientConnection connection, ProjectsManager projectsManager, boolean isDiagnosticTagSupported) {
this.connection = connection;
this.projectsManager = projectsManager;
this.isDiagnosticTagSupported = isDiagnosticTagSupported;
}

public void addResourceChangeListener() {
Expand Down Expand Up @@ -150,7 +157,7 @@ else if (projectsManager.isBuildFile(file)) {
}
if (document != null) {
String uri = JDTUtils.getFileURI(resource);
this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(document, markers)));
this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(document, markers, isDiagnosticTagSupported)));
}
return false;
}
Expand All @@ -173,13 +180,13 @@ private void publishMarkers(IProject project, IMarker[] markers) throws CoreExce
projectMarkers.add(marker);
}
}
List<Diagnostic> diagnostics = toDiagnosticArray(range, projectMarkers);
List<Diagnostic> diagnostics = toDiagnosticArray(range, projectMarkers, isDiagnosticTagSupported);
String clientUri = ResourceUtils.toClientUri(uri);
connection.publishDiagnostics(new PublishDiagnosticsParams(clientUri, diagnostics));
if (pom.exists()) {
IDocument document = JsonRpcHelpers.toDocument(pom);
diagnostics = toDiagnosticsArray(document, pom.findMarkers(null, true, IResource.DEPTH_ZERO));
List<Diagnostic> diagnosicts2 = toDiagnosticArray(range, pomMarkers);
diagnostics = toDiagnosticsArray(document, pom.findMarkers(null, true, IResource.DEPTH_ZERO), isDiagnosticTagSupported);
List<Diagnostic> diagnosicts2 = toDiagnosticArray(range, pomMarkers, isDiagnosticTagSupported);
diagnostics.addAll(diagnosicts2);
connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(clientUri + "/pom.xml"), diagnostics));
}
Expand Down Expand Up @@ -252,13 +259,16 @@ private void publishDiagnostics(List<IMarker> markers) {
document = JsonRpcHelpers.toDocument(file);
}
if (document != null) {
List<Diagnostic> diagnostics = WorkspaceDiagnosticsHandler.toDiagnosticsArray(document, entry.getValue().toArray(new IMarker[0]));
List<Diagnostic> diagnostics = WorkspaceDiagnosticsHandler.toDiagnosticsArray(document, entry.getValue().toArray(new IMarker[0]), isDiagnosticTagSupported);
connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics));
}
}
}


@Deprecated
public static List<Diagnostic> toDiagnosticArray(Range range, Collection<IMarker> markers) {
return toDiagnosticArray(range, markers, false);
}

/**
* Transforms {@link IMarker}s into a list of {@link Diagnostic}s
Expand All @@ -267,12 +277,12 @@ private void publishDiagnostics(List<IMarker> markers) {
* @param markers
* @return a list of {@link Diagnostic}s
*/
public static List<Diagnostic> toDiagnosticArray(Range range, Collection<IMarker> markers) {
List<Diagnostic> diagnostics = markers.stream().map(m -> toDiagnostic(range, m)).filter(d -> d != null).collect(Collectors.toList());
public static List<Diagnostic> toDiagnosticArray(Range range, Collection<IMarker> markers, boolean isDiagnosticTagSupported) {
List<Diagnostic> diagnostics = markers.stream().map(m -> toDiagnostic(range, m, isDiagnosticTagSupported)).filter(d -> d != null).collect(Collectors.toList());
return diagnostics;
}

private static Diagnostic toDiagnostic(Range range, IMarker marker) {
private static Diagnostic toDiagnostic(Range range, IMarker marker, boolean isDiagnosticTagSupported) {
if (marker == null || !marker.exists()) {
return null;
}
Expand All @@ -284,11 +294,20 @@ private static Diagnostic toDiagnostic(Range range, IMarker marker) {
}
d.setMessage(message);
d.setSeverity(convertSeverity(marker.getAttribute(IMarker.SEVERITY, -1)));
d.setCode(String.valueOf(marker.getAttribute(IJavaModelMarker.ID, 0)));
int problemId = marker.getAttribute(IJavaModelMarker.ID, 0);
d.setCode(String.valueOf(problemId));
if (isDiagnosticTagSupported) {
d.setTags(DiagnosticsHandler.getDiagnosticTag(problemId));
}
d.setRange(range);
return d;
}

@Deprecated
public static List<Diagnostic> toDiagnosticsArray(IDocument document, IMarker[] markers) {
return toDiagnosticsArray(document, markers, false);
}

/**
* Transforms {@link IMarker}s of a {@link IDocument} into a list of
* {@link Diagnostic}s.
Expand All @@ -297,15 +316,15 @@ private static Diagnostic toDiagnostic(Range range, IMarker marker) {
* @param markers
* @return a list of {@link Diagnostic}s
*/
public static List<Diagnostic> toDiagnosticsArray(IDocument document, IMarker[] markers) {
public static List<Diagnostic> toDiagnosticsArray(IDocument document, IMarker[] markers, boolean isDiagnosticTagSupported) {
List<Diagnostic> diagnostics = Stream.of(markers)
.map(m -> toDiagnostic(document, m))
.map(m -> toDiagnostic(document, m, isDiagnosticTagSupported))
.filter(d -> d != null)
.collect(Collectors.toList());
return diagnostics;
}

private static Diagnostic toDiagnostic(IDocument document, IMarker marker) {
private static Diagnostic toDiagnostic(IDocument document, IMarker marker, boolean isDiagnosticTagSupported) {
if (marker == null || !marker.exists()) {
return null;
}
Expand All @@ -316,7 +335,9 @@ private static Diagnostic toDiagnostic(IDocument document, IMarker marker) {
d.setCode(String.valueOf(problemId));
d.setSeverity(convertSeverity(marker.getAttribute(IMarker.SEVERITY, -1)));
d.setRange(convertRange(document, marker));
d.setTags(DiagnosticsHandler.getDiagnosticTag(problemId));
if (isDiagnosticTagSupported) {
d.setTags(DiagnosticsHandler.getDiagnosticTag(problemId));
}
return d;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,17 @@ public boolean isSupportedCodeActionKind(String kind) {
.stream().filter(k -> kind.startsWith(k)).findAny().isPresent();
//@formatter:on
}

/**
* {@code true} if the client has explicitly set the
* {@code textDocument.publishDiagnostics.tagSupport} to
* {@code true} when initializing the LS. Otherwise, {@code false}.
*/
public boolean isDiagnosticTagSupported() {
//@formatter:off
return v3supported && capabilities.getTextDocument().getPublishDiagnostics() != null
&& capabilities.getTextDocument().getPublishDiagnostics().getTagSupport() != null
&& capabilities.getTextDocument().getPublishDiagnostics().getTagSupport().booleanValue();
//@formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ protected List<Either<Command, CodeAction>> evaluateCodeActions(ICompilationUnit
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
context.setOnly(onlyKinds);
parms.setContext(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testMultipleLineRange() throws Exception {

CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, monitor);
IProblem[] problems = astRoot.getProblems();
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems));
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true);
assertEquals(1, diagnostics.size());
Range range = diagnostics.get(0).getRange();
assertNotEquals(range.getStart().getLine(), range.getEnd().getLine());
Expand Down Expand Up @@ -123,7 +123,7 @@ public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
List<IProblem> problems = handler.getProblems();
assertEquals(problems.size(), 1);
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, problems);
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, problems, true);
assertEquals(diagnostics.size(), 1);
DiagnosticSeverity severity = diagnostics.get(0).getSeverity();
assertEquals(severity, DiagnosticSeverity.Information);
Expand Down Expand Up @@ -175,7 +175,7 @@ public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
List<IProblem> problems = handler.getProblems();
assertEquals(problems.size(), 1);
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, problems);
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, problems, true);
assertEquals(diagnostics.size(), 1);
DiagnosticSeverity severity = diagnostics.get(0).getSeverity();
assertEquals(severity, DiagnosticSeverity.Warning);
Expand All @@ -197,7 +197,7 @@ public void testDeprecated() throws Exception {

CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, monitor);
IProblem[] problems = astRoot.getProblems();
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems));
List<Diagnostic> diagnostics = DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true);
assertEquals(2, diagnostics.size());
List<DiagnosticTag> tags = diagnostics.get(0).getTags();
assertEquals(1, tags.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected List<Either<Command, CodeAction>> getCodeActions(ICompilationUnit cu)
parms.setTextDocument(textDocument);
parms.setRange(range);
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems)));
context.setDiagnostics(DiagnosticsHandler.toDiagnosticsArray(cu, Arrays.asList(problems), true));
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
parms.setContext(context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class WorkspaceDiagnosticsHandlerTest extends AbstractProjectsManagerBase

@Before
public void setup() throws Exception {
handler = new WorkspaceDiagnosticsHandler(connection, projectsManager);
handler = new WorkspaceDiagnosticsHandler(connection, projectsManager, true);
handler.addResourceChangeListener();
}

Expand Down

0 comments on commit 6eb9f78

Please sign in to comment.