aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/index.ts
blob: dc075aa8289c61890f5db1fcbed95120e678b191 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

import { Ctx, Cmd } from '../ctx';
import * as sourceChange from '../source_change';

import { analyzerStatus } from './analyzer_status';
import { matchingBrace } from './matching_brace';
import { joinLines } from './join_lines';
import { onEnter } from './on_enter';
import { parentModule } from './parent_module';
import { syntaxTree } from './syntax_tree';
import { expandMacro } from './expand_macro';
import { run, runSingle } from './runnables';

function collectGarbage(ctx: Ctx): Cmd {
    return async () => {
        ctx.client?.sendRequest<null>('rust-analyzer/collectGarbage', null);
    };
}

function showReferences(ctx: Ctx): Cmd {
    return (uri: string, position: lc.Position, locations: lc.Location[]) => {
        let client = ctx.client;
        if (client) {
            vscode.commands.executeCommand(
                'editor.action.showReferences',
                vscode.Uri.parse(uri),
                client.protocol2CodeConverter.asPosition(position),
                locations.map(client.protocol2CodeConverter.asLocation),
            );
        }
    };
}

function applySourceChange(ctx: Ctx): Cmd {
    return async (change: sourceChange.SourceChange) => {
        sourceChange.applySourceChange(ctx, change);
    };
}

function selectAndApplySourceChange(ctx: Ctx): Cmd {
    return async (changes: sourceChange.SourceChange[]) => {
        if (changes.length === 1) {
            await sourceChange.applySourceChange(ctx, changes[0]);
        } else if (changes.length > 0) {
            const selectedChange = await vscode.window.showQuickPick(changes);
            if (!selectedChange) return;
            await sourceChange.applySourceChange(ctx, selectedChange);
        }
    };
}

function reload(ctx: Ctx): Cmd {
    return async () => {
        vscode.window.showInformationMessage('Reloading rust-analyzer...');
        await ctx.restartServer();
    };
}

export {
    analyzerStatus,
    expandMacro,
    joinLines,
    matchingBrace,
    parentModule,
    syntaxTree,
    onEnter,
    collectGarbage,
    run,
    runSingle,
    showReferences,
    applySourceChange,
    selectAndApplySourceChange,
    reload
};