diff --git a/README.md b/README.md index 3a1cbd63..3809508a 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,7 @@ You can use these commands by `:CocCommand XYZ`. | rust-analyzer.ssr | Structural Search Replace | | rust-analyzer.syntaxTree | Show syntax tree | | rust-analyzer.testCurrent | Test Current | +| rust-analyzer.install | Install latest `rust-analyzer` from [GitHub release](https://github.com/rust-lang/rust-analyzer/releases) | | rust-analyzer.upgrade | Download latest `rust-analyzer` from [GitHub release](https://github.com/rust-lang/rust-analyzer/releases) | | rust-analyzer.viewHir | View Hir | | rust-analyzer.viewMir | View Mir | diff --git a/package.json b/package.json index 8df6bf45..62839498 100644 --- a/package.json +++ b/package.json @@ -1438,6 +1438,11 @@ "title": "Test Current", "category": "rust-analyzer" }, + { + "command": "rust-analyzer.install", + "title": "Install Rust Analyzer from GitHub release", + "category": "rust-analyzer" + }, { "command": "rust-analyzer.upgrade", "title": "Upgrade Rust Analyzer from GitHub release", diff --git a/src/commands.ts b/src/commands.ts index 539c2916..daf2cd95 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -564,6 +564,12 @@ export function showReferences(): Cmd { }; } +export function install(ctx: Ctx): Cmd { + return async () => { + await ctx.installServerFromGitHub(); + }; +} + export function upgrade(ctx: Ctx) { return async () => { await ctx.checkUpdate(false); diff --git a/src/ctx.ts b/src/ctx.ts index 3aeda6d0..8ee91b13 100644 --- a/src/ctx.ts +++ b/src/ctx.ts @@ -4,7 +4,7 @@ import { join } from 'path'; import which from 'which'; import { createClient } from './client'; import { Config } from './config'; -import { downloadServer, getLatestRelease } from './downloader'; +import { downloadServer, getLatestRelease, ReleaseTag } from './downloader'; import * as ra from './lsp_ext'; export type RustDocument = TextDocument & { languageId: 'rust' }; @@ -104,6 +104,33 @@ export class Ctx { return; } + async installServerFromGitHub() { + const latest = await getLatestRelease(this.config.channel); + if (!latest) { + return; + } + try { + if (process.platform === 'win32') { + await this.client.stop(); + } + + await downloadServer(this.extCtx, latest); + } catch (e) { + console.error(e); + let msg = 'Install rust-analyzer failed, please try again'; + // @ts-ignore + if (e.code === 'EBUSY' || e.code === 'ETXTBSY' || e.code === 'EPERM') { + msg = 'Install rust-analyzer failed, other Vim instances might be using it, you should close them and try again'; + } + window.showInformationMessage(msg, 'error'); + return; + } + await this.client.stop(); + this.client.start(); + + this.extCtx.globalState.update('release', latest.tag); + } + async checkUpdate(auto = true) { if (this.config.serverPath || this.usingSystemServer) { // no update checking if using custom or system server diff --git a/src/index.ts b/src/index.ts index 8d2f375f..dc96c46c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,6 +50,7 @@ export async function activate(context: ExtensionContext): Promise { ctx.registerCommand('ssr', cmds.ssr); ctx.registerCommand('debug', cmds.debug); ctx.registerCommand('reload', cmds.reload); + ctx.registerCommand('install', cmds.install); ctx.registerCommand('upgrade', cmds.upgrade); ctx.registerCommand('viewHir', cmds.viewHir); ctx.registerCommand('viewMir', cmds.viewMir);