From 9b21ec06e0367f03a5ac07df4c3713a3bec00e9e Mon Sep 17 00:00:00 2001 From: azerr Date: Tue, 12 Sep 2023 18:09:49 +0200 Subject: [PATCH] fix: Set project as workspace folder Fixes #1155 Signed-off-by: azerr --- .../devtools/intellij/lsp4ij/LSPIJUtils.java | 4 ++-- .../lsp4ij/LanguageServerWrapper.java | 19 +++++++++---------- .../lsp4ij/client/LanguageClientImpl.java | 12 +++++++----- .../lsp4ij/internal/SupportedFeatures.java | 2 +- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java index ee59687d8..121e7d22e 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java @@ -195,9 +195,9 @@ public static Position toPosition(int offset, Document document) { } @Nonnull - public static WorkspaceFolder toWorkspaceFolder(@Nonnull Module project) { + public static WorkspaceFolder toWorkspaceFolder(@Nonnull Project project) { WorkspaceFolder folder = new WorkspaceFolder(); - folder.setUri(toUri(project).toString()); + folder.setUri(toUri(project).toASCIIString()); folder.setName(project.getName()); return folder; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java index aaef7abc1..c3d361203 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java @@ -22,6 +22,7 @@ import com.intellij.openapi.fileEditor.FileEditorManagerListener; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.*; import com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter; @@ -51,6 +52,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import java.util.function.UnaryOperator; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Language server wrapper. @@ -129,8 +132,6 @@ private void didCloseOldFile(VirtualFile virtualParentFile, String fileName) { @Nullable protected final Project initialProject; @Nonnull - protected final Set allWatchedProjects; - @Nonnull protected Map connectedDocuments; @Nullable protected final URI initialPath; @@ -184,7 +185,6 @@ private LanguageServerWrapper(@Nullable Project project, @Nonnull LanguageServer @Nullable URI initialPath) { this.initialProject = project; this.initialPath = initialPath; - this.allWatchedProjects = new HashSet<>(); this.serverDefinition = serverDefinition; this.connectedDocuments = new HashMap<>(); String projectName = (project != null && project.getName() != null && !serverDefinition.isSingleton) ? ("@" + project.getName()) : ""; //$NON-NLS-1$//$NON-NLS-2$ @@ -207,11 +207,6 @@ private LanguageServerWrapper(@Nullable Project project, @Nonnull LanguageServer } } - @Nonnull - public Set getAllWatchedProjects() { - return allWatchedProjects; - } - public Project getProject() { return initialProject; } @@ -334,7 +329,6 @@ public synchronized void start() throws LanguageServerException { currentConnectionProvider.handleMessage(message, this.languageServer, rootURI); } }); - // initParams.setWorkspaceFolders(getRelevantWorkspaceFolders()); Launcher launcher = serverDefinition.createLauncherBuilder() // .setLocalService(languageClient)// .setRemoteInterface(serverDefinition.getServerInterface())// @@ -422,6 +416,11 @@ private CompletableFuture initServer(final URI rootURI) { initParams.setClientInfo(getClientInfo()); initParams.setTrace(this.lspStreamProvider.getTrace(rootURI)); + if (initialProject != null) { + var folders = Arrays.asList(LSPIJUtils.toWorkspaceFolder(initialProject)); + initParams.setWorkspaceFolders(folders); + } + // no then...Async future here as we want this chain of operation to be sequential and "atomic"-ish return languageServer.initialize(initParams); } @@ -675,7 +674,7 @@ CompletableFuture connect(Document document) throws IOException * @since 0.5 */ public boolean canOperate(Project project) { - if (project != null && (project.equals(this.initialProject) || this.allWatchedProjects.contains(project))) { + if (project != null && project.equals(this.initialProject)) { return true; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/LanguageClientImpl.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/LanguageClientImpl.java index 64b5c9274..94282d1f1 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/LanguageClientImpl.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/LanguageClientImpl.java @@ -14,6 +14,7 @@ import org.eclipse.lsp4j.services.LanguageServer; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; @@ -95,11 +96,12 @@ public CompletableFuture unregisterCapability(UnregistrationParams params) @Override public CompletableFuture> workspaceFolders() { - List res = new ArrayList<>(wrapper.getAllWatchedProjects().size()); - for (final Module project : wrapper.getAllWatchedProjects()) { - res.add(LSPIJUtils.toWorkspaceFolder(project)); + List folders = null; + Project project = wrapper.getProject(); + if (project != null) { + folders = Arrays.asList(LSPIJUtils.toWorkspaceFolder(project)); } - return CompletableFuture.completedFuture(res); + return CompletableFuture.completedFuture(folders); } @Override @@ -129,7 +131,7 @@ protected void triggerChangeConfiguration() { return; } Object settings = createSettings(); - if(settings == null) { + if (settings == null) { return; } DidChangeConfigurationParams params = new DidChangeConfigurationParams(settings); diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/SupportedFeatures.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/SupportedFeatures.java index 9b62c2dc8..7f63c59be 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/SupportedFeatures.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/internal/SupportedFeatures.java @@ -127,7 +127,7 @@ public class SupportedFeatures { workspaceClientCapabilities.setExecuteCommand(new ExecuteCommandCapabilities(Boolean.TRUE)); // TODO // workspaceClientCapabilities.setSymbol(new SymbolCapabilities(Boolean.TRUE)); - workspaceClientCapabilities.setWorkspaceFolders(Boolean.FALSE); + workspaceClientCapabilities.setWorkspaceFolders(Boolean.TRUE); WorkspaceEditCapabilities editCapabilities = new WorkspaceEditCapabilities(); editCapabilities.setDocumentChanges(Boolean.TRUE); // TODO