|
| 1 | +import * as vscode from 'vscode'; |
1 | 2 | import * as lc from 'vscode-languageclient';
|
2 | 3 |
|
3 | 4 | import { applySourceChange, SourceChange } from '../source_change';
|
4 | 5 | import { Cmd, Ctx } from '../ctx';
|
5 | 6 |
|
6 |
| -export function onEnter(ctx: Ctx): Cmd { |
| 7 | +async function handleKeypress(ctx: Ctx) { |
| 8 | + const editor = ctx.activeRustEditor; |
| 9 | + const client = ctx.client; |
| 10 | + if (!editor) 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; |
| 24 | + |
| 25 | + await applySourceChange(ctx, change); |
| 26 | + return true; |
| 27 | +} |
| 28 | + |
| 29 | +export function onEnterOverride(ctx: Ctx): Cmd { |
7 | 30 | return async (event: { text: string }) => {
|
8 |
| - const editor = ctx.activeRustEditor; |
9 |
| - const client = ctx.client; |
10 |
| - if (!editor || event.text !== '\n') return false; |
11 |
| - if (!client) return false; |
| 31 | + if (event.text === '\n') { |
| 32 | + handleKeypress(ctx); |
| 33 | + } |
| 34 | + }; |
| 35 | +} |
12 | 36 |
|
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; |
| 37 | +export function onEnter(ctx: Ctx): Cmd { |
| 38 | + return async () => { |
| 39 | + if (handleKeypress(ctx)) return; |
24 | 40 |
|
25 |
| - await applySourceChange(ctx, change); |
26 |
| - return true; |
| 41 | + await vscode.commands.executeCommand('default:type', { text: '\n' }); |
27 | 42 | };
|
28 | 43 | }
|
0 commit comments