diff options
author | Kirill Bulatov <[email protected]> | 2020-11-15 22:19:04 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2021-05-23 20:46:20 +0100 |
commit | d9a5490646f68efdb70f84713d3a418a2b2a0b00 (patch) | |
tree | 2cad71ad7056d07c3e06c6566bd5b74a6cae5054 /editors/code | |
parent | 16054887102104208f4a0fc0e75e702b85a2eae8 (diff) |
Start rust-analyzer server for arbitrary rust files
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/client.ts | 2 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 2 | ||||
-rw-r--r-- | editors/code/src/main.ts | 81 |
3 files changed, 46 insertions, 39 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 116f41df6..131a2f19a 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts | |||
@@ -23,7 +23,7 @@ function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownStri | |||
23 | return result; | 23 | return result; |
24 | } | 24 | } |
25 | 25 | ||
26 | export function createClient(serverPath: string, cwd: string, extraEnv: Env): lc.LanguageClient { | 26 | export function createClient(serverPath: string, cwd: string | undefined, extraEnv: Env): lc.LanguageClient { |
27 | // '.' Is the fallback if no folder is open | 27 | // '.' Is the fallback if no folder is open |
28 | // TODO?: Workspace folders support Uri's (eg: file://test.txt). | 28 | // TODO?: Workspace folders support Uri's (eg: file://test.txt). |
29 | // It might be a good idea to test if the uri points to a file. | 29 | // It might be a good idea to test if the uri points to a file. |
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index bd023f803..9d8620823 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -22,7 +22,7 @@ export class Ctx { | |||
22 | config: Config, | 22 | config: Config, |
23 | extCtx: vscode.ExtensionContext, | 23 | extCtx: vscode.ExtensionContext, |
24 | serverPath: string, | 24 | serverPath: string, |
25 | cwd: string, | 25 | cwd?: string, |
26 | ): Promise<Ctx> { | 26 | ): Promise<Ctx> { |
27 | const client = createClient(serverPath, cwd, config.serverExtraEnv); | 27 | const client = createClient(serverPath, cwd, config.serverExtraEnv); |
28 | 28 | ||
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index aaedc2431..f0f47a75b 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -7,7 +7,7 @@ import * as commands from './commands'; | |||
7 | import { activateInlayHints } from './inlay_hints'; | 7 | import { activateInlayHints } from './inlay_hints'; |
8 | import { Ctx } from './ctx'; | 8 | import { Ctx } from './ctx'; |
9 | import { Config } from './config'; | 9 | import { Config } from './config'; |
10 | import { log, assert, isValidExecutable } from './util'; | 10 | import { log, assert, isValidExecutable, isRustDocument } from './util'; |
11 | import { PersistentState } from './persistent_state'; | 11 | import { PersistentState } from './persistent_state'; |
12 | import { fetchRelease, download } from './net'; | 12 | import { fetchRelease, download } from './net'; |
13 | import { activateTaskProvider } from './tasks'; | 13 | import { activateTaskProvider } from './tasks'; |
@@ -28,26 +28,6 @@ export async function activate(context: vscode.ExtensionContext) { | |||
28 | } | 28 | } |
29 | 29 | ||
30 | async function tryActivate(context: vscode.ExtensionContext) { | 30 | async function tryActivate(context: vscode.ExtensionContext) { |
31 | // Register a "dumb" onEnter command for the case where server fails to | ||
32 | // start. | ||
33 | // | ||
34 | // FIXME: refactor command registration code such that commands are | ||
35 | // **always** registered, even if the server does not start. Use API like | ||
36 | // this perhaps? | ||
37 | // | ||
38 | // ```TypeScript | ||
39 | // registerCommand( | ||
40 | // factory: (Ctx) => ((Ctx) => any), | ||
41 | // fallback: () => any = () => vscode.window.showErrorMessage( | ||
42 | // "rust-analyzer is not available" | ||
43 | // ), | ||
44 | // ) | ||
45 | const defaultOnEnter = vscode.commands.registerCommand( | ||
46 | 'rust-analyzer.onEnter', | ||
47 | () => vscode.commands.executeCommand('default:type', { text: '\n' }), | ||
48 | ); | ||
49 | context.subscriptions.push(defaultOnEnter); | ||
50 | |||
51 | const config = new Config(context); | 31 | const config = new Config(context); |
52 | const state = new PersistentState(context.globalState); | 32 | const state = new PersistentState(context.globalState); |
53 | const serverPath = await bootstrap(config, state).catch(err => { | 33 | const serverPath = await bootstrap(config, state).catch(err => { |
@@ -67,14 +47,52 @@ async function tryActivate(context: vscode.ExtensionContext) { | |||
67 | 47 | ||
68 | const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; | 48 | const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; |
69 | if (workspaceFolder === undefined) { | 49 | if (workspaceFolder === undefined) { |
70 | throw new Error("no folder is opened"); | 50 | let rustDocuments = vscode.workspace.textDocuments.filter(document => isRustDocument(document)); |
51 | if (rustDocuments.length > 0) { | ||
52 | ctx = await Ctx.create(config, context, serverPath); | ||
53 | } else { | ||
54 | throw new Error("no rust files are opened"); | ||
55 | } | ||
56 | } else { | ||
57 | // Note: we try to start the server before we activate type hints so that it | ||
58 | // registers its `onDidChangeDocument` handler before us. | ||
59 | // | ||
60 | // This a horribly, horribly wrong way to deal with this problem. | ||
61 | ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath); | ||
62 | ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config)); | ||
71 | } | 63 | } |
64 | await initCommonContext(context, ctx); | ||
65 | |||
66 | activateInlayHints(ctx); | ||
67 | warnAboutExtensionConflicts(); | ||
68 | |||
69 | vscode.workspace.onDidChangeConfiguration( | ||
70 | _ => ctx?.client?.sendNotification('workspace/didChangeConfiguration', { settings: "" }), | ||
71 | null, | ||
72 | ctx.subscriptions, | ||
73 | ); | ||
74 | } | ||
72 | 75 | ||
73 | // Note: we try to start the server before we activate type hints so that it | 76 | async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) { |
74 | // registers its `onDidChangeDocument` handler before us. | 77 | // Register a "dumb" onEnter command for the case where server fails to |
78 | // start. | ||
79 | // | ||
80 | // FIXME: refactor command registration code such that commands are | ||
81 | // **always** registered, even if the server does not start. Use API like | ||
82 | // this perhaps? | ||
75 | // | 83 | // |
76 | // This a horribly, horribly wrong way to deal with this problem. | 84 | // ```TypeScript |
77 | ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath); | 85 | // registerCommand( |
86 | // factory: (Ctx) => ((Ctx) => any), | ||
87 | // fallback: () => any = () => vscode.window.showErrorMessage( | ||
88 | // "rust-analyzer is not available" | ||
89 | // ), | ||
90 | // ) | ||
91 | const defaultOnEnter = vscode.commands.registerCommand( | ||
92 | 'rust-analyzer.onEnter', | ||
93 | () => vscode.commands.executeCommand('default:type', { text: '\n' }), | ||
94 | ); | ||
95 | context.subscriptions.push(defaultOnEnter); | ||
78 | 96 | ||
79 | await setContextValue(RUST_PROJECT_CONTEXT_NAME, true); | 97 | await setContextValue(RUST_PROJECT_CONTEXT_NAME, true); |
80 | 98 | ||
@@ -134,17 +152,6 @@ async function tryActivate(context: vscode.ExtensionContext) { | |||
134 | ctx.registerCommand('resolveCodeAction', commands.resolveCodeAction); | 152 | ctx.registerCommand('resolveCodeAction', commands.resolveCodeAction); |
135 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); | 153 | ctx.registerCommand('applyActionGroup', commands.applyActionGroup); |
136 | ctx.registerCommand('gotoLocation', commands.gotoLocation); | 154 | ctx.registerCommand('gotoLocation', commands.gotoLocation); |
137 | |||
138 | ctx.pushCleanup(activateTaskProvider(workspaceFolder, ctx.config)); | ||
139 | |||
140 | activateInlayHints(ctx); | ||
141 | warnAboutExtensionConflicts(); | ||
142 | |||
143 | vscode.workspace.onDidChangeConfiguration( | ||
144 | _ => ctx?.client?.sendNotification('workspace/didChangeConfiguration', { settings: "" }), | ||
145 | null, | ||
146 | ctx.subscriptions, | ||
147 | ); | ||
148 | } | 155 | } |
149 | 156 | ||
150 | export async function deactivate() { | 157 | export async function deactivate() { |