Skip to content

Commit

Permalink
console: avoid redundant completion appendage
Browse files Browse the repository at this point in the history
So far completing something like `${some-pro}` with the cursor between
`o}` would result in `${some-property}}`. Adding that superfluous `}` can
be avoided by checking if it's already in the string after the cursor.
  • Loading branch information
christoph-heinrich authored and Dudemanguy committed Nov 7, 2023
1 parent 4aa3866 commit 43ed0a8
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions player/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,33 @@ function complete_match(part, list)
return completions, prefix
end

function common_prefix_length(s1, s2)
local common_count = 0
for i = 1, #s1 do
if s1:byte(i) ~= s2:byte(i) then
break
end
common_count = common_count + 1
end
return common_count
end

function max_overlap_length(s1, s2)
for s1_offset = 0, #s1 - 1 do
local match = true
for i = 1, #s1 - s1_offset do
if s1:byte(s1_offset + i) ~= s2:byte(i) then
match = false
break
end
end
if match then
return #s1 - s1_offset
end
end
return 0
end

-- Complete the option or property at the cursor (TAB)
function complete()
local before_cur = line:sub(1, cursor - 1)
Expand Down Expand Up @@ -859,8 +886,15 @@ function complete()
-- 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
if #completions == 1 then
prefix = prefix .. (completer.append or '')
local append = completer.append or ''
prefix = prefix .. append

-- calculate offset into after_cur
local prefix_len = common_prefix_length(append, after_cur)
local overlap_size = max_overlap_length(append, after_cur)
after_cur_index = math.max(prefix_len, overlap_size) + 1
else
table.sort(completions)
suggestion_buffer = completions
Expand All @@ -869,7 +903,7 @@ function complete()
-- Insert the completion and update
before_cur = before_cur:sub(1, s - 1) .. prefix
cursor = before_cur:len() + 1
line = before_cur .. after_cur
line = before_cur .. after_cur:sub(after_cur_index)
update()
return
end
Expand Down

0 comments on commit 43ed0a8

Please sign in to comment.