diff --git a/.config/nvim/lua/lspconfig/ltex.lua b/.config/nvim/lua/lspconfig/ltex.lua new file mode 100644 index 0000000..d5aa384 --- /dev/null +++ b/.config/nvim/lua/lspconfig/ltex.lua @@ -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 diff --git a/.config/nvim/lua/lspconfig/settings.lua b/.config/nvim/lua/lspconfig/settings.lua index 80f4044..6d74d65 100644 --- a/.config/nvim/lua/lspconfig/settings.lua +++ b/.config/nvim/lua/lspconfig/settings.lua @@ -1,5 +1,7 @@ local servers = { 'pyright', 'sumneko_lua', 'eslint', 'tsserver', 'ltex' } +local ltex = require('lspconfig.ltex') + -- Mappings. -- See `:help vim.diagnostic.*` for documentation on any of the below functions local opts = { noremap=true, silent=true } @@ -10,7 +12,7 @@ vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) -- Use an on_attach function to only map the following keys -- after the language server attaches to the current buffer -local on_attach = function(client, bufnr) +local on_attach = function(_, bufnr) -- Mappings. -- See `:help vim.lsp.*` for documentation on any of the below functions local bufopts = { noremap=true, silent=true, buffer=bufnr } @@ -52,6 +54,28 @@ for _, lsp in pairs(servers) do } 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 local luasnip = require 'luasnip' require('luasnip.loaders.from_vscode').lazy_load() diff --git a/.config/nvim/spell/.gitignore b/.config/nvim/spell/.gitignore new file mode 100644 index 0000000..1bce8f8 --- /dev/null +++ b/.config/nvim/spell/.gitignore @@ -0,0 +1 @@ +false.txt diff --git a/.config/nvim/spell/disable.txt b/.config/nvim/spell/disable.txt new file mode 100644 index 0000000..bf9972e --- /dev/null +++ b/.config/nvim/spell/disable.txt @@ -0,0 +1 @@ +PASSIVE_VOICE diff --git a/.config/nvim/spell/en.utf-8.add b/.config/nvim/spell/en.utf-8.add new file mode 100644 index 0000000..8381d96 --- /dev/null +++ b/.config/nvim/spell/en.utf-8.add @@ -0,0 +1,10 @@ +Read-Eval-Print-Loop +Flexowriter +Teitelman +Interlisp +JOHNNIAC +REPLs +Mathematica +DSLs +GPLs +Plasil diff --git a/.config/nvim/spell/en.utf-8.add.spl b/.config/nvim/spell/en.utf-8.add.spl new file mode 100644 index 0000000..d6c3e5d Binary files /dev/null and b/.config/nvim/spell/en.utf-8.add.spl differ