]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/on_enter.ts
Merge #3299
[rust.git] / editors / code / src / commands / on_enter.ts
1 import * as vscode from 'vscode';
2 import * as ra from '../rust-analyzer-api';
3
4 import { applySourceChange } 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 change = await client.sendRequest(ra.onEnter, {
14         textDocument: { uri: editor.document.uri.toString() },
15         position: client.code2ProtocolConverter.asPosition(
16             editor.selection.active,
17         ),
18     }).catch(_error => {
19         // client.logFailedRequest(OnEnterRequest.type, error);
20         return null;
21     });
22     if (!change) return false;
23
24     await applySourceChange(ctx, change);
25     return true;
26 }
27
28 export function onEnter(ctx: Ctx): Cmd {
29     return async () => {
30         if (await handleKeypress(ctx)) return;
31
32         await vscode.commands.executeCommand('default:type', { text: '\n' });
33     };
34 }