Skip to content

Commit c1a0649

Browse files
bors[bot]71
andauthored
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]>
2 parents 73c36fd + 875dc6d commit c1a0649

File tree

4 files changed

+37
-51
lines changed

4 files changed

+37
-51
lines changed

editors/code/package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
"command": "rust-analyzer.reload",
115115
"title": "Restart server",
116116
"category": "Rust Analyzer"
117+
},
118+
{
119+
"command": "rust-analyzer.onEnter",
120+
"title": "Enhanced enter key",
121+
"category": "Rust Analyzer"
117122
}
118123
],
119124
"keybindings": [
@@ -136,6 +141,11 @@
136141
"command": "rust-analyzer.run",
137142
"key": "ctrl+r",
138143
"when": "editorTextFocus && editorLangId == rust"
144+
},
145+
{
146+
"command": "rust-analyzer.onEnter",
147+
"key": "enter",
148+
"when": "editorTextFocus && !suggestWidgetVisible && editorLangId == rust"
139149
}
140150
],
141151
"configuration": {
@@ -157,11 +167,6 @@
157167
"default": {},
158168
"description": "Fine grained feature flags to disable annoying features"
159169
},
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-
},
165170
"rust-analyzer.raLspServerPath": {
166171
"type": [
167172
"string"

editors/code/src/commands/on_enter.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1+
import * as vscode from 'vscode';
12
import * as lc from 'vscode-languageclient';
23

34
import { applySourceChange, SourceChange } from '../source_change';
45
import { Cmd, Ctx } from '../ctx';
56

6-
export function onEnter(ctx: Ctx): Cmd {
7-
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;
7+
async function handleKeypress(ctx: Ctx) {
8+
const editor = ctx.activeRustEditor;
9+
const client = ctx.client;
10+
if (!editor) return false;
11+
if (!editor || !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;
1224

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;
25+
await applySourceChange(ctx, change);
26+
return true;
27+
}
28+
29+
export function onEnter(ctx: Ctx): Cmd {
30+
return async () => {
31+
if (await handleKeypress(ctx)) return;
2432

25-
await applySourceChange(ctx, change);
26-
return true;
33+
await vscode.commands.executeCommand('default:type', { text: '\n' });
2734
};
2835
}

editors/code/src/ctx.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,6 @@ export class Ctx {
5050
this.pushCleanup(d);
5151
}
5252

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-
7753
get subscriptions(): Disposable[] {
7854
return this.extCtx.subscriptions;
7955
}

editors/code/src/main.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ export async function activate(context: vscode.ExtensionContext) {
2121
ctx.registerCommand('expandMacro', commands.expandMacro);
2222
ctx.registerCommand('run', commands.run);
2323
ctx.registerCommand('reload', commands.reload);
24+
ctx.registerCommand('onEnter', commands.onEnter);
2425

2526
// Internal commands which are invoked by the server.
2627
ctx.registerCommand('runSingle', commands.runSingle);
2728
ctx.registerCommand('showReferences', commands.showReferences);
2829
ctx.registerCommand('applySourceChange', commands.applySourceChange);
2930
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
3031

31-
if (ctx.config.enableEnhancedTyping) {
32-
ctx.overrideCommand('type', commands.onEnter);
33-
}
3432
activateStatusDisplay(ctx);
3533

3634
activateHighlighting(ctx);

0 commit comments

Comments
 (0)