Skip to content

fix: preserve prefix in blink.cmp org directive completions #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/orgmode/org/autocompletion/blink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function Source:get_completions(ctx, callback)
local triggers = { '#', '+', ':', '*', '/' }

local getInsertTextOffset = function(word)
if #word > 1 and word:sub(1, 2) == '#+' then
return 0
end
local word_length = #word + 1
while word_length > 0 do
local char = word:sub(word_length - 1, word_length - 1)
Expand Down
61 changes: 61 additions & 0 deletions tests/plenary/org/autocompletion_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,64 @@ describe('Autocompletion', function()
end)
end)
end)

describe('Blink completion', function()
after_each(function()
vim.cmd([[silent! %bw!]])
end)

it('should preserve `#+` prefix when completing directives', function()
helpers.create_file({
'#+fi',
})

vim.fn.cursor(1, 5)

local blink = require('orgmode.org.autocompletion.blink')
local source = blink.new()

local line = vim.fn.getline('.')
local col = vim.fn.col('.') - 1 -- blink uses 0-indexed columns

local ctx = {
line = line,
cursor = { 1, col },
bufnr = vim.api.nvim_get_current_buf(),
}

local completion_result = nil
local completed = false

source:get_completions(ctx, function(result)
completion_result = result
completed = true
end)

vim.wait(500, function()
return completed
end)

assert(completed, 'Completion should have finished')
assert(completion_result and completion_result.items, 'Should have completion items')

if #completion_result.items == 0 then
error('No completion items returned')
end

local directive_item = nil
for _, item in ipairs(completion_result.items) do
if item.label and item.label:match('^#%+.*') then
directive_item = item
break
end
end

assert(directive_item, 'Should find a directive completion item')
assert(
directive_item.insertText:match('^#%+'),
string.format("insertText should start with '+#', got: %s", directive_item.insertText)
)

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