diff options
-rw-r--r-- | editors/code/package.json | 5 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 51 | ||||
-rw-r--r-- | editors/code/src/main.ts | 3 |
3 files changed, 40 insertions, 19 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index c0d8f0183..9987a28f5 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -114,6 +114,11 @@ | |||
114 | "command": "rust-analyzer.reload", | 114 | "command": "rust-analyzer.reload", |
115 | "title": "Restart server", | 115 | "title": "Restart server", |
116 | "category": "Rust Analyzer" | 116 | "category": "Rust Analyzer" |
117 | }, | ||
118 | { | ||
119 | "command": "rust-analyzer.onEnter", | ||
120 | "title": "Enhanced enter key", | ||
121 | "category": "Rust Analyzer" | ||
117 | } | 122 | } |
118 | ], | 123 | ], |
119 | "keybindings": [ | 124 | "keybindings": [ |
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 @@ | |||
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) { |
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 | 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 = { | 37 | export 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 | } |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 6813c3c4c..5c061e72f 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
21 | ctx.registerCommand('expandMacro', commands.expandMacro); | 21 | ctx.registerCommand('expandMacro', commands.expandMacro); |
22 | ctx.registerCommand('run', commands.run); | 22 | ctx.registerCommand('run', commands.run); |
23 | ctx.registerCommand('reload', commands.reload); | 23 | ctx.registerCommand('reload', commands.reload); |
24 | ctx.registerCommand('onEnter', commands.onEnter); | ||
24 | 25 | ||
25 | // Internal commands which are invoked by the server. | 26 | // Internal commands which are invoked by the server. |
26 | ctx.registerCommand('runSingle', commands.runSingle); | 27 | ctx.registerCommand('runSingle', commands.runSingle); |
@@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
29 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 30 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
30 | 31 | ||
31 | if (ctx.config.enableEnhancedTyping) { | 32 | if (ctx.config.enableEnhancedTyping) { |
32 | ctx.overrideCommand('type', commands.onEnter); | 33 | ctx.overrideCommand('type', commands.onEnterOverride); |
33 | } | 34 | } |
34 | activateStatusDisplay(ctx); | 35 | activateStatusDisplay(ctx); |
35 | 36 | ||