From 70438ac5016d3ab609f422d1ef084870cb9ceb29 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Thu, 24 Oct 2024 17:44:29 -0400 Subject: [PATCH] fix: exclude prefix including one char --- lua/blink/cmp/accept/text-edits.lua | 5 ++--- lua/blink/cmp/fuzzy/init.lua | 5 +---- lua/blink/cmp/utils.lua | 20 +++++++++++--------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lua/blink/cmp/accept/text-edits.lua b/lua/blink/cmp/accept/text-edits.lua index 1b589378..f6af37b4 100644 --- a/lua/blink/cmp/accept/text-edits.lua +++ b/lua/blink/cmp/accept/text-edits.lua @@ -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, } diff --git a/lua/blink/cmp/fuzzy/init.lua b/lua/blink/cmp/fuzzy/init.lua index d25affdc..c268065d 100644 --- a/lua/blink/cmp/fuzzy/init.lua +++ b/lua/blink/cmp/fuzzy/init.lua @@ -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 diff --git a/lua/blink/cmp/utils.lua b/lua/blink/cmp/utils.lua index c95d4e20..210734dd 100644 --- a/lua/blink/cmp/utils.lua +++ b/lua/blink/cmp/utils.lua @@ -149,7 +149,7 @@ 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 @@ -157,33 +157,35 @@ function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex) -- 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