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) --- CUSTOM_SERVERS.md | 3 ++ README.md | 52 ++++++++++++++++++++- doc/nvim-lsp-installer.txt | 66 +++++++++++++++++++-------- 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 +++--- plugin/nvim-lsp-installer.vim | 18 +++++++- 10 files changed, 172 insertions(+), 51 deletions(-) delete mode 100644 lua/nvim-lsp-installer/opts.lua create mode 100644 lua/nvim-lsp-installer/settings.lua diff --git a/CUSTOM_SERVERS.md b/CUSTOM_SERVERS.md index bcf4dd0a..c68ba305 100644 --- a/CUSTOM_SERVERS.md +++ b/CUSTOM_SERVERS.md @@ -1,5 +1,8 @@ # Custom servers +*Note that there may be breaking changes introduced over time that may have an impact on the functionality of custom +servers. These breaking changes should generally be easy to address.* + You may create your own server installers by using the same APIs that nvim-lsp-installer itself uses. Each installable LSP server is represented as an instance of the `Server` class. This class is responsible for diff --git a/README.md b/README.md index fa59b5fb..ff320a00 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,56 @@ if ok then end ``` +### Configuration + +You can configure certain behavior of nvim-lsp-installer by calling the `.settings()` function. +Refer to the [default configuration](#default-configuration) for all available settings. + +Example: + +```lua +require("nvim-lsp-installer").settings { + ui = { + icons = { + server_installed = "✓", + server_pending = "➜", + server_uninstalled = "✗" + } + } +} +``` + +#### Default configuration + +```lua +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, +} +``` + ## Available LSPs | Language | Server name | @@ -163,6 +213,6 @@ Illustrations in the logo are derived from [@Kaligule](https://schauderbasis.de/ ## Roadmap -- Managed versioning of installed servers - Command (and corresponding Lua API) to update outdated servers (e.g., `:LspUpdate {server}`) +- More helpful metadata displayed in the UI window - Cross-platform CI for all server installers diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index 49189054..44b57b83 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -104,28 +104,57 @@ Prints all installed language servers. ============================================================================== -OPTIONS *nvim-lsp-installer-options* +SETTINGS *nvim-lsp-installer-settings* - *g:lsp_installer_allow_federated_servers* - *vim.g.lsp_installer_allow_federated_servers* -VimL: g:lsp_installer_allow_federated_servers -Lua: vim.g.lsp_installer_allow_federated_servers +You can configure certain behavior of nvim-lsp-installer by calling the +`.settings()` function. - Type: |Boolean| - Default: `true` (`v:true`) +Refer to the |nvim-lsp-installer-default-settings| for all available settings. - 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`. +Example: > - For example, installing `cssls` will also install both `jsonls` and `html` - (and the other ways around), as these all share the same underlying - package. + require("nvim-lsp-installer").settings { + ui = { + icons = { + server_installed = "✓", + server_pending = "➜", + server_uninstalled = "✗" + } + } + } +< + + *nvim-lsp-installer-default-settings* - Example: > - let g:lsp_installer_allow_federated_servers = v:false - vim.g.lsp_installer_allow_federated_servers = false +The following settings are applied by default. + +> + 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, + } < ============================================================================== @@ -223,8 +252,7 @@ class: Server Sets up the language server. This has the same function signature as the setup function in nvim-lspconfig. - See |lspconfig-custom-config| for more information on - {opts}. + Refer to nvim-lspconfig for more information on {opts}. - get_default_options() Returns a deep copy of the default options provided to 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" }, }, diff --git a/plugin/nvim-lsp-installer.vim b/plugin/nvim-lsp-installer.vim index 2e2bc315..f8458f44 100644 --- a/plugin/nvim-lsp-installer.vim +++ b/plugin/nvim-lsp-installer.vim @@ -1,6 +1,5 @@ if exists('g:loaded_nvim_lsp_installer') | finish | endif let g:loaded_nvim_lsp_installer = v:true -let g:lsp_installer_allow_federated_servers = get(g:, "lsp_installer_allow_federated_servers", v:true) let s:save_cpo = &cpo set cpo&vim @@ -52,3 +51,20 @@ autocmd User LspAttachBuffers lua require"nvim-lsp-installer".lsp_attach_proxy() let &cpo = s:save_cpo unlet s:save_cpo + + + +""" +""" Backward compat for deprecated g:lsp_installer* options. Remove by 2021-12-01-ish. +""" +if exists("g:lsp_installer_allow_federated_servers") + " legacy global variable option + call luaeval("require('nvim-lsp-installer').settings { allow_federated_servers = _A }", g:lsp_installer_allow_federated_servers) + lua vim.notify("[Deprecation notice] Providing settings via global variables (g:lsp_installer_allow_federated_servers) is deprecated. Please refer to https://github.com/williamboman/nvim-lsp-installer#configuration.", vim.log.levels.WARN) +endif + +if exists("g:lsp_installer_log_level") + " legacy global variable option + call luaeval("require('nvim-lsp-installer').settings { log_level = _A }", g:lsp_installer_log_level) + lua vim.notify("[Deprecation notice] Providing settings via global variables (g:lsp_installer_log_level) is deprecated. Please refer to https://github.com/williamboman/nvim-lsp-installer#configuration.", vim.log.levels.WARN) +endif -- cgit v1.2.3-70-g09d2