diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/IMarkerAttributeComputer.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/IMarkerAttributeComputer.java index d9e224612..3941682cd 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/IMarkerAttributeComputer.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/IMarkerAttributeComputer.java @@ -17,6 +17,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.IDocument; import org.eclipse.lsp4j.Diagnostic; +import org.eclipse.lsp4j.jsonrpc.messages.Either; /** * An interface that allows adding custom attributes to a @@ -41,4 +42,14 @@ public interface IMarkerAttributeComputer { */ public void addMarkerAttributesForDiagnostic(Diagnostic diagnostic, @Nullable IDocument document, IResource resource, Map attributes); + + /** + * Computes a string to be used as Marker message. + */ + public default String computeMarkerMessage(Diagnostic diagnostic) { + final Either code = diagnostic.getCode(); + return code == null // + ? diagnostic.getMessage() + : diagnostic.getMessage() + " [" + code.get() + "]"; //$NON-NLS-1$//$NON-NLS-2$ + } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/DiagnosticAnnotation.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/DiagnosticAnnotation.java index 7166d272e..59604e505 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/DiagnosticAnnotation.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/DiagnosticAnnotation.java @@ -11,16 +11,20 @@ *******************************************************************************/ package org.eclipse.lsp4e.operations.diagnostics; +import java.util.function.Function; + import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.source.Annotation; import org.eclipse.lsp4j.Diagnostic; -public class DiagnosticAnnotation extends Annotation { +class DiagnosticAnnotation extends Annotation { private final Diagnostic diagnostic; + private final Function textComputer; - public DiagnosticAnnotation(Diagnostic diagnostic) { + public DiagnosticAnnotation(Diagnostic diagnostic, Function textComputer) { this.diagnostic = diagnostic; + this.textComputer = textComputer; } @Override @@ -29,7 +33,7 @@ public String getType() { case Error -> "org.eclipse.ui.workbench.texteditor.error"; //$NON-NLS-1$ case Warning -> "org.eclipse.ui.workbench.texteditor.warning"; //$NON-NLS-1$ case Information -> "org.eclipse.ui.workbench.texteditor.info"; //$NON-NLS-1$ - case Hint ->"org.eclipse.ui.workbench.texteditor.info"; //$NON-NLS-1$ + case Hint -> "org.eclipse.ui.workbench.texteditor.info"; //$NON-NLS-1$ }; } @@ -40,7 +44,7 @@ public void setType(@Nullable String type) { @Override public String getText() { - return LSPDiagnosticsToMarkers.computeMarkerMessage(diagnostic); + return textComputer.apply(diagnostic); } @Override diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java index 840669cb3..5ac8acfea 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/diagnostics/LSPDiagnosticsToMarkers.java @@ -19,7 +19,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; -import java.util.Optional; import java.util.Set; import java.util.function.Consumer; @@ -59,21 +58,24 @@ public class LSPDiagnosticsToMarkers implements Consumer code = diagnostic.getCode(); - return code == null // - ? diagnostic.getMessage() - : diagnostic.getMessage() + " [" + code.get() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ - } + private static final IMarkerAttributeComputer DEFAULT_MARKER_ATTRIBUTE_COMPUTER = new IMarkerAttributeComputer() { + + @Override + public void addMarkerAttributesForDiagnostic(Diagnostic diagnostic, @Nullable IDocument document, + IResource resource, Map attributes) { + // nothing to do + } + }; private final String languageServerId; private final String markerType; - private final Optional markerAttributeComputer; + private final IMarkerAttributeComputer markerAttributeComputer; public LSPDiagnosticsToMarkers(String serverId, @Nullable String markerType, @Nullable IMarkerAttributeComputer markerAttributeComputer) { this.languageServerId = serverId; this.markerType = markerType != null ? markerType : LS_DIAGNOSTIC_MARKER_TYPE; - this.markerAttributeComputer = Optional.ofNullable(markerAttributeComputer); + this.markerAttributeComputer = markerAttributeComputer == null ? DEFAULT_MARKER_ATTRIBUTE_COMPUTER + : markerAttributeComputer; } public LSPDiagnosticsToMarkers(String serverId) { @@ -131,7 +133,8 @@ private void updateEditorAnnotations(ISourceViewer sourceViewer, PublishDiagnost if (doc != null) { int startOffset = LSPEclipseUtils.toOffset(diagnostic.getRange().getStart(), doc); int endOffset = LSPEclipseUtils.toOffset(diagnostic.getRange().getEnd(), doc); - toAdd.put(new DiagnosticAnnotation(diagnostic), new Position(startOffset, endOffset - startOffset)); + toAdd.put(new DiagnosticAnnotation(diagnostic, markerAttributeComputer::computeMarkerMessage), + new Position(startOffset, endOffset - startOffset)); } } catch (BadLocationException ex) { LanguageServerPlugin.logError(ex); @@ -228,7 +231,7 @@ protected void updateMarker(Map targetAttributes, IMarker marker return null; } - final var markerMessage = computeMarkerMessage(diagnostic); + final var markerMessage = markerAttributeComputer.computeMarkerMessage(diagnostic); for (IMarker marker : remainingMarkers) { if (!marker.exists()) { continue; @@ -260,7 +263,7 @@ private Map computeMarkerAttributes(@Nullable IDocument document final var attributes = new HashMap(8); attributes.put(LSP_DIAGNOSTIC, diagnostic); attributes.put(LANGUAGE_SERVER_ID, languageServerId); - attributes.put(IMarker.MESSAGE, computeMarkerMessage(diagnostic)); + attributes.put(IMarker.MESSAGE, markerAttributeComputer.computeMarkerMessage(diagnostic)); attributes.put(IMarker.SEVERITY, LSPEclipseUtils.toEclipseMarkerSeverity(diagnostic.getSeverity())); if (document != null) { @@ -296,8 +299,7 @@ private Map computeMarkerAttributes(@Nullable IDocument document attributes.put(IMarker.CHAR_END, end); } - markerAttributeComputer - .ifPresent(c -> c.addMarkerAttributesForDiagnostic(diagnostic, document, resource, attributes)); + markerAttributeComputer.addMarkerAttributesForDiagnostic(diagnostic, document, resource, attributes); return attributes; }