From 090c8a87b31de5c5a21eebc55adb22ddab625015 Mon Sep 17 00:00:00 2001 From: William Boman Date: Wed, 27 Apr 2022 21:20:45 +0200 Subject: feat: integrate with lspconfig's on_setup hook (#631) * feat: integrate with lspconfig's on_setup hook * fix!: don't use aliased installation directories if new .setup() fn is used This makes it so servers are always installed in a directory name that corresponds with the server name. The reason aliased installation directories is supported is lost on me, but it's legacy and complicates things unnecessarily. This is a breaking change for users who previously were using the `.on_server_ready()` hook, and now transitioned to setting up servers directly via lspconfig. These users will need to reinstall the server. * fix: block usage of the deprecated server:setup() method if new setup method is used * fix: allow passing no arg to setup() * docs: ok final.v3 readme --- lua/nvim-lsp-installer.lua | 9 +++++++++ lua/nvim-lsp-installer/middleware.lua | 32 ++++++++++++++++++++++++++++++++ lua/nvim-lsp-installer/server.lua | 4 ++++ lua/nvim-lsp-installer/servers/init.lua | 11 ++++++++++- lua/nvim-lsp-installer/settings.lua | 4 ++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lua/nvim-lsp-installer/middleware.lua (limited to 'lua') diff --git a/lua/nvim-lsp-installer.lua b/lua/nvim-lsp-installer.lua index 62b20a1e..a910440e 100644 --- a/lua/nvim-lsp-installer.lua +++ b/lua/nvim-lsp-installer.lua @@ -14,6 +14,15 @@ local M = {} M.settings = settings.set +---@param config table +function M.setup(config) + if config then + settings.set(config) + end + settings.uses_new_setup = true + require("nvim-lsp-installer.middleware").register_lspconfig_hook() +end + M.info_window = { ---Opens the status window. open = function() diff --git a/lua/nvim-lsp-installer/middleware.lua b/lua/nvim-lsp-installer/middleware.lua new file mode 100644 index 00000000..3e01b976 --- /dev/null +++ b/lua/nvim-lsp-installer/middleware.lua @@ -0,0 +1,32 @@ +local util = require "lspconfig.util" +local servers = require "nvim-lsp-installer.servers" + +local M = {} + +---@param t1 table +---@param t2 table +local function merge_in_place(t1, t2) + for k, v in pairs(t2) do + if type(v) == "table" then + if type(t1[k]) == "table" and not vim.tbl_islist(t1[k]) then + merge_in_place(t1[k], v) + else + t1[k] = v + end + else + t1[k] = v + end + end + return t1 +end + +function M.register_lspconfig_hook() + util.on_setup = util.add_hook_before(util.on_setup, function(config) + local ok, server = servers.get_server(config.name) + if ok then + merge_in_place(config, server._default_options) + end + end) +end + +return M diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index de6481a4..66d9655f 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -71,6 +71,10 @@ end ---Sets up the language server and attaches all open buffers. ---@param opts table @The lspconfig server configuration. function M.Server:setup(opts) + assert( + not settings.uses_new_setup, + "Please set up servers directly via lspconfig instead of going through nvim-lsp-installer (this method is now deprecated)! Refer to :h nvim-lsp-installer-quickstart for more information." + ) self:setup_lsp(opts) if not (opts.autostart == false) then self:attach_buffers() diff --git a/lua/nvim-lsp-installer/servers/init.lua b/lua/nvim-lsp-installer/servers/init.lua index efca1dd5..9af4c1bb 100644 --- a/lua/nvim-lsp-installer/servers/init.lua +++ b/lua/nvim-lsp-installer/servers/init.lua @@ -2,6 +2,7 @@ local Data = require "nvim-lsp-installer.data" local path = require "nvim-lsp-installer.path" local fs = require "nvim-lsp-installer.fs" local settings = require "nvim-lsp-installer.settings" +local log = require "nvim-lsp-installer.log" local M = {} @@ -180,15 +181,22 @@ end ---@param server_name string ---@return string local function get_server_install_dir(server_name) - return INSTALL_DIRS[server_name] or server_name + log.fmt_trace("Getting server installation dirname. uses_new_setup=%s", settings.uses_new_setup) + if settings.uses_new_setup then + return server_name + else + return INSTALL_DIRS[server_name] or server_name + end end function M.get_server_install_path(dirname) + log.trace("Getting server installation path", settings.current.install_root_dir, dirname) return path.concat { settings.current.install_root_dir, dirname } end ---@param server_name string function M.is_server_installed(server_name) + log.trace("Checking if server is installed", server_name) local scanned_server_dirs = scan_server_roots() local dirname = get_server_install_dir(server_name) return scanned_server_dirs[dirname] or false @@ -213,6 +221,7 @@ function M.get_server(server_name) local ok, server_factory = pcall(require, ("nvim-lsp-installer.servers.%s"):format(server_name)) if ok then + log.trace("Initializing core server", server_name) INITIALIZED_SERVERS[server_name] = server_factory( server_name, M.get_server_install_path(get_server_install_dir(server_name)) diff --git a/lua/nvim-lsp-installer/settings.lua b/lua/nvim-lsp-installer/settings.lua index ad875b72..87499354 100644 --- a/lua/nvim-lsp-installer/settings.lua +++ b/lua/nvim-lsp-installer/settings.lua @@ -59,4 +59,8 @@ function M.set(opts) M.current = vim.tbl_deep_extend("force", M.current, opts) end +-- Whether the new .setup() function has been called. +-- This will temporarily be used as a flag to toggle certain behavior. +M.uses_new_setup = false + return M -- cgit v1.2.3-70-g09d2