Skip to content

Commit 23ef22d

Browse files
committed
Add regular onEnter command, allowing onEnter to be called without overriding the type command.
1 parent b090ee5 commit 23ef22d

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

editors/code/package.json

Lines changed: 5 additions & 0 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": [

editors/code/src/commands/on_enter.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
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+
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 {
730
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+
}
1236

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;
2440

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

editors/code/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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);
@@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) {
2930
ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
3031

3132
if (ctx.config.enableEnhancedTyping) {
32-
ctx.overrideCommand('type', commands.onEnter);
33+
ctx.overrideCommand('type', commands.onEnterOverride);
3334
}
3435
activateStatusDisplay(ctx);
3536

0 commit comments

Comments
 (0)