diff options
Diffstat (limited to 'editors/code/src/commands/on_enter.ts')
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 45 |
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 @@ | |||
1 | import * as vscode from 'vscode'; | ||
1 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
2 | 3 | ||
3 | import { applySourceChange, SourceChange } from '../source_change'; | 4 | import { applySourceChange, SourceChange } from '../source_change'; |
4 | import { Cmd, Ctx } from '../ctx'; | 5 | import { Cmd, Ctx } from '../ctx'; |
5 | 6 | ||
6 | export function onEnter(ctx: Ctx): Cmd { | 7 | async 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 | ), | 29 | export 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 | } |