Skip to content

Commit 4cad01e

Browse files
author
Sebastian Flügge
committed
fix: fuzzy autocompletion with blink
1 parent 468570f commit 4cad01e

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lua/orgmode/org/autocompletion/blink.lua

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,34 @@ end
1717
function Source:get_completions(ctx, callback)
1818
local line = ctx.line:sub(1, ctx.cursor[2])
1919
local offset = org.completion:get_start({ line = line }) + 1
20-
local base = string.sub(line, offset)
20+
local full_base = string.sub(line, offset)
21+
22+
-- Create a simplified base that preserves completion context but avoids over-filtering
23+
local simplified_base = full_base
24+
25+
-- For file links, keep only the protocol part to preserve context
26+
if full_base:match('^file:') then
27+
simplified_base = 'file:'
28+
elseif full_base:match('^~/') then
29+
simplified_base = '~/'
30+
elseif full_base:match('^%./') then
31+
simplified_base = './'
32+
elseif full_base:match('^/') then
33+
simplified_base = '/'
34+
-- For other contexts, use a minimal base to get all results
35+
elseif full_base:match('^%*') then
36+
simplified_base = '*'
37+
elseif full_base:match('^#%+') then
38+
simplified_base = '#+'
39+
elseif full_base:match('^:') then
40+
simplified_base = ':'
41+
end
42+
43+
-- Pass simplified base to orgmode sources to preserve context but get more results
2144
local results = org.completion:complete({
2245
line = line,
23-
base = base,
46+
base = simplified_base,
47+
framework = 'blink', -- Still signal framework for any remaining filtering
2448
})
2549

2650
local cb = function(items)
@@ -55,14 +79,16 @@ function Source:get_completions(ctx, callback)
5579
return 0
5680
end
5781

58-
local baseOffset = getInsertTextOffset(base)
82+
-- Use full_base for insertText calculation
83+
local baseOffset = getInsertTextOffset(full_base)
5984
local insertTextOffset = baseOffset > 0 and math.max(2, baseOffset) or 0
6085

6186
local items = {}
6287

6388
for _, item in ipairs(results) do
6489
table.insert(items, {
6590
label = item.word,
91+
filterText = item.word, -- Text to fuzzy match against
6692
insertText = insertTextOffset > 0 and item.word:sub(insertTextOffset) or item.word,
6793
labelDetails = item.menu and { description = item.menu } or nil,
6894
})

lua/orgmode/org/autocompletion/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ end
5555

5656
function OrgCompletion:_get_valid_results(results, context)
5757
local base = context.base or ''
58+
local framework = context.framework or 'nvim-cmp'
5859

5960
local valid_results = {}
6061
for _, item in ipairs(results) do
61-
if base == '' or item:find('^' .. vim.pesc(base)) then
62+
-- For blink.cmp, skip prefix filtering and return all results
63+
-- Let blink.cmp's fuzzy matcher handle the filtering
64+
local should_include = framework == 'blink' or base == '' or item:find('^' .. vim.pesc(base))
65+
66+
if should_include then
6267
table.insert(valid_results, {
6368
word = item,
6469
menu = self.menu,

tests/plenary/org/autocompletion_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ describe('Blink completion', function()
436436

437437
assert(directive_item, 'Should find a directive completion item')
438438
assert(
439-
directive_item.insertText:match('^#%+'),
440-
string.format("insertText should start with '+#', got: %s", directive_item.insertText)
439+
directive_item.insertText and directive_item.insertText:match('^#%+'),
440+
string.format("completion text should start with '#+', got: %s", directive_item.insertText or 'nil')
441441
)
442442

443443
assert(line:sub(1, 4) == '#+fi', "Original line should contain '#+fi'")

0 commit comments

Comments
 (0)