Skip to content

Commit

Permalink
feat: make diagnostic marker messages configurable (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom authored Aug 22, 2024
1 parent 0b96183 commit 0ceb13a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,4 +42,14 @@ public interface IMarkerAttributeComputer {
*/
public void addMarkerAttributesForDiagnostic(Diagnostic diagnostic, @Nullable IDocument document,
IResource resource, Map<String, Object> attributes);

/**
* Computes a string to be used as Marker message.
*/
public default String computeMarkerMessage(Diagnostic diagnostic) {
final Either<String, Integer> code = diagnostic.getCode();
return code == null //
? diagnostic.getMessage()
: diagnostic.getMessage() + " [" + code.get() + "]"; //$NON-NLS-1$//$NON-NLS-2$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diagnostic, String> textComputer;

public DiagnosticAnnotation(Diagnostic diagnostic) {
public DiagnosticAnnotation(Diagnostic diagnostic, Function<Diagnostic, String> textComputer) {
this.diagnostic = diagnostic;
this.textComputer = textComputer;
}

@Override
Expand All @@ -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$
};
}

Expand All @@ -40,7 +44,7 @@ public void setType(@Nullable String type) {

@Override
public String getText() {
return LSPDiagnosticsToMarkers.computeMarkerMessage(diagnostic);
return textComputer.apply(diagnostic);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -59,21 +58,24 @@ public class LSPDiagnosticsToMarkers implements Consumer<PublishDiagnosticsParam
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 static final IMarkerAttributeComputer DEFAULT_MARKER_ATTRIBUTE_COMPUTER = new IMarkerAttributeComputer() {

@Override
public void addMarkerAttributesForDiagnostic(Diagnostic diagnostic, @Nullable IDocument document,
IResource resource, Map<String, Object> attributes) {
// nothing to do
}
};

private final String languageServerId;
private final String markerType;
private final Optional<IMarkerAttributeComputer> 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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -228,7 +231,7 @@ protected void updateMarker(Map<String, Object> targetAttributes, IMarker marker
return null;
}

final var markerMessage = computeMarkerMessage(diagnostic);
final var markerMessage = markerAttributeComputer.computeMarkerMessage(diagnostic);
for (IMarker marker : remainingMarkers) {
if (!marker.exists()) {
continue;
Expand Down Expand Up @@ -260,7 +263,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, computeMarkerMessage(diagnostic));
attributes.put(IMarker.MESSAGE, markerAttributeComputer.computeMarkerMessage(diagnostic));
attributes.put(IMarker.SEVERITY, LSPEclipseUtils.toEclipseMarkerSeverity(diagnostic.getSeverity()));

if (document != null) {
Expand Down Expand Up @@ -296,8 +299,7 @@ private Map<String, Object> 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;
}
Expand Down

0 comments on commit 0ceb13a

Please sign in to comment.