diff options
| author | William Boman <william@redwill.se> | 2021-10-05 21:48:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-05 21:48:05 +0200 |
| commit | 216d704212c52b7691b3e8e665b0bc6d60b49082 (patch) | |
| tree | 15ec50df91e244b2fc2f2b19effc3ab4473dc705 /lua | |
| parent | installers/std: fix archive name (diff) | |
| download | mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar.gz mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar.bz2 mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar.lz mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar.xz mason-216d704212c52b7691b3e8e665b0bc6d60b49082.tar.zst mason-216d704212c52b7691b3e8e665b0bc6d60b49082.zip | |
add ltex (#134)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-lsp-installer/installers/std.lua | 3 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/server.lua | 3 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/servers/init.lua | 1 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/servers/ltex/configure.lua | 188 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/servers/ltex/init.lua | 86 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/servers/texlab/init.lua | 10 |
6 files changed, 286 insertions, 5 deletions
diff --git a/lua/nvim-lsp-installer/installers/std.lua b/lua/nvim-lsp-installer/installers/std.lua index 201061e9..07e28d16 100644 --- a/lua/nvim-lsp-installer/installers/std.lua +++ b/lua/nvim-lsp-installer/installers/std.lua @@ -53,9 +53,10 @@ function M.unzip_remote(url, dest) end function M.untar(file, opts) - opts = opts or { + local default_opts = { strip_components = 0, } + opts = vim.tbl_deep_extend("force", default_opts, opts or {}) return installers.pipe { function(server, callback, context) process.spawn("tar", { diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index 4c9bef52..f3ad3fe1 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -1,5 +1,6 @@ local dispatcher = require "nvim-lsp-installer.dispatcher" local fs = require "nvim-lsp-installer.fs" +local log = require "nvim-lsp-installer.log" local installers = require "nvim-lsp-installer.installers" local servers = require "nvim-lsp-installer.servers" local status_win = require "nvim-lsp-installer.ui.status-win" @@ -46,6 +47,7 @@ end function M.Server:setup(opts) if self._pre_setup then + log.fmt_debug("Calling pre_setup for server=%s", self.name) self._pre_setup() end -- We require the lspconfig server here in order to do it as late as possible. @@ -55,6 +57,7 @@ function M.Server:setup(opts) if lsp_server then lsp_server.setup(vim.tbl_deep_extend("force", self._default_options, opts or {})) if self._post_setup then + log.fmt_debug("Calling post_setup for server=%s", self.name) self._post_setup() end else diff --git a/lua/nvim-lsp-installer/servers/init.lua b/lua/nvim-lsp-installer/servers/init.lua index fbae3c47..2e0c518c 100644 --- a/lua/nvim-lsp-installer/servers/init.lua +++ b/lua/nvim-lsp-installer/servers/init.lua @@ -69,6 +69,7 @@ local CORE_SERVERS = Data.set_of { "jsonls", "kotlin_language_server", "lemminx", + "ltex", "ocamlls", "omnisharp", "prismals", diff --git a/lua/nvim-lsp-installer/servers/ltex/configure.lua b/lua/nvim-lsp-installer/servers/ltex/configure.lua new file mode 100644 index 00000000..f77d54da --- /dev/null +++ b/lua/nvim-lsp-installer/servers/ltex/configure.lua @@ -0,0 +1,188 @@ +-- This is taken directly from https://github.com/neovim/nvim-lspconfig/pull/863. + +local configs = require "lspconfig/configs" +local util = require "lspconfig/util" + +if not configs.ltex then + local function readFiles(files) + local dict = {} + for _, file in pairs(files) do + local f = io.open(file, "r") + for l in f:lines() do + table.insert(dict, l) + end + end + return dict + end + + local function findLtexLang() + local buf_clients = vim.lsp.buf_get_clients() + for _, client in pairs(buf_clients) do + if client.name == "ltex" then + return client.config.settings.ltex.language + end + end + end + + local function findLtexFiles(filetype, value) + local buf_clients = vim.lsp.buf_get_clients() + for _, client in pairs(buf_clients) do + if client.name == "ltex" then + local files = nil + if filetype == "dictionary" then + files = client.config.dictionary_files[value or findLtexLang()] + elseif filetype == "disable" then + files = client.config.disabledrules_files[value or findLtexLang()] + elseif filetype == "falsePositive" then + files = client.config.falsepositive_files[value or findLtexLang()] + end + + if files then + return files + else + return nil + end + end + end + end + + local function updateConfig(lang, configtype) + local buf_clients = vim.lsp.buf_get_clients() + local client = nil + for _, lsp in pairs(buf_clients) do + if lsp.name == "ltex" then + client = lsp + end + end + + if client then + if configtype == "dictionary" then + -- if client.config.settings.ltex.dictionary then + client.config.settings.ltex.dictionary = { + [lang] = readFiles(client.config.dictionary_files[lang]), + } + return client.notify("workspace/didChangeConfiguration", client.config.settings) + -- else + -- return vim.notify("Error when reading dictionary config, check it") + -- end + elseif configtype == "disable" then + if client.config.settings.ltex.disabledRules then + client.config.settings.ltex.disabledRules = { + [lang] = readFiles(client.config.disabledrules_files[lang]), + } + return client.notify("workspace/didChangeConfiguration", client.config.settings) + else + return vim.notify "Error when reading disabledRules config, check it" + end + elseif configtype == "falsePositive" then + if client.config.settings.ltex.hiddenFalsePositives then + client.config.settings.ltex.hiddenFalsePositives = { + [lang] = readFiles(client.config.falsepositive_files[lang]), + } + return client.notify("workspace/didChangeConfiguration", client.config.settings) + else + return vim.notify "Error when reading hiddenFalsePositives config, check it" + end + end + else + return nil + end + end + + local function addToFile(filetype, lang, file, value) + file = io.open(file[#file - 0], "a+") -- add only to last file defined. + if file then + file:write(value .. "\n") + file:close() + else + return print("Failed insert %q", value) + end + if filetype == "dictionary" then + return updateConfig(lang, "dictionary") + elseif filetype == "disable" then + return updateConfig(lang, "disable") + elseif filetype == "falsePositive" then + return updateConfig(lang, "falsePositive") + end + end + + local function addTo(filetype, lang, file, value) + local dict = readFiles(file) + for _, v in ipairs(dict) do + if v == value then + return nil + end + end + return addToFile(filetype, lang, file, value) + end + + configs.ltex = { + default_config = { + cmd = { "ltex-ls" }, + filetypes = { "tex", "bib", "markdown" }, + root_dir = function(filename) + return util.path.dirname(filename) + end, + dictionary_files = { ["en"] = { vim.fn.getcwd() .. "dictionary.ltex" } }, + disabledrules_files = { ["en"] = { vim.fn.getcwd() .. "disable.ltex" } }, + falsepositive_files = { ["en"] = { vim.fn.getcwd() .. "false.ltex" } }, + settings = { + ltex = { + enabled = { "latex", "tex", "bib", "markdown" }, + checkFrequency = "save", + language = "en", + diagnosticSeverity = "information", + setenceCacheSize = 2000, + additionalRules = { + enablePickyRules = true, + motherTongue = "en", + }, + dictionary = {}, + disabledRules = {}, + hiddenFalsePositives = {}, + }, + }, + on_attach = function(client, bufnr) + -- local lang = client.config.settings.ltex.language + for lang, _ in ipairs(client.config.dictionary_files) do -- + updateConfig(lang, "dictionary") + updateConfig(lang, "disable") + updateConfig(lang, "falsePositive") + end + end, + }, + } + -- + -- https://github.com/neovim/nvim-lspconfig/issues/858 can't intercept, + -- override it then. + local orig_execute_command = vim.lsp.buf.execute_command + vim.lsp.buf.execute_command = function(command) + if command.command == "_ltex.addToDictionary" then + local arg = command.arguments[1].words -- can I really access like this? + for lang, words in pairs(arg) do + for _, word in ipairs(words) do + local filetype = "dictionary" + addTo(filetype, lang, findLtexFiles(filetype, lang), word) + end + end + elseif command.command == "_ltex.disableRules" then + local arg = command.arguments[1].ruleIds -- can I really access like this? + for lang, rules in pairs(arg) do + for _, rule in ipairs(rules) do + local filetype = "disable" + addTo(filetype, lang, findLtexFiles(filetype, lang), rule) + end + end + elseif command.command == "_ltex.hideFalsePositives" then + local arg = command.arguments[1].falsePositives -- can I really access like this? + for lang, rules in pairs(arg) do + for _, rule in ipairs(rules) do + local filetype = "falsePositive" + addTo(filetype, lang, findLtexFiles(filetype, lang), rule) + end + end + else + orig_execute_command(command) + end + end +end diff --git a/lua/nvim-lsp-installer/servers/ltex/init.lua b/lua/nvim-lsp-installer/servers/ltex/init.lua new file mode 100644 index 00000000..4bbdf69a --- /dev/null +++ b/lua/nvim-lsp-installer/servers/ltex/init.lua @@ -0,0 +1,86 @@ +local server = require "nvim-lsp-installer.server" +local path = require "nvim-lsp-installer.path" +local std = require "nvim-lsp-installer.installers.std" +local context = require "nvim-lsp-installer.installers.context" +local Data = require "nvim-lsp-installer.data" +local platform = require "nvim-lsp-installer.platform" +local installers = require "nvim-lsp-installer.installers" + +local uv = vim.loop + +local coalesce, when = Data.coalesce, Data.when + +return function(name, root_dir) + return server.Server:new { + name = name, + root_dir = root_dir, + installer = { + context.github_release_file("valentjn/ltex-ls", function(version) + return coalesce( + when(platform.is_mac, "ltex-ls-%s-mac-x64.tar.gz"), + when(platform.is_linux, "ltex-ls-%s-linux-x64.tar.gz"), + when(platform.is_win, "ltex-ls-%s-windows-x64.zip") + ):format(version) + end), + context.capture(function(ctx) + if platform.is_win then + -- todo strip components unzip + return std.unzip_remote(ctx.github_release_file) + else + return std.untargz_remote(ctx.github_release_file) + end + end), + installers.when { + unix = function(server, callback, context) + local executable = path.concat { + server.root_dir, + ("ltex-ls-%s"):format(context.requested_server_version), + "bin", + "ltex-ls", + } + local new_path = path.concat { server.root_dir, "ltex-ls" } + context.stdio_sink.stdout(("Creating symlink from %s to %s\n"):format(executable, new_path)) + uv.fs_symlink(executable, new_path, { dir = false, junction = false }, function(err, success) + if not success then + context.stdio_sink.stderr(tostring(err) .. "\n") + callback(false) + else + callback(true) + end + end) + end, + win = function(server, callback, context) + context.stdio_sink.stdout "Creating ltex-ls.bat...\n" + uv.fs_open(path.concat { server.root_dir, "ltex-ls.bat" }, "w", 438, function(open_err, fd) + local executable = path.concat { + server.root_dir, + ("ltex-ls-%s"):format(context.requested_server_version), + "bin", + "ltex-ls.bat", + } + if open_err then + context.stdio_sink.stderr(tostring(open_err) .. "\n") + return callback(false) + end + uv.fs_write(fd, ("@call %q %%*"):format(executable), -1, function(write_err) + if write_err then + context.stdio_sink.stderr(tostring(write_err) .. "\n") + callback(false) + else + context.stdio_sink.stdout "Created ltex-ls.bat\n" + callback(true) + end + assert(uv.fs_close(fd)) + end) + end) + end, + }, + }, + pre_setup = function() + require "nvim-lsp-installer.servers.ltex.configure" + end, + default_options = { + cmd = { path.concat { root_dir, platform.is_win and "ltex-ls.bat" or "ltex-ls" } }, + }, + } +end diff --git a/lua/nvim-lsp-installer/servers/texlab/init.lua b/lua/nvim-lsp-installer/servers/texlab/init.lua index cbb17ea3..1a38be26 100644 --- a/lua/nvim-lsp-installer/servers/texlab/init.lua +++ b/lua/nvim-lsp-installer/servers/texlab/init.lua @@ -5,6 +5,8 @@ local context = require "nvim-lsp-installer.installers.context" local Data = require "nvim-lsp-installer.data" local platform = require "nvim-lsp-installer.platform" +local coalesce, when = Data.coalesce, Data.when + return function(name, root_dir) return server.Server:new { name = name, @@ -15,10 +17,10 @@ return function(name, root_dir) }, context.github_release_file( "latex-lsp/texlab", - Data.coalesce( - Data.when(platform.is_mac, "texlab-x86_64-macos.tar.gz"), - Data.when(platform.is_linux, "texlab-x86_64-linux.tar.gz"), - Data.when(platform.is_win, "texlab-x86_64-windows.tar.gz") + coalesce( + when(platform.is_mac, "texlab-x86_64-macos.tar.gz"), + when(platform.is_linux, "texlab-x86_64-linux.tar.gz"), + when(platform.is_win, "texlab-x86_64-windows.tar.gz") ) ), context.capture(function(ctx) |
