From b30c68fa512cd633afeb59460ad64239948de7d6 Mon Sep 17 00:00:00 2001 From: glacambre Date: Tue, 6 Nov 2018 02:19:34 +0100 Subject: [PATCH] Fix commandline failing to insert spaces in middle of words Before this commit, it was impossible to insert a space in the middle of a word in the command line ; the space would always be inserted at its end. --- src/commandline_frame.ts | 26 +++++++++++++++++++++++--- src/excmds.ts | 1 + src/lib/config.ts | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 074fb6bf..191153b4 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -164,6 +164,7 @@ export function complete() { let matches = state.cmdHistory.filter(key => key.startsWith(fragment)) let mostrecent = matches[matches.length - 1] if (mostrecent != undefined) clInput.value = mostrecent + clInput.dispatchEvent(new Event("input")) // dirty hack for completions } /** @@ -182,13 +183,31 @@ export function prev_completion() { /** * Inserts the currently selected completion and a space in the command line. - * If no completion option is selected, inserts a space in the command line. */ export function insert_completion() { const command = getCompletion() activeCompletions.forEach(comp => (comp.completion = undefined)) - if (command) clInput.value = command - clInput.value += " " + if (command) { + clInput.value = command + " " + clInput.dispatchEvent(new Event("input")) // dirty hack for completions + } +} + +/** + * If a completion is selected, inserts it in the command line with a space. + * If no completion is selected, inserts a space where the caret is. + */ +export function insert_space_or_completion() { + const command = getCompletion() + activeCompletions.forEach(comp => (comp.completion = undefined)) + if (command) { + clInput.value = command + " " + } else { + const selectionStart = clInput.selectionStart + const selectionEnd = clInput.selectionEnd + clInput.value = clInput.value.substring(0, selectionStart) + " " + clInput.value.substring(selectionEnd) + clInput.selectionStart = clInput.selectionEnd = selectionStart + 1 + } clInput.dispatchEvent(new Event("input")) // dirty hack for completions } @@ -401,6 +420,7 @@ export function getContent() { export function editor_function(fn_name) { if (tri_editor[fn_name]) { tri_editor[fn_name](clInput) + clInput.dispatchEvent(new Event("input")) // dirty hack for completions } else { // The user is using the command line so we can't log message there // logger.error(`No editor function named ${fn_name}!`) diff --git a/src/excmds.ts b/src/excmds.ts index 85088e01..1c10fd03 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -2433,6 +2433,7 @@ const cmdframe_fns: { [key: string]: [string, any[]] } = { next_completion: ["next_completion", []], prev_completion: ["prev_completion", []], insert_completion: ["insert_completion", []], + insert_space_or_completion: ["insert_space_or_completion", []], complete: ["complete", []], hide_and_clear: ["hide_and_clear", []], } diff --git a/src/lib/config.ts b/src/lib/config.ts index b79da75c..5fe88bc3 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -86,7 +86,7 @@ class default_config { "": "ex.complete", "": "ex.next_completion", "": "ex.prev_completion", - "": "ex.insert_completion", + "": "ex.insert_space_or_completion", } /**