Skip to content

Commit

Permalink
feat: add diagnostic code as suffix to marker message
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 16, 2024
1 parent 2ca461c commit 89c39df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public void testDiagnostics() throws CoreException {
assertEquals(markerCharStart, MarkerUtilities.getCharStart(marker.get()));
assertEquals(markerCharEnd, MarkerUtilities.getCharEnd(marker.get()));
assertEquals(markerLineIndex + 1, MarkerUtilities.getLineNumber(marker.get()));
assertEquals(diagnostic.getMessage(), MarkerUtilities.getMessage(marker.get()));
assertEquals(diagnostic.getMessage() + " [" + diagnostic.getCode().get() + "]",
MarkerUtilities.getMessage(marker.get()));
}

diagnosticsToMarkers.accept(new PublishDiagnosticsParams(file.getLocationURI().toString(), Collections.emptyList()));
Expand Down Expand Up @@ -229,7 +230,8 @@ public void testDiagnosticsRangeAfterDocument() throws CoreException {
Diagnostic diagnostic = diagnostics.get(i);
IMarker marker = markers[i];

assertEquals(diagnostic.getMessage(), MarkerUtilities.getMessage(marker));
assertEquals(diagnostic.getMessage() + " [" + diagnostic.getCode().get() + "]",
MarkerUtilities.getMessage(marker));
assertEquals(content.length(), MarkerUtilities.getCharStart(marker));
assertEquals(content.length(), MarkerUtilities.getCharEnd(marker));
assertEquals(1, MarkerUtilities.getLineNumber(marker));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void setType(@Nullable String type) {

@Override
public String getText() {
return this.diagnostic.getMessage();
return LSPDiagnosticsToMarkers.computeMarkerMessage(diagnostic);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public class LSPDiagnosticsToMarkers implements Consumer<PublishDiagnosticsParam
public static final String LSP_DIAGNOSTIC = "lspDiagnostic"; //$NON-NLS-1$
public static final String LANGUAGE_SERVER_ID = "languageServerId"; //$NON-NLS-1$
public static final String LS_DIAGNOSTIC_MARKER_TYPE = "org.eclipse.lsp4e.diagnostic"; //$NON-NLS-1$

static String computeMarkerMessage(final Diagnostic diagnostic) {
final Either<String, Integer> code = diagnostic.getCode();
return code == null //
? diagnostic.getMessage()
: diagnostic.getMessage() + " [" + code.get() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
}

private final String languageServerId;
private final String markerType;
private final Optional<IMarkerAttributeComputer> markerAttributeComputer;
Expand Down Expand Up @@ -220,14 +228,15 @@ protected void updateMarker(Map<String, Object> targetAttributes, IMarker marker
return null;
}

final var markerMessage = computeMarkerMessage(diagnostic);
for (IMarker marker : remainingMarkers) {
if (!marker.exists()) {
continue;
}
try {
if (LSPEclipseUtils.toOffset(diagnostic.getRange().getStart(), document) == MarkerUtilities.getCharStart(marker)
&& (LSPEclipseUtils.toOffset(diagnostic.getRange().getEnd(), document) == MarkerUtilities.getCharEnd(marker) || Objects.equals(diagnostic.getRange().getStart(), diagnostic.getRange().getEnd()))
&& Objects.equals(marker.getAttribute(IMarker.MESSAGE), diagnostic.getMessage())
&& Objects.equals(marker.getAttribute(IMarker.MESSAGE), markerMessage)
&& Objects.equals(marker.getAttribute(LANGUAGE_SERVER_ID), this.languageServerId)) {
return marker;
}
Expand All @@ -251,7 +260,7 @@ private Map<String, Object> computeMarkerAttributes(@Nullable IDocument document
final var attributes = new HashMap<String, Object>(8);
attributes.put(LSP_DIAGNOSTIC, diagnostic);
attributes.put(LANGUAGE_SERVER_ID, languageServerId);
attributes.put(IMarker.MESSAGE, diagnostic.getMessage());
attributes.put(IMarker.MESSAGE, computeMarkerMessage(diagnostic));
attributes.put(IMarker.SEVERITY, LSPEclipseUtils.toEclipseMarkerSeverity(diagnostic.getSeverity()));

if (document != null) {
Expand Down

0 comments on commit 89c39df

Please sign in to comment.