From 852a423c250dfb5ed282f2688fc83dc43e3a9e2d Mon Sep 17 00:00:00 2001 From: Trias Date: Wed, 6 Jan 2021 14:28:25 +0100 Subject: [PATCH 1/9] quickfix: disable code actions as they make annotations diasappear --- .../contributors/annotator/LSPAnnotator.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java index 81eee7f8..db94f2cb 100644 --- a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java +++ b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java @@ -58,10 +58,6 @@ public Object collectInformation(@NotNull PsiFile file, @NotNull Editor editor, return null; } - // If the diagnostics list is locked, we need to skip annotating the file. - if (!(eventManager.isDiagnosticSyncRequired() || eventManager.isCodeActionSyncRequired())) { - return null; - } return RESULT; } catch (Exception e) { return null; @@ -77,12 +73,20 @@ public Object doAnnotate(Object collectedInfo) { @Override public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull AnnotationHolder holder) { + VirtualFile virtualFile = file.getVirtualFile(); if (FileUtils.isFileSupported(virtualFile) && IntellijLanguageClient.isExtensionSupported(virtualFile)) { String uri = FileUtils.VFSToURI(virtualFile); // 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 the diagnostics list is locked, we need to skip annotating the file. + if (!(eventManager.isDiagnosticSyncRequired() || eventManager.isCodeActionSyncRequired())) { + // return; + } + + createAnnotations(holder, eventManager); +/* if (eventManager.isCodeActionSyncRequired()) { try { updateAnnotations(holder, eventManager); @@ -101,7 +105,13 @@ public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull Annot } catch (Throwable t) { LOG.warn("Error occurred when updating LSP code actions.", t); } + } else{ + LOG.warn("no annotations"); } + + */ + }else{ + LOG.warn("no annotations"); } } From e30c7ea795cf6a0cde52de8dd5af5f0b42f2514d Mon Sep 17 00:00:00 2001 From: Trias Date: Mon, 12 Apr 2021 17:08:06 +0200 Subject: [PATCH 2/9] add navigation for lspPsiElements --- .../contributors/psi/LSPPsiElement.java | 34 ++++++++++++++----- .../wso2/lsp4intellij/utils/FileUtils.java | 5 +++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java b/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java index 926517ea..12f72ae4 100644 --- a/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java +++ b/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java @@ -48,13 +48,19 @@ import com.intellij.util.IncorrectOperationException; import com.intellij.util.concurrency.AtomicFieldUpdater; import com.intellij.util.keyFMap.KeyFMap; +import org.eclipse.lsp4j.Location; +import org.eclipse.lsp4j.Range; import org.jetbrains.annotations.NotNull; -import org.wso2.lsp4intellij.utils.ApplicationUtils; +import org.wso2.lsp4intellij.editor.EditorEventManager; +import org.wso2.lsp4intellij.editor.EditorEventManagerBase; +import org.wso2.lsp4intellij.utils.DocumentUtils; import org.wso2.lsp4intellij.utils.FileUtils; import javax.annotation.Nullable; import javax.swing.*; +import static org.wso2.lsp4intellij.utils.ApplicationUtils.writeAction; + /** * A simple PsiElement for LSP */ @@ -720,13 +726,25 @@ public String getName() { } public void navigate(boolean requestFocus) { - Editor editor = FileUtils.editorFromPsiFile(getContainingFile()); - if (editor == null) { - OpenFileDescriptor descriptor = new OpenFileDescriptor(getProject(), getContainingFile().getVirtualFile(), - getTextOffset()); - ApplicationUtils.invokeLater(() -> ApplicationUtils - .writeAction(() -> FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false))); - } + writeAction(() -> { + Editor editor = FileUtils.editorFromPsiFile(getContainingFile()); + if (editor == null) { + OpenFileDescriptor descriptor = new OpenFileDescriptor(getProject(), getContainingFile().getVirtualFile(), + getTextOffset()); + + editor = FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false); + } + + EditorEventManager manager = EditorEventManagerBase.forEditor(editor); + if(manager != null){ + manager.gotoLocation( + new Location( + FileUtils.uriFromVirtualFile(getContainingFile().getVirtualFile()), + new Range( + DocumentUtils.offsetToLSPPos(editor, this.start), + DocumentUtils.offsetToLSPPos(editor, this.end)))); + } + }); } /** diff --git a/src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java b/src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java index 5d5f10cc..bb86c914 100644 --- a/src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java +++ b/src/main/java/org/wso2/lsp4intellij/utils/FileUtils.java @@ -299,6 +299,11 @@ public static String documentToUri(Document document) { return sanitizeURI(VFSToURI(FileDocumentManager.getInstance().getFile(document))); } + @Nullable + public static String uriFromVirtualFile(@NotNull VirtualFile vf) { + return pathToUri(vf.getCanonicalPath()); + } + /** * Object representing the OS type (Windows or Unix) */ From 5b0a1a52900e43e5f63be2a6c34c2b6ceab0a305 Mon Sep 17 00:00:00 2001 From: Trias Date: Thu, 29 Apr 2021 12:47:19 +0200 Subject: [PATCH 3/9] navigate to position --- .../contributors/psi/LSPPsiElement.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java b/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java index 12f72ae4..baec0d19 100644 --- a/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java +++ b/src/main/java/org/wso2/lsp4intellij/contributors/psi/LSPPsiElement.java @@ -59,6 +59,7 @@ import javax.annotation.Nullable; import javax.swing.*; +import static org.wso2.lsp4intellij.utils.ApplicationUtils.invokeAfterPsiEvents; import static org.wso2.lsp4intellij.utils.ApplicationUtils.writeAction; /** @@ -735,15 +736,18 @@ public void navigate(boolean requestFocus) { editor = FileEditorManager.getInstance(getProject()).openTextEditor(descriptor, false); } - EditorEventManager manager = EditorEventManagerBase.forEditor(editor); - if(manager != null){ - manager.gotoLocation( - new Location( - FileUtils.uriFromVirtualFile(getContainingFile().getVirtualFile()), - new Range( - DocumentUtils.offsetToLSPPos(editor, this.start), - DocumentUtils.offsetToLSPPos(editor, this.end)))); - } + Editor finalEditor = editor; + invokeAfterPsiEvents(() -> { + EditorEventManager manager = EditorEventManagerBase.forEditor(finalEditor); + if (manager != null) { + manager.gotoLocation( + new Location( + FileUtils.uriFromVirtualFile(getContainingFile().getVirtualFile()), + new Range( + DocumentUtils.offsetToLSPPos(finalEditor, this.start), + DocumentUtils.offsetToLSPPos(finalEditor, this.end)))); + } + }); }); } From 2e331772b09dc413a9bcb8fef91ed7387ca2ef39 Mon Sep 17 00:00:00 2001 From: Trias Date: Thu, 29 Apr 2021 16:25:32 +0200 Subject: [PATCH 4/9] update to java 11 --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dd0ed194..93572afb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - java_version: ['8', '11'] + java_version: ['11'] os: [ubuntu-latest, windows-latest, macOS-latest] steps: - uses: actions/checkout@v1 From f7ce96305634af458f8c98b04d4098a013984860 Mon Sep 17 00:00:00 2001 From: Sascha Kliche Date: Mon, 11 Apr 2022 11:47:03 +0200 Subject: [PATCH 5/9] HLS-167 Revert use of createAnnotation(), it causes deprecation exception --- .../contributors/annotator/LSPAnnotator.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java index 95519090..237c87c5 100644 --- a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java +++ b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java @@ -25,6 +25,7 @@ import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; +import com.intellij.util.SmartList; import org.eclipse.lsp4j.Diagnostic; import org.eclipse.lsp4j.DiagnosticSeverity; import org.eclipse.lsp4j.DiagnosticTag; @@ -159,9 +160,13 @@ protected Annotation createAnnotation(Editor editor, AnnotationHolder holder, Di } final TextRange range = new TextRange(start, end); - return holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage()) + // Revert change from c05c7abe because createAnnotation() causes deprecation exception + holder.newAnnotation(lspToIntellijAnnotationsMap.get(diagnostic.getSeverity()), diagnostic.getMessage()) .range(range) - .createAnnotation(); + .create(); + SmartList asList = (SmartList) holder; + + return asList.get(asList.size() - 1); } private void createAnnotations(AnnotationHolder holder, EditorEventManager eventManager) { From 2f0a48b5aa00e463de9d87c077d69945e6b5bc95 Mon Sep 17 00:00:00 2001 From: Sascha Kliche Date: Mon, 11 Apr 2022 12:50:54 +0200 Subject: [PATCH 6/9] HLS-167 Check for null values --- .../contributors/annotator/LSPAnnotator.java | 10 ++++++++-- .../statusbar/LSPServerStatusWidget.java | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java index 237c87c5..7dbf6642 100644 --- a/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java +++ b/src/main/java/org/wso2/lsp4intellij/contributors/annotator/LSPAnnotator.java @@ -40,7 +40,6 @@ import org.wso2.lsp4intellij.utils.FileUtils; import java.util.ArrayList; -import java.util.ConcurrentModificationException; import java.util.HashMap; import java.util.List; @@ -90,7 +89,14 @@ public Object doAnnotate(Object collectedInfo) { @Override public void apply(@NotNull PsiFile file, Object annotationResult, @NotNull AnnotationHolder holder) { - + if (file.getVirtualFile() == null) { + LOG.warn("file == null || file.getVirtualFile() == null || file.getProject() == null: " + this); + return; + } + if (LanguageServerWrapper.forVirtualFile(file.getVirtualFile(), file.getProject()) == null) { + LOG.warn("LanguageServerWrapper.forVirtualFile(file.getVirtualFile(), file.getProject()): " + this); + return; + } if (LanguageServerWrapper.forVirtualFile(file.getVirtualFile(), file.getProject()).getStatus() != ServerStatus.INITIALIZED) { return; } diff --git a/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java b/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java index f23544fd..7ba5a65f 100644 --- a/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java +++ b/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java @@ -21,6 +21,7 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.DefaultActionGroup; import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; @@ -54,6 +55,7 @@ public class LSPServerStatusWidget implements StatusBarWidget { + private static final Logger LOG = Logger.getInstance(LSPServerStatusWidget.class); private final Map> timeouts = new HashMap<>(); private final Project project; private final String projectName; @@ -112,7 +114,11 @@ public void dispose() { if (statusBar != null) ApplicationUtils.invokeLater(() -> { StatusBarWidgetFactory factory = ServiceManager.getService(StatusBarWidgetFactory.class); - factory.disposeWidget(this); + if (factory != null) { + factory.disposeWidget(this); + } else { + LOG.warn("factory is null in dispose(): " + this); + } }); } } @@ -147,6 +153,10 @@ public Consumer getClickConsumer() { JBPopupFactory.ActionSelectionAid mnemonics = JBPopupFactory.ActionSelectionAid.MNEMONICS; Component component = t.getComponent(); List actions = new ArrayList<>(); + if (LanguageServerWrapper.forProject(project) == null) { + LOG.warn("LanguageServerWrapper.forProject(project) is null: " + this); + return; + } if (LanguageServerWrapper.forProject(project).getStatus() == ServerStatus.INITIALIZED) { actions.add(new ShowConnectedFiles()); } From eb98e31d34bbfc54a0f13ed464b957ce66147f76 Mon Sep 17 00:00:00 2001 From: Sascha Kliche Date: Mon, 11 Apr 2022 12:52:07 +0200 Subject: [PATCH 7/9] HLS-167 Improve messages --- .../client/connection/ProcessStreamConnectionProvider.java | 4 ++-- .../wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/client/connection/ProcessStreamConnectionProvider.java b/src/main/java/org/wso2/lsp4intellij/client/connection/ProcessStreamConnectionProvider.java index b9d3ad5a..a2af5317 100644 --- a/src/main/java/org/wso2/lsp4intellij/client/connection/ProcessStreamConnectionProvider.java +++ b/src/main/java/org/wso2/lsp4intellij/client/connection/ProcessStreamConnectionProvider.java @@ -56,13 +56,13 @@ public ProcessStreamConnectionProvider(@NotNull ProcessBuilder processBuilder) { public void start() throws IOException { if ((workingDir == null || commands == null || commands.isEmpty() || commands.contains(null)) && builder == null) { - throw new IOException("Unable to start language server: " + this.toString()); + throw new IOException("Unable to start language server: " + this); } ProcessBuilder builder = createProcessBuilder(); LOG.info("Starting server process with commands " + commands + " and workingDir " + workingDir); process = builder.start(); if (!process.isAlive()) { - throw new IOException("Unable to start language server: " + this.toString()); + throw new IOException("Unable to start language server, process is not alive: " + this); } else { LOG.info("Server process started " + process); } diff --git a/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java b/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java index 7ba5a65f..0fea9ee8 100644 --- a/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java +++ b/src/main/java/org/wso2/lsp4intellij/statusbar/LSPServerStatusWidget.java @@ -237,7 +237,7 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) { public String getTooltipText() { LanguageServerWrapper wrapper = LanguageServerWrapper.forProject(project); if (wrapper == null) { - return "Language server, project " + projectName; + return "Language server wrapper is null for project " + projectName; } else { return "Language server for extension " + wrapper.getServerDefinition().ext + ", project " + projectName; } From d09626d9f825981c98c7a88f75318451e0a8b6b8 Mon Sep 17 00:00:00 2001 From: Sascha Kliche Date: Mon, 11 Apr 2022 12:53:47 +0200 Subject: [PATCH 8/9] HLS-167 Wrap EXECUTOR_SERVICE.submit in try/catch --- .../org/wso2/lsp4intellij/utils/ApplicationUtils.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/wso2/lsp4intellij/utils/ApplicationUtils.java b/src/main/java/org/wso2/lsp4intellij/utils/ApplicationUtils.java index 32a84b5e..03c660c1 100644 --- a/src/main/java/org/wso2/lsp4intellij/utils/ApplicationUtils.java +++ b/src/main/java/org/wso2/lsp4intellij/utils/ApplicationUtils.java @@ -16,7 +16,7 @@ package org.wso2.lsp4intellij.utils; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.application.ModalityState; +import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.NoAccessDuringPsiEvents; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Condition; @@ -29,6 +29,7 @@ public class ApplicationUtils { + private static final Logger LOG = Logger.getInstance(ApplicationUtils.class); private static ExecutorService EXECUTOR_SERVICE; static { @@ -49,7 +50,11 @@ static public void invokeLater(Runnable runnable) { } static public void pool(Runnable runnable) { - EXECUTOR_SERVICE.submit(runnable); + try { + EXECUTOR_SERVICE.submit(runnable); + } catch(Exception e) { + LOG.warn(e + ": ApplicationUtils"); + } } static public void restartPool() { From 21920bffb1296cea3befa20095e7dd7836d093cd Mon Sep 17 00:00:00 2001 From: Sascha Kliche Date: Mon, 11 Apr 2022 12:54:25 +0200 Subject: [PATCH 9/9] HLS-167 Replace deprecated compile/testCompile with implementation/testImplementation --- build.gradle | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index cee65fdf..b6c1c6ca 100644 --- a/build.gradle +++ b/build.gradle @@ -30,13 +30,13 @@ intellij { } dependencies { - compile group: 'org.eclipse.lsp4j', name: 'org.eclipse.lsp4j', version: '0.10.0' - compile group: 'com.vladsch.flexmark', name: 'flexmark', version: '0.34.60' - compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' - testCompile group: 'junit', name: 'junit', version: '4.13.2' - testCompile group: 'org.mockito', name: 'mockito-core', version: '3.9.0' - testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9' - testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9' + implementation group: 'org.eclipse.lsp4j', name: 'org.eclipse.lsp4j', version: '0.10.0' + implementation group: 'com.vladsch.flexmark', name: 'flexmark', version: '0.34.60' + implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' + testImplementation group: 'junit', name: 'junit', version: '4.13.2' + testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.9.0' + testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.9' + testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.9' } sourceSets {