ballerina-platform / lsp4intellij

This language client library provides language server protocol support for IntelliJ IDEA and other Jetbrains IDEs.
Apache License 2.0
438 stars 104 forks source link

Fix error diagnostics and code actions display issues #354

Closed ThemiraChathumina closed 5 months ago

ThemiraChathumina commented 5 months ago

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Updated the condition in protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Diagnostic diagnostic) to properly display annotations with identical start and end offsets.
  6. 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.
  7. 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.
  8. 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

image

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