Skip to content

Commit

Permalink
fix: Set project as workspace folder
Browse files Browse the repository at this point in the history
Fixes #1155

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 13, 2023
1 parent a3f4add commit 0268ae1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
18 changes: 14 additions & 4 deletions src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.*;
import com.intellij.psi.PsiDocumentManager;
Expand Down Expand Up @@ -195,19 +197,27 @@ 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;
}

public static URI toUri(Module project) {
File file = new File(project.getModuleFilePath()).getParentFile();
public static URI toUri(Module module) {
VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
if (roots.length > 0) {
return toUri(roots[0]);
}
File file = new File(module.getModuleFilePath()).getParentFile();
return file.toURI();
}

public static URI toUri(Project project) {
VirtualFile[] roots = ProjectRootManager.getInstance(project).getContentRoots();
if (roots.length > 0) {
return toUri(roots[0]);
}
File file = new File(project.getProjectFilePath()).getParentFile();
return file.toURI();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -129,8 +132,6 @@ private void didCloseOldFile(VirtualFile virtualParentFile, String fileName) {
@Nullable
protected final Project initialProject;
@Nonnull
protected final Set<Module> allWatchedProjects;
@Nonnull
protected Map<URI, DocumentContentSynchronizer> connectedDocuments;
@Nullable
protected final URI initialPath;
Expand Down Expand Up @@ -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$
Expand All @@ -207,11 +207,6 @@ private LanguageServerWrapper(@Nullable Project project, @Nonnull LanguageServer
}
}

@Nonnull
public Set<Module> getAllWatchedProjects() {
return allWatchedProjects;
}

public Project getProject() {
return initialProject;
}
Expand Down Expand Up @@ -334,7 +329,6 @@ public synchronized void start() throws LanguageServerException {
currentConnectionProvider.handleMessage(message, this.languageServer, rootURI);
}
});
// initParams.setWorkspaceFolders(getRelevantWorkspaceFolders());
Launcher<LanguageServer> launcher = serverDefinition.createLauncherBuilder() //
.setLocalService(languageClient)//
.setRemoteInterface(serverDefinition.getServerInterface())//
Expand Down Expand Up @@ -422,6 +416,11 @@ private CompletableFuture<InitializeResult> 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);
}
Expand Down Expand Up @@ -675,7 +674,7 @@ CompletableFuture<LanguageServer> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,11 +96,12 @@ public CompletableFuture<Void> unregisterCapability(UnregistrationParams params)

@Override
public CompletableFuture<List<WorkspaceFolder>> workspaceFolders() {
List<WorkspaceFolder> res = new ArrayList<>(wrapper.getAllWatchedProjects().size());
for (final Module project : wrapper.getAllWatchedProjects()) {
res.add(LSPIJUtils.toWorkspaceFolder(project));
List<WorkspaceFolder> folders = null;
Project project = wrapper.getProject();
if (project != null) {
folders = Arrays.asList(LSPIJUtils.toWorkspaceFolder(project));
}
return CompletableFuture.completedFuture(res);
return CompletableFuture.completedFuture(folders);
}

@Override
Expand Down Expand Up @@ -129,7 +131,7 @@ protected void triggerChangeConfiguration() {
return;
}
Object settings = createSettings();
if(settings == null) {
if (settings == null) {
return;
}
DidChangeConfigurationParams params = new DidChangeConfigurationParams(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private PsiQuteProjectUtils() {

public static ProjectInfo getProjectInfo(Module javaProject) {
String projectUri = getProjectURI(javaProject);
String templateBaseDir = LSPIJUtils.toUri(QuarkusModuleUtil.getModuleDirPath(javaProject).findFileByRelativePath(TEMPLATES_BASE_DIR)).toString();
String templateBaseDir = LSPIJUtils.toUriAsString(QuarkusModuleUtil.getModuleDirPath(javaProject)) + TEMPLATES_BASE_DIR;
return new ProjectInfo(projectUri, templateBaseDir);
}

Expand Down

0 comments on commit 0268ae1

Please sign in to comment.