]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/on_enter.ts
Merge branch 'master' into kdelorey/complete-trait-impl
[rust.git] / editors / code / src / commands / on_enter.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3
4 import { applySourceChange, SourceChange } from '../source_change';
5 import { Cmd, Ctx } from '../ctx';
6
7 async function handleKeypress(ctx: Ctx) {
8     const editor = ctx.activeRustEditor;
9     const client = ctx.client;
10
11     if (!editor || !client) return false;
12
13     const request: lc.TextDocumentPositionParams = {
14         textDocument: { uri: editor.document.uri.toString() },
15         position: client.code2ProtocolConverter.asPosition(
16             editor.selection.active,
17         ),
18     };
19     const change = await client.sendRequest<undefined | SourceChange>(
20         'rust-analyzer/onEnter',
21         request,
22     );
23     if (!change) return false;
24
25     await applySourceChange(ctx, change);
26     return true;
27 }
28
29 export function onEnter(ctx: Ctx): Cmd {
30     return async () => {
31         if (await handleKeypress(ctx)) return;
32
33         await vscode.commands.executeCommand('default:type', { text: '\n' });
34     };
35 }