aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/package.json16
-rw-r--r--editors/code/src/commands.ts81
-rw-r--r--editors/code/src/lsp_ext.ts6
-rw-r--r--editors/code/src/main.ts1
4 files changed, 81 insertions, 23 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index b29f006f0..a2ed9b2d5 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -203,6 +203,11 @@
203 "command": "rust-analyzer.openCargoToml", 203 "command": "rust-analyzer.openCargoToml",
204 "title": "Open Cargo.toml", 204 "title": "Open Cargo.toml",
205 "category": "Rust Analyzer" 205 "category": "Rust Analyzer"
206 },
207 {
208 "command": "rust-analyzer.peekTests",
209 "title": "Peek related tests",
210 "category": "Rust Analyzer"
206 } 211 }
207 ], 212 ],
208 "keybindings": [ 213 "keybindings": [
@@ -685,7 +690,7 @@
685 }, 690 },
686 "rust-analyzer.procMacro.enable": { 691 "rust-analyzer.procMacro.enable": {
687 "markdownDescription": "Enable support for procedural macros, implies `#rust-analyzer.cargo.runBuildScripts#`.", 692 "markdownDescription": "Enable support for procedural macros, implies `#rust-analyzer.cargo.runBuildScripts#`.",
688 "default": false, 693 "default": true,
689 "type": "boolean" 694 "type": "boolean"
690 }, 695 },
691 "rust-analyzer.procMacro.server": { 696 "rust-analyzer.procMacro.server": {
@@ -1165,7 +1170,14 @@
1165 "command": "rust-analyzer.openCargoToml", 1170 "command": "rust-analyzer.openCargoToml",
1166 "when": "inRustProject" 1171 "when": "inRustProject"
1167 } 1172 }
1173 ],
1174 "editor/context": [
1175 {
1176 "command": "rust-analyzer.peekTests",
1177 "when": "inRustProject",
1178 "group": "navigation@1000"
1179 }
1168 ] 1180 ]
1169 } 1181 }
1170 } 1182 }
1171} 1183} \ No newline at end of file
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 283b9a160..bed1f0116 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -9,6 +9,7 @@ import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run
9import { AstInspector } from './ast_inspector'; 9import { AstInspector } from './ast_inspector';
10import { isRustDocument, sleep, isRustEditor } from './util'; 10import { isRustDocument, sleep, isRustEditor } from './util';
11import { startDebugSession, makeDebugConfig } from './debug'; 11import { startDebugSession, makeDebugConfig } from './debug';
12import { LanguageClient } from 'vscode-languageclient/node';
12 13
13export * from './ast_inspector'; 14export * from './ast_inspector';
14export * from './run'; 15export * from './run';
@@ -169,22 +170,28 @@ export function parentModule(ctx: Ctx): Cmd {
169 const client = ctx.client; 170 const client = ctx.client;
170 if (!editor || !client) return; 171 if (!editor || !client) return;
171 172
172 const response = await client.sendRequest(ra.parentModule, { 173 const locations = await client.sendRequest(ra.parentModule, {
173 textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document), 174 textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
174 position: client.code2ProtocolConverter.asPosition( 175 position: client.code2ProtocolConverter.asPosition(
175 editor.selection.active, 176 editor.selection.active,
176 ), 177 ),
177 }); 178 });
178 const loc = response[0];
179 if (!loc) return;
180 179
181 const uri = client.protocol2CodeConverter.asUri(loc.targetUri); 180 if (locations.length === 1) {
182 const range = client.protocol2CodeConverter.asRange(loc.targetRange); 181 const loc = locations[0];
183 182
184 const doc = await vscode.workspace.openTextDocument(uri); 183 const uri = client.protocol2CodeConverter.asUri(loc.targetUri);
185 const e = await vscode.window.showTextDocument(doc); 184 const range = client.protocol2CodeConverter.asRange(loc.targetRange);
186 e.selection = new vscode.Selection(range.start, range.start); 185
187 e.revealRange(range, vscode.TextEditorRevealType.InCenter); 186 const doc = await vscode.workspace.openTextDocument(uri);
187 const e = await vscode.window.showTextDocument(doc);
188 e.selection = new vscode.Selection(range.start, range.start);
189 e.revealRange(range, vscode.TextEditorRevealType.InCenter);
190 } else {
191 const uri = editor.document.uri.toString();
192 const position = client.code2ProtocolConverter.asPosition(editor.selection.active);
193 await showReferencesImpl(client, uri, position, locations.map(loc => lc.Location.create(loc.targetUri, loc.targetRange)));
194 }
188 }; 195 };
189} 196}
190 197
@@ -253,11 +260,10 @@ export function ssr(ctx: Ctx): Cmd {
253export function serverVersion(ctx: Ctx): Cmd { 260export function serverVersion(ctx: Ctx): Cmd {
254 return async () => { 261 return async () => {
255 const { stdout } = spawnSync(ctx.serverPath, ["--version"], { encoding: "utf8" }); 262 const { stdout } = spawnSync(ctx.serverPath, ["--version"], { encoding: "utf8" });
256 const commitHash = stdout.slice(`rust-analyzer `.length).trim(); 263 const versionString = stdout.slice(`rust-analyzer `.length).trim();
257 const { releaseTag } = ctx.config.package;
258 264
259 void vscode.window.showInformationMessage( 265 void vscode.window.showInformationMessage(
260 `rust-analyzer version: ${releaseTag ?? "unreleased"} (${commitHash})` 266 `rust-analyzer version: ${versionString}`
261 ); 267 );
262 }; 268 };
263} 269}
@@ -456,17 +462,20 @@ export function reloadWorkspace(ctx: Ctx): Cmd {
456 return async () => ctx.client.sendRequest(ra.reloadWorkspace); 462 return async () => ctx.client.sendRequest(ra.reloadWorkspace);
457} 463}
458 464
465async function showReferencesImpl(client: LanguageClient, uri: string, position: lc.Position, locations: lc.Location[]) {
466 if (client) {
467 await vscode.commands.executeCommand(
468 'editor.action.showReferences',
469 vscode.Uri.parse(uri),
470 client.protocol2CodeConverter.asPosition(position),
471 locations.map(client.protocol2CodeConverter.asLocation),
472 );
473 }
474}
475
459export function showReferences(ctx: Ctx): Cmd { 476export function showReferences(ctx: Ctx): Cmd {
460 return async (uri: string, position: lc.Position, locations: lc.Location[]) => { 477 return async (uri: string, position: lc.Position, locations: lc.Location[]) => {
461 const client = ctx.client; 478 await showReferencesImpl(ctx.client, uri, position, locations);
462 if (client) {
463 await vscode.commands.executeCommand(
464 'editor.action.showReferences',
465 vscode.Uri.parse(uri),
466 client.protocol2CodeConverter.asPosition(position),
467 locations.map(client.protocol2CodeConverter.asLocation),
468 );
469 }
470 }; 479 };
471} 480}
472 481
@@ -555,6 +564,36 @@ export function run(ctx: Ctx): Cmd {
555 }; 564 };
556} 565}
557 566
567export function peekTests(ctx: Ctx): Cmd {
568 const client = ctx.client;
569
570 return async () => {
571 const editor = ctx.activeRustEditor;
572 if (!editor || !client) return;
573
574 await vscode.window.withProgress({
575 location: vscode.ProgressLocation.Notification,
576 title: "Looking for tests...",
577 cancellable: false,
578 }, async (_progress, _token) => {
579 const uri = editor.document.uri.toString();
580 const position = client.code2ProtocolConverter.asPosition(
581 editor.selection.active,
582 );
583
584 const tests = await client.sendRequest(ra.relatedTests, {
585 textDocument: { uri: uri },
586 position: position,
587 });
588 const locations: lc.Location[] = tests.map(it =>
589 lc.Location.create(it.runnable.location!.targetUri, it.runnable.location!.targetSelectionRange));
590
591 await showReferencesImpl(client, uri, position, locations);
592 });
593 };
594}
595
596
558export function runSingle(ctx: Ctx): Cmd { 597export function runSingle(ctx: Ctx): Cmd {
559 return async (runnable: ra.Runnable) => { 598 return async (runnable: ra.Runnable) => {
560 const editor = ctx.activeRustEditor; 599 const editor = ctx.activeRustEditor;
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index 2de1e427d..52de29e04 100644
--- a/editors/code/src/lsp_ext.ts
+++ b/editors/code/src/lsp_ext.ts
@@ -72,6 +72,12 @@ export interface Runnable {
72} 72}
73export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables"); 73export const runnables = new lc.RequestType<RunnablesParams, Runnable[], void>("experimental/runnables");
74 74
75export interface TestInfo {
76 runnable: Runnable;
77}
78
79export const relatedTests = new lc.RequestType<lc.TextDocumentPositionParams, TestInfo[], void>("rust-analyzer/relatedTests");
80
75export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint; 81export type InlayHint = InlayHint.TypeHint | InlayHint.ParamHint | InlayHint.ChainingHint;
76 82
77export namespace InlayHint { 83export namespace InlayHint {
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 1be4f1758..925103f56 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -113,6 +113,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
113 ctx.registerCommand('newDebugConfig', commands.newDebugConfig); 113 ctx.registerCommand('newDebugConfig', commands.newDebugConfig);
114 ctx.registerCommand('openDocs', commands.openDocs); 114 ctx.registerCommand('openDocs', commands.openDocs);
115 ctx.registerCommand('openCargoToml', commands.openCargoToml); 115 ctx.registerCommand('openCargoToml', commands.openCargoToml);
116 ctx.registerCommand('peekTests', commands.peekTests);
116 117
117 defaultOnEnter.dispose(); 118 defaultOnEnter.dispose();
118 ctx.registerCommand('onEnter', commands.onEnter); 119 ctx.registerCommand('onEnter', commands.onEnter);