1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3 import { Server } from './server';
4 import { Config } from './config';
7 private extCtx: vscode.ExtensionContext;
9 constructor(extCtx: vscode.ExtensionContext) {
13 get client(): lc.LanguageClient {
17 get config(): Config {
21 get activeRustEditor(): vscode.TextEditor | undefined {
22 const editor = vscode.window.activeTextEditor;
23 return editor && editor.document.languageId === 'rust'
28 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
29 const fullName = `rust-analyzer.${name}`;
30 const cmd = factory(this);
31 const d = vscode.commands.registerCommand(fullName, cmd);
35 overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) {
36 const defaultCmd = `default:${name}`;
37 const override = factory(this);
38 const original = (...args: any[]) =>
39 vscode.commands.executeCommand(defaultCmd, ...args);
41 const d = vscode.commands.registerCommand(
43 async (...args: any[]) => {
44 if (!(await override(...args))) {
45 return await original(...args);
51 vscode.window.showWarningMessage(
52 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
57 get subscriptions(): { dispose(): any }[] {
58 return this.extCtx.subscriptions;
61 pushCleanup(d: { dispose(): any }) {
62 this.extCtx.subscriptions.push(d);
65 async sendRequestWithRetry<R>(
68 token?: vscode.CancellationToken,
70 await this.client.onReady();
71 for (const delay of [2, 4, 6, 8, 10, null]) {
73 return await this.client.sendRequest(method, param, token);
76 e.code === lc.ErrorCodes.ContentModified &&
79 await sleep(10 * (1 << delay));
89 export type Cmd = (...args: any[]) => any;
91 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));