diff --git a/editors/code/package.json b/editors/code/package.json index c0d8f0183b..9987a28f57 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -114,6 +114,11 @@ "command": "rust-analyzer.reload", "title": "Restart server", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.onEnter", + "title": "Enhanced enter key", + "category": "Rust Analyzer" } ], "keybindings": [ diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 6f61883cdd..1b3d3d741c 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,28 +1,43 @@ +import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import { applySourceChange, SourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; -export function onEnter(ctx: Ctx): Cmd { +async function handleKeypress(ctx: Ctx) { + const editor = ctx.activeRustEditor; + const client = ctx.client; + if (!editor) return false; + if (!client) return false; + + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const change = await client.sendRequest( + 'rust-analyzer/onEnter', + request, + ); + if (!change) return false; + + await applySourceChange(ctx, change); + return true; +} + +export function onEnterOverride(ctx: Ctx): Cmd { return async (event: { text: string }) => { - const editor = ctx.activeRustEditor; - const client = ctx.client; - if (!editor || event.text !== '\n') return false; - if (!client) return false; - - const request: lc.TextDocumentPositionParams = { - textDocument: { uri: editor.document.uri.toString() }, - position: client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const change = await client.sendRequest( - 'rust-analyzer/onEnter', - request, - ); - if (!change) return false; - - await applySourceChange(ctx, change); - return true; + if (event.text === '\n') { + handleKeypress(ctx); + } + }; +} + +export function onEnter(ctx: Ctx): Cmd { + return async () => { + if (handleKeypress(ctx)) return; + + await vscode.commands.executeCommand('default:type', { text: '\n' }); }; } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 6813c3c4cc..5c061e72fd 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('expandMacro', commands.expandMacro); ctx.registerCommand('run', commands.run); ctx.registerCommand('reload', commands.reload); + ctx.registerCommand('onEnter', commands.onEnter); // Internal commands which are invoked by the server. ctx.registerCommand('runSingle', commands.runSingle); @@ -29,7 +30,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); if (ctx.config.enableEnhancedTyping) { - ctx.overrideCommand('type', commands.onEnter); + ctx.overrideCommand('type', commands.onEnterOverride); } activateStatusDisplay(ctx);