aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/extension.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/extension.ts')
-rw-r--r--editors/code/src/extension.ts218
1 files changed, 0 insertions, 218 deletions
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
deleted file mode 100644
index 815f3692c..000000000
--- a/editors/code/src/extension.ts
+++ /dev/null
@@ -1,218 +0,0 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3
4import * as commands from './commands';
5import { CargoWatchProvider } from './commands/cargo_watch';
6import { ExpandMacroContentProvider } from './commands/expand_macro';
7import { HintsUpdater } from './commands/inlay_hints';
8import {
9 interactivelyStartCargoWatch,
10 startCargoWatch,
11} from './commands/runnables';
12import { SyntaxTreeContentProvider } from './commands/syntaxTree';
13import * as events from './events';
14import * as notifications from './notifications';
15import { Server } from './server';
16
17export async function activate(context: vscode.ExtensionContext) {
18 function disposeOnDeactivation(disposable: vscode.Disposable) {
19 context.subscriptions.push(disposable);
20 }
21
22 function registerCommand(name: string, f: any) {
23 disposeOnDeactivation(vscode.commands.registerCommand(name, f));
24 }
25 function overrideCommand(
26 name: string,
27 f: (...args: any[]) => Promise<boolean>,
28 ) {
29 const defaultCmd = `default:${name}`;
30 const original = (...args: any[]) =>
31 vscode.commands.executeCommand(defaultCmd, ...args);
32
33 try {
34 registerCommand(name, async (...args: any[]) => {
35 const editor = vscode.window.activeTextEditor;
36 if (
37 !editor ||
38 !editor.document ||
39 editor.document.languageId !== 'rust'
40 ) {
41 return await original(...args);
42 }
43 if (!(await f(...args))) {
44 return await original(...args);
45 }
46 });
47 } catch (_) {
48 vscode.window.showWarningMessage(
49 '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',
50 );
51 }
52 }
53
54 // Commands are requests from vscode to the language server
55 registerCommand(
56 'rust-analyzer.analyzerStatus',
57 commands.analyzerStatus.makeCommand(context),
58 );
59 registerCommand('rust-analyzer.collectGarbage', () =>
60 Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
61 );
62 registerCommand(
63 'rust-analyzer.matchingBrace',
64 commands.matchingBrace.handle,
65 );
66 registerCommand('rust-analyzer.joinLines', commands.joinLines.handle);
67 registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
68 registerCommand('rust-analyzer.run', commands.runnables.handle);
69 // Unlike the above this does not send requests to the language server
70 registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle);
71 registerCommand(
72 'rust-analyzer.applySourceChange',
73 commands.applySourceChange.handle,
74 );
75 registerCommand(
76 'rust-analyzer.showReferences',
77 (uri: string, position: lc.Position, locations: lc.Location[]) => {
78 vscode.commands.executeCommand(
79 'editor.action.showReferences',
80 vscode.Uri.parse(uri),
81 Server.client.protocol2CodeConverter.asPosition(position),
82 locations.map(Server.client.protocol2CodeConverter.asLocation),
83 );
84 },
85 );
86
87 if (Server.config.enableEnhancedTyping) {
88 overrideCommand('type', commands.onEnter.handle);
89 }
90
91 // Notifications are events triggered by the language server
92 const allNotifications: Iterable<[
93 string,
94 lc.GenericNotificationHandler,
95 ]> = [
96 [
97 'rust-analyzer/publishDecorations',
98 notifications.publishDecorations.handle,
99 ],
100 ];
101 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
102 const expandMacroContentProvider = new ExpandMacroContentProvider();
103
104 // The events below are plain old javascript events, triggered and handled by vscode
105 vscode.window.onDidChangeActiveTextEditor(
106 events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider),
107 );
108
109 disposeOnDeactivation(
110 vscode.workspace.registerTextDocumentContentProvider(
111 'rust-analyzer',
112 syntaxTreeContentProvider,
113 ),
114 );
115 disposeOnDeactivation(
116 vscode.workspace.registerTextDocumentContentProvider(
117 'rust-analyzer',
118 expandMacroContentProvider,
119 ),
120 );
121
122 registerCommand(
123 'rust-analyzer.syntaxTree',
124 commands.syntaxTree.createHandle(syntaxTreeContentProvider),
125 );
126 registerCommand(
127 'rust-analyzer.expandMacro',
128 commands.expandMacro.createHandle(expandMacroContentProvider),
129 );
130
131 vscode.workspace.onDidChangeTextDocument(
132 events.changeTextDocument.createHandler(syntaxTreeContentProvider),
133 null,
134 context.subscriptions,
135 );
136
137 const startServer = () => Server.start(allNotifications);
138 const reloadCommand = () => reloadServer(startServer);
139
140 vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
141
142 // Executing `cargo watch` provides us with inline diagnostics on save
143 let provider: CargoWatchProvider | undefined;
144 interactivelyStartCargoWatch(context).then(p => {
145 provider = p;
146 });
147 registerCommand('rust-analyzer.startCargoWatch', () => {
148 if (provider) {
149 provider.start();
150 } else {
151 startCargoWatch(context).then(p => {
152 provider = p;
153 });
154 }
155 });
156 registerCommand('rust-analyzer.stopCargoWatch', () => {
157 if (provider) {
158 provider.stop();
159 }
160 });
161
162 // Start the language server, finally!
163 try {
164 await startServer();
165 } catch (e) {
166 vscode.window.showErrorMessage(e.message);
167 }
168
169 if (Server.config.displayInlayHints) {
170 const hintsUpdater = new HintsUpdater();
171 hintsUpdater.refreshHintsForVisibleEditors().then(() => {
172 // vscode may ignore top level hintsUpdater.refreshHintsForVisibleEditors()
173 // so update the hints once when the focus changes to guarantee their presence
174 let editorChangeDisposable: vscode.Disposable | null = null;
175 editorChangeDisposable = vscode.window.onDidChangeActiveTextEditor(
176 _ => {
177 if (editorChangeDisposable !== null) {
178 editorChangeDisposable.dispose();
179 }
180 return hintsUpdater.refreshHintsForVisibleEditors();
181 },
182 );
183
184 disposeOnDeactivation(
185 vscode.window.onDidChangeVisibleTextEditors(_ =>
186 hintsUpdater.refreshHintsForVisibleEditors(),
187 ),
188 );
189 disposeOnDeactivation(
190 vscode.workspace.onDidChangeTextDocument(e =>
191 hintsUpdater.refreshHintsForVisibleEditors(e),
192 ),
193 );
194 disposeOnDeactivation(
195 vscode.workspace.onDidChangeConfiguration(_ =>
196 hintsUpdater.toggleHintsDisplay(
197 Server.config.displayInlayHints,
198 ),
199 ),
200 );
201 });
202 }
203}
204
205export function deactivate(): Thenable<void> {
206 if (!Server.client) {
207 return Promise.resolve();
208 }
209 return Server.client.stop();
210}
211
212async function reloadServer(startServer: () => Promise<void>) {
213 if (Server.client != null) {
214 vscode.window.showInformationMessage('Reloading rust-analyzer...');
215 await Server.client.stop();
216 await startServer();
217 }
218}