]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/parent_module.ts
Move parentModule to the new Ctx
[rust.git] / editors / code / src / commands / parent_module.ts
1 import * as vscode from 'vscode';
2
3 import * as lc from 'vscode-languageclient';
4 import { Ctx, Cmd } from '../ctx';
5
6 export function parentModule(ctx: Ctx): Cmd {
7     return async () => {
8         const editor = ctx.activeRustEditor;
9         if (!editor) return;
10
11         const request: lc.TextDocumentPositionParams = {
12             textDocument: { uri: editor.document.uri.toString() },
13             position: ctx.client.code2ProtocolConverter.asPosition(
14                 editor.selection.active,
15             ),
16         };
17         const response = await ctx.client.sendRequest<lc.Location[]>(
18             'rust-analyzer/parentModule',
19             request,
20         );
21         const loc = response[0];
22         if (loc == null) return;
23
24         const uri = ctx.client.protocol2CodeConverter.asUri(loc.uri);
25         const range = ctx.client.protocol2CodeConverter.asRange(loc.range);
26
27         const doc = await vscode.workspace.openTextDocument(uri);
28         const e = await vscode.window.showTextDocument(doc);
29         e.selection = new vscode.Selection(range.start, range.start);
30         e.revealRange(range, vscode.TextEditorRevealType.InCenter);
31     }
32 }