fix: Annotator jank breaking diagnostics
This commit is contained in:
parent
7b2ad7c324
commit
85bd68393c
1 changed files with 44 additions and 10 deletions
|
@ -33,8 +33,10 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.util.TextRange;
|
import com.intellij.openapi.util.TextRange;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
import com.intellij.psi.PsiDocumentManager;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.util.SmartList;
|
import com.intellij.util.SmartList;
|
||||||
|
import lombok.val;
|
||||||
import org.eclipse.lsp4j.Diagnostic;
|
import org.eclipse.lsp4j.Diagnostic;
|
||||||
import org.eclipse.lsp4j.DiagnosticSeverity;
|
import org.eclipse.lsp4j.DiagnosticSeverity;
|
||||||
import org.eclipse.lsp4j.DiagnosticTag;
|
import org.eclipse.lsp4j.DiagnosticTag;
|
||||||
|
@ -162,14 +164,17 @@ public class LSPAnnotator extends ExternalAnnotator<Object, Object> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
protected AnnotationBuilder createAnnotation(Editor editor, AnnotationHolder holder, Diagnostic diagnostic) {
|
protected TextRange getTextRange(Editor editor, Diagnostic diagnostic) {
|
||||||
final int start = DocumentUtils.LSPPosToOffset(editor, diagnostic.getRange().getStart());
|
final int start = DocumentUtils.LSPPosToOffset(editor, diagnostic.getRange().getStart());
|
||||||
final int end = DocumentUtils.LSPPosToOffset(editor, diagnostic.getRange().getEnd());
|
final int end = DocumentUtils.LSPPosToOffset(editor, diagnostic.getRange().getEnd());
|
||||||
if (start >= end) {
|
if (start > end) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final TextRange range = new TextRange(start, end);
|
return new TextRange(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
protected AnnotationBuilder createAnnotation(@NotNull TextRange range, AnnotationHolder holder, Diagnostic diagnostic) {
|
||||||
return holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
|
return holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage())
|
||||||
.range(range);
|
.range(range);
|
||||||
}
|
}
|
||||||
|
@ -177,18 +182,47 @@ public class LSPAnnotator extends ExternalAnnotator<Object, Object> {
|
||||||
private void createAnnotations(AnnotationHolder holder, EditorEventManager eventManager) {
|
private void createAnnotations(AnnotationHolder holder, EditorEventManager eventManager) {
|
||||||
final List<Diagnostic> diagnostics = eventManager.getDiagnostics();
|
final List<Diagnostic> diagnostics = eventManager.getDiagnostics();
|
||||||
final Editor editor = eventManager.editor;
|
final Editor editor = eventManager.editor;
|
||||||
|
PsiFile file;
|
||||||
|
val document = editor.getDocument();
|
||||||
|
if (document != null) {
|
||||||
|
file = PsiDocumentManager.getInstance(eventManager.getProject()).getPsiFile(document);
|
||||||
|
} else {
|
||||||
|
file = null;
|
||||||
|
}
|
||||||
|
|
||||||
List<Annotation> annotations = new ArrayList<>();
|
List<Annotation> annotations = new ArrayList<>();
|
||||||
diagnostics.forEach(d -> {
|
diagnostics.forEach(d -> {
|
||||||
var annotation = createAnnotation(editor, holder, d);
|
var range = getTextRange(editor, d);
|
||||||
if (annotation != null) {
|
if (range == null)
|
||||||
if (d.getTags() != null && d.getTags().contains(DiagnosticTag.Deprecated)) {
|
return;
|
||||||
annotation = annotation.highlightType(ProblemHighlightType.LIKE_DEPRECATED);
|
blk:
|
||||||
|
if (file != null && range.getLength() == 0) {
|
||||||
|
val psiElement = file.findElementAt(range.getStartOffset());
|
||||||
|
if (psiElement != null && !psiElement.getText().equals(".")) {
|
||||||
|
range = psiElement.getTextRange();
|
||||||
|
break blk;
|
||||||
|
}
|
||||||
|
val psiElementForward = file.findElementAt(range.getStartOffset() + 1);
|
||||||
|
if (psiElementForward != null) {
|
||||||
|
range = psiElementForward.getTextRange();
|
||||||
|
break blk;
|
||||||
|
}
|
||||||
|
val psiElementBack = file.findElementAt(range.getStartOffset() - 1);
|
||||||
|
if (psiElementBack != null) {
|
||||||
|
range = psiElementBack.getTextRange();
|
||||||
|
break blk;
|
||||||
|
}
|
||||||
|
if (psiElement != null) {
|
||||||
|
range = psiElement.getTextRange();
|
||||||
}
|
}
|
||||||
annotation.create();
|
|
||||||
var theList = (SmartList<Annotation>) holder;
|
|
||||||
annotations.add(theList.get(theList.size() - 1));
|
|
||||||
}
|
}
|
||||||
|
var annotation = createAnnotation(range, holder, d);
|
||||||
|
if (d.getTags() != null && d.getTags().contains(DiagnosticTag.Deprecated)) {
|
||||||
|
annotation = annotation.highlightType(ProblemHighlightType.LIKE_DEPRECATED);
|
||||||
|
}
|
||||||
|
annotation.create();
|
||||||
|
var theList = (SmartList<Annotation>) holder;
|
||||||
|
annotations.add(theList.get(theList.size() - 1));
|
||||||
});
|
});
|
||||||
|
|
||||||
eventManager.setAnnotations(annotations);
|
eventManager.setAnnotations(annotations);
|
||||||
|
|
Loading…
Add table
Reference in a new issue