From 9a66f867f93729b4b753abc4117fb65f3cba72a3 Mon Sep 17 00:00:00 2001 From: axetroy Date: Tue, 4 Feb 2020 23:56:01 +0800 Subject: [PATCH] feat: add `deno.restart_server` command to restart `Deno Language Server`. close #28 --- client/src/extension.ts | 153 ++++++++++++++++++++++------------------ package.json | 22 +++--- package.nls.json | 1 + package.nls.zh-cn.json | 1 + 4 files changed, 98 insertions(+), 79 deletions(-) diff --git a/client/src/extension.ts b/client/src/extension.ts index 06108c8..6323e81 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -180,81 +180,96 @@ class Extension { } // start Deno Language Server private async StartDenoLanguageServer() { - // create server connection - const port = await getport({ port: 9523 }); - - // The server is implemented in node - const serverModule = this.context.asAbsolutePath( - path.join("server", "out", "server.js") - ); + if (this.client) { + await this.client.stop(); + this.client = null; + } + const statusbar = window.createStatusBarItem(StatusBarAlignment.Left, -100); + statusbar.text = "$(loading) Initializing Deno Language Server"; + statusbar.show(); - // If the extension is launched in debug mode then the debug server options are used - // Otherwise the run options are used - const serverOptions: ServerOptions = { - run: { - module: serverModule, - transport: TransportKind.ipc, - options: { cwd: process.cwd() } - }, - debug: { - module: serverModule, - transport: TransportKind.ipc, - options: { - cwd: process.cwd(), - execArgv: ["--nolazy", `--inspect=${port}`] - } - } - }; - - // Options to control the language client - const clientOptions: LanguageClientOptions = { - documentSelector: [ - { scheme: "file", language: "javascript" }, - { scheme: "file", language: "javascriptreact" }, - { scheme: "file", language: "typescript" }, - { scheme: "file", language: "typescriptreact" }, - { scheme: "file", language: "markdown" }, - { scheme: "file", language: "json" } - ], - diagnosticCollectionName: this.configurationSection, - synchronize: { - configurationSection: this.configurationSection - }, - progressOnInitialization: true - }; - - // Create the language client and start the client. - const client = (this.client = new LanguageClient( - "Deno Language Server", - "Deno Language Server", - serverOptions, - clientOptions - )); - - client.onReady().then(() => { - console.log("Deno Language Server is ready!"); - client.onNotification("init", (info: DenoInfo) => { - this.denoInfo = { ...this.denoInfo, ...info }; - this.updateStatusBarVisibility(window.activeTextEditor); - }); - client.onNotification("error", window.showErrorMessage); + try { + // create server connection + const port = await getport({ port: 9523 }); - client.onRequest("getWorkspaceFolder", async (uri: string) => - workspace.getWorkspaceFolder(Uri.parse(uri)) + // The server is implemented in node + const serverModule = this.context.asAbsolutePath( + path.join("server", "out", "server.js") ); - client.onRequest("getWorkspaceConfig", async (uri: string) => { - const workspaceFolder = workspace.getWorkspaceFolder(Uri.parse(uri)); + // If the extension is launched in debug mode then the debug server options are used + // Otherwise the run options are used + const serverOptions: ServerOptions = { + run: { + module: serverModule, + transport: TransportKind.ipc, + options: { cwd: process.cwd() } + }, + debug: { + module: serverModule, + transport: TransportKind.ipc, + options: { + cwd: process.cwd(), + execArgv: ["--nolazy", `--inspect=${port}`] + } + } + }; + + // Options to control the language client + const clientOptions: LanguageClientOptions = { + documentSelector: [ + { scheme: "file", language: "javascript" }, + { scheme: "file", language: "javascriptreact" }, + { scheme: "file", language: "typescript" }, + { scheme: "file", language: "typescriptreact" }, + { scheme: "file", language: "markdown" }, + { scheme: "file", language: "json" } + ], + diagnosticCollectionName: this.configurationSection, + synchronize: { + configurationSection: this.configurationSection + }, + progressOnInitialization: true + }; - const config = this.getConfiguration( - workspaceFolder?.uri || Uri.parse(uri) + // Create the language client and start the client. + const client = (this.client = new LanguageClient( + "Deno Language Server", + "Deno Language Server", + serverOptions, + clientOptions + )); + + this.context.subscriptions.push(client.start()); + + await client.onReady().then(() => { + console.log("Deno Language Server is ready!"); + client.onNotification("init", (info: DenoInfo) => { + this.denoInfo = { ...this.denoInfo, ...info }; + this.updateStatusBarVisibility(window.activeTextEditor); + }); + client.onNotification("error", window.showErrorMessage); + + client.onRequest("getWorkspaceFolder", async (uri: string) => + workspace.getWorkspaceFolder(Uri.parse(uri)) ); - return config; - }); - }); + client.onRequest("getWorkspaceConfig", async (uri: string) => { + const workspaceFolder = workspace.getWorkspaceFolder(Uri.parse(uri)); - this.context.subscriptions.push(client.start()); + const config = this.getConfiguration( + workspaceFolder?.uri || Uri.parse(uri) + ); + + return config; + }); + }); + } catch (err) { + throw err; + } finally { + statusbar.hide(); + statusbar.dispose(); + } } // update status bar visibility private updateStatusBarVisibility( @@ -397,6 +412,10 @@ Executable ${this.denoInfo.executablePath} this.registerCommand("enable", this.enable.bind(this)); this.registerCommand("disable", this.disable.bind(this)); + this.registerCommand( + "restart_server", + this.StartDenoLanguageServer.bind(this) + ); this.watchConfiguration(() => { const uri = window.activeTextEditor?.document.uri; if (uri) { diff --git a/package.json b/package.json index a8b22a9..6b2c0a2 100644 --- a/package.json +++ b/package.json @@ -19,18 +19,14 @@ "engines": { "vscode": "^1.41.0" }, - "categories": [ - "Programming Languages", - "Other" - ], - "keywords": [ - "deno" - ], + "categories": ["Programming Languages", "Other"], + "keywords": ["deno"], "activationEvents": [ "onLanguage:typescript", "onLanguage:typescriptreact", "onCommand:deno.enable", - "onCommand:deno.disable" + "onCommand:deno.disable", + "onCommand:deno.restart_server" ], "main": "./client/out/extension.js", "contributes": { @@ -50,6 +46,11 @@ "command": "deno.disable", "title": "%deno.command.disable%", "category": "deno" + }, + { + "command": "deno.restart_server", + "title": "%deno.command.restart_server%", + "category": "deno" } ], "configuration": { @@ -89,10 +90,7 @@ } }, "lint-staged": { - "**/*.{js,ts,json,md}": [ - "yarn format", - "git add" - ] + "**/*.{js,ts,json,md}": ["yarn format", "git add"] }, "devDependencies": { "@types/node": "12.12.26", diff --git a/package.nls.json b/package.nls.json index 04c61dd..db6e6ff 100644 --- a/package.nls.json +++ b/package.nls.json @@ -3,6 +3,7 @@ "deno.description": "Deno support for VSCode", "deno.command.enable": "Enable Deno", "deno.command.disable": "Disable Deno", + "deno.command.restart_server": "Restart Deno Language Server", "deno.config.enabled": "Controls whether deno is enabled or not.", "deno.config.dtsFilepaths": "The file paths of the TypeScript declaration file(.d.ts). It can be a relative which path relative to the project directory or an absolute path." } diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 95210d1..04842d4 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -3,6 +3,7 @@ "deno.description": "Deno support for VSCode", "deno.command.enable": "启用 Deno", "deno.command.disable": "禁用 Deno", + "deno.command.restart_server": "重启 Deno 服务器", "deno.config.enabled": "是否启用 Deno。", "deno.config.dtsFilepaths": "Typescript 的声明文件(.d.ts)路径。它可以是相对于项目目录的相对路径或者是绝对路径" }