Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-root workspace #1928

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ import {
import { resolve } from 'path';
import { existsSync } from 'fs';

export function initializeLanguageClient(vlsModulePath: string, globalSnippetDir: string): LanguageClient {
export function initializeLanguageClient(
vlsModulePath: string,
globalSnippetDir: string,
workspaceFolder?: vscode.WorkspaceFolder
): LanguageClient {
const debugOptions = { execArgv: ['--nolazy', '--inspect=6005'] };

const documentSelector = ['vue'];
const documentSelector = workspaceFolder
? [
{
language: 'vue',
scheme: 'file',
pattern: `${workspaceFolder.uri.fsPath}/**/*`
}
]
: ['vue'];
const config = vscode.workspace.getConfiguration();

let serverPath;
Expand Down Expand Up @@ -56,7 +68,8 @@ export function initializeLanguageClient(vlsModulePath: string, globalSnippetDir
config,
globalSnippetDir
},
revealOutputChannelOn: RevealOutputChannelOn.Never
revealOutputChannelOn: RevealOutputChannelOn.Never,
workspaceFolder
};

return new LanguageClient('vetur', 'Vue Language Server', serverOptions, clientOptions);
Expand Down
110 changes: 79 additions & 31 deletions client/vueMain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { LanguageClient, WorkspaceEdit } from 'vscode-languageclient';
import { LanguageClient, WorkspaceEdit, WorkspaceFolder } from 'vscode-languageclient';
import { generateGrammarCommandHandler } from './commands/generateGrammarCommand';
import { registerLanguageConfigurations } from './languages';
import { initializeLanguageClient } from './client';
Expand All @@ -12,6 +12,11 @@ import {
import { getGlobalSnippetDir } from './userSnippetDir';
import { generateOpenUserScaffoldSnippetFolderCommand } from './commands/openUserScaffoldSnippetFolderCommand';

/**
* Map of workspace folder URI -> LanguageClient
*/
const clients = new Map<string, LanguageClient>();

export async function activate(context: vscode.ExtensionContext) {
const isInsiders = vscode.env.appName.includes('Insiders');
const globalSnippetDir = getGlobalSnippetDir(isInsiders);
Expand All @@ -28,6 +33,15 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('vetur.generateGrammar', generateGrammarCommandHandler(context.extensionPath))
);

registerLanguageConfigurations();

/**
* Vue Language Server path
* To be able to work with multiple workspaces, we initialize one language service per workspace folder
*/

const serverModule = context.asAbsolutePath(join('server', 'dist', 'vueServerMain.js'));

/**
* Open custom snippet folder
*/
Expand All @@ -38,40 +52,74 @@ export async function activate(context: vscode.ExtensionContext) {
)
);

context.subscriptions.push(
vscode.commands.registerCommand('vetur.applyWorkspaceEdits', (args: WorkspaceEdit) => {
const edit = client.protocol2CodeConverter.asWorkspaceEdit(args)!;
vscode.workspace.applyEdit(edit);
})
);
function onDidOpenTextDocument(document: vscode.TextDocument) {
const client = initializeClientForTextDocument(context, serverModule, globalSnippetDir, document);

context.subscriptions.push(
vscode.commands.registerCommand('vetur.chooseTypeScriptRefactoring', (args: any) => {
client
.sendRequest<vscode.Command | undefined>('requestCodeActionEdits', args)
.then(command => command && vscode.commands.executeCommand(command.command, ...command.arguments!));
})
);
// TODO handle commands properly
if (!client || clients.size > 1) {
return;
}

registerLanguageConfigurations();
context.subscriptions.push(
vscode.commands.registerCommand('vetur.applyWorkspaceEdits', (args: WorkspaceEdit) => {
const edit = client.protocol2CodeConverter.asWorkspaceEdit(args)!;
vscode.workspace.applyEdit(edit);
})
);

/**
* Vue Language Server Initialization
*/
context.subscriptions.push(
vscode.commands.registerCommand('vetur.chooseTypeScriptRefactoring', (args: any) => {
client
.sendRequest<vscode.Command | undefined>('requestCodeActionEdits', args)
.then(command => command && vscode.commands.executeCommand(command.command, ...command.arguments!));
})
);
}

const serverModule = context.asAbsolutePath(join('server', 'dist', 'vueServerMain.js'));
const client = initializeLanguageClient(serverModule, globalSnippetDir);
context.subscriptions.push(client.start());

client
.onReady()
.then(() => {
registerCustomClientNotificationHandlers(client);
registerCustomLSPCommands(context, client);
})
.catch(e => {
console.log('Client initialization failed');
});
vscode.workspace.onDidOpenTextDocument(onDidOpenTextDocument);
vscode.workspace.textDocuments.forEach(onDidOpenTextDocument);

vscode.workspace.onDidChangeWorkspaceFolders(event => {
for (const folder of event.removed) {
const client = clients.get(folder.uri.toString());
if (client) {
clients.delete(folder.uri.toString());
client.stop();
}
}
});
}

function initializeClientForTextDocument(
context: vscode.ExtensionContext,
serverModule: string,
globalSnippetDir: string,
document: vscode.TextDocument
): LanguageClient | undefined {
const folder = vscode.workspace.getWorkspaceFolder(document.uri);

if (!folder || document.languageId !== 'vue') {
return;
}

if (!clients.has(folder.uri.toString())) {
const client = initializeLanguageClient(serverModule, globalSnippetDir, folder);

client
.onReady()
.then(() => {
registerCustomClientNotificationHandlers(client);
registerCustomLSPCommands(context, client);
})
.catch(e => {
console.log(`Client initialization failed for workspace: ${folder.uri.toString()}`);
});

context.subscriptions.push(client.start());
clients.set(folder.uri.toString(), client);

return client;
}
}

function registerCustomClientNotificationHandlers(client: LanguageClient) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/vueServerMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ connection.onInitialize(
async (params: InitializeParams): Promise<InitializeResult> => {
await vls.init(params);

console.log('Vetur initialized');
console.log(`Vetur initialized for workspace folder: ${params.rootPath}`);

return {
capabilities: vls.capabilities
Expand Down