about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-10-14 10:56:08 +0000
committerGitHub <noreply@github.com>2021-10-14 10:56:08 +0000
commitf87debcf87c16690e8d5e185a35d03a402a2d5bf (patch)
tree9961aee1029f5c23a0fcf8b0380bc124935fc831 /editors/code
parent641fa374edca00e33df51b05ffcbebbbef41cac5 (diff)
parent59c755227d3f6903095cf03bce5e76b7747138ec (diff)
downloadrust-f87debcf87c16690e8d5e185a35d03a402a2d5bf.tar.gz
rust-f87debcf87c16690e8d5e185a35d03a402a2d5bf.zip
Merge #10434
10434: Allow `Locate parent module` command in Cargo.toml r=Veykril a=rainy-me

close #10355

Co-authored-by: rainy-me <github@rainy.me>
Co-authored-by: rainy-me <github@yue.coffee>
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/commands.ts6
-rw-r--r--editors/code/src/lsp_ext.ts2
-rw-r--r--editors/code/src/util.ts5
3 files changed, 10 insertions, 3 deletions
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 0e08b60a93c..c9385361f88 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -8,7 +8,7 @@ import { applySnippetWorkspaceEdit, applySnippetTextEdits } from './snippets';
 import { spawnSync } from 'child_process';
 import { RunnableQuickPick, selectRunnable, createTask, createArgs } from './run';
 import { AstInspector } from './ast_inspector';
-import { isRustDocument, sleep, isRustEditor } from './util';
+import { isRustDocument, isCargoTomlDocument, sleep, isRustEditor } from './util';
 import { startDebugSession, makeDebugConfig } from './debug';
 import { LanguageClient } from 'vscode-languageclient/node';
 
@@ -185,9 +185,10 @@ export function onEnter(ctx: Ctx): Cmd {
 
 export function parentModule(ctx: Ctx): Cmd {
     return async () => {
-        const editor = ctx.activeRustEditor;
+        const editor = vscode.window.activeTextEditor;
         const client = ctx.client;
         if (!editor || !client) return;
+        if (!(isRustDocument(editor.document) || isCargoTomlDocument(editor.document))) return;
 
         const locations = await client.sendRequest(ra.parentModule, {
             textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
@@ -195,6 +196,7 @@ export function parentModule(ctx: Ctx): Cmd {
                 editor.selection.active,
             ),
         });
+        if (!locations) return;
 
         if (locations.length === 1) {
             const loc = locations[0];
diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts
index ac632a01567..90796e611e6 100644
--- a/editors/code/src/lsp_ext.ts
+++ b/editors/code/src/lsp_ext.ts
@@ -62,7 +62,7 @@ export interface MatchingBraceParams {
 }
 export const matchingBrace = new lc.RequestType<MatchingBraceParams, lc.Position[], void>("experimental/matchingBrace");
 
-export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[], void>("experimental/parentModule");
+export const parentModule = new lc.RequestType<lc.TextDocumentPositionParams, lc.LocationLink[] | null, void>("experimental/parentModule");
 
 export interface JoinLinesParams {
     textDocument: lc.TextDocumentIdentifier;
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts
index aa57081a5f3..057a3d2e194 100644
--- a/editors/code/src/util.ts
+++ b/editors/code/src/util.ts
@@ -104,6 +104,11 @@ export function isRustDocument(document: vscode.TextDocument): document is RustD
     return document.languageId === 'rust' && document.uri.scheme === 'file';
 }
 
+export function isCargoTomlDocument(document: vscode.TextDocument): document is RustDocument {
+    // ideally `document.languageId` should be 'toml' but user maybe not have toml extension installed
+    return document.uri.scheme === 'file' && document.fileName.endsWith('Cargo.toml');
+}
+
 export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
     return isRustDocument(editor.document);
 }