Skip to content

Commit 9b39633

Browse files
committed
Add vscode support for gotoRelevantFile
1 parent 30962b6 commit 9b39633

File tree

5 files changed

+120
-18
lines changed

5 files changed

+120
-18
lines changed

vscode/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,23 @@
5959
"command": "rubyLsp.fileOperation",
6060
"when": "rubyLsp.activated && view == 'workbench.explorer.fileView'",
6161
"group": "navigation"
62+
},
63+
{
64+
"command": "rubyLsp.gotoRelevantFile",
65+
"when": "rubyLsp.activated && view == 'workbench.explorer.fileView'",
66+
"group": "navigation"
6267
}
6368
],
6469
"explorer/context": [
6570
{
6671
"command": "rubyLsp.fileOperation",
6772
"when": "rubyLsp.activated",
6873
"group": "2_workspace"
74+
},
75+
{
76+
"command": "rubyLsp.gotoRelevantFile",
77+
"when": "rubyLsp.activated",
78+
"group": "2_workspace"
6979
}
7080
]
7181
},
@@ -159,6 +169,11 @@
159169
"category": "Ruby LSP",
160170
"icon": "$(ruby)"
161171
},
172+
{
173+
"command": "rubyLsp.gotoRelevantFile",
174+
"title": "Goto relevant file (test <> source code)",
175+
"category": "Ruby LSP"
176+
},
162177
{
163178
"command": "rubyLsp.collectRubyLspInfo",
164179
"title": "Collect Ruby LSP information for issue reporting",

vscode/src/client.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ class ExperimentalCapabilities implements StaticFeature {
325325
initialize(
326326
_capabilities: ServerCapabilities,
327327
_documentSelector: DocumentSelector | undefined,
328-
): void {}
328+
): void { }
329329

330330
getState(): FeatureState {
331331
return { kind: "static" };
332332
}
333333

334-
clear(): void {}
334+
clear(): void { }
335335
}
336336

337337
export default class Client extends LanguageClient implements ClientInterface {
@@ -482,6 +482,14 @@ export default class Client extends LanguageClient implements ClientInterface {
482482
return super.dispose(timeout);
483483
}
484484

485+
async sendGotoRelevantFileRequest(
486+
uri: vscode.Uri,
487+
): Promise<{ locations: string[] } | null> {
488+
return this.sendRequest("experimental/gotoRelevantFile", {
489+
textDocument: { uri: uri.toString() },
490+
});
491+
}
492+
485493
private async benchmarkMiddleware<T>(
486494
type: string | MessageSignature,
487495
params: any,

vscode/src/common.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export enum Command {
3333
StartServerInDebugMode = "rubyLsp.startServerInDebugMode",
3434
ShowOutput = "rubyLsp.showOutput",
3535
MigrateLaunchConfiguration = "rubyLsp.migrateLaunchConfiguration",
36+
GotoRelevantFile = "rubyLsp.gotoRelevantFile",
3637
}
3738

3839
export interface RubyInterface {

vscode/src/rubyLsp.ts

+29-15
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,14 @@ export class RubyLsp {
574574
command: string;
575575
args: any[];
576576
} & vscode.QuickPickItem)[] = [
577-
{
578-
label: "Minitest test",
579-
description: "Create a new Minitest test",
580-
iconPath: new vscode.ThemeIcon("new-file"),
581-
command: Command.NewMinitestFile,
582-
args: [],
583-
},
584-
];
577+
{
578+
label: "Minitest test",
579+
description: "Create a new Minitest test",
580+
iconPath: new vscode.ThemeIcon("new-file"),
581+
command: Command.NewMinitestFile,
582+
args: [],
583+
},
584+
];
585585

586586
if (
587587
workspace.lspClient?.addons?.some(
@@ -693,6 +693,20 @@ export class RubyLsp {
693693
);
694694
},
695695
),
696+
vscode.commands.registerCommand(Command.GotoRelevantFile, async () => {
697+
const uri = vscode.window.activeTextEditor?.document.uri;
698+
if (!uri) {
699+
return;
700+
}
701+
const response: { locations: string[] } | null | undefined =
702+
await this.currentActiveWorkspace()?.lspClient?.sendGotoRelevantFileRequest(
703+
uri,
704+
);
705+
706+
if (response) {
707+
return openUris(response.locations);
708+
}
709+
}),
696710
];
697711
}
698712

