Skip to content

Commit

Permalink
Merge pull request #384 from NipunaRanasinghe/NPE-fix
Browse files Browse the repository at this point in the history
 Fix LSPAnnotator crash when diagnostics have no defined severity
  • Loading branch information
NipunaRanasinghe authored Dec 14, 2024
2 parents d5d241e + 5c82a15 commit 3bd015c
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,25 @@
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class LSPAnnotator extends ExternalAnnotator<Object, Object> {

private static final Logger LOG = Logger.getInstance(LSPAnnotator.class);
private static final Object RESULT = new Object();
private static final HashMap<DiagnosticSeverity, HighlightSeverity> lspToIntellijAnnotationsMap = new HashMap<>();
private static final Map<DiagnosticSeverity, HighlightSeverity> annotationsMap = new HashMap<>();

static {
lspToIntellijAnnotationsMap.put(DiagnosticSeverity.Error, HighlightSeverity.ERROR);
lspToIntellijAnnotationsMap.put(DiagnosticSeverity.Warning, HighlightSeverity.WARNING);
annotationsMap.put(DiagnosticSeverity.Error, HighlightSeverity.ERROR);
annotationsMap.put(DiagnosticSeverity.Warning, HighlightSeverity.WARNING);

// seem flipped, but just different semantics lsp<->intellij. Hint is rendered without any squiggle
lspToIntellijAnnotationsMap.put(DiagnosticSeverity.Information, HighlightSeverity.WEAK_WARNING);
lspToIntellijAnnotationsMap.put(DiagnosticSeverity.Hint, HighlightSeverity.INFORMATION);
annotationsMap.put(DiagnosticSeverity.Information, HighlightSeverity.WEAK_WARNING);
annotationsMap.put(DiagnosticSeverity.Hint, HighlightSeverity.INFORMATION);

// As per the LSP spec, it’s recommended for the client to use error severity if the severity is not defined.
annotationsMap.put(null, HighlightSeverity.ERROR);
}

@Nullable
Expand Down Expand Up @@ -105,7 +110,7 @@ public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull Annot
// TODO annotations are applied to a file / document not to an editor. so store them by file and not by editor..
EditorEventManager eventManager = EditorEventManagerBase.forUri(uri);

if (eventManager.isDiagnosticSyncRequired()) {
if (Objects.nonNull(eventManager) && eventManager.isDiagnosticSyncRequired()) {
try {
createAnnotations(holder, eventManager);
} catch (ConcurrentModificationException e) {
Expand All @@ -130,7 +135,11 @@ public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull Annot
}

private void updateSilentAnnotations(AnnotationHolder holder, EditorEventManager eventManager) {
final List<Tuple3<HighlightSeverity,TextRange, LSPCodeActionFix>> annotations = eventManager.getSilentAnnotations();
if (Objects.isNull(holder) || Objects.isNull(eventManager)) {
return;
}

final List<Tuple3<HighlightSeverity, TextRange, LSPCodeActionFix>> annotations = eventManager.getSilentAnnotations();
if (annotations == null) {
return;
}
Expand Down Expand Up @@ -177,7 +186,7 @@ protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Di
}
final TextRange range = new TextRange(start, end);

holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
holder.newAnnotation(annotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
.range(range)
.create();

Expand Down

0 comments on commit 3bd015c

Please sign in to comment.