From bb24fca211c5d5e8d3cf88151e60d0b2b0555127 Mon Sep 17 00:00:00 2001 From: William Boman Date: Thu, 30 Sep 2021 16:34:15 +0200 Subject: add Lua API to override default settings (#111) --- lua/nvim-lsp-installer.lua | 3 +++ lua/nvim-lsp-installer/log.lua | 7 +++--- lua/nvim-lsp-installer/opts.lua | 16 ------------ lua/nvim-lsp-installer/servers/init.lua | 7 +++--- lua/nvim-lsp-installer/settings.lua | 36 +++++++++++++++++++++++++++ lua/nvim-lsp-installer/ui/status-win/init.lua | 15 +++++------ 6 files changed, 54 insertions(+), 30 deletions(-) delete mode 100644 lua/nvim-lsp-installer/opts.lua create mode 100644 lua/nvim-lsp-installer/settings.lua (limited to 'lua') diff --git a/lua/nvim-lsp-installer.lua b/lua/nvim-lsp-installer.lua index 58b909dd..b05ac31c 100644 --- a/lua/nvim-lsp-installer.lua +++ b/lua/nvim-lsp-installer.lua @@ -3,9 +3,12 @@ local dispatcher = require "nvim-lsp-installer.dispatcher" local process = require "nvim-lsp-installer.process" local status_win = require "nvim-lsp-installer.ui.status-win" local servers = require "nvim-lsp-installer.servers" +local settings = require "nvim-lsp-installer.settings" local M = {} +M.settings = settings.set + function M.display() status_win().open() end diff --git a/lua/nvim-lsp-installer/log.lua b/lua/nvim-lsp-installer/log.lua index 1fa3209e..8aa2e16e 100644 --- a/lua/nvim-lsp-installer/log.lua +++ b/lua/nvim-lsp-installer/log.lua @@ -1,3 +1,5 @@ +local settings = require "nvim-lsp-installer.settings" + local config = { -- Name of the plugin. Prepended to log messages name = "lsp-installer", @@ -28,9 +30,6 @@ local config = { local log = {} --- Default log level is warn. -vim.g.lsp_installer_log_level = vim.g.lsp_installer_log_level or vim.log.levels.WARN - local unpack = unpack or table.unpack do @@ -62,7 +61,7 @@ do local log_at_level = function(level, level_config, message_maker, ...) -- Return early if we're below the current_log_level - if level < vim.g.lsp_installer_log_level then + if level < settings.current.log_level then return end local nameupper = level_config.name:upper() diff --git a/lua/nvim-lsp-installer/opts.lua b/lua/nvim-lsp-installer/opts.lua deleted file mode 100644 index 71128808..00000000 --- a/lua/nvim-lsp-installer/opts.lua +++ /dev/null @@ -1,16 +0,0 @@ -local M = {} - -local function boolean_val(val, default) - if type(val) == "nil" then - return default - elseif type(val) == "number" then - return val == 1 - end - return val and true or false -end - -function M.allow_federated_servers() - return boolean_val(vim.g.lsp_installer_allow_federated_servers, true) -end - -return M diff --git a/lua/nvim-lsp-installer/servers/init.lua b/lua/nvim-lsp-installer/servers/init.lua index 497d5375..f0ed0533 100644 --- a/lua/nvim-lsp-installer/servers/init.lua +++ b/lua/nvim-lsp-installer/servers/init.lua @@ -1,17 +1,18 @@ local Data = require "nvim-lsp-installer.data" local path = require "nvim-lsp-installer.path" local fs = require "nvim-lsp-installer.fs" -local opts = require "nvim-lsp-installer.opts" +local settings = require "nvim-lsp-installer.settings" local M = {} local function vscode_langservers_extracted(name) - return opts.allow_federated_servers() and "vscode-langservers-extracted" or "vscode-langservers-extracted_" .. name + return settings.current.allow_federated_servers and "vscode-langservers-extracted" + or "vscode-langservers-extracted_" .. name end -- By default the install dir will be the same as the server's name. -- There are two cases when servers should install to a different location: --- 1. federated server installations, (see :help vim.g.lsp_installer_allow_federated_servers) +-- 1. federated server installations, (see :help nvim-lsp-installer-settings) -- 2. legacy reasons, where some servers were previously installed to a location different than their name local INSTALL_DIRS = { ["bashls"] = "bash", diff --git a/lua/nvim-lsp-installer/settings.lua b/lua/nvim-lsp-installer/settings.lua new file mode 100644 index 00000000..ec7f622b --- /dev/null +++ b/lua/nvim-lsp-installer/settings.lua @@ -0,0 +1,36 @@ +local DEFAULT_SETTINGS = { + ui = { + icons = { + -- The list icon to use for installed servers. + server_installed = "◍", + -- The list icon to use for servers that are pending installation. + server_pending = "◍", + -- The list icon to use for servers that are not installed. + server_uninstalled = "◍", + }, + }, + + -- Controls to which degree logs are written to the log file. For example, it's useful to set this to + -- vim.log.levels.TRACE when debugging issues with server installations. + log_level = vim.log.levels.WARN, + + -- Whether to allow LSP servers to share the same installation directory. + -- For some servers, this effectively causes more than one server to be + -- installed (and uninstalled) when executing `:LspInstall` and + -- `:LspUninstall`. + + -- For example, installing `cssls` will also install both `jsonls` and `html` + -- (and the other ways around), as these all share the same underlying + -- package. + allow_federated_servers = true, +} + +local M = {} + +function M.set(opts) + M.current = vim.tbl_deep_extend("force", DEFAULT_SETTINGS, opts) +end + +M.current = DEFAULT_SETTINGS + +return M diff --git a/lua/nvim-lsp-installer/ui/status-win/init.lua b/lua/nvim-lsp-installer/ui/status-win/init.lua index a8bad050..76569352 100644 --- a/lua/nvim-lsp-installer/ui/status-win/init.lua +++ b/lua/nvim-lsp-installer/ui/status-win/init.lua @@ -1,5 +1,6 @@ local Ui = require "nvim-lsp-installer.ui" local fs = require "nvim-lsp-installer.fs" +local settings = require "nvim-lsp-installer.settings" local Log = require "nvim-lsp-installer.log" local Data = require "nvim-lsp-installer.data" local display = require "nvim-lsp-installer.ui.display" @@ -23,9 +24,6 @@ local function Header() }) end --- TODO make configurable -local LIST_ICON = "◍" - local Seconds = { DAY = 86400, -- 60 * 60 * 24 WEEK = 604800, -- 60 * 60 * 24 * 7 @@ -45,7 +43,7 @@ local function get_relative_install_time(time) elseif delta < (Seconds.MONTH * 2) then return "last month" elseif delta < Seconds.YEAR then - return ("%d months ago"):format(math.floor((delta / 2419200) + 0.5)) + return ("%d months ago"):format(math.floor((delta / Seconds.MONTH) + 0.5)) else return "more than a year ago" end @@ -56,7 +54,7 @@ local function InstalledServers(servers) return Ui.Node { Ui.HlTextNode { { - { LIST_ICON, "LspInstallerGreen" }, + { settings.current.ui.icons.server_installed, "LspInstallerGreen" }, { " " .. server.name, "Normal" }, { (" installed %s"):format(get_relative_install_time(server.creation_time)), @@ -91,7 +89,10 @@ local function PendingServers(servers) return Ui.Node { Ui.HlTextNode { { - { LIST_ICON, has_failed and "LspInstallerError" or "LspInstallerOrange" }, + { + settings.current.ui.icons.server_pending, + has_failed and "LspInstallerError" or "LspInstallerOrange", + }, { " " .. server.name, server.installer.is_running and "Normal" or "LspInstallerGray" }, { " " .. note, "Comment" }, { @@ -118,7 +119,7 @@ local function UninstalledServers(servers) return Ui.Node { Ui.HlTextNode { { - { LIST_ICON, "LspInstallerGray" }, + { settings.current.ui.icons.server_uninstalled, "LspInstallerGray" }, { " " .. server.name, "Comment" }, { server.uninstaller.has_run and " (just uninstalled)" or "", "Comment" }, }, -- cgit v1.2.3-70-g09d2