aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/analyzer_status.ts6
-rw-r--r--editors/code/src/commands/expand_macro.ts18
-rw-r--r--editors/code/src/commands/index.ts9
-rw-r--r--editors/code/src/commands/join_lines.ts17
-rw-r--r--editors/code/src/commands/matching_brace.ts15
-rw-r--r--editors/code/src/commands/on_enter.ts21
-rw-r--r--editors/code/src/commands/parent_module.ts10
-rw-r--r--editors/code/src/commands/runnables.ts29
-rw-r--r--editors/code/src/commands/ssr.ts16
-rw-r--r--editors/code/src/commands/syntax_tree.ts27
10 files changed, 47 insertions, 121 deletions
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index 6631e8db7..1c6ea399b 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -1,5 +1,6 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2 2
3import * as ra from '../rust-analyzer-api';
3import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
4 5
5// Shows status of rust-analyzer (for debugging) 6// Shows status of rust-analyzer (for debugging)
@@ -50,10 +51,7 @@ class TextDocumentContentProvider
50 const client = this.ctx.client; 51 const client = this.ctx.client;
51 if (!editor || !client) return ''; 52 if (!editor || !client) return '';
52 53
53 return client.sendRequest<string>( 54 return client.sendRequest(ra.analyzerStatus, null);
54 'rust-analyzer/analyzerStatus',
55 null,
56 );
57 } 55 }
58 56
59 get onDidChange(): vscode.Event<vscode.Uri> { 57 get onDidChange(): vscode.Event<vscode.Uri> {
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts
index edec9bbc1..23f2ef1d5 100644
--- a/editors/code/src/commands/expand_macro.ts
+++ b/editors/code/src/commands/expand_macro.ts
@@ -1,5 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from '../rust-analyzer-api';
3 3
4import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
5 5
@@ -26,12 +26,7 @@ export function expandMacro(ctx: Ctx): Cmd {
26 }; 26 };
27} 27}
28 28
29interface ExpandedMacro { 29function codeFormat(expanded: ra.ExpandedMacro): string {
30 name: string;
31 expansion: string;
32}
33
34function codeFormat(expanded: ExpandedMacro): string {
35 let result = `// Recursive expansion of ${expanded.name}! macro\n`; 30 let result = `// Recursive expansion of ${expanded.name}! macro\n`;
36 result += '// ' + '='.repeat(result.length - 3); 31 result += '// ' + '='.repeat(result.length - 3);
37 result += '\n\n'; 32 result += '\n\n';
@@ -54,14 +49,11 @@ class TextDocumentContentProvider
54 if (!editor || !client) return ''; 49 if (!editor || !client) return '';
55 50
56 const position = editor.selection.active; 51 const position = editor.selection.active;
57 const request: lc.TextDocumentPositionParams = { 52
53 const expanded = await client.sendRequest(ra.expandMacro, {
58 textDocument: { uri: editor.document.uri.toString() }, 54 textDocument: { uri: editor.document.uri.toString() },
59 position, 55 position,
60 }; 56 });
61 const expanded = await client.sendRequest<ExpandedMacro>(
62 'rust-analyzer/expandMacro',
63 request,
64 );
65 57
66 if (expanded == null) return 'Not available'; 58 if (expanded == null) return 'Not available';
67 59
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 839245f48..bdb7fc3b0 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -1,5 +1,6 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import * as ra from '../rust-analyzer-api';
3 4
4import { Ctx, Cmd } from '../ctx'; 5import { Ctx, Cmd } from '../ctx';
5import * as sourceChange from '../source_change'; 6import * as sourceChange from '../source_change';
@@ -16,9 +17,7 @@ export * from './ssr';
16export * from './server_version'; 17export * from './server_version';
17 18
18export function collectGarbage(ctx: Ctx): Cmd { 19export function collectGarbage(ctx: Ctx): Cmd {
19 return async () => { 20 return async () => ctx.client.sendRequest(ra.collectGarbage, null);
20 await ctx.client?.sendRequest<null>('rust-analyzer/collectGarbage', null);
21 };
22} 21}
23 22
24export function showReferences(ctx: Ctx): Cmd { 23export function showReferences(ctx: Ctx): Cmd {
@@ -36,13 +35,13 @@ export function showReferences(ctx: Ctx): Cmd {
36} 35}
37 36
38export function applySourceChange(ctx: Ctx): Cmd { 37export function applySourceChange(ctx: Ctx): Cmd {
39 return async (change: sourceChange.SourceChange) => { 38 return async (change: ra.SourceChange) => {
40 await sourceChange.applySourceChange(ctx, change); 39 await sourceChange.applySourceChange(ctx, change);
41 }; 40 };
42} 41}
43 42
44export function selectAndApplySourceChange(ctx: Ctx): Cmd { 43export function selectAndApplySourceChange(ctx: Ctx): Cmd {
45 return async (changes: sourceChange.SourceChange[]) => { 44 return async (changes: ra.SourceChange[]) => {
46 if (changes.length === 1) { 45 if (changes.length === 1) {
47 await sourceChange.applySourceChange(ctx, changes[0]); 46 await sourceChange.applySourceChange(ctx, changes[0]);
48 } else if (changes.length > 0) { 47 } else if (changes.length > 0) {
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts
index 7b08c3255..de0614653 100644
--- a/editors/code/src/commands/join_lines.ts
+++ b/editors/code/src/commands/join_lines.ts
@@ -1,7 +1,7 @@
1import * as lc from 'vscode-languageclient'; 1import * as ra from '../rust-analyzer-api';
2 2
3import { Ctx, Cmd } from '../ctx'; 3import { Ctx, Cmd } from '../ctx';
4import { applySourceChange, SourceChange } from '../source_change'; 4import { applySourceChange } from '../source_change';
5 5
6export function joinLines(ctx: Ctx): Cmd { 6export function joinLines(ctx: Ctx): Cmd {
7 return async () => { 7 return async () => {
@@ -9,19 +9,10 @@ export function joinLines(ctx: Ctx): Cmd {
9 const client = ctx.client; 9 const client = ctx.client;
10 if (!editor || !client) return; 10 if (!editor || !client) return;
11 11
12 const request: JoinLinesParams = { 12 const change = await client.sendRequest(ra.joinLines, {
13 range: client.code2ProtocolConverter.asRange(editor.selection), 13 range: client.code2ProtocolConverter.asRange(editor.selection),
14 textDocument: { uri: editor.document.uri.toString() }, 14 textDocument: { uri: editor.document.uri.toString() },
15 }; 15 });
16 const change = await client.sendRequest<SourceChange>(
17 'rust-analyzer/joinLines',
18 request,
19 );
20 await applySourceChange(ctx, change); 16 await applySourceChange(ctx, change);
21 }; 17 };
22} 18}
23
24interface JoinLinesParams {
25 textDocument: lc.TextDocumentIdentifier;
26 range: lc.Range;
27}
diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts
index 7c58bb7e7..a60776e2d 100644
--- a/editors/code/src/commands/matching_brace.ts
+++ b/editors/code/src/commands/matching_brace.ts
@@ -1,5 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from '../rust-analyzer-api';
3 3
4import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
5 5
@@ -9,16 +9,12 @@ export function matchingBrace(ctx: Ctx): Cmd {
9 const client = ctx.client; 9 const client = ctx.client;
10 if (!editor || !client) return; 10 if (!editor || !client) return;
11 11
12 const request: FindMatchingBraceParams = { 12 const response = await client.sendRequest(ra.findMatchingBrace, {
13 textDocument: { uri: editor.document.uri.toString() }, 13 textDocument: { uri: editor.document.uri.toString() },
14 offsets: editor.selections.map(s => 14 offsets: editor.selections.map(s =>
15 client.code2ProtocolConverter.asPosition(s.active), 15 client.code2ProtocolConverter.asPosition(s.active),
16 ), 16 ),
17 }; 17 });
18 const response = await client.sendRequest<lc.Position[]>(
19 'rust-analyzer/findMatchingBrace',
20 request,
21 );
22 editor.selections = editor.selections.map((sel, idx) => { 18 editor.selections = editor.selections.map((sel, idx) => {
23 const active = client.protocol2CodeConverter.asPosition( 19 const active = client.protocol2CodeConverter.asPosition(
24 response[idx], 20 response[idx],
@@ -29,8 +25,3 @@ export function matchingBrace(ctx: Ctx): Cmd {
29 editor.revealRange(editor.selection); 25 editor.revealRange(editor.selection);
30 }; 26 };
31} 27}
32
33interface FindMatchingBraceParams {
34 textDocument: lc.TextDocumentIdentifier;
35 offsets: lc.Position[];
36}
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
index 27ae8ec23..285849db7 100644
--- a/editors/code/src/commands/on_enter.ts
+++ b/editors/code/src/commands/on_enter.ts
@@ -1,7 +1,7 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from '../rust-analyzer-api';
3 3
4import { applySourceChange, SourceChange } from '../source_change'; 4import { applySourceChange } from '../source_change';
5import { Cmd, Ctx } from '../ctx'; 5import { Cmd, Ctx } from '../ctx';
6 6
7async function handleKeypress(ctx: Ctx) { 7async function handleKeypress(ctx: Ctx) {
@@ -10,22 +10,15 @@ async function handleKeypress(ctx: Ctx) {
10 10
11 if (!editor || !client) return false; 11 if (!editor || !client) return false;
12 12
13 const request: lc.TextDocumentPositionParams = { 13 const change = await client.sendRequest(ra.onEnter, {
14 textDocument: { uri: editor.document.uri.toString() }, 14 textDocument: { uri: editor.document.uri.toString() },
15 position: client.code2ProtocolConverter.asPosition( 15 position: client.code2ProtocolConverter.asPosition(
16 editor.selection.active, 16 editor.selection.active,
17 ), 17 ),
18 }; 18 }).catch(_error => {
19 const change = await client.sendRequest<undefined | SourceChange>( 19 // client.logFailedRequest(OnEnterRequest.type, error);
20 'rust-analyzer/onEnter', 20 return null;
21 request, 21 });
22 ).catch(
23 (_error: any) => {
24 // FIXME: switch to the more modern (?) typed request infrastructure
25 // client.logFailedRequest(OnEnterRequest.type, error);
26 return Promise.resolve(null);
27 }
28 );
29 if (!change) return false; 22 if (!change) return false;
30 23
31 await applySourceChange(ctx, change); 24 await applySourceChange(ctx, change);
diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts
index bf40b4021..8f78ddd71 100644
--- a/editors/code/src/commands/parent_module.ts
+++ b/editors/code/src/commands/parent_module.ts
@@ -1,5 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from '../rust-analyzer-api';
3 3
4import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
5 5
@@ -9,16 +9,12 @@ export function parentModule(ctx: Ctx): Cmd {
9 const client = ctx.client; 9 const client = ctx.client;
10 if (!editor || !client) return; 10 if (!editor || !client) return;
11 11
12 const request: lc.TextDocumentPositionParams = { 12 const response = await client.sendRequest(ra.parentModule, {
13 textDocument: { uri: editor.document.uri.toString() }, 13 textDocument: { uri: editor.document.uri.toString() },
14 position: client.code2ProtocolConverter.asPosition( 14 position: client.code2ProtocolConverter.asPosition(
15 editor.selection.active, 15 editor.selection.active,
16 ), 16 ),
17 }; 17 });
18 const response = await client.sendRequest<lc.Location[]>(
19 'rust-analyzer/parentModule',
20 request,
21 );
22 const loc = response[0]; 18 const loc = response[0];
23 if (loc == null) return; 19 if (loc == null) return;
24 20
diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts
index 7919997ce..06b513466 100644
--- a/editors/code/src/commands/runnables.ts
+++ b/editors/code/src/commands/runnables.ts
@@ -1,5 +1,6 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import * as ra from '../rust-analyzer-api';
3 4
4import { Ctx, Cmd } from '../ctx'; 5import { Ctx, Cmd } from '../ctx';
5 6
@@ -14,16 +15,13 @@ export function run(ctx: Ctx): Cmd {
14 const textDocument: lc.TextDocumentIdentifier = { 15 const textDocument: lc.TextDocumentIdentifier = {
15 uri: editor.document.uri.toString(), 16 uri: editor.document.uri.toString(),
16 }; 17 };
17 const params: RunnablesParams = { 18
19 const runnables = await client.sendRequest(ra.runnables, {
18 textDocument, 20 textDocument,
19 position: client.code2ProtocolConverter.asPosition( 21 position: client.code2ProtocolConverter.asPosition(
20 editor.selection.active, 22 editor.selection.active,
21 ), 23 ),
22 }; 24 });
23 const runnables = await client.sendRequest<Runnable[]>(
24 'rust-analyzer/runnables',
25 params,
26 );
27 const items: RunnableQuickPick[] = []; 25 const items: RunnableQuickPick[] = [];
28 if (prevRunnable) { 26 if (prevRunnable) {
29 items.push(prevRunnable); 27 items.push(prevRunnable);
@@ -48,7 +46,7 @@ export function run(ctx: Ctx): Cmd {
48} 46}
49 47
50export function runSingle(ctx: Ctx): Cmd { 48export function runSingle(ctx: Ctx): Cmd {
51 return async (runnable: Runnable) => { 49 return async (runnable: ra.Runnable) => {
52 const editor = ctx.activeRustEditor; 50 const editor = ctx.activeRustEditor;
53 if (!editor) return; 51 if (!editor) return;
54 52
@@ -64,26 +62,13 @@ export function runSingle(ctx: Ctx): Cmd {
64 }; 62 };
65} 63}
66 64
67interface RunnablesParams {
68 textDocument: lc.TextDocumentIdentifier;
69 position?: lc.Position;
70}
71
72interface Runnable {
73 label: string;
74 bin: string;
75 args: string[];
76 env: { [index: string]: string };
77 cwd?: string;
78}
79
80class RunnableQuickPick implements vscode.QuickPickItem { 65class RunnableQuickPick implements vscode.QuickPickItem {
81 public label: string; 66 public label: string;
82 public description?: string | undefined; 67 public description?: string | undefined;
83 public detail?: string | undefined; 68 public detail?: string | undefined;
84 public picked?: boolean | undefined; 69 public picked?: boolean | undefined;
85 70
86 constructor(public runnable: Runnable) { 71 constructor(public runnable: ra.Runnable) {
87 this.label = runnable.label; 72 this.label = runnable.label;
88 } 73 }
89} 74}
@@ -96,7 +81,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
96 env?: { [key: string]: string }; 81 env?: { [key: string]: string };
97} 82}
98 83
99function createTask(spec: Runnable): vscode.Task { 84function createTask(spec: ra.Runnable): vscode.Task {
100 const TASK_SOURCE = 'Rust'; 85 const TASK_SOURCE = 'Rust';
101 const definition: CargoTaskDefinition = { 86 const definition: CargoTaskDefinition = {
102 type: 'cargo', 87 type: 'cargo',
diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts
index 9b814612a..eee48c693 100644
--- a/editors/code/src/commands/ssr.ts
+++ b/editors/code/src/commands/ssr.ts
@@ -1,6 +1,8 @@
1import { Ctx, Cmd } from '../ctx';
2import { applySourceChange, SourceChange } from '../source_change';
3import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as ra from "../rust-analyzer-api";
3
4import { Ctx, Cmd } from '../ctx';
5import { applySourceChange } from '../source_change';
4 6
5export function ssr(ctx: Ctx): Cmd { 7export function ssr(ctx: Ctx): Cmd {
6 return async () => { 8 return async () => {
@@ -21,16 +23,8 @@ export function ssr(ctx: Ctx): Cmd {
21 23
22 if (!request) return; 24 if (!request) return;
23 25
24 const ssrRequest: SsrRequest = { arg: request }; 26 const change = await client.sendRequest(ra.ssr, { arg: request });
25 const change = await client.sendRequest<SourceChange>(
26 'rust-analyzer/ssr',
27 ssrRequest,
28 );
29 27
30 await applySourceChange(ctx, change); 28 await applySourceChange(ctx, change);
31 }; 29 };
32} 30}
33
34interface SsrRequest {
35 arg: string;
36}
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
index 2887c96c8..7218bfb90 100644
--- a/editors/code/src/commands/syntax_tree.ts
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -1,5 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from '../rust-analyzer-api';
3 3
4import { Ctx, Cmd } from '../ctx'; 4import { Ctx, Cmd } from '../ctx';
5 5
@@ -61,13 +61,8 @@ function afterLs(f: () => void) {
61 setTimeout(f, 10); 61 setTimeout(f, 10);
62} 62}
63 63
64interface SyntaxTreeParams {
65 textDocument: lc.TextDocumentIdentifier;
66 range?: lc.Range;
67}
68 64
69class TextDocumentContentProvider 65class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
70 implements vscode.TextDocumentContentProvider {
71 uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); 66 uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
72 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 67 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
73 68
@@ -79,23 +74,15 @@ class TextDocumentContentProvider
79 const client = this.ctx.client; 74 const client = this.ctx.client;
80 if (!editor || !client) return ''; 75 if (!editor || !client) return '';
81 76
82 let range: lc.Range | undefined;
83
84 // When the range based query is enabled we take the range of the selection 77 // When the range based query is enabled we take the range of the selection
85 if (uri.query === 'range=true') { 78 const range = uri.query === 'range=true' && !editor.selection.isEmpty
86 range = editor.selection.isEmpty 79 ? client.code2ProtocolConverter.asRange(editor.selection)
87 ? undefined 80 : null;
88 : client.code2ProtocolConverter.asRange(editor.selection);
89 }
90 81
91 const request: SyntaxTreeParams = { 82 return client.sendRequest(ra.syntaxTree, {
92 textDocument: { uri: editor.document.uri.toString() }, 83 textDocument: { uri: editor.document.uri.toString() },
93 range, 84 range,
94 }; 85 });
95 return client.sendRequest<string>(
96 'rust-analyzer/syntaxTree',
97 request,
98 );
99 } 86 }
100 87
101 get onDidChange(): vscode.Event<vscode.Uri> { 88 get onDidChange(): vscode.Event<vscode.Uri> {