aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package-lock.json6
-rw-r--r--editors/code/package.json19
-rw-r--r--editors/code/src/commands/analyzer_status.ts19
-rw-r--r--editors/code/src/commands/index.ts11
-rw-r--r--editors/code/src/commands/matching_brace.ts53
-rw-r--r--editors/code/src/ctx.ts35
-rw-r--r--editors/code/src/main.ts19
7 files changed, 110 insertions, 52 deletions
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index 982238fa4..806ebdb81 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -534,6 +534,12 @@
534 "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=", 534 "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=",
535 "dev": true 535 "dev": true
536 }, 536 },
537 "prettier": {
538 "version": "1.19.1",
539 "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz",
540 "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==",
541 "dev": true
542 },
537 "read": { 543 "read": {
538 "version": "1.0.7", 544 "version": "1.0.7",
539 "resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz", 545 "resolved": "https://registry.npmjs.org/read/-/read-1.0.7.tgz",
diff --git a/editors/code/package.json b/editors/code/package.json
index d14a36f8c..6662747f6 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -18,7 +18,8 @@
18 "scripts": { 18 "scripts": {
19 "vscode:prepublish": "rollup -c", 19 "vscode:prepublish": "rollup -c",
20 "package": "vsce package", 20 "package": "vsce package",
21 "watch": "tsc -watch -p ./" 21 "watch": "tsc -watch -p ./",
22 "prettier": "prettier --write **/*.ts"
22 }, 23 },
23 "dependencies": { 24 "dependencies": {
24 "jsonc-parser": "^2.1.0", 25 "jsonc-parser": "^2.1.0",
@@ -26,16 +27,22 @@
26 "vscode-languageclient": "^6.0.0-next.9" 27 "vscode-languageclient": "^6.0.0-next.9"
27 }, 28 },
28 "devDependencies": { 29 "devDependencies": {
29 "rollup": "^1.27.14",
30 "@rollup/plugin-commonjs": "^11.0.0", 30 "@rollup/plugin-commonjs": "^11.0.0",
31 "@rollup/plugin-node-resolve": "^6.0.0", 31 "@rollup/plugin-node-resolve": "^6.0.0",
32 "@rollup/plugin-typescript": "^2.0.1", 32 "@rollup/plugin-typescript": "^2.0.1",
33 "typescript": "^3.7.3",
34 "tslib": "^1.10.0",
35 "vsce": "^1.71.0",
36 "@types/node": "^12.12.21", 33 "@types/node": "^12.12.21",
37 "@types/seedrandom": "^2.4.28", 34 "@types/seedrandom": "^2.4.28",
38 "@types/vscode": "^1.41.0" 35 "@types/vscode": "^1.41.0",
36 "prettier": "^1.19.1",
37 "rollup": "^1.27.14",
38 "tslib": "^1.10.0",
39 "typescript": "^3.7.3",
40 "vsce": "^1.71.0"
41 },
42 "prettier": {
43 "singleQuote": true,
44 "tabWidth": 4,
45 "trailingComma": "all"
39 }, 46 },
40 "activationEvents": [ 47 "activationEvents": [
41 "onLanguage:rust", 48 "onLanguage:rust",
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index 5840e8fc0..c9d32fe07 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -1,19 +1,19 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import { Server } from '../server'; 2import { Ctx, Cmd } from '../ctx';
3// Shows status of rust-analyzer (for debugging) 3// Shows status of rust-analyzer (for debugging)
4 4
5export function makeCommand(context: vscode.ExtensionContext) { 5export function analyzerStatus(ctx: Ctx): Cmd {
6 let poller: NodeJS.Timer | null = null; 6 let poller: NodeJS.Timer | null = null;
7 const tdcp = new TextDocumentContentProvider(); 7 const tdcp = new TextDocumentContentProvider(ctx);
8 8
9 context.subscriptions.push( 9 ctx.pushCleanup(
10 vscode.workspace.registerTextDocumentContentProvider( 10 vscode.workspace.registerTextDocumentContentProvider(
11 'rust-analyzer-status', 11 'rust-analyzer-status',
12 tdcp, 12 tdcp,
13 ), 13 ),
14 ); 14 );
15 15
16 context.subscriptions.push({ 16 ctx.pushCleanup({
17 dispose() { 17 dispose() {
18 if (poller != null) { 18 if (poller != null) {
19 clearInterval(poller); 19 clearInterval(poller);
@@ -39,9 +39,16 @@ export function makeCommand(context: vscode.ExtensionContext) {
39 39
40class TextDocumentContentProvider 40class TextDocumentContentProvider
41 implements vscode.TextDocumentContentProvider { 41 implements vscode.TextDocumentContentProvider {
42
42 uri = vscode.Uri.parse('rust-analyzer-status://status'); 43 uri = vscode.Uri.parse('rust-analyzer-status://status');
43 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 44 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
44 45
46 ctx: Ctx
47
48 constructor(ctx: Ctx) {
49 this.ctx = ctx
50 }
51
45 provideTextDocumentContent( 52 provideTextDocumentContent(
46 _uri: vscode.Uri, 53 _uri: vscode.Uri,
47 ): vscode.ProviderResult<string> { 54 ): vscode.ProviderResult<string> {
@@ -49,7 +56,7 @@ class TextDocumentContentProvider
49 if (editor == null) { 56 if (editor == null) {
50 return ''; 57 return '';
51 } 58 }
52 return Server.client.sendRequest<string>( 59 return this.ctx.client.sendRequest<string>(
53 'rust-analyzer/analyzerStatus', 60 'rust-analyzer/analyzerStatus',
54 null, 61 null,
55 ); 62 );
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 13a696758..9d9b9c575 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -1,14 +1,20 @@
1import * as analyzerStatus from './analyzer_status'; 1import { Ctx, Cmd } from '../ctx'
2
3import { analyzerStatus } from './analyzer_status';
4import { matchingBrace } from './matching_brace';
2import * as applySourceChange from './apply_source_change'; 5import * as applySourceChange from './apply_source_change';
3import * as expandMacro from './expand_macro'; 6import * as expandMacro from './expand_macro';
4import * as inlayHints from './inlay_hints'; 7import * as inlayHints from './inlay_hints';
5import * as joinLines from './join_lines'; 8import * as joinLines from './join_lines';
6import * as matchingBrace from './matching_brace';
7import * as onEnter from './on_enter'; 9import * as onEnter from './on_enter';
8import * as parentModule from './parent_module'; 10import * as parentModule from './parent_module';
9import * as runnables from './runnables'; 11import * as runnables from './runnables';
10import * as syntaxTree from './syntaxTree'; 12import * as syntaxTree from './syntaxTree';
11 13
14function collectGarbage(ctx: Ctx): Cmd {
15 return async () => { ctx.client.sendRequest<null>('rust-analyzer/collectGarbage', null) }
16}
17
12export { 18export {
13 analyzerStatus, 19 analyzerStatus,
14 applySourceChange, 20 applySourceChange,
@@ -20,4 +26,5 @@ export {
20 syntaxTree, 26 syntaxTree,
21 onEnter, 27 onEnter,
22 inlayHints, 28 inlayHints,
29 collectGarbage
23}; 30};
diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts
index 364208cc7..665b0c33c 100644
--- a/editors/code/src/commands/matching_brace.ts
+++ b/editors/code/src/commands/matching_brace.ts
@@ -1,34 +1,33 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2
3import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; 2import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
4import { Server } from '../server'; 3import { Ctx, Cmd } from '../ctx';
4
5export function matchingBrace(ctx: Ctx): Cmd {
6 return async () => {
7 const editor = ctx.activeRustEditor;
8 if (!editor) {
9 return;
10 }
11 const request: FindMatchingBraceParams = {
12 textDocument: { uri: editor.document.uri.toString() },
13 offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)),
14 };
15 const response = await ctx.client.sendRequest<Position[]>(
16 'rust-analyzer/findMatchingBrace',
17 request,
18 );
19 editor.selections = editor.selections.map((sel, idx) => {
20 const active = ctx.client.protocol2CodeConverter.asPosition(
21 response[idx],
22 );
23 const anchor = sel.isEmpty ? active : sel.anchor;
24 return new vscode.Selection(anchor, active);
25 });
26 editor.revealRange(editor.selection);
27 }
28}
5 29
6interface FindMatchingBraceParams { 30interface FindMatchingBraceParams {
7 textDocument: TextDocumentIdentifier; 31 textDocument: TextDocumentIdentifier;
8 offsets: Position[]; 32 offsets: Position[];
9} 33}
10
11export async function handle() {
12 const editor = vscode.window.activeTextEditor;
13 if (editor == null || editor.document.languageId !== 'rust') {
14 return;
15 }
16 const request: FindMatchingBraceParams = {
17 textDocument: { uri: editor.document.uri.toString() },
18 offsets: editor.selections.map(s => {
19 return Server.client.code2ProtocolConverter.asPosition(s.active);
20 }),
21 };
22 const response = await Server.client.sendRequest<Position[]>(
23 'rust-analyzer/findMatchingBrace',
24 request,
25 );
26 editor.selections = editor.selections.map((sel, idx) => {
27 const active = Server.client.protocol2CodeConverter.asPosition(
28 response[idx],
29 );
30 const anchor = sel.isEmpty ? active : sel.anchor;
31 return new vscode.Selection(anchor, active);
32 });
33 editor.revealRange(editor.selection);
34}
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
new file mode 100644
index 000000000..712337fe7
--- /dev/null
+++ b/editors/code/src/ctx.ts
@@ -0,0 +1,35 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3import { Server } from './server';
4
5export class Ctx {
6 private extCtx: vscode.ExtensionContext;
7
8 constructor(extCtx: vscode.ExtensionContext) {
9 this.extCtx = extCtx;
10 }
11
12 get client(): lc.LanguageClient {
13 return Server.client;
14 }
15
16 get activeRustEditor(): vscode.TextEditor | undefined {
17 const editor = vscode.window.activeTextEditor;
18 return editor && editor.document.languageId === 'rust'
19 ? editor
20 : undefined;
21 }
22
23 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
24 const fullName = `rust-analyzer.${name}`;
25 const cmd = factory(this);
26 const d = vscode.commands.registerCommand(fullName, cmd);
27 this.pushCleanup(d);
28 }
29
30 pushCleanup(d: { dispose(): any }) {
31 this.extCtx.subscriptions.push(d);
32 }
33}
34
35export type Cmd = (...args: any[]) => any;
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 1da10ebd0..a4149a059 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -9,8 +9,16 @@ import { StatusDisplay } from './commands/watch_status';
9import * as events from './events'; 9import * as events from './events';
10import * as notifications from './notifications'; 10import * as notifications from './notifications';
11import { Server } from './server'; 11import { Server } from './server';
12import { Ctx } from './ctx';
13
14let ctx!: Ctx;
12 15
13export async function activate(context: vscode.ExtensionContext) { 16export async function activate(context: vscode.ExtensionContext) {
17 ctx = new Ctx(context);
18 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
19 ctx.registerCommand('collectGarbage', commands.collectGarbage);
20 ctx.registerCommand('matchingBrace', commands.matchingBrace);
21
14 function disposeOnDeactivation(disposable: vscode.Disposable) { 22 function disposeOnDeactivation(disposable: vscode.Disposable) {
15 context.subscriptions.push(disposable); 23 context.subscriptions.push(disposable);
16 } 24 }
@@ -48,17 +56,6 @@ export async function activate(context: vscode.ExtensionContext) {
48 } 56 }
49 57
50 // Commands are requests from vscode to the language server 58 // Commands are requests from vscode to the language server
51 registerCommand(
52 'rust-analyzer.analyzerStatus',
53 commands.analyzerStatus.makeCommand(context),
54 );
55 registerCommand('rust-analyzer.collectGarbage', () =>
56 Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
57 );
58 registerCommand(
59 'rust-analyzer.matchingBrace',
60 commands.matchingBrace.handle,
61 );
62 registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); 59 registerCommand('rust-analyzer.joinLines', commands.joinLines.handle);
63 registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); 60 registerCommand('rust-analyzer.parentModule', commands.parentModule.handle);
64 registerCommand('rust-analyzer.run', commands.runnables.handle); 61 registerCommand('rust-analyzer.run', commands.runnables.handle);