Skip to content

Commit

Permalink
feat: add rust-analyzer.install command
Browse files Browse the repository at this point in the history
to install rust-analyzer from GitHub release
  • Loading branch information
fannheyward committed May 20, 2024
1 parent 78a4e91 commit 6158b88
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 28 additions & 1 deletion src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 7 in src/ctx.ts

View workflow job for this annotation

GitHub Actions / build (16)

'ReleaseTag' is defined but never used
import * as ra from './lsp_ext';

export type RustDocument = TextDocument & { languageId: 'rust' };
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
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);
Expand Down

0 comments on commit 6158b88

Please sign in to comment.