From 339fa1a614515aacd79a9405375f6059d606b68e Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 26 Apr 2022 21:05:02 +0200 Subject: [PATCH] feat: integrate with lspconfig's on_setup hook --- README.md | 45 +++------------------------ doc/nvim-lsp-installer.txt | 31 ++---------------- lua/nvim-lsp-installer/middleware.lua | 32 +++++++++++++++++++ plugin/nvim-lsp-installer.vim | 2 ++ tests/minimal_debug_init.lua | 10 +++--- 5 files changed, 45 insertions(+), 75 deletions(-) create mode 100644 lua/nvim-lsp-installer/middleware.lua diff --git a/README.md b/README.md index 82454362f..5db0b16ee 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,9 @@ - [About](#about) - [Screenshots](#screenshots) - [Installation](#installation) - - [Packer](#packer) - - [vim-plug](#vim-plug) - [Usage](#usage) - [Commands](#commands) - - [Setup](#setup) - - [Configuration](#configuration) + - [Configuration (optional)](#configuration-optional) - [Available LSPs](#available-lsps) - [Custom servers](#custom-servers) - [Logo](#logo) @@ -28,7 +25,6 @@ On top of just providing commands for installing & uninstalling LSP servers, it: - provides the ability to check for new server versions - supports installing custom versions of LSP servers (for example `:LspInstall rust_analyzer@nightly`) - relaxes the minimum requirements by attempting multiple different utilities (for example, only one of `wget`, `curl`, or `Invoke-WebRequest` is required for HTTP requests) -- allows you to install and setup servers without having to restart neovim - hosts [a suite of system tests](https://github.com/williamboman/nvim-lspconfig-test) for all supported servers - has full support for Windows @@ -83,6 +79,9 @@ Plug 'williamboman/nvim-lsp-installer' ## Usage +nvim-lsp-installer requires no changes to your configuration. +You may optionally override default behavior, see [configuration](#configuration-optional). + ### Commands - `:LspInstallInfo` - opens a graphical overview of your language servers @@ -92,41 +91,7 @@ Plug 'williamboman/nvim-lsp-installer' - `:LspInstallLog` - opens the log file in a new tab window - `:LspPrintInstalled` - prints all installed language servers -### Setup - -The recommended way of setting up your installed servers is to do it through nvim-lsp-installer. -By doing so, nvim-lsp-installer will make sure to inject any necessary properties before calling lspconfig's setup -function for you. You may find a minimal example below. To see how you can override the default settings for a server, -refer to the [Wiki][overriding-default-settings]. - -Make sure you don't also set up your servers directly via lspconfig (e.g. `require("lspconfig").clangd.setup {}`), as -this will cause servers to be set up twice! - -[overriding-default-settings]: https://github.com/williamboman/nvim-lsp-installer/wiki/Advanced-Configuration#overriding-the-default-lsp-server-options - -```lua -local lsp_installer = require("nvim-lsp-installer") - --- Register a handler that will be called for each installed server when it's ready (i.e. when installation is finished --- or if the server is already installed). -lsp_installer.on_server_ready(function(server) - local opts = {} - - -- (optional) Customize the options passed to the server - -- if server.name == "tsserver" then - -- opts.root_dir = function() ... end - -- end - - -- This setup() function will take the provided server configuration and decorate it with the necessary properties - -- before passing it onwards to lspconfig. - -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md - server:setup(opts) -end) -``` - -For more advanced use cases you may also interact with more APIs nvim-lsp-installer has to offer, refer to `:help nvim-lsp-installer` for more docs. - -### Configuration +### Configuration (optional) You can configure certain behavior of nvim-lsp-installer by calling the `.settings()` function. diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index 863ad2c93..9d3a52e17 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -20,7 +20,6 @@ it: - relaxes the minimum requirements by attempting multiple different utilities (for example, only one of `wget`, `curl`, or `Invoke-WebRequest` is required for HTTP requests) -- allows you to install and setup servers without having to restart neovim - hosts a suite of system tests for all supported servers - has full support for Windows @@ -49,6 +48,8 @@ https://github.com/williamboman/nvim-lsp-installer/blob/main/CUSTOM_SERVERS.md. ============================================================================== QUICK START *nvim-lsp-installer-quickstart* +No custom configuration is needed to use nvim-lsp-installer. + To view the UI for nvim-lsp-installer, run: > :LspInstallInfo @@ -79,30 +80,6 @@ buffer, simply just run: > Please refer to each server's own release page to find which versions are available. -Then, somewhere in your initialization script (see `:h init.lua`): > - - -- Register a handler that will be called for each installed server when it's ready (i.e. when installation is finished - -- or if the server is already installed). - lsp_installer.on_server_ready(function(server) - local opts = {} - - -- (optional) Customize the options passed to the server - -- if server.name == "tsserver" then - -- opts.root_dir = function() ... end - -- end - - -- This setup() function will take the provided server configuration and decorate it with the necessary properties - -- before passing it onwards to lspconfig. - -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md - server:setup(opts) - end) -< - -Make sure you don't also set up your servers directly via lspconfig (e.g. -`require("lspconfig").clangd.setup {}`), as this will cause servers to be set -up twice! - - ============================================================================== COMMANDS *nvim-lsp-installer-commands* @@ -178,10 +155,6 @@ Example: > } } }) - - lsp_installer.on_server_ready(function (server) - server:setup {} - end) < *nvim-lsp-installer-default-settings* diff --git a/lua/nvim-lsp-installer/middleware.lua b/lua/nvim-lsp-installer/middleware.lua new file mode 100644 index 000000000..1e5858f26 --- /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] or false) == "table" then + merge_in_place(t1[k] or {}, t2[k] or {}) + 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/plugin/nvim-lsp-installer.vim b/plugin/nvim-lsp-installer.vim index 3829384c9..cfcee4516 100644 --- a/plugin/nvim-lsp-installer.vim +++ b/plugin/nvim-lsp-installer.vim @@ -1,6 +1,8 @@ if exists('g:loaded_nvim_lsp_installer') | finish | endif let g:loaded_nvim_lsp_installer = v:true +lua require("nvim-lsp-installer.middleware").register_lspconfig_hook() + let s:save_cpo = &cpo set cpo&vim diff --git a/tests/minimal_debug_init.lua b/tests/minimal_debug_init.lua index cd42ed246..d5083a69b 100644 --- a/tests/minimal_debug_init.lua +++ b/tests/minimal_debug_init.lua @@ -35,7 +35,7 @@ function _G.load_config() -- ================================================== -- ======= MODIFY YOUR CONFIG HERE, IF NEEDED ======= -- ================================================== - local lsp_installer = require "nvim-lsp-installer" + local lspconfig = require "lspconfig" local function on_attach(client, bufnr) vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") @@ -45,11 +45,9 @@ function _G.load_config() log = vim.log.levels.DEBUG, } - lsp_installer.on_server_ready(function(server) - server:setup { - on_attach = on_attach, - } - end) + lspconfig.sumneko_lua.setup { on_attach = on_attach } + lspconfig.tsserver.setup { on_attach = on_attach } + lspconfig.ltex.setup { on_attach = on_attach } -- ================================================== end