133 lines
3.2 KiB
Lua
133 lines
3.2 KiB
Lua
local M = {}
|
|
|
|
local files = {
|
|
dictionary = vim.fn.stdpath('config') .. '/spell/en.utf-8.add',
|
|
disabledRules = vim.fn.stdpath('config') .. '/spell/disable.txt',
|
|
hiddenFalsePositives = vim.fn.stdpath('config') .. '/spell/false.txt',
|
|
}
|
|
|
|
local file_exists = function(file)
|
|
local f = io.open(file, 'rb')
|
|
if f then
|
|
f:close()
|
|
end
|
|
return f ~= nil
|
|
end
|
|
|
|
local lines_from = function(file)
|
|
if not file_exists(file) then
|
|
return {}
|
|
end
|
|
local lines = {}
|
|
for line in io.lines(file) do
|
|
table.insert(lines, line)
|
|
end
|
|
return lines
|
|
end
|
|
|
|
local get_client_by_name = function(name)
|
|
local buf_clients = vim.lsp.buf_get_clients()
|
|
for _, client in ipairs(buf_clients) do
|
|
if client.name == name then
|
|
return client
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local update_config = function(lang, configtype)
|
|
local client = get_client_by_name('ltex')
|
|
if client then
|
|
if client.config.settings.ltex[configtype] then
|
|
client.config.settings.ltex[configtype] = {
|
|
[lang] = lines_from(files[configtype]),
|
|
}
|
|
return client.notify('workspace/didChangeConfiguration', client.config.settings)
|
|
else
|
|
return vim.notify('Error when reading dictionary config, check it')
|
|
end
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
|
|
local add_to_file = function(configtype, lang, file, value)
|
|
local dict = lines_from(file)
|
|
for _, v in ipairs(dict) do
|
|
if v == value then
|
|
return nil
|
|
end
|
|
end
|
|
file = io.open(file, 'a+')
|
|
if file then
|
|
file:write(value .. '\n')
|
|
file:close()
|
|
else
|
|
return vim.notify(string.format('Failed insert %s', value))
|
|
end
|
|
return update_config(lang, configtype)
|
|
end
|
|
|
|
local do_command = function(arg, configtype)
|
|
for lang, words in pairs(arg) do
|
|
for _, word in ipairs(words) do
|
|
add_to_file(configtype, lang, files[configtype], word)
|
|
end
|
|
end
|
|
end
|
|
|
|
vim.lsp.commands['_ltex.addToDictionary'] = function(command, _)
|
|
do_command(command.arguments[1].words, 'dictionary')
|
|
end
|
|
vim.lsp.commands['_ltex.disableRules'] = function(command, _)
|
|
do_command(command.arguments[1].ruleIds, 'disabledRules')
|
|
end
|
|
vim.lsp.commands['_ltex.hideFalsePositives'] = function(command, _)
|
|
do_command(command.arguments[1].falsePositives, 'hiddenFalsePositives')
|
|
end
|
|
|
|
local post_attach = function()
|
|
vim.cmd('setlocal spell')
|
|
vim.cmd('setlocal nospell')
|
|
update_config('en-US', 'dictionary')
|
|
update_config('en-US', 'disabledRules')
|
|
update_config('en-US', 'hiddenFalsePositives')
|
|
vim.keymap.set('n', 'zug', function()
|
|
vim.cmd('normal! zug')
|
|
update_config('en-US', 'dictionary')
|
|
end, {
|
|
buffer = true,
|
|
desc = 'Remove word from spellfile and update ltex',
|
|
})
|
|
vim.keymap.set('n', 'zg', function()
|
|
vim.cmd('normal! zg')
|
|
update_config('en-US', 'dictionary')
|
|
end, {
|
|
buffer = true,
|
|
desc = 'Add word to spellfile and update ltex',
|
|
})
|
|
end
|
|
|
|
M.setup = function(opts)
|
|
require('lspconfig')['ltex'].setup {
|
|
on_attach = function(client)
|
|
opts.on_attach(client)
|
|
post_attach()
|
|
end,
|
|
capabilities = opts.capabilities,
|
|
flags = opts.flags,
|
|
settings = {
|
|
ltex = {
|
|
dictionary = {},
|
|
disabledRules = {},
|
|
hiddenFalsePositives = {},
|
|
additionalRules = {
|
|
enablePickyRules = true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
end
|
|
|
|
return M
|