aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/on_enter.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/commands/on_enter.ts')
-rw-r--r--editors/code/src/commands/on_enter.ts45
1 files changed, 26 insertions, 19 deletions
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
index 6f61883cd..c636234da 100644
--- a/editors/code/src/commands/on_enter.ts
+++ b/editors/code/src/commands/on_enter.ts
@@ -1,28 +1,35 @@
1import * as vscode from 'vscode';
1import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
2 3
3import { applySourceChange, SourceChange } from '../source_change'; 4import { applySourceChange, SourceChange } from '../source_change';
4import { Cmd, Ctx } from '../ctx'; 5import { Cmd, Ctx } from '../ctx';
5 6
6export function onEnter(ctx: Ctx): Cmd { 7async function handleKeypress(ctx: Ctx) {
7 return async (event: { text: string }) => { 8 const editor = ctx.activeRustEditor;
8 const editor = ctx.activeRustEditor; 9 const client = ctx.client;
9 const client = ctx.client; 10 if (!editor) return false;
10 if (!editor || event.text !== '\n') return false; 11 if (!editor || !client) return false;
11 if (!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;
12 24
13 const request: lc.TextDocumentPositionParams = { 25 await applySourceChange(ctx, change);
14 textDocument: { uri: editor.document.uri.toString() }, 26 return true;
15 position: client.code2ProtocolConverter.asPosition( 27}
16 editor.selection.active, 28
17 ), 29export function onEnter(ctx: Ctx): Cmd {
18 }; 30 return async () => {
19 const change = await client.sendRequest<undefined | SourceChange>( 31 if (await handleKeypress(ctx)) return;
20 'rust-analyzer/onEnter',
21 request,
22 );
23 if (!change) return false;
24 32
25 await applySourceChange(ctx, change); 33 await vscode.commands.executeCommand('default:type', { text: '\n' });
26 return true;
27 }; 34 };
28} 35}