diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-02-04 09:52:57 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-02-04 09:52:57 +0000 |
commit | c1a06499fa5f9a40750cbfc30a7d399228a5fab0 (patch) | |
tree | 9f2a049a5d9e49c55f41fd27b1a264d051f044c9 /editors/code | |
parent | 73c36fdbd2060bd455ffeef33dca1ecc2ae07ebb (diff) | |
parent | 875dc6d1a4973f70cd48b797ae755d1bd7a83fa5 (diff) |
Merge #2981
2981: vscode: Add ability to call onEnter without overriding "type". r=matklad a=71
Before this PR, the only way to get enhanced typing (right now, only with `onEnter`) was to override VS Code's `type` command. This leads to issues with extensions like [VsCodeVim](https://github.com/VSCodeVim/Vim) that need to override `type` as well.
This PR adds an additional command, `onEnter`. This command can be used with the following keybinding, which allows the user to get smart `onEnter` behavior without overriding `type`.
```json
{
"key": "enter",
"command": "rust-analyzer.onEnter",
"when": "editorTextFocus && editorLangId == rust"
}
```
Co-authored-by: Gregoire Geis <[email protected]>
Co-authored-by: Grégoire Geis <[email protected]>
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/package.json | 15 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 45 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 24 | ||||
-rw-r--r-- | editors/code/src/main.ts | 4 |
4 files changed, 37 insertions, 51 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index c0d8f0183..c9404a4dd 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": [ |
@@ -136,6 +141,11 @@ | |||
136 | "command": "rust-analyzer.run", | 141 | "command": "rust-analyzer.run", |
137 | "key": "ctrl+r", | 142 | "key": "ctrl+r", |
138 | "when": "editorTextFocus && editorLangId == rust" | 143 | "when": "editorTextFocus && editorLangId == rust" |
144 | }, | ||
145 | { | ||
146 | "command": "rust-analyzer.onEnter", | ||
147 | "key": "enter", | ||
148 | "when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust" | ||
139 | } | 149 | } |
140 | ], | 150 | ], |
141 | "configuration": { | 151 | "configuration": { |
@@ -157,11 +167,6 @@ | |||
157 | "default": {}, | 167 | "default": {}, |
158 | "description": "Fine grained feature flags to disable annoying features" | 168 | "description": "Fine grained feature flags to disable annoying features" |
159 | }, | 169 | }, |
160 | "rust-analyzer.enableEnhancedTyping": { | ||
161 | "type": "boolean", | ||
162 | "default": true, | ||
163 | "description": "Enables enhanced typing. NOTE: If using a VIM extension, you should set this to false" | ||
164 | }, | ||
165 | "rust-analyzer.raLspServerPath": { | 170 | "rust-analyzer.raLspServerPath": { |
166 | "type": [ | 171 | "type": [ |
167 | "string" | 172 | "string" |
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 | } |
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 05d21ae56..aa75943bf 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -50,30 +50,6 @@ export class Ctx { | |||
50 | this.pushCleanup(d); | 50 | this.pushCleanup(d); |
51 | } | 51 | } |
52 | 52 | ||
53 | overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) { | ||
54 | const defaultCmd = `default:${name}`; | ||
55 | const override = factory(this); | ||
56 | const original = (...args: unknown[]) => | ||
57 | vscode.commands.executeCommand(defaultCmd, ...args); | ||
58 | try { | ||
59 | const d = vscode.commands.registerCommand( | ||
60 | name, | ||
61 | async (...args: unknown[]) => { | ||
62 | if (!(await override(...args))) { | ||
63 | return await original(...args); | ||
64 | } | ||
65 | }, | ||
66 | ); | ||
67 | this.pushCleanup(d); | ||
68 | } catch (_) { | ||
69 | vscode.window.showWarningMessage( | ||
70 | 'Enhanced typing feature is disabled because of incompatibility ' + | ||
71 | 'with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: ' + | ||
72 | 'https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings', | ||
73 | ); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | get subscriptions(): Disposable[] { | 53 | get subscriptions(): Disposable[] { |
78 | return this.extCtx.subscriptions; | 54 | return this.extCtx.subscriptions; |
79 | } | 55 | } |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 6813c3c4c..efc31b2e2 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); |
@@ -28,9 +29,6 @@ export async function activate(context: vscode.ExtensionContext) { | |||
28 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | 29 | ctx.registerCommand('applySourceChange', commands.applySourceChange); |
29 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 30 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
30 | 31 | ||
31 | if (ctx.config.enableEnhancedTyping) { | ||
32 | ctx.overrideCommand('type', commands.onEnter); | ||
33 | } | ||
34 | activateStatusDisplay(ctx); | 32 | activateStatusDisplay(ctx); |
35 | 33 | ||
36 | activateHighlighting(ctx); | 34 | activateHighlighting(ctx); |