mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Added multi server LSP example
This commit is contained in:
3
multi-lsp-sample/.gitignore
vendored
Normal file
3
multi-lsp-sample/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
out
|
||||
node_modules
|
||||
client/server
|
||||
17
multi-lsp-sample/client/.vscode/launch.json
vendored
Normal file
17
multi-lsp-sample/client/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
// A launch configuration that compiles the extension and then opens it inside a new window
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Extension",
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/out/**/*.js"],
|
||||
"preLaunchTask": "npm: watch"
|
||||
}
|
||||
]
|
||||
}
|
||||
13
multi-lsp-sample/client/.vscode/settings.json
vendored
Normal file
13
multi-lsp-sample/client/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"files.exclude": {
|
||||
"out": false // set this to true to hide the "out" folder with the compiled JS files
|
||||
},
|
||||
"search.exclude": {
|
||||
"out": true
|
||||
},
|
||||
"editor.insertSpaces": false,
|
||||
"editor.tabSize": 4,
|
||||
"typescript.tsdk": "./node_modules/typescript/lib", "testbed.trace.server": "verbose"
|
||||
, "testbed.server.trace": "verbose"
|
||||
}
|
||||
17
multi-lsp-sample/client/.vscode/tasks.json
vendored
Normal file
17
multi-lsp-sample/client/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "watch",
|
||||
"isBackground": true,
|
||||
"problemMatcher": [
|
||||
"$tsc-watch"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
7
multi-lsp-sample/client/.vscodeignore
Normal file
7
multi-lsp-sample/client/.vscodeignore
Normal file
@ -0,0 +1,7 @@
|
||||
.vscode/**
|
||||
typings/**
|
||||
**/*.ts
|
||||
**/*.map
|
||||
.gitignore
|
||||
tsconfig.json
|
||||
vsc-extension-quickstart.md
|
||||
100
multi-lsp-sample/client/extension.ts
Normal file
100
multi-lsp-sample/client/extension.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import * as path from 'path';
|
||||
import {
|
||||
workspace as Workspace, window as Window, ExtensionContext, TextDocument, OutputChannel
|
||||
} from 'vscode';
|
||||
|
||||
import {
|
||||
LanguageClient, LanguageClientOptions, TransportKind, ProposedProtocol
|
||||
} from 'vscode-languageclient';
|
||||
|
||||
let defaultClient: LanguageClient;
|
||||
let clients: Map<string, LanguageClient> = new Map();
|
||||
|
||||
export function activate(_context: ExtensionContext) {
|
||||
|
||||
|
||||
let module = path.join(__dirname, '..', 'server', 'server.js');
|
||||
let outputChannel: OutputChannel = Window.createOutputChannel('Multi-LSP-Example');
|
||||
|
||||
function didOpenTextDocument(document: TextDocument): void {
|
||||
// We are only interested in language mode text
|
||||
if (document.languageId !== 'plaintext' || (document.uri.scheme !== 'file' && document.uri.scheme !== 'untitled')) {
|
||||
return;
|
||||
}
|
||||
|
||||
let uri = document.uri;
|
||||
// Untitled files go to a default client.
|
||||
if (uri.scheme === 'untitled' && !defaultClient) {
|
||||
let debugOptions = { execArgv: ["--nolazy", "--inspect=6010"] };
|
||||
let serverOptions = {
|
||||
run: { module, transport: TransportKind.ipc },
|
||||
debug: { module, transport: TransportKind.ipc, options: debugOptions}
|
||||
};
|
||||
let clientOptions: LanguageClientOptions = {
|
||||
documentSelector: [
|
||||
{ scheme: 'untitled', language: 'plaintext' }
|
||||
],
|
||||
synchronize: {
|
||||
configurationSection: 'multi-lsp'
|
||||
},
|
||||
diagnosticCollectionName: 'multi-lsp',
|
||||
outputChannel: outputChannel
|
||||
}
|
||||
defaultClient = new LanguageClient('multi-lsp', 'Multi-LSP', serverOptions, clientOptions);
|
||||
defaultClient.registerFeatures(ProposedProtocol(defaultClient));
|
||||
defaultClient.start();
|
||||
}
|
||||
let folder = Workspace.getWorkspaceFolder(uri);
|
||||
// Files outside a folder can't be handled. This might depend on the language
|
||||
// Single file languages like JSON might handle files outside the workspace folders.
|
||||
if (!folder) {
|
||||
return;
|
||||
}
|
||||
if (!clients.has(folder.uri.toString())) {
|
||||
let debugOptions = { execArgv: ["--nolazy", `--inspect=${6011 + clients.size}`] };
|
||||
let serverOptions = {
|
||||
run: { module, transport: TransportKind.ipc },
|
||||
debug: { module, transport: TransportKind.ipc, options: debugOptions}
|
||||
};
|
||||
let clientOptions: LanguageClientOptions = {
|
||||
documentSelector: [
|
||||
{ scheme: 'file', language: 'plaintext', pattern: `${folder.uri.fsPath}/**/*` }
|
||||
],
|
||||
synchronize: {
|
||||
configurationSection: 'multi-lsp'
|
||||
},
|
||||
diagnosticCollectionName: 'multi-lsp',
|
||||
workspaceFolder: folder,
|
||||
outputChannel: outputChannel
|
||||
}
|
||||
let client = new LanguageClient('multi-lsp', 'Multi-LSP', serverOptions, clientOptions);
|
||||
client.registerFeatures(ProposedProtocol(client));
|
||||
client.start();
|
||||
clients.set(folder.uri.toString(), client);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Workspace.onDidOpenTextDocument(didOpenTextDocument);
|
||||
Workspace.textDocuments.forEach(didOpenTextDocument);
|
||||
Workspace.onDidChangeWorkspaceFolders((event) => {
|
||||
for (let folder of event.removed) {
|
||||
let client = clients.get(folder.uri.toString());
|
||||
if (client) {
|
||||
clients.delete(folder.uri.toString());
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function deactivate(): Thenable<void> {
|
||||
let promises: Thenable<void>[] = [];
|
||||
if (defaultClient) {
|
||||
promises.push(defaultClient.stop());
|
||||
}
|
||||
for (let client of clients.values()) {
|
||||
promises.push(client.stop());
|
||||
}
|
||||
return Promise.all(promises).then(() => undefined);
|
||||
}
|
||||
46
multi-lsp-sample/client/package.json
Normal file
46
multi-lsp-sample/client/package.json
Normal file
@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "multi-lsp",
|
||||
"description": "Example showing multiple LSP servers running",
|
||||
"version": "0.0.1",
|
||||
"publisher": "vscode",
|
||||
"engines": {
|
||||
"vscode": "^1.15.0"
|
||||
},
|
||||
"activationEvents": [
|
||||
"onLanguage:plaintext"
|
||||
],
|
||||
"contributes": {
|
||||
"configuration": {
|
||||
"type": "object",
|
||||
"title": "Multi LSP configuration",
|
||||
"properties": {
|
||||
"multi-lsp.enable": {
|
||||
"scope": "resource",
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Controls the enablement."
|
||||
},
|
||||
"multi-lsp.options": {
|
||||
"scope": "resource",
|
||||
"type": "object",
|
||||
"default": {},
|
||||
"description": "Additional options."
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"main": "./out/extension",
|
||||
"scripts": {
|
||||
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
|
||||
"watch": "tsc -watch -p ./",
|
||||
"update-vscode": "node ./node_modules/vscode/bin/install"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vscode": "^1.1.5",
|
||||
"typescript": "^2.4.2",
|
||||
"@types/node": "^6.0.87"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-languageclient": "next"
|
||||
}
|
||||
}
|
||||
17
multi-lsp-sample/client/tsconfig.json
Normal file
17
multi-lsp-sample/client/tsconfig.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitReturns": true,
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"outDir": "out",
|
||||
"lib": [ "es6"],
|
||||
"sourceMap": true
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"server"
|
||||
]
|
||||
}
|
||||
18
multi-lsp-sample/server/.vscode/launch.json
vendored
Normal file
18
multi-lsp-sample/server/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
// List of configurations. Add new configurations or edit existing ones.
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Attach",
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
// TCP/IP address. Default is "localhost".
|
||||
"address": "localhost",
|
||||
// Port to attach to.
|
||||
"port": 6012,
|
||||
"protocol": "inspector",
|
||||
"sourceMaps": false,
|
||||
"outDir": "${workspaceRoot}/../testbed/server"
|
||||
}
|
||||
]
|
||||
}
|
||||
8
multi-lsp-sample/server/.vscode/settings.json
vendored
Normal file
8
multi-lsp-sample/server/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
// Place your settings in this file to overwrite default and user settings.
|
||||
{
|
||||
"javascript.validate.enable": false,
|
||||
"files.trimTrailingWhitespace": true,
|
||||
"editor.insertSpaces": false,
|
||||
"editor.tabSize": 4,
|
||||
"typescript.tsdk": "./node_modules/typescript/lib"
|
||||
}
|
||||
17
multi-lsp-sample/server/.vscode/tasks.json
vendored
Normal file
17
multi-lsp-sample/server/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "npm",
|
||||
"script": "watch",
|
||||
"isBackground": true,
|
||||
"problemMatcher": [
|
||||
"$tsc-watch"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
21
multi-lsp-sample/server/package.json
Normal file
21
multi-lsp-sample/server/package.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "multi-lsp-server",
|
||||
"version": "0.1.0",
|
||||
"description": "Multi server example",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"private": true,
|
||||
"main": "./lib/server.js",
|
||||
"dependencies": {
|
||||
"vscode-languageserver": "next"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^2.4.2",
|
||||
"@types/node": "^6.0.87"
|
||||
},
|
||||
"scripts": {
|
||||
"compile": "installServerIntoExtension ../client ./package.json ./src/tsconfig.json && tsc -p ./src",
|
||||
"watch": "installServerIntoExtension ../client ./package.json ./src/tsconfig.json && tsc --watch -p ./src"
|
||||
}
|
||||
}
|
||||
31
multi-lsp-sample/server/src/server.ts
Normal file
31
multi-lsp-sample/server/src/server.ts
Normal file
@ -0,0 +1,31 @@
|
||||
/*---------------------------------------------------------
|
||||
* Copyright (C) Microsoft Corporation. All rights reserved.
|
||||
*--------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import {
|
||||
createConnection, TextDocuments, ProposedProtocol, TextDocumentSyncKind
|
||||
} from 'vscode-languageserver';
|
||||
|
||||
let connection = createConnection(ProposedProtocol);
|
||||
let documents = new TextDocuments();
|
||||
let rootUri: string;
|
||||
|
||||
documents.onDidOpen((event) => {
|
||||
connection.console.log(`[Server ${rootUri}] Document opened: ${event.document.uri}`);
|
||||
})
|
||||
documents.listen(connection);
|
||||
|
||||
connection.onInitialize((params) => {
|
||||
rootUri = params.rootUri;
|
||||
connection.console.log(`Server started for folder: ${rootUri}`);
|
||||
return {
|
||||
capabilities: {
|
||||
textDocumentSync: {
|
||||
openClose: true,
|
||||
change: TextDocumentSyncKind.None
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
connection.listen();
|
||||
17
multi-lsp-sample/server/src/tsconfig.json
Normal file
17
multi-lsp-sample/server/src/tsconfig.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitReturns": true,
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"target": "es6",
|
||||
"lib": ["es6"],
|
||||
"sourceMap": true,
|
||||
"outDir": "../../client/server"
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user