[NeoVim] Configure LTeX

This commit is contained in:
2022-08-22 12:51:12 +02:00
parent 69b73d8323
commit 84dbb0666a
6 changed files with 148 additions and 1 deletions

View File

@@ -0,0 +1,111 @@
local M = {}
M.file = {
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
M.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] = M.lines_from(M.file[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 = M.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, M.file[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
M.on_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
return M