diff options
| author | William Boman <william@redwill.se> | 2021-10-31 12:05:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-31 12:05:44 +0100 |
| commit | 14b64a21e850eee2b9b351c92394e33df57a7f08 (patch) | |
| tree | a77c72d348d944060d912dcc6b104a61b3d81785 | |
| parent | scripts: fix generating filetypes for servers w/ no lspconfig entry (diff) | |
| download | mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar.gz mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar.bz2 mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar.lz mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar.xz mason-14b64a21e850eee2b9b351c92394e33df57a7f08.tar.zst mason-14b64a21e850eee2b9b351c92394e33df57a7f08.zip | |
server: add attach_buffers() method (#220)
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | doc/nvim-lsp-installer.txt | 31 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer.lua | 7 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/server.lua | 23 | ||||
| -rw-r--r-- | plugin/nvim-lsp-installer.vim | 2 |
5 files changed, 49 insertions, 18 deletions
@@ -79,9 +79,9 @@ lsp_installer.on_server_ready(function(server) -- opts.root_dir = function() ... end -- end - -- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart) + -- This setup() function is exactly the same as lspconfig's setup function. + -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/ADVANCED_README.md server:setup(opts) - vim.cmd [[ do User LspAttachBuffers ]] end) ``` diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index bed9498f..78e2ebab 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -84,8 +84,9 @@ Then, somewhere in your initialization script (see `:h init.lua`): > -- opts.root_dir = function() ... end -- end + -- This setup() function is exactly the same as lspconfig's setup function. + -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/ADVANCED_README.md server:setup(opts) - vim.cmd [[ do User LspAttachBuffers ]] end) < @@ -160,7 +161,9 @@ Example: > } } - lsp_installer.on_server_ready(function (server) server:setup {} end) + lsp_installer.on_server_ready(function (server) + server:setup {} + end) < *nvim-lsp-installer-default-settings* @@ -323,10 +326,28 @@ class: Server Methods: ~ - setup({opts}) - Sets up the language server. This has the same function - signature as the setup function in nvim-lspconfig. + Sets up the language server and attaches all open buffers. + + See: + - setup_lsp({opts}) + - attach_buffers() + + Parameters: ~ + {opts} (table) The lspconfig server configuration. + See https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md + + - setup_lsp({opts}) + Sets up the language server via lspconfig. + This function has the same signature as the setup function + in nvim-lspconfig. + + Parameters: ~ + {opts} (table) The lspconfig server configuration. + See https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md - Refer to nvim-lspconfig for more information on {opts}. + - attach_buffers() + Attaches this server to all current open buffers with a + 'filetype' that matches the server's configured filetypes. - 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 d1ab5f20..52f8289a 100644 --- a/lua/nvim-lsp-installer.lua +++ b/lua/nvim-lsp-installer.lua @@ -188,13 +188,6 @@ function M.on_server_ready(cb) end) end --- "Proxy" function for triggering attachment of LSP servers to all buffers (useful when just installed a new server --- that wasn't installed at launch) -M.lsp_attach_proxy = process.debounced(function() - -- As of writing, if the lspconfig server provides a filetypes setting, it uses FileType as trigger, otherwise it uses BufReadPost - vim.cmd [[ doautoall FileType | doautoall BufReadPost ]] -end) - -- old API M.get_server = servers.get_server M.get_available_servers = servers.get_available_servers diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index 922be0db..3cdcf192 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -45,8 +45,9 @@ function M.Server:new(opts) }, M.Server) end ----@param opts table @User-defined options. This is directly passed to the lspconfig's setup() method. -function M.Server:setup(opts) +---Sets up the language server via lspconfig. This function has the same signature as the setup function in nvim-lspconfig. +---@param opts table @The lspconfig server configuration. +function M.Server:setup_lsp(opts) if self._pre_setup then log.fmt_debug("Calling pre_setup for server=%s", self.name) self._pre_setup() @@ -70,6 +71,24 @@ function M.Server:setup(opts) end end +---Sets up the language server and attaches all open buffers. +---@param opts table @The lspconfig server configuration. +function M.Server:setup(opts) + self:setup_lsp(opts) + self:attach_buffers() +end + +---Attaches this server to all current open buffers with a 'filetype' that matches the server's configured filetypes. +function M.Server:attach_buffers() + log.debug("Attaching server to buffers", self.name) + local lsp_server = require("lspconfig")[self.name] + for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do + log.fmt_trace("Attaching server=%s to bufnr=%s", self.name, bufnr) + lsp_server.manager.try_add_wrapper(bufnr) + end + log.debug("Successfully attached server to buffers", self.name) +end + ---Registers a handler (callback) to be executed when the server is ready to be setup. ---@param handler fun(server: Server) function M.Server:on_ready(handler) diff --git a/plugin/nvim-lsp-installer.vim b/plugin/nvim-lsp-installer.vim index e0bf4e8f..cb1e9da0 100644 --- a/plugin/nvim-lsp-installer.vim +++ b/plugin/nvim-lsp-installer.vim @@ -71,8 +71,6 @@ command! LspPrintInstalled call s:LspPrintInstalled() command! LspInstallInfo call s:LspInstallInfo() command! LspInstallLog call s:LspInstallLog() -autocmd User LspAttachBuffers lua require"nvim-lsp-installer".lsp_attach_proxy() - let &cpo = s:save_cpo unlet s:save_cpo |
