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

Spawn multi lang server merge fix #2129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 84 additions & 2 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 @@ -44,7 +58,6 @@ export async function activate(context: vscode.ExtensionContext) {
* Vue Language Server Initialization
*/

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

Expand All @@ -65,6 +78,43 @@ export async function activate(context: vscode.ExtensionContext) {
},
() => promise
);

function onDidOpenTextDocument(document: vscode.TextDocument) {
const client = initializeClientForTextDocument(context, serverModule, globalSnippetDir, document);
// TODO handle commands properly
if (!client || clients.size > 1) {
return;
}
context.subscriptions.push(
vscode.commands.registerCommand('vetur.applyWorkspaceEdits', (args: WorkspaceEdit) => {
const edit = client.protocol2CodeConverter.asWorkspaceEdit(args)!;
vscode.workspace.applyEdit(edit);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('vetur.chooseTypeScriptRefactoring', (args: any) => {
client.sendRequest<WorkspaceEdit | undefined>('requestCodeActionEdits', args).then(edits => {
if (edits) {
vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edits)!);
}
});
})
);
}

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 registerCustomClientNotificationHandlers(client: LanguageClient) {
Expand All @@ -87,3 +137,35 @@ function registerCustomLSPCommands(context: vscode.ExtensionContext, client: Lan
vscode.commands.registerCommand('vetur.showCorrespondingVirtualFile', generateShowVirtualFileCommand(client))
);
}

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;
}
}
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