]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/on_enter.ts
Merge #2514
[rust.git] / editors / code / src / commands / on_enter.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3 import { Server } from '../server';
4 import {
5     handle as applySourceChange,
6     SourceChange,
7 } from './apply_source_change';
8
9 export async function handle(event: { text: string }): Promise<boolean> {
10     const editor = vscode.window.activeTextEditor;
11     if (
12         editor == null ||
13         editor.document.languageId !== 'rust' ||
14         event.text !== '\n'
15     ) {
16         return false;
17     }
18     const request: lc.TextDocumentPositionParams = {
19         textDocument: { uri: editor.document.uri.toString() },
20         position: Server.client.code2ProtocolConverter.asPosition(
21             editor.selection.active,
22         ),
23     };
24     const change = await Server.client.sendRequest<undefined | SourceChange>(
25         'rust-analyzer/onEnter',
26         request,
27     );
28     if (!change) {
29         return false;
30     }
31     await applySourceChange(change);
32     return true;
33 }