Skip to content

Commit 5e6f4ca

Browse files
bors[bot]Veetaha
and
Veetaha
authored
Merge #3299
3299: vscode: migrate to request type api r=matklad a=Veetaha More type-safety to the god of type-safety. Co-authored-by: Veetaha <[email protected]>
2 parents 558d263 + 18b97d9 commit 5e6f4ca

15 files changed

+203
-198
lines changed

editors/code/src/commands/analyzer_status.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22

3+
import * as ra from '../rust-analyzer-api';
34
import { Ctx, Cmd } from '../ctx';
45

56
// Shows status of rust-analyzer (for debugging)
@@ -50,10 +51,7 @@ class TextDocumentContentProvider
5051
const client = this.ctx.client;
5152
if (!editor || !client) return '';
5253

53-
return client.sendRequest<string>(
54-
'rust-analyzer/analyzerStatus',
55-
null,
56-
);
54+
return client.sendRequest(ra.analyzerStatus, null);
5755
}
5856

5957
get onDidChange(): vscode.Event<vscode.Uri> {

editors/code/src/commands/expand_macro.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from '../rust-analyzer-api';
33

44
import { Ctx, Cmd } from '../ctx';
55

@@ -26,12 +26,7 @@ export function expandMacro(ctx: Ctx): Cmd {
2626
};
2727
}
2828

