-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(completion): Add blink.cmp source
- Loading branch information
1 parent
1d8c9b9
commit 73f3263
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |