Skip to content

Commit c720d9c

Browse files
seflueSebastian Flügge
and
Sebastian Flügge
authored
fix: prevent blink.cmp from removing directive prefix (#978)
Co-authored-by: Sebastian Flügge <sebastian.fluegge@dnv.com>
1 parent d81a9fd commit c720d9c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

lua/orgmode/org/autocompletion/blink.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function Source:get_completions(ctx, callback)
4141
local triggers = { '#', '+', ':', '*', '/' }
4242

4343
local getInsertTextOffset = function(word)
44+
if #word > 1 and word:sub(1, 2) == '#+' then
45+
return 0
46+
end
4447
local word_length = #word + 1
4548
while word_length > 0 do
4649
local char = word:sub(word_length - 1, word_length - 1)

tests/plenary/org/autocompletion_spec.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,64 @@ describe('Autocompletion', function()
382382
end)
383383
end)
384384
end)
385+
386+
describe('Blink completion', function()
387+
after_each(function()
388+
vim.cmd([[silent! %bw!]])
389+
end)
390+
391+
it('should preserve `#+` prefix when completing directives', function()
392+
helpers.create_file({
393+
'#+fi',
394+
})
395+
396+
vim.fn.cursor(1, 5)
397+
398+
local blink = require('orgmode.org.autocompletion.blink')
399+
local source = blink.new()
400+
401+
local line = vim.fn.getline('.')
402+
local col = vim.fn.col('.') - 1 -- blink uses 0-indexed columns
403+
404+
local ctx = {
405+
line = line,
406+
cursor = { 1, col },
407+
bufnr = vim.api.nvim_get_current_buf(),
408+
}
409+
410+
local completion_result = nil
411+
local completed = false
412+
413+
source:get_completions(ctx, function(result)
414+
completion_result = result
415+
completed = true
416+
end)
417+
418+
vim.wait(500, function()
419+
return completed
420+
end)
421+
422+
assert(completed, 'Completion should have finished')
423+
assert(completion_result and completion_result.items, 'Should have completion items')
424+
425+
if #completion_result.items == 0 then
426+
error('No completion items returned')
427+
end
428+
429+
local directive_item = nil
430+
for _, item in ipairs(completion_result.items) do
431+
if item.label and item.label:match('^#%+.*') then
432+
directive_item = item
433+
break
434+
end
435+
end
436+
437+
assert(directive_item, 'Should find a directive completion item')
438+
assert(
439+
directive_item.insertText:match('^#%+'),
440+
string.format("insertText should start with '+#', got: %s", directive_item.insertText)
441+
)
442+
443+
assert(line:sub(1, 4) == '#+fi', "Original line should contain '#+fi'")
444+
end)
445+
end)

0 commit comments

Comments
 (0)