29-
interface ExpandedMacro {
30-
name: string;
31-
expansion: string;
32-
}
33-
34-
function codeFormat(expanded: ExpandedMacro): string {
29+
function codeFormat(expanded: ra.ExpandedMacro): string {
3530
let result = `// Recursive expansion of ${expanded.name}! macro\n`;
3631
result += '// ' + '='.repeat(result.length - 3);
3732
result += '\n\n';
@@ -54,14 +49,11 @@ class TextDocumentContentProvider
5449
if (!editor || !client) return '';
5550

5651
const position = editor.selection.active;
57-
const request: lc.TextDocumentPositionParams = {
52+
53+
const expanded = await client.sendRequest(ra.expandMacro, {
5854
textDocument: { uri: editor.document.uri.toString() },
5955
position,
60-
};
61-
const expanded = await client.sendRequest<ExpandedMacro>(
62-
'rust-analyzer/expandMacro',
63-
request,
64-
);
56+
});
6557

6658
if (expanded == null) return 'Not available';
6759

editors/code/src/commands/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
3+
import * as ra from '../rust-analyzer-api';
34

45
import { Ctx, Cmd } from '../ctx';
56
import * as sourceChange from '../source_change';
@@ -16,9 +17,7 @@ export * from './ssr';
1617
export * from './server_version';
1718

1819
export function collectGarbage(ctx: Ctx): Cmd {
19-
return async () => {
20-
await ctx.client?.sendRequest<null>('rust-analyzer/collectGarbage', null);
21-
};
20+
return async () => ctx.client.sendRequest(ra.collectGarbage, null);
2221
}
2322

2423
export function showReferences(ctx: Ctx): Cmd {
@@ -36,13 +35,13 @@ export function showReferences(ctx: Ctx): Cmd {
3635
}
3736

3837
export function applySourceChange(ctx: Ctx): Cmd {
39-
return async (change: sourceChange.SourceChange) => {
38+
return async (change: ra.SourceChange) => {
4039
await sourceChange.applySourceChange(ctx, change);
4140
};
4241
}
4342

4443
export function selectAndApplySourceChange(ctx: Ctx): Cmd {
45-
return async (changes: sourceChange.SourceChange[]) => {
44+
return async (changes: ra.SourceChange[]) => {
4645
if (changes.length === 1) {
4746
await sourceChange.applySourceChange(ctx, changes[0]);
4847
} else if (changes.length > 0) {
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import * as lc from 'vscode-languageclient';
1+
import * as ra from '../rust-analyzer-api';
22

33
import { Ctx, Cmd } from '../ctx';
4-
import { applySourceChange, SourceChange } from '../source_change';
4+
import { applySourceChange } from '../source_change';
55

66
export function joinLines(ctx: Ctx): Cmd {
77
return async () => {
88
const editor = ctx.activeRustEditor;
99
const client = ctx.client;
1010
if (!editor || !client) return;
1111

12-
const request: JoinLinesParams = {
12+
const change = await client.sendRequest(ra.joinLines, {
1313
range: client.code2ProtocolConverter.asRange(editor.selection),
1414
textDocument: { uri: editor.document.uri.toString() },
15-
};
16-
const change = await client.sendRequest<SourceChange>(
17-
'rust-analyzer/joinLines',
18-
request,
19-
);
15+
});
2016
await applySourceChange(ctx, change);
2117
};
2218
}
23-
24-
interface JoinLinesParams {
25-
textDocument: lc.TextDocumentIdentifier;
26-
range: lc.Range;
27-
}
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from '../rust-analyzer-api';
33

44
import { Ctx, Cmd } from '../ctx';
55

@@ -9,16 +9,12 @@ export function matchingBrace(ctx: Ctx): Cmd {
99
const client = ctx.client;
1010
if (!editor || !client) return;
1111

12-
const request: FindMatchingBraceParams = {
12+
const response = await client.sendRequest(ra.findMatchingBrace, {
1313
textDocument: { uri: editor.document.uri.toString() },
1414
offsets: editor.selections.map(s =>
1515
client.code2ProtocolConverter.asPosition(s.active),
1616
),
17-
};
18-
const response = await client.sendRequest<lc.Position[]>(
19-
'rust-analyzer/findMatchingBrace',
20-
request,
21-
);
17+
});
2218
editor.selections = editor.selections.map((sel, idx) => {
2319
const active = client.protocol2CodeConverter.asPosition(
2420
response[idx],
@@ -29,8 +25,3 @@ export function matchingBrace(ctx: Ctx): Cmd {
2925
editor.revealRange(editor.selection);
3026
};
3127
}
32-
33-
interface FindMatchingBraceParams {
34-
textDocument: lc.TextDocumentIdentifier;
35-
offsets: lc.Position[];
36-
}

editors/code/src/commands/on_enter.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from '../rust-analyzer-api';
33

4-
import { applySourceChange, SourceChange } from '../source_change';
4+
import { applySourceChange } from '../source_change';
55
import { Cmd, Ctx } from '../ctx';
66

77
async function handleKeypress(ctx: Ctx) {
@@ -10,22 +10,15 @@ async function handleKeypress(ctx: Ctx) {
1010

1111
if (!editor || !client) return false;
1212

13-
const request: lc.TextDocumentPositionParams = {
13+
const change = await client.sendRequest(ra.onEnter, {
1414
textDocument: { uri: editor.document.uri.toString() },
1515
position: client.code2ProtocolConverter.asPosition(
1616
editor.selection.active,
1717
),
18-
};
19-
const change = await client.sendRequest<undefined | SourceChange>(
20-
'rust-analyzer/onEnter',
21-
request,
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-
);
18+
}).catch(_error => {
19+
// client.logFailedRequest(OnEnterRequest.type, error);
20+
return null;
21+
});
2922
if (!change) return false;
3023

3124
await applySourceChange(ctx, change);

editors/code/src/commands/parent_module.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from '../rust-analyzer-api';
33

44
import { Ctx, Cmd } from '../ctx';
55

@@ -9,16 +9,12 @@ export function parentModule(ctx: Ctx): Cmd {
99
const client = ctx.client;
1010
if (!editor || !client) return;
1111

12-
const request: lc.TextDocumentPositionParams = {
12+
const response = await client.sendRequest(ra.parentModule, {
1313
textDocument: { uri: editor.document.uri.toString() },
1414
position: client.code2ProtocolConverter.asPosition(
1515
editor.selection.active,
1616
),
17-
};
18-
const response = await client.sendRequest<lc.Location[]>(
19-
'rust-analyzer/parentModule',
20-
request,
21-
);
17+
});
2218
const loc = response[0];
2319
if (loc == null) return;
2420

editors/code/src/commands/runnables.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
3+
import * as ra from '../rust-analyzer-api';
34

45
import { Ctx, Cmd } from '../ctx';
56

@@ -14,16 +15,13 @@ export function run(ctx: Ctx): Cmd {
1415
const textDocument: lc.TextDocumentIdentifier = {
1516
uri: editor.document.uri.toString(),
1617
};
17-
const params: RunnablesParams = {
18+
19+
const runnables = await client.sendRequest(ra.runnables, {
1820
textDocument,
1921
position: client.code2ProtocolConverter.asPosition(
2022
editor.selection.active,
2123
),
22-
};
23-
const runnables = await client.sendRequest<Runnable[]>(
24-
'rust-analyzer/runnables',
25-
params,
26-
);
24+
});
2725
const items: RunnableQuickPick[] = [];
2826
if (prevRunnable) {
2927
items.push(prevRunnable);
@@ -48,7 +46,7 @@ export function run(ctx: Ctx): Cmd {
4846
}
4947

5048
export function runSingle(ctx: Ctx): Cmd {
51-
return async (runnable: Runnable) => {
49+
return async (runnable: ra.Runnable) => {
5250
const editor = ctx.activeRustEditor;
5351
if (!editor) return;
5452

@@ -64,26 +62,13 @@ export function runSingle(ctx: Ctx): Cmd {
6462
};
6563
}
6664

67-
interface RunnablesParams {
68-
textDocument: lc.TextDocumentIdentifier;
69-
position?: lc.Position;
70-
}
71-
72-
interface Runnable {
73-
label: string;
74-
bin: string;
75-
args: string[];
76-
env: { [index: string]: string };
77-
cwd?: string;
78-
}
79-
8065
class RunnableQuickPick implements vscode.QuickPickItem {
8166
public label: string;
8267
public description?: string | undefined;
8368
public detail?: string | undefined;
8469
public picked?: boolean | undefined;
8570

86-
constructor(public runnable: Runnable) {
71+
constructor(public runnable: ra.Runnable) {
8772
this.label = runnable.label;
8873
}
8974
}
@@ -96,7 +81,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
9681
env?: { [key: string]: string };
9782
}
9883

99-
function createTask(spec: Runnable): vscode.Task {
84+
function createTask(spec: ra.Runnable): vscode.Task {
10085
const TASK_SOURCE = 'Rust';
10186
const definition: CargoTaskDefinition = {
10287
type: 'cargo',

editors/code/src/commands/ssr.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Ctx, Cmd } from '../ctx';
2-
import { applySourceChange, SourceChange } from '../source_change';
31
import * as vscode from 'vscode';
2+
import * as ra from "../rust-analyzer-api";
3+
4+
import { Ctx, Cmd } from '../ctx';
5+
import { applySourceChange } from '../source_change';
46

57
export function ssr(ctx: Ctx): Cmd {
68
return async () => {
@@ -21,16 +23,8 @@ export function ssr(ctx: Ctx): Cmd {
2123

2224
if (!request) return;
2325

24-
const ssrRequest: SsrRequest = { arg: request };
25-
const change = await client.sendRequest<SourceChange>(
26-
'rust-analyzer/ssr',
27-
ssrRequest,
28-
);
26+
const change = await client.sendRequest(ra.ssr, { arg: request });
2927

3028
await applySourceChange(ctx, change);
3129
};
3230
}
33-
34-
interface SsrRequest {
35-
arg: string;
36-
}

editors/code/src/commands/syntax_tree.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from '../rust-analyzer-api';
33

44
import { Ctx, Cmd } from '../ctx';
55

@@ -61,13 +61,8 @@ function afterLs(f: () => void) {
6161
setTimeout(f, 10);
6262
}
6363

64-
interface SyntaxTreeParams {
65-
textDocument: lc.TextDocumentIdentifier;
66-
range?: lc.Range;
67-
}
6864

69-
class TextDocumentContentProvider
70-
implements vscode.TextDocumentContentProvider {
65+
class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
7166
uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
7267
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
7368

@@ -79,23 +74,15 @@ class TextDocumentContentProvider
7974
const client = this.ctx.client;
8075
if (!editor || !client) return '';
8176

82-
let range: lc.Range | undefined;
83-
8477
// When the range based query is enabled we take the range of the selection
85-
if (uri.query === 'range=true') {
86-
range = editor.selection.isEmpty
87-
? undefined
88-
: client.code2ProtocolConverter.asRange(editor.selection);
89-
}
78+
const range = uri.query === 'range=true' && !editor.selection.isEmpty
79+
? client.code2ProtocolConverter.asRange(editor.selection)
80+
: null;
9081

91-
const request: SyntaxTreeParams = {
82+
return client.sendRequest(ra.syntaxTree, {
9283
textDocument: { uri: editor.document.uri.toString() },
9384
range,
94-
};
95-
return client.sendRequest<string>(
96-
'rust-analyzer/syntaxTree',
97-
request,
98-
);
85+
});
9986
}
10087

10188
get onDidChange(): vscode.Event<vscode.Uri> {

0 commit comments

Comments
 (0)