Skip to content

Commit 70438ac

Browse files
committed
fix: exclude prefix including one char
1 parent 0b493ff commit 70438ac

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

lua/blink/cmp/accept/text-edits.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ function text_edits.guess_text_edit(item)
8787
-- convert to 0-index
8888
return {
8989
range = {
90-
start = { line = current_line - 1, character = range[1] - 1 },
91-
-- don't - 1 on the end col since it's exclusive while get_regex_around_cursor assumes inclusive
92-
['end'] = { line = current_line - 1, character = range[2] },
90+
start = { line = current_line - 1, character = range.start_col - 1 },
91+
['end'] = { line = current_line - 1, character = range.start_col - 1 + range.length },
9392
},
9493
newText = word,
9594
}

lua/blink/cmp/fuzzy/init.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ function fuzzy.get_query()
6565
cmp_config.keyword_regex,
6666
cmp_config.exclude_from_prefix_regex
6767
)
68-
-- Since sub(1, 1) returns a single char string, we need to check if that single char matches
69-
-- and otherwise return an empty string
70-
if range[1] == range[2] and line:sub(range[1], range[1]):match(cmp_config.keyword_regex) == nil then return '' end
71-
return string.sub(line, range[1], range[2])
68+
return string.sub(line, range.start_col, range.start_col + range.length - 1)
7269
end
7370

7471
return fuzzy

lua/blink/cmp/utils.lua

+11-9
Original file line numberDiff line numberDiff line change
@@ -149,41 +149,43 @@ end
149149
--- @param range 'prefix' | 'full'
150150
--- @param regex string
151151
--- @param exclude_from_prefix_regex string
152-
--- @return number[]
152+
--- @return { start_col: number, length: number }
153153
--- TODO: switch to return start_col, length to simplify downstream logic
154154
function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex)
155155
local current_col = vim.api.nvim_win_get_cursor(0)[2] + 1
156156
local line = vim.api.nvim_get_current_line()
157157

158158
-- Search backward for the start of the word
159159
local start_col = current_col
160+
local length = 0
160161
while start_col > 0 do
161162
local char = line:sub(start_col - 1, start_col - 1)
162163
if char:match(regex) == nil then break end
163164
start_col = start_col - 1
165+
length = length + 1
164166
end
165167

166-
local end_col = current_col - 1
167-
168168
-- Search forward for the end of the word if configured
169169
if range == 'full' then
170-
while end_col < #line do
171-
local char = line:sub(end_col, end_col)
170+
while start_col + length < #line do
171+
local col = start_col + length
172+
local char = line:sub(col, col)
172173
if char:match(regex) == nil then break end
173-
end_col = end_col + 1
174+
length = length + 1
174175
end
175176
end
176177

177178
-- exclude characters matching exclude_prefix_regex from the beginning of the bounds
178179
if exclude_from_prefix_regex ~= nil then
179-
while start_col <= end_col do
180-
local char = line:sub(start_col + 1, start_col + 1)
180+
while length > 0 do
181+
local char = line:sub(start_col, start_col)
181182
if char:match(exclude_from_prefix_regex) == nil then break end
182183
start_col = start_col + 1
184+
length = length - 1
183185
end
184186
end
185187

186-
return { start_col, end_col }
188+
return { start_col = start_col, length = length }
187189
end
188190

189191
return utils

0 commit comments

Comments
 (0)