nvim-config

Log | Files | Refs | Submodules | README

commit 8f8d6eb17811dbaffad9d86394c33e4b25b56cc5
parent e2902e9dad0a1fb66c30e82dd2867c61d0cef27c
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Fri, 10 Jun 2022 15:30:30 +0200

feat(lsp): add custom handler for command for ltex

Diffstat:
Mlua/lsp_config.lua | 74+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 69 insertions(+), 5 deletions(-)

diff --git a/lua/lsp_config.lua b/lua/lsp_config.lua @@ -134,6 +134,56 @@ local function texlab_attach(client, bufnr) on_attach(client, bufnr) end +local function ltex_refresh(client, uri) + client.notify("workspace/didChangeConfiguration") + client.request("workspace/executeCommand", { command = "_ltex.checkDocument", arguments = { { uri = uri } } }) +end +local function ltex_execute_command(err, actions, ctx) + local client = vim.lsp.get_client_by_id(ctx.client_id) + if ctx.method ~= "workspace/executeCommand" or client.name ~= "ltex" then + error "LTeX command handler invalid usage" + end + + local ltex_settings = client.config.settings.ltex + + local command_name = ctx.params.command + local args = ctx.params.arguments + if command_name == "_ltex.addToDictionary" then + for lang, words in pairs(args[1].words) do + for _, fpath in ipairs(vim.api.nvim_get_runtime_file("dict/" .. lang .. ".txt", false)) do + local file = io.open(fpath, "a") + if not file then + error("Could not open" .. fpath) + end + file:write(table.concat(words, "\n") .. "\n") + file:close() + vim.list_extend(ltex_settings.dictionary[lang] or {}, words) + end + end + ltex_refresh(client, args[1].uri) + elseif command_name == "_ltex.hideFalsePositives" then + local cache_path = vim.fn.fnamemodify(vim.uri_to_fname(args[1].uri), ":p:h") .. "/.ltex_fp.json" + if not ltex_settings.hiddenFalsePositives then + ltex_settings.hiddenFalsePositives = {} + end + for lang, words in pairs(args[1].falsePositives) do + if ltex_settings.hiddenFalsePositives[lang] then + vim.list_extend(ltex_settings.hiddenFalsePositives[lang], words) + else + ltex_settings.hiddenFalsePositives[lang] = words + end + end + local cache = io.open(cache_path, "w") + cache:write(vim.fn.json_encode(ltex_settings.hiddenFalsePositives)) + cache:close() + ltex_refresh(client, args[1].uri) + end + + vim.lsp.handlers[ctx.method](err, actions, ctx) +end + +-- System lsps + local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities.textDocument.completion.completionItem.snippetSupport = true local default_config = { capabilities = capabilities, on_attach = on_attach } @@ -225,15 +275,29 @@ local lsps = { capabilities = capabilities, filetypes = { "latex", "tex", "bib", "markdown", "gitcommit" }, on_init = function(client) + -- Read false positives cache if present + local cache_path = vim.fn.expand "%:p:h" .. "/.ltex_fp.json" + print(cache_path) + local cache = io.open(cache_path, "r") + if cache then + local content = cache:read("*a") + cache:close() + client.config.settings.ltex.hiddenFalsePositives = vim.fn.json_decode(content) + end + + -- Set up language if spellang is set local langs = vim.opt.spellang:get() if #langs > 0 then - client.config.settings.ltex.language = langs[0] + client.config.settings.ltex.language = langs[1] else client.config.settings.ltex.language = "auto" end client.notify("workspace/didChangeConfiguration") return true end, + handlers = { + ["workspace/executeCommand"] = ltex_execute_command + }, settings = { ltex = { enabled = { "latex", "tex", "bib", "markdown", }, @@ -243,11 +307,11 @@ local lsps = { local files = {} for _, file in ipairs(vim.api.nvim_get_runtime_file("dict/*", true)) do local lang = vim.fn.fnamemodify(file, ":t:r") - local vals = {} - for line in io.lines(file) do - table.insert(vals, line) + local fullpath = vim.fn.fnamemodify(file, ":p") + files[lang] = {} + for line in io.lines(fullpath) do + table.insert(files[lang], line) end - files[lang] = vals end if files.default then