[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

View File

@@ -1,5 +1,7 @@
local servers = { 'pyright', 'sumneko_lua', 'eslint', 'tsserver', 'ltex' } local servers = { 'pyright', 'sumneko_lua', 'eslint', 'tsserver', 'ltex' }
local ltex = require('lspconfig.ltex')
-- Mappings. -- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions -- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap=true, silent=true } local opts = { noremap=true, silent=true }
@@ -10,7 +12,7 @@ vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
-- Use an on_attach function to only map the following keys -- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer -- after the language server attaches to the current buffer
local on_attach = function(client, bufnr) local on_attach = function(_, bufnr)
-- Mappings. -- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions -- See `:help vim.lsp.*` for documentation on any of the below functions
local bufopts = { noremap=true, silent=true, buffer=bufnr } local bufopts = { noremap=true, silent=true, buffer=bufnr }
@@ -52,6 +54,28 @@ for _, lsp in pairs(servers) do
} }
end end
require('lspconfig')['ltex'].setup {
on_attach = function(client)
on_attach(client)
ltex.on_attach()
end,
capabilities = capabilities,
flags = {
-- This will be the default in neovim 0.7+
debounce_text_changes = 150,
},
settings = {
ltex = {
dictionary = {},
disabledRules = {},
hiddenFalsePositives = {},
additionalRules = {
enablePickyRules = true,
},
},
},
}
-- luasnip setup -- luasnip setup
local luasnip = require 'luasnip' local luasnip = require 'luasnip'
require('luasnip.loaders.from_vscode').lazy_load() require('luasnip.loaders.from_vscode').lazy_load()

1
.config/nvim/spell/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
false.txt

View File

@@ -0,0 +1 @@
PASSIVE_VOICE

View File

@@ -0,0 +1,10 @@
Read-Eval-Print-Loop
Flexowriter
Teitelman
Interlisp
JOHNNIAC
REPLs
Mathematica
DSLs
GPLs
Plasil

Binary file not shown.