aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
authorGregoire Geis <[email protected]>2020-02-02 01:21:04 +0000
committerGregoire Geis <[email protected]>2020-02-03 19:18:10 +0000
commit23ef22dd4880606c4c3dc908d30c2cbeabc37f58 (patch)
tree7b68d0cce259e3e945223b478d21b5d444d9e6c6 /editors/code/src/commands
parentb090ee5a65f9630146c2842bc51fcfcc8da08da1 (diff)
Add regular onEnter command, allowing onEnter to be called without overriding the type command.
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/on_enter.ts51
1 files changed, 33 insertions, 18 deletions
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts
index 6f61883cd..1b3d3d741 100644
--- a/editors/code/src/commands/on_enter.ts
+++ b/editors/code/src/commands/on_enter.ts
@@ -1,28 +1,43 @@
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) {
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
29export function onEnterOverride(ctx: Ctx): Cmd {
7 return async (event: { text: string }) => { 30 return async (event: { text: string }) => {
8 const editor = ctx.activeRustEditor; 31 if (event.text === '\n') {
9 const client = ctx.client; 32 handleKeypress(ctx);
10 if (!editor || event.text !== '\n') return false; 33 }
11 if (!client) return false; 34 };
35}
12 36
13 const request: lc.TextDocumentPositionParams = { 37export function onEnter(ctx: Ctx): Cmd {
14 textDocument: { uri: editor.document.uri.toString() }, 38 return async () => {
15 position: client.code2ProtocolConverter.asPosition( 39 if (handleKeypress(ctx)) return;
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 40
25 await applySourceChange(ctx, change); 41 await vscode.commands.executeCommand('default:type', { text: '\n' });
26 return true;
27 }; 42 };
28} 43}