]> git.lizzy.rs Git - rust.git/blob - editors/code/src/commands/on_enter.ts
Merge #115
[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 interface OnEnterParams {
10     textDocument: lc.TextDocumentIdentifier;
11     position: lc.Position;
12 }
13
14 export async function handle(event: { text: string }): Promise<boolean> {
15     const editor = vscode.window.activeTextEditor;
16     if (
17         editor == null ||
18         editor.document.languageId !== 'rust' ||
19         event.text !== '\n'
20     ) {
21         return false;
22     }
23     const request: OnEnterParams = {
24         textDocument: { uri: editor.document.uri.toString() },
25         position: Server.client.code2ProtocolConverter.asPosition(
26             editor.selection.active
27         )
28     };
29     const change = await Server.client.sendRequest<undefined | SourceChange>(
30         'm/onEnter',
31         request
32     );
33     if (!change) {
34         return false;
35     }
36     await applySourceChange(change);
37     return true;
38 }