From 731378d1bb566852a9a6f83f8f183462991eb573 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Mon, 25 Dec 2023 16:50:14 +0100 Subject: [PATCH] console.lua: don't reinsert completion_append after cycling 43ed0a83d0 avoided reinserting the string that is appended after certain completions when it is already after the cursor when inserting the longest common prefix of the suggestions. Do the same when cycling through them. --- player/lua/console.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/player/lua/console.lua b/player/lua/console.lua index 767abc9dd2..9a7ecc0821 100644 --- a/player/lua/console.lua +++ b/player/lua/console.lua @@ -994,6 +994,13 @@ function max_overlap_length(s1, s2) return 0 end +-- If str starts with the first or last characters of prefix, strip them. +local function strip_common_characters(str, prefix) + return str:sub(1 + math.max( + common_prefix_length(prefix, str), + max_overlap_length(prefix, str))) +end + local function cycle_through_suggestions(backwards) selected_suggestion_index = selected_suggestion_index + (backwards and -1 or 1) @@ -1005,7 +1012,7 @@ local function cycle_through_suggestions(backwards) local before_cur = line:sub(1, completion_start_position - 1) .. suggestion_buffer[selected_suggestion_index] .. completion_append - line = before_cur .. line:sub(cursor) + line = before_cur .. strip_common_characters(line:sub(cursor), completion_append) cursor = before_cur:len() + 1 update() end @@ -1049,15 +1056,10 @@ function complete(backwards) -- If there was only one full match from the list, add -- completer.append to the final string. This is normally a -- space or a quotation mark followed by a space. - local after_cur_index = 1 completion_append = completer.append or '' if #completions == 1 then prefix = prefix .. completion_append - - -- calculate offset into after_cur - local prefix_len = common_prefix_length(completion_append, after_cur) - local overlap_size = max_overlap_length(completion_append, after_cur) - after_cur_index = math.max(prefix_len, overlap_size) + 1 + after_cur = strip_common_characters(after_cur, completion_append) else table.sort(completions) suggestion_buffer = completions @@ -1068,7 +1070,7 @@ function complete(backwards) before_cur = before_cur:sub(1, completion_start_position - 1) .. prefix cursor = before_cur:len() + 1 - line = before_cur .. after_cur:sub(after_cur_index) + line = before_cur .. after_cur update() return end