Skip to content

Commit

Permalink
fix: exclude prefix including one char
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 24, 2024
1 parent 0b493ff commit 70438ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
5 changes: 2 additions & 3 deletions lua/blink/cmp/accept/text-edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ function text_edits.guess_text_edit(item)
-- convert to 0-index
return {
range = {
start = { line = current_line - 1, character = range[1] - 1 },
-- don't - 1 on the end col since it's exclusive while get_regex_around_cursor assumes inclusive
['end'] = { line = current_line - 1, character = range[2] },
start = { line = current_line - 1, character = range.start_col - 1 },
['end'] = { line = current_line - 1, character = range.start_col - 1 + range.length },
},
newText = word,
}
Expand Down
5 changes: 1 addition & 4 deletions lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ function fuzzy.get_query()
cmp_config.keyword_regex,
cmp_config.exclude_from_prefix_regex
)
-- Since sub(1, 1) returns a single char string, we need to check if that single char matches
-- and otherwise return an empty string
if range[1] == range[2] and line:sub(range[1], range[1]):match(cmp_config.keyword_regex) == nil then return '' end
return string.sub(line, range[1], range[2])
return string.sub(line, range.start_col, range.start_col + range.length - 1)
end

return fuzzy
20 changes: 11 additions & 9 deletions lua/blink/cmp/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,43 @@ end
--- @param range 'prefix' | 'full'
--- @param regex string
--- @param exclude_from_prefix_regex string
--- @return number[]
--- @return { start_col: number, length: number }
--- TODO: switch to return start_col, length to simplify downstream logic
function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex)
local current_col = vim.api.nvim_win_get_cursor(0)[2] + 1
local line = vim.api.nvim_get_current_line()

-- Search backward for the start of the word
local start_col = current_col
local length = 0
while start_col > 0 do
local char = line:sub(start_col - 1, start_col - 1)
if char:match(regex) == nil then break end
start_col = start_col - 1
length = length + 1
end

local end_col = current_col - 1

-- Search forward for the end of the word if configured
if range == 'full' then
while end_col < #line do
local char = line:sub(end_col, end_col)
while start_col + length < #line do
local col = start_col + length
local char = line:sub(col, col)
if char:match(regex) == nil then break end
end_col = end_col + 1
length = length + 1
end
end

-- exclude characters matching exclude_prefix_regex from the beginning of the bounds
if exclude_from_prefix_regex ~= nil then
while start_col <= end_col do
local char = line:sub(start_col + 1, start_col + 1)
while length > 0 do
local char = line:sub(start_col, start_col)
if char:match(exclude_from_prefix_regex) == nil then break end
start_col = start_col + 1
length = length - 1
end
end

return { start_col, end_col }
return { start_col = start_col, length = length }
end

return utils

0 comments on commit 70438ac

Please sign in to comment.