Skip to content

Commit 569156f

Browse files
committed
feat: ignore some characters at prefix of keyword
closes #135
1 parent 6e15864 commit 569156f

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,16 @@ MiniDeps.add({
182182

183183
trigger = {
184184
completion = {
185+
-- 'prefix' will fuzzy match on the text before the cursor
186+
-- 'full' will fuzzy match on the text before *and* after the cursor
187+
-- example: 'foo_|_bar' will match 'foo_' for 'prefix' and 'foo__bar' for 'full'
188+
keyword_range = 'prefix',
185189
-- regex used to get the text when fuzzy matching
186190
-- changing this may break some sources, so please report if you run into issues
187191
-- todo: shouldnt this also affect the accept command? should this also be per language?
188192
keyword_regex = '[%w_\\-]',
193+
-- after matching with keyword_regex, any characters matching this regex at the prefix will be excluded
194+
exclude_from_prefix_regex = '[\\-]',
189195
-- LSPs can indicate when to show the completion window via trigger characters
190196
-- however, some LSPs (*cough* tsserver *cough*) return characters that would essentially
191197
-- always show the window. We block these by default
@@ -206,10 +212,6 @@ MiniDeps.add({
206212
},
207213

208214
fuzzy = {
209-
-- 'prefix' will fuzzy match on the text before the cursor
210-
-- 'full' will fuzzy match on the text before *and* after the cursor
211-
-- example: 'foo_|_bar' will match 'foo_' for 'prefix' and 'foo__bar' for 'full'
212-
keyword_range = 'prefix',
213215
-- frencency tracks the most recently/frequently used items and boosts the score of the item
214216
use_frecency = true,
215217
-- proximity bonus boosts the score of items with a value in the buffer

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ end
6161
function text_edits.guess_text_edit(item)
6262
local word = item.textEditText or item.insertText or item.label
6363

64-
local range = require('blink.cmp.utils').get_regex_around_cursor('[%w_\\-]', config.fuzzy.keyword_range)
64+
local cmp_config = config.trigger.completion
65+
local range = require('blink.cmp.utils').get_regex_around_cursor(
66+
cmp_config.keyword_range,
67+
cmp_config.keyword_regex,
68+
cmp_config.exclude_from_prefix_regex
69+
)
6570
local current_line = vim.api.nvim_win_get_cursor(0)[1]
6671

6772
-- convert to 0-index

lua/blink/cmp/config.lua

+8-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
--- @field timeout_ms? number How long to wait for semantic tokens to return before assuming no brackets should be added
3232

3333
--- @class blink.cmp.CompletionTriggerConfig
34+
--- @field keyword_range? 'prefix' | 'full'
3435
--- @field keyword_regex? string
36+
--- @field exclude_from_prefix_regex? string
3537
--- @field blocked_trigger_characters? string[]
3638
--- @field show_on_insert_on_trigger_character? boolean When true, will show the completion window when the cursor comes after a trigger character when entering insert mode
3739
--- @field show_on_insert_blocked_trigger_characters? string[] List of additional trigger characters that won't trigger the completion window when the cursor comes after a trigger character when entering insert mode
@@ -68,7 +70,6 @@
6870
--- @field forceVersion? string | nil
6971

7072
--- @class blink.cmp.FuzzyConfig
71-
--- @field keyword_range? 'prefix' | 'full'
7273
--- @field use_frecency? boolean
7374
--- @field use_proximity? boolean
7475
--- @field max_items? number
@@ -184,10 +185,16 @@ local config = {
184185

185186
trigger = {
186187
completion = {
188+
-- 'prefix' will fuzzy match on the text before the cursor
189+
-- 'full' will fuzzy match on the text before *and* after the cursor
190+
-- example: 'foo_|_bar' will match 'foo_' for 'prefix' and 'foo__bar' for 'full'
191+
keyword_range = 'prefix',
187192
-- regex used to get the text when fuzzy matching
188193
-- changing this may break some sources, so please report if you run into issues
189194
-- todo: shouldnt this also affect the accept command? should this also be per language?
190195
keyword_regex = '[%w_\\-]',
196+
-- after matching with keyword_regex, any characters matching this regex at the prefix will be excluded
197+
exclude_from_prefix_regex = '[\\-]',
191198
-- LSPs can indicate when to show the completion window via trigger characters
192199
-- however, some LSPs (*cough* tsserver *cough*) return characters that would essentially
193200
-- always show the window. We block these by default
@@ -208,10 +215,6 @@ local config = {
208215
},
209216

210217
fuzzy = {
211-
-- 'prefix' will fuzzy match on the text before the cursor
212-
-- 'full' will fuzzy match on the text before *and* after the cursor
213-
-- example: 'foo_|_bar' will match 'foo_' for 'prefix' and 'foo__bar' for 'full'
214-
keyword_range = 'prefix',
215218
-- frencency tracks the most recently/frequently used items and boosts the score of the item
216219
use_frecency = true,
217220
-- proximity bonus boosts the score of items with a value in the buffer

lua/blink/cmp/fuzzy/init.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ end
9191
--- @return string
9292
function fuzzy.get_query()
9393
local line = vim.api.nvim_get_current_line()
94+
local cmp_config = config.trigger.completion
9495
local range = require('blink.cmp.utils').get_regex_around_cursor(
95-
config.trigger.completion.keyword_regex,
96-
config.fuzzy.keyword_range
96+
cmp_config.keyword_range,
97+
cmp_config.keyword_regex,
98+
cmp_config.exclude_from_prefix_regex
9799
)
100+
vim.print(range)
98101
return string.sub(line, range[1] + 1, range[2] + 1)
99102
end
100103

lua/blink/cmp/utils.lua

+20-9
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ function utils.highlight_with_treesitter(bufnr, filetype, start_line, end_line)
136136
end
137137

138138
--- Gets characters around the cursor and returns the range, 0-indexed
139-
--- @param regex string
140139
--- @param range 'prefix' | 'full'
140+
--- @param regex string
141+
--- @param exclude_from_prefix_regex string
141142
--- @return number[]
142-
function utils.get_regex_around_cursor(regex, range)
143+
function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex)
143144
local current_col = vim.api.nvim_win_get_cursor(0)[2]
144145
local line = vim.api.nvim_get_current_line()
145146

@@ -151,14 +152,24 @@ function utils.get_regex_around_cursor(regex, range)
151152
start_col = start_col - 1
152153
end
153154

154-
if range == 'prefix' then return { start_col, current_col } end
155-
156-
-- Search forward for the end of the word
157155
local end_col = current_col
158-
while end_col < #line do
159-
local char = line:sub(end_col + 1, end_col + 1)
160-
if char:match(regex) == nil then break end
161-
end_col = end_col + 1
156+
157+
-- Search forward for the end of the word if configured
158+
if range == 'full' then
159+
while end_col < #line do
160+
local char = line:sub(end_col + 1, end_col + 1)
161+
if char:match(regex) == nil then break end
162+
end_col = end_col + 1
163+
end
164+
end
165+
166+
-- exclude characters matching exclude_prefix_regex from the beginning of the bounds
167+
if exclude_from_prefix_regex ~= nil then
168+
while start_col <= end_col do
169+
local char = line:sub(start_col + 1, start_col + 1)
170+
if char:match(exclude_from_prefix_regex) == nil then break end
171+
start_col = start_col + 1
172+
end
162173
end
163174

164175
return { start_col, end_col }

0 commit comments

Comments
 (0)