aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/on_enter.ts
blob: efc0dfe1d1a02fe11ba5f85f185e37be6e1bc357 (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
import * as lc from 'vscode-languageclient';
import { applySourceChange, SourceChange } from '../source_change';
import { Cmd, Ctx } from '../ctx';

export function onEnter(ctx: Ctx): Cmd {
    return async (event: { text: string }) => {
        const editor = ctx.activeRustEditor;
        if (!editor || event.text !== '\n') return false;

        const request: lc.TextDocumentPositionParams = {
            textDocument: { uri: editor.document.uri.toString() },
            position: ctx.client.code2ProtocolConverter.asPosition(
                editor.selection.active,
            ),
        };
        const change = await ctx.client.sendRequest<undefined | SourceChange>(
            'rust-analyzer/onEnter',
            request,
        );
        if (!change) return false;

        await applySourceChange(ctx, change);
        return true;
    };
}