From cb2b2a6d8bf733b3a95e1be20e5ec6c6d334de21 Mon Sep 17 00:00:00 2001 From: Dirk Baeumer Date: Wed, 11 Oct 2017 08:13:11 +0200 Subject: [PATCH] Udapte multi root LSP example --- lsp-multi-root-sample/client/src/extension.ts | 21 ++++--- lsp-multi-root-sample/server/src/server.ts | 55 ++++++++++++------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/lsp-multi-root-sample/client/src/extension.ts b/lsp-multi-root-sample/client/src/extension.ts index 1cd542ca..b3f2ed16 100644 --- a/lsp-multi-root-sample/client/src/extension.ts +++ b/lsp-multi-root-sample/client/src/extension.ts @@ -16,6 +16,7 @@ interface MultiRootExampleSettings { maxNumberOfProblems: number; } +let client: LanguageClient; export function activate(context: ExtensionContext) { // The server is implemented in node @@ -69,20 +70,26 @@ export function activate(context: ExtensionContext) { synchronize: { // Notify the server about file changes to '.clientrc files contain in the workspace fileEvents: workspace.createFileSystemWatcher('**/.clientrc'), - configurationSection: [ 'lspMultiRootSample' ] + // In the past this told the client to actively synchronize settings. Since the + // client now supports 'getConfiguration' requests this active synchronization is not + // necessary anymore. + // configurationSection: [ 'lspMultiRootSample' ] }, middleware: middleware as Middleware } // Create the language client and start the client. - let client = new LanguageClient('languageServerExample', 'Language Server Example', serverOptions, clientOptions); + client = new LanguageClient('languageServerExample', 'Language Server Example', serverOptions, clientOptions); // Register new proposed protocol if available. client.registerProposedFeatures(); // 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); + client.start(); +} + +export function deactivate(): Thenable { + if (!client) { + return undefined; + } + return client.stop(); } \ No newline at end of file diff --git a/lsp-multi-root-sample/server/src/server.ts b/lsp-multi-root-sample/server/src/server.ts index 4f4262c5..f0f551fc 100644 --- a/lsp-multi-root-sample/server/src/server.ts +++ b/lsp-multi-root-sample/server/src/server.ts @@ -6,13 +6,13 @@ import { createConnection, TextDocuments, TextDocument, Diagnostic, DiagnosticSeverity, - ProposedFeatures, InitializeParams, + ProposedFeatures, InitializeParams, DidChangeConfigurationNotification } from 'vscode-languageserver'; -// create a connection for the server. The connection uses Node's IPC as a transport +// Create a connection for the server. The connection uses Node's IPC as a transport let connection = createConnection(ProposedFeatures.all); -// create a simple text document manager. The text document manager +// Create a simple text document manager. The text document manager // supports full document sync only let documents: TextDocuments = new TextDocuments(); @@ -26,8 +26,8 @@ connection.onInitialize((params: InitializeParams) => { } return !!c; } - // does the client support the `workspace/configuration` request? - // if not, we will fall back using global settings + // Does the client support the `workspace/configuration` request? + // If not, we will fall back using global settings hasConfigurationCapability = hasClientCapability('workspace', 'configuration'); return { capabilities: { @@ -36,24 +36,37 @@ connection.onInitialize((params: InitializeParams) => { } }); -// the example settings +connection.onInitialized(() => { + if (hasConfigurationCapability) { + // Register for configuration change events if the client has + // support for the configuration capability + connection.client.register(DidChangeConfigurationNotification.type); + } +}); + +// The example settings interface MultiRootExampleSettings { maxNumberOfProblems: number; } -// the global settings, used when the `workspace/configuration` request is not supported by the client -let globalSettings: MultiRootExampleSettings = { maxNumberOfProblems: 1000 }; +// The global settings, used when the `workspace/configuration` request is not supported by the client. +// Please note that this is not the case when using this server with the client provided in this example +// but could happen with other clients. +const defaultSettings: MultiRootExampleSettings = { maxNumberOfProblems: 1000 }; +let globalSettings: MultiRootExampleSettings = defaultSettings; -// cache the settings of all open documents +// Cache the settings of all open documents let documentSettings: Map> = new Map(); connection.onDidChangeConfiguration(change => { - globalSettings = (change.settings.lspMultiRootSample || {}); + if (hasConfigurationCapability) { + // Reset all cached document settings + documentSettings.clear(); + } else { + globalSettings = (change.settings.lspMultiRootSample || defaultSettings); + } - // reset all document settings - documentSettings.clear(); - - // revalidate all open text documents + // Revalidate all open text documents documents.all().forEach(validateTextDocument); }); @@ -69,22 +82,22 @@ function getDocumentSettings(resource: string): Thenable { documentSettings.delete(e.document.uri); }); -// the content of a text document has changed. This event is emitted +// The content of a text document has changed. This event is emitted // when the text document first opened or when its content has changed. documents.onDidChangeContent((change) => { validateTextDocument(change.document); }); async function validateTextDocument(textDocument: TextDocument): Promise { - // in this simple example we get the settings for every validate run. + // In this simple example we get the settings for every validate run. let settings = await getDocumentSettings(textDocument.uri); - // the validator creates diagnostics for all uppercase words length 2 and more + // The validator creates diagnostics for all uppercase words length 2 and more let text = textDocument.getText(); let pattern = /\b[A-Z]{2,}\b/g; let m: RegExpExecArray; @@ -104,13 +117,13 @@ async function validateTextDocument(textDocument: TextDocument): Promise { }); } - // send the computed diagnostics to VSCode. + // Send the computed diagnostics to VSCode. connection.sendDiagnostics({ uri: textDocument.uri, diagnostics }); } -// make the text document manager listen on the connection +// Make the text document manager listen on the connection // for open, change and close text document events documents.listen(connection); -// listen on the connection +// Listen on the connection connection.listen(); \ No newline at end of file