Skip to content

Commit

Permalink
console.lua: don't reinsert completion_append after cycling
Browse files Browse the repository at this point in the history
43ed0a8 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.
  • Loading branch information
guidocella authored and sfan5 committed Jan 1, 2024
1 parent abc2a74 commit 731378d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 731378d

Please sign in to comment.