Skip to content

Commit

Permalink
fixup! fixup! Rework LSP to allow multi-project/workspace setups
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Jan 16, 2023
1 parent 947c56b commit a94457b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/lsp/commands/vscode-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const startHandler = async (

const pickClient = async (clients: defs.LSPClientMap): Promise<string> => {
if (clients.size === 0) {
return
return;
}
if (clients.size === 1) {
return Array.from(clients.keys())[0]
return Array.from(clients.keys())[0];
}
const choices = Array.from(clients.keys()).map((uri) => {
return {
Expand Down
92 changes: 50 additions & 42 deletions src/lsp/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export const createClientProvider = (params: CreateClientProviderParams) => {
client.onDidChangeState(async (state) => {
if (state.newState === vscode_lsp.State.Stopped) {
clients.delete(uri_path);

if (api.getClientForActiveDocument(clients)) {
return status_bar.updateStatusBar(status_bar_item, status_bar.LspStatus.Running);
}
}

if (await utils.activeDocumentIsInSameRoot(uri)) {
Expand Down Expand Up @@ -130,59 +134,63 @@ export const createClientProvider = (params: CreateClientProviderParams) => {
}

// Provision new LSP clients when clojure files are opened and for all already opened clojure files.
vscode.workspace.onDidOpenTextDocument((document) => {
if (config.get<boolean>('enableClojureLspOnStart')) {
void provision_queue.push(document).catch((err) => console.error(err));
}
});
params.context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument((document) => {
if (config.get<boolean>('enableClojureLspOnStart')) {
void provision_queue.push(document).catch((err) => console.error(err));
}
})
);
const auto_start = config.get<boolean>('enableClojureLspOnStart');
if (auto_start) {
vscode.workspace.textDocuments.forEach((document) => {
void provision_queue.push(document).catch((err) => console.error(err));
});
}

/**
* We setup a listener for the active editor changing and use that to update the status bar with the
* status of the LSP server related to the currently active document.
*/
vscode.window.onDidChangeActiveTextEditor((event) => {
if (!event) {
status_bar_item.hide();
return;
}
params.context.subscriptions.push(
/**
* We setup a listener for the active editor changing and use that to update the status bar with the
* status of the LSP server related to the currently active document.
*/
vscode.window.onDidChangeActiveTextEditor((event) => {
if (!event) {
status_bar_item.hide();
return;
}

if (event?.document.languageId === 'clojure') {
status_bar_item.show();
} else {
status_bar_item.hide();
}
if (event?.document.languageId === 'clojure') {
status_bar_item.show();
} else {
status_bar_item.hide();
}

const client = api.getClientForDocumentUri(clients, event.document.uri);
if (!client) {
status_bar.updateStatusBar(status_bar_item, status_bar.LspStatus.Stopped);
return;
}
const client = api.getClientForDocumentUri(clients, event.document.uri);
if (!client) {
status_bar.updateStatusBar(status_bar_item, status_bar.LspStatus.Stopped);
return;
}

status_bar.updateStatusBar(
status_bar_item,
status_bar.lspClientStateToStatus(client.state)
);
});

vscode.workspace.onDidChangeWorkspaceFolders((event) => {
event.removed.forEach((folder) => {
clients.forEach((client, dir) => {
const relative = path.relative(folder.uri.path, dir);
if ((relative && !relative.includes('../')) || dir === folder.uri.path) {
console.log(
`Shutting down the LSP client at ${dir} as it belongs to a removed workspace`
);
void client.stop().catch((err) => console.error(err));
}
status_bar.updateStatusBar(
status_bar_item,
status_bar.lspClientStateToStatus(client.state)
);
}),

vscode.workspace.onDidChangeWorkspaceFolders((event) => {
event.removed.forEach((folder) => {
clients.forEach((client, dir) => {
const relative = path.relative(folder.uri.path, dir);
if ((relative && !relative.includes('../')) || dir === folder.uri.path) {
console.log(
`Shutting down the LSP client at ${dir} as it belongs to a removed workspace`
);
void client.stop().catch((err) => console.error(err));
}
});
});
});
});
})
);

params.context.subscriptions.push(
...commands.registerLspCommands(clients),
Expand Down

0 comments on commit a94457b

Please sign in to comment.