Skip to content

Commit

Permalink
Only auto-start clojure-lsp in valid projects
Browse files Browse the repository at this point in the history
When the `"when-workspace-opened-use-workspace-root"` setting is enabled
the clojure-lsp is started in _all_ workspace roots open instead of only
valid clojure project roots.

This commit reworks this behaviour to first check if the workspace root
is a valid Clojure project before attempting to auto-start the lsp
server.

Addresses BetterThanTomorrow#2041
  • Loading branch information
julienvincent committed Jan 29, 2023
1 parent 26f8b44 commit 5827fb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function activate(context: vscode.ExtensionContext) {
testRunner.onTestTree(testController, tree);
},
});
clientProvider.init();
await clientProvider.init();

lsp.registerGlobally(clientProvider);

Expand Down
24 changes: 18 additions & 6 deletions src/lsp/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode_lsp from 'vscode-languageclient/node';
import * as project_utils from '../project-root';
import * as vscode from 'vscode';
import * as path from 'node:path';

Expand Down Expand Up @@ -143,7 +144,7 @@ export const createClientProvider = (params: CreateClientProviderParams) => {
return {
getClientForDocumentUri: (uri: vscode.Uri) => api.getClientForDocumentUri(clients, uri),

init: () => {
init: async () => {
status_bar_item.show();

if (vscode.window.activeTextEditor?.document.languageId === 'clojure') {
Expand All @@ -163,8 +164,15 @@ export const createClientProvider = (params: CreateClientProviderParams) => {

switch (config.getAutoStartBehaviour()) {
case config.AutoStartBehaviour.WorkspaceOpened: {
vscode.workspace.workspaceFolders.forEach((folder) => {
void provision_queue.push(folder.uri).catch((err) => console.error(err));
const roots = await project_utils.findProjectRootsWithReasons();
const workspace_folders = roots
.filter((root) => {
return root.valid_project && root.workspace_root;
})
.map((root) => root.uri);

workspace_folders.forEach((root) => {
void provision_queue.push(root).catch((err) => console.error(err));
});
break;
}
Expand Down Expand Up @@ -199,9 +207,13 @@ export const createClientProvider = (params: CreateClientProviderParams) => {
});

if (config.getAutoStartBehaviour() === config.AutoStartBehaviour.WorkspaceOpened) {
event.added.forEach((folder) => {
void provision_queue.push(folder.uri).catch((err) => console.error(err));
});
void Promise.allSettled(
event.added.map(async (folder) => {
if (await project_utils.isValidClojureProject(folder.uri)) {
void provision_queue.push(folder.uri).catch((err) => console.error(err));
}
})
);
}
})
);
Expand Down
8 changes: 8 additions & 0 deletions src/project-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export function excludePattern(moreExcludes: string[] = []) {
return `**/{${[...moreExcludes, ...config.getConfig().projectRootsSearchExclude].join(',')}}/**`;
}

export async function isValidClojureProject(uri: vscode.Uri) {
return findProjectRootsWithReasons().then((roots) => {
return !!roots.find((root) => {
return uri.path === root.uri.path && root.valid_project;
});
});
}

function findMatchingParent(
from: vscode.Uri,
uris: vscode.Uri[],
Expand Down

0 comments on commit 5827fb7

Please sign in to comment.