feat(completion): add spell suggestions to blink
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
---@class ow.completion.blink.spell.Source
|
||||
---@field opts ow.completion.blink.spell.SourceOpts
|
||||
local Source = {}
|
||||
Source.__index = Source
|
||||
|
||||
---@class ow.completion.blink.spell.SourceOpts
|
||||
---@field max_items integer
|
||||
---@field min_word_length integer
|
||||
|
||||
---@param opts? ow.completion.blink.spell.SourceOpts
|
||||
---@return ow.completion.blink.spell.Source
|
||||
function Source.new(opts)
|
||||
local self = setmetatable({}, Source)
|
||||
self.opts = vim.tbl_deep_extend("force", {
|
||||
max_items = 20,
|
||||
min_word_length = 3,
|
||||
}, opts or {})
|
||||
return self
|
||||
end
|
||||
|
||||
---@return boolean
|
||||
function Source.enabled()
|
||||
return vim.wo.spell
|
||||
end
|
||||
|
||||
---@param context blink.cmp.Context
|
||||
---@param callback fun(response?: blink.cmp.CompletionResponse)
|
||||
function Source:get_completions(context, callback)
|
||||
local cursor_line = assert(context.cursor[1])
|
||||
local cursor_col = assert(context.cursor[2])
|
||||
---@cast cursor_line integer
|
||||
---@cast cursor_col integer
|
||||
|
||||
local word = context.line:sub(1, cursor_col):match("([%a']+)$")
|
||||
|
||||
if not word or #word < self.opts.min_word_length then
|
||||
callback()
|
||||
return
|
||||
end
|
||||
|
||||
local start_col = cursor_col - #word
|
||||
local range = {
|
||||
start = {
|
||||
line = cursor_line - 1,
|
||||
character = start_col,
|
||||
},
|
||||
["end"] = {
|
||||
line = cursor_line - 1,
|
||||
character = cursor_col,
|
||||
},
|
||||
}
|
||||
|
||||
---@type blink.cmp.CompletionItem[]
|
||||
local items = {}
|
||||
---@type integer
|
||||
local max_items = self.opts.max_items
|
||||
local suggestions =
|
||||
vim.fn.spellsuggest(word, max_items, word:match("^%u") ~= nil)
|
||||
|
||||
for index, suggestion in ipairs(suggestions) do
|
||||
if suggestion ~= word then
|
||||
table.insert(items, {
|
||||
label = suggestion,
|
||||
filterText = word,
|
||||
sortText = ("%04d"):format(index),
|
||||
kind = vim.lsp.protocol.CompletionItemKind.Text,
|
||||
textEdit = {
|
||||
newText = suggestion,
|
||||
range = range,
|
||||
},
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
callback({
|
||||
is_incomplete_backward = true,
|
||||
is_incomplete_forward = true,
|
||||
items = items,
|
||||
})
|
||||
end
|
||||
|
||||
return Source
|
||||
@@ -186,7 +186,7 @@ vim.api.nvim_create_autocmd("FileType", {
|
||||
"text",
|
||||
},
|
||||
callback = function()
|
||||
vim.wo.spell = true
|
||||
vim.api.nvim_set_option_value("spell", true, { scope = "local" })
|
||||
end,
|
||||
})
|
||||
|
||||
|
||||
@@ -72,6 +72,13 @@ blink.setup({
|
||||
"lsp",
|
||||
"path",
|
||||
"snippets",
|
||||
"spell",
|
||||
},
|
||||
providers = {
|
||||
spell = {
|
||||
name = "Spell",
|
||||
module = "completion.blink.spell",
|
||||
},
|
||||
},
|
||||
transform_items = function(_, items)
|
||||
---@param item blink.cmp.CompletionItem
|
||||
|
||||
Reference in New Issue
Block a user