aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/parent_module.ts
blob: 258b61b21fe540265cdba243c9c3d8420a2b2845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

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

export function parentModule(ctx: Ctx): Cmd {
    return async () => {
        const editor = ctx.activeRustEditor;
        if (!editor) return;

        const request: lc.TextDocumentPositionParams = {
            textDocument: { uri: editor.document.uri.toString() },
            position: ctx.client.code2ProtocolConverter.asPosition(
                editor.selection.active,
            ),
        };
        const response = await ctx.client.sendRequest<lc.Location[]>(
            'rust-analyzer/parentModule',
            request,
        );
        const loc = response[0];
        if (loc == null) return;

        const uri = ctx.client.protocol2CodeConverter.asUri(loc.uri);
        const range = ctx.client.protocol2CodeConverter.asRange(loc.range);

        const doc = await vscode.workspace.openTextDocument(uri);
        const e = await vscode.window.showTextDocument(doc);
        e.selection = new vscode.Selection(range.start, range.start);
        e.revealRange(range, vscode.TextEditorRevealType.InCenter);
    };
}