aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/on_enter.ts
blob: a7871c31eed0bde9d209833eb2e9ff82b06df673 (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
import * as vscode from 'vscode';
import * as ra from '../rust-analyzer-api';

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

async function handleKeypress(ctx: Ctx) {
    const editor = ctx.activeRustEditor;
    const client = ctx.client;

    if (!editor || !client) return false;

    const change = await client.sendRequest(ra.onEnter, {
        textDocument: { uri: editor.document.uri.toString() },
        position: client.code2ProtocolConverter.asPosition(
            editor.selection.active,
        ),
    }).catch(_error => {
        // client.logFailedRequest(OnEnterRequest.type, error);
        return null;
    });
    if (!change) return false;

    const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change);
    await applySnippetWorkspaceEdit(workspaceEdit);
    return true;
}

export function onEnter(ctx: Ctx): Cmd {
    return async () => {
        if (await handleKeypress(ctx)) return;

        await vscode.commands.executeCommand('default:type', { text: '\n' });
    };
}