Skip to content

Commit

Permalink
feat(completion): Add blink.cmp source
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Dec 4, 2024
1 parent 1d8c9b9 commit 73f3263
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,32 @@ require('cmp').setup({

#+HTML:</details>

#+HTML:<details> <summary><a href="https://github.com/Saghen/blink.cmp"><b>blink.cmp</b></a></summary> </br>
#+BEGIN_SRC lua
require('blink.cmp').setup({
sources = {
completion = {
enabled_providers = { 'lsp', 'path', 'snippets', 'buffer', 'orgmode' }
-- Or if you want to use only this provider in org files
-- enabled_providers = function()
-- if vim.bo.filetype == 'org' then
-- return { 'orgmode' }
-- end
-- return { 'lsp', 'path', 'snippets', 'buffer' }
-- end
},
providers = {
orgmode = {
name = 'Orgmode',
module = 'orgmode.org.autocompletion.blink',
},
},
},
})
#+END_SRC

#+HTML:</details>

#+HTML:<details> <summary><a href="https://github.com/nvim-lua/completion-nvim"><b>completion-nvim</b></a></summary> </br>

#+BEGIN_SRC lua
Expand Down
71 changes: 71 additions & 0 deletions lua/orgmode/org/autocompletion/blink.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local org = require('orgmode')

local Source = {}

Source.new = function()
return setmetatable({}, { __index = Source })
end

function Source:enabled()
return vim.bo.filetype == 'org'
end

function Source:get_trigger_characters(_)
return { '#', '+', ':', '*', '.', '/' }
end

function Source:get_completions(ctx, callback)
local line = ctx.line:sub(1, ctx.cursor[2])
local offset = org.completion:get_start({ line = line }) + 1
local base = string.sub(line, offset)
local results = org.completion:complete({
line = line,
base = base,
})

local cb = function(items)
callback({
context = ctx,
is_incomplete_forward = true,
is_incomplete_backward = true,
items = items,
})
return function() end
end

if not results or #results == 0 then
return cb({})
end

-- Does not contain dot to avoid stopping on file paths
local triggers = { '#', '+', ':', '*', '/' }

local getInsertTextOffset = function(word)
local word_length = #word + 1
while word_length > 0 do
local char = word:sub(word_length - 1, word_length - 1)
if vim.tbl_contains(triggers, char) or char:match('%s') then
return word_length
end
word_length = word_length - 1
end
return 0
end

local baseOffset = getInsertTextOffset(base)
local insertTextOffset = baseOffset > 0 and math.max(2, baseOffset) or 0

local items = {}

for _, item in ipairs(results) do
table.insert(items, {
label = item.word,
insertText = insertTextOffset > 0 and item.word:sub(insertTextOffset) or item.word,
labelDetails = item.menu and { description = item.menu } or nil,
})
end

return cb(items)
end

return Source

0 comments on commit 73f3263

Please sign in to comment.