@@ -788,15 +802,15 @@ export class RubyLsp {
788802

789803
const response:
790804
| {
791-
workerAlive: boolean;
792-
backtrace: string[];
793-
documents: { uri: string; source: string };
794-
incomingQueueSize: number;
795-
}
805+
workerAlive: boolean;
806+
backtrace: string[];
807+
documents: { uri: string; source: string };
808+
incomingQueueSize: number;
809+
}
796810
| null
797811
| undefined = await workspace?.lspClient?.sendRequest(
798-
"rubyLsp/diagnoseState",
799-
);
812+
"rubyLsp/diagnoseState",
813+
);
800814

801815
if (response) {
802816
const documentData = Object.entries(response.documents);

vscode/src/test/suite/client.test.ts

+65-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
ShowMessageParams,
2727
MessageType,
2828
} from "vscode-languageclient/node";
29-
import { after, afterEach, before } from "mocha";
29+
import { after, afterEach, before, setup } from "mocha";
3030

3131
import { Ruby, ManagerIdentifier } from "../../ruby";
3232
import Client from "../../client";
@@ -997,4 +997,68 @@ suite("Client", () => {
997997

998998
assert.ok(response.length > 0);
999999
}).timeout(20000);
1000+
1001+
suite("goto relevant file", () => {
1002+
let testUri: vscode.Uri;
1003+
let implUri: vscode.Uri;
1004+
1005+
setup(() => {
1006+
testUri = vscode.Uri.joinPath(
1007+
workspaceUri,
1008+
"test",
1009+
"requests",
1010+
"goto_relevant_file_test.rb",
1011+
);
1012+
implUri = vscode.Uri.joinPath(
1013+
workspaceUri,
1014+
"lib",
1015+
"ruby_lsp",
1016+
"requests",
1017+
"goto_relevant_file.rb",
1018+
);
1019+
});
1020+
1021+
test("for test file", async () => {
1022+
const response: { locations: string[] } = await client.sendRequest(
1023+
"experimental/gotoRelevantFile",
1024+
{
1025+
textDocument: {
1026+
uri: testUri.toString(),
1027+
},
1028+
},
1029+
);
1030+
1031+
assert.ok(response.locations.length === 1);
1032+
assert.match(response.locations[0], /lib\/ruby_lsp\/requests\/goto_relevant_file\.rb$/);
1033+
});
1034+
1035+
test("for implementation file", async () => {
1036+
const response: { locations: string[] } = await client.sendRequest(
1037+
"experimental/gotoRelevantFile",
1038+
{
1039+
textDocument: {
1040+
uri: implUri.toString(),
1041+
},
1042+
},
1043+
);
1044+
1045+
assert.ok(response.locations.length === 1);
1046+
assert.match(response.locations[0], /test\/requests\/goto_relevant_file_test\.rb$/);
1047+
});
1048+
1049+
test("returns empty array for invalid file", async () => {
1050+
const uri = vscode.Uri.joinPath(workspaceUri, "nonexistent", "file.rb");
1051+
1052+
const response: { locations: string[] } = await client.sendRequest(
1053+
"experimental/gotoRelevantFile",
1054+
{
1055+
textDocument: {
1056+
uri: uri.toString(),
1057+
},
1058+
},
1059+
);
1060+
1061+
assert.deepStrictEqual(response, { locations: [] });
1062+
});
1063+
});
10001064
});

0 commit comments

Comments
 (0)