mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
Add example to use a single server on a multi workspace folder setup
This commit is contained in:
89
lsp-multi-root-sample/client/src/extension.ts
Normal file
89
lsp-multi-root-sample/client/src/extension.ts
Normal file
@ -0,0 +1,89 @@
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
* ------------------------------------------------------------------------------------------ */
|
||||
'use strict';
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
import { workspace, Disposable, ExtensionContext, WorkspaceConfiguration } from 'vscode';
|
||||
import {
|
||||
LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TransportKind, ProposedProtocol, Middleware, CancellationToken,
|
||||
ConfigurationMiddleware, GetConfigurationParams
|
||||
} from 'vscode-languageclient';
|
||||
|
||||
// The example settings
|
||||
interface MultiRootExampleSettings {
|
||||
maxNumberOfProblems: number;
|
||||
}
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
|
||||
// The server is implemented in node
|
||||
let serverModule = context.asAbsolutePath(path.join('server', 'server.js'));
|
||||
// The debug options for the server
|
||||
let debugOptions = { execArgv: ["--nolazy", "--inspect=6009"] };
|
||||
|
||||
// If the extension is launched in debug mode then the debug server options are used
|
||||
// Otherwise the run options are used
|
||||
let serverOptions: ServerOptions = {
|
||||
run : { module: serverModule, transport: TransportKind.ipc },
|
||||
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
|
||||
}
|
||||
|
||||
// Convert VS Code specific settings to a format acceptable by the server. Since
|
||||
// both client and server do use JSON the conversion is trivial.
|
||||
let configurationMiddleware: ConfigurationMiddleware = {
|
||||
configuration: (params: GetConfigurationParams, _token: CancellationToken, _next: Function): any[] => {
|
||||
if (!params.items) {
|
||||
return null;
|
||||
}
|
||||
let result: (MultiRootExampleSettings | null)[] = [];
|
||||
for (let item of params.items) {
|
||||
// The server asks the client for configuration settings without a section
|
||||
// If a section is present we return null to indicate that the configuration
|
||||
// is not supported.
|
||||
if (item.section) {
|
||||
result.push(null);
|
||||
continue;
|
||||
}
|
||||
let config: WorkspaceConfiguration;
|
||||
if (item.scopeUri) {
|
||||
config = workspace.getConfiguration('lsp-multi-root-sample', client.protocol2CodeConverter.asUri(item.scopeUri));
|
||||
} else {
|
||||
config = workspace.getConfiguration('lsp-multi-root-sample');
|
||||
}
|
||||
result.push({
|
||||
maxNumberOfProblems: config.get('maxNumberOfProblems')
|
||||
});
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// Options to control the language client
|
||||
let clientOptions: LanguageClientOptions = {
|
||||
// Register the server for plain text documents
|
||||
documentSelector: [{scheme: 'file', language: 'plaintext'}],
|
||||
synchronize: {
|
||||
// Notify the server about file changes to '.clientrc files contain in the workspace
|
||||
fileEvents: workspace.createFileSystemWatcher('**/.clientrc')
|
||||
},
|
||||
middleware: {
|
||||
workspace: configurationMiddleware as any // cast to any due to proposed API
|
||||
}
|
||||
}
|
||||
|
||||
// Create the language client and start the client.
|
||||
let client = new LanguageClient('languageServerExample', 'Language Server Example', serverOptions, clientOptions);
|
||||
// Register new propose protocol if available.
|
||||
client.registerFeatures(ProposedProtocol(client));
|
||||
|
||||
// Start the client. This will also launch the server
|
||||
let disposable = client.start();
|
||||
|
||||
// Push the disposable to the context's subscriptions so that the
|
||||
// client can be deactivated on extension deactivation
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
Reference in New Issue
Block a user