From 23ef22dd4880606c4c3dc908d30c2cbeabc37f58 Mon Sep 17 00:00:00 2001 From: Gregoire Geis Date: Sun, 2 Feb 2020 02:21:04 +0100 Subject: Add regular onEnter command, allowing onEnter to be called without overriding the type command. --- editors/code/src/commands/on_enter.ts | 51 ++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) (limited to 'editors/code/src/commands') 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 @@ +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; + if (event.text === '\n') { + handleKeypress(ctx); + } + }; +} - 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; +export function onEnter(ctx: Ctx): Cmd { + return async () => { + if (handleKeypress(ctx)) return; - await applySourceChange(ctx, change); - return true; + await vscode.commands.executeCommand('default:type', { text: '\n' }); }; } -- cgit v1.2.3