aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/main.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/main.ts')
-rw-r--r--editors/code/src/main.ts81
1 files changed, 44 insertions, 37 deletions
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index aaedc2431..b735186fe 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -7,7 +7,7 @@ import * as commands from './commands';
7import { activateInlayHints } from './inlay_hints'; 7import { activateInlayHints } from './inlay_hints';
8import { Ctx } from './ctx'; 8import { Ctx } from './ctx';
9import { Config } from './config'; 9import { Config } from './config';
10import { log, assert, isValidExecutable } from './util'; 10import { log, assert, isValidExecutable, isRustDocument } from './util';
11import { PersistentState } from './persistent_state'; 11import { PersistentState } from './persistent_state';
12import { fetchRelease, download } from './net'; 12import { fetchRelease, download } from './net';
13import { activateTaskProvider } from './tasks'; 13import { activateTaskProvider } from './tasks';
@@ -28,26 +28,6 @@ export async function activate(context: vscode.ExtensionContext) {
28} 28}
29 29
30async function tryActivate(context: vscode.ExtensionContext) { 30async 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 const rustDocuments = vscode.workspace.textDocuments.filter(document => isRustDocument(document));
51 if (rustDocuments.length > 0) {
52 ctx = await Ctx.create(config, context, serverPath, { kind: 'Detached Files', files: rustDocuments });
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, { kind: "Workspace Folder", folder: workspaceFolder.uri });
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 76async 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
150export async function deactivate() { 157export async function deactivate() {