These fixes will ensure that error diagnostics are displayed upon opening an editor and address the issue of error diagnostics disappearing. Additionally, code actions will be displayed as expected, and error highlights will remain visible when a code action is triggered. Error diagnostics with identical start and end offsets will also be displayed correctly.
Related to https://github.com/ballerina-platform/lsp4intellij/issues/353, https://github.com/ballerina-platform/lsp4intellij/issues/265
Approach
Added an extra condition in the didOpen(DidOpenTextDocumentParams params) function to check if the language server supports file syncing and to trigger the language server's didOpen command. This ensures that error diagnostics are visible upon opening an editor.
Removed a condition in the public Object collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) method, enabling error diagnostics to consistently display when the corresponding functions in the LSPAnnotator class are invoked.
Changed an else if condition to an else condition in public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull AnnotationHolder holder) to ensure annotation updates are triggered, preventing annotations from disappearing under certain circumstances.
Modified private void updateAnnotations(AnnotationHolder holder, EditorEventManager eventManager) to process and display all annotations in the editor, rather than only those with code action fixes.
Updated the condition in protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Diagnostic diagnostic) to properly display annotations with identical start and end offsets.
eventManager.refreshAnnotations() is invoked to show the code actions balloon when the caret moves to a position with available code actions. Initially, no code actions are displayed since they must be fetched from the Language Server. Once available, this function is called again to display the annotations and, consequently, the code actions balloon, using a private boolean annotationsRefreshed state variable to prevent function looping.
For code actions lacking diagnostic context for each editor, a new list private List<Tuple3<HighlightSeverity,TextRange,LSPCodeActionFix>> silentAnnotations = new ArrayList<>() stores them separately. This is because the newAnnotation function cannot be called within requestAndShowCodeActions(), and these code actions are displayed as silent annotations via private void updateSilentAnnotations(AnnotationHolder holder, EditorEventManager eventManager). Silent annotations do not show a hover popup but will display the code actions balloon.
Implemented minor optimizations to prevent duplicate code actions in the list above and to prioritize displaying code actions before error highlights.
Samples
Displaying error diagnostics with identical start and end offset
Purpose
These fixes will ensure that error diagnostics are displayed upon opening an editor and address the issue of error diagnostics disappearing. Additionally, code actions will be displayed as expected, and error highlights will remain visible when a code action is triggered. Error diagnostics with identical start and end offsets will also be displayed correctly. Related to https://github.com/ballerina-platform/lsp4intellij/issues/353, https://github.com/ballerina-platform/lsp4intellij/issues/265
Approach
didOpen(DidOpenTextDocumentParams params)
function to check if the language server supports file syncing and to trigger the language server's didOpen command. This ensures that error diagnostics are visible upon opening an editor.public Object collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors)
method, enabling error diagnostics to consistently display when the corresponding functions in the LSPAnnotator class are invoked.public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull AnnotationHolder holder)
to ensure annotation updates are triggered, preventing annotations from disappearing under certain circumstances.private void updateAnnotations(AnnotationHolder holder, EditorEventManager eventManager)
to process and display all annotations in the editor, rather than only those with code action fixes.protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Diagnostic diagnostic)
to properly display annotations with identical start and end offsets.eventManager.refreshAnnotations()
is invoked to show the code actions balloon when the caret moves to a position with available code actions. Initially, no code actions are displayed since they must be fetched from the Language Server. Once available, this function is called again to display the annotations and, consequently, the code actions balloon, using aprivate boolean annotationsRefreshed
state variable to prevent function looping.private List<Tuple3<HighlightSeverity,TextRange,LSPCodeActionFix>> silentAnnotations = new ArrayList<>()
stores them separately. This is because thenewAnnotation
function cannot be called withinrequestAndShowCodeActions()
, and these code actions are displayed as silent annotations viaprivate void updateSilentAnnotations(AnnotationHolder holder, EditorEventManager eventManager)
. Silent annotations do not show a hover popup but will display the code actions balloon.Samples
Displaying error diagnostics with identical start and end offset
Error diagnostics do not disappear
https://github.com/ballerina-platform/lsp4intellij/assets/110608712/57ddb601-720a-4c95-b3d5-4a8fa1f19777
Code actions displaying properly
https://github.com/ballerina-platform/lsp4intellij/assets/110608712/00f448a3-cfc1-4328-80f6-0e77e784f666