-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.lua
75 lines (64 loc) · 2.13 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local source = {}
local defaults = {
keep_all_entries = false,
}
function source.new()
return setmetatable({}, { __index = source })
end
function source:is_available()
return vim.wo.spell
end
function source:get_keyword_pattern()
return [[\K\+]]
end
local function validate_option(params)
local option = vim.tbl_deep_extend('keep', params.option, defaults)
vim.validate({
keep_all_entries = { option.keep_all_entries, 'boolean' },
})
return option
end
local function number_to_text(input, number, len)
local loglen = math.ceil(math.log10(len + 1))
return string.format(input .. '%0' .. loglen .. 'd', number)
end
local function candidates(input, option)
local items = {}
local entries = vim.fn.spellsuggest(input)
local offset = 0
if vim.tbl_isempty(vim.spell.check(input)) then
offset = 1
items[offset] = {
label = input,
filterText = input,
insertText = input,
-- add a
sortText = number_to_text(input, offset, #entries + offset),
-- If the current word is spelled correctly, preselect it.
preselect = true,
}
end
for k, v in ipairs(entries) do
items[k + offset] = {
label = v,
-- Using the `input` word as filterText, all suggestions are displayed in completion menu.
filterText = option.keep_all_entries and input or v,
insertText = v,
-- To keep the order of suggestions, add the index at the end of sortText and trick the compare algorithms.
-- TODO: Add a custom compare function.
sortText = option.keep_all_entries and number_to_text(input, k + offset, #entries + offset) or v,
preselect = false,
}
end
return items
end
function source:complete(params, callback)
local option = validate_option(params)
local input = string.sub(params.context.cursor_before_line, params.offset)
callback({ items = candidates(input, option), isIncomplete = true })
end
local debug_name = 'spell'
function source:get_debug_name()
return debug_name
end
return source