diff options
| author | William Boman <william@redwill.se> | 2021-08-18 20:35:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-18 20:35:02 +0200 |
| commit | bb6c9441af692daf9b967a15324f574f601d4b52 (patch) | |
| tree | 853987daa609778bf3a64658f09eb0626d54ae7c | |
| parent | doc: add section for custom servers (diff) | |
| download | mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar.gz mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar.bz2 mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar.lz mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar.xz mason-bb6c9441af692daf9b967a15324f574f601d4b52.tar.zst mason-bb6c9441af692daf9b967a15324f574f601d4b52.zip | |
add new on_server_ready() API (#56)
| -rw-r--r-- | CUSTOM_SERVERS.md | 9 | ||||
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | doc/nvim-lsp-installer.txt | 37 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer.lua | 17 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/dispatcher.lua | 21 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/server.lua | 2 | ||||
| -rw-r--r-- | plugin/nvim-lsp-installer.vim | 2 |
7 files changed, 84 insertions, 17 deletions
diff --git a/CUSTOM_SERVERS.md b/CUSTOM_SERVERS.md index 31938b48..f0d548d8 100644 --- a/CUSTOM_SERVERS.md +++ b/CUSTOM_SERVERS.md @@ -11,10 +11,11 @@ to the [Lua docs](./lua/nvim-lsp-installer/server.lua) for more details. Each `Server` instance must provide an `installer` property. This _must_ be a function with the signature `function (server, callback)`, where `server` is the server instance that is being installed, and `callback` is a function that _must_ be called upon completion (successful or not) by the installer implementation. -Most likely, nvim-lsp-installer already have the installer implementations you'd need. You may find the available installers below. - ## Core installers +Most likely, nvim-lsp-installer already have the installer implementations you need. Below are all the currently +available installers that are available out of the box. + - ### Go #### `go.packages(packages: table)` @@ -36,7 +37,7 @@ Most likely, nvim-lsp-installer already have the installer implementations you'd - ### npm - #### `npm3.packages(packages: table)` + #### `npm.packages(packages: table)` Returns an installer that installs the provided list of `packages`. @@ -45,7 +46,7 @@ Most likely, nvim-lsp-installer already have the installer implementations you'd ```lua local npm = require "nvim-lsp-installer.installers.npm" - local installer = npm.packages { "graphql-language-service-cli", "graphql" }, + local installer = npm.packages { "graphql-language-service-cli", "graphql" } ``` #### `npm.executable(root_dir: string, executable: string)` @@ -48,16 +48,14 @@ use { ### Setup ```lua -local lsp_installer = require'nvim-lsp-installer' +local lsp_installer = require("nvim-lsp-installer") function common_on_attach(client, bufnr) - -- setup buffer keymaps etc. + -- ... set up buffer keymaps, etc. end -local installed_servers = lsp_installer.get_installed_servers() - -for _, server in pairs(installed_servers) do - opts = { +lsp_installer.on_server_ready(function(server) + local opts = { on_attach = common_on_attach, } @@ -67,7 +65,8 @@ for _, server in pairs(installed_servers) do -- end server:setup(opts) -end + vim.cmd [[ do User LspAttachBuffers ]] +end) ``` For more advanced use cases you may also interact with more APIs nvim-lsp-installer has to offer, for example the following (refer to `:help nvim-lsp-installer` for more docs): diff --git a/doc/nvim-lsp-installer.txt b/doc/nvim-lsp-installer.txt index 80a004a3..7f4c68a3 100644 --- a/doc/nvim-lsp-installer.txt +++ b/doc/nvim-lsp-installer.txt @@ -29,14 +29,14 @@ Install a language server via `:LspInstall`, for example: > Then, somewhere in your initialization script (see `:h init.lua`): > + local lsp_installer = require("nvim-lsp-installer") + function common_on_attach(client, bufnr) - -- .. set up keymaps, etc. + -- ... set up buffer keymaps, etc. end - local installed_servers = lsp_installer.get_installed_servers() - - for _, server in pairs(installed_servers) do - opts = { + lsp_installer.on_server_ready(function(server) + local opts = { on_attach = common_on_attach, } @@ -46,7 +46,8 @@ Then, somewhere in your initialization script (see `:h init.lua`): > -- end server:setup(opts) - end + vim.cmd [[ do User LspAttachBuffers ]] + end) < ============================================================================== @@ -123,6 +124,30 @@ uninstall({server_name}) Parameters: ~ {server_name} (string) The server to uninstall. +register({server}) + Registers a {server} instance with nvim-lsp-installer. + + {server} must be an instance of |lsp_installer.Server|. + + Parameters: ~ + {server} (|lsp_installer.Server|) The server to register. + +on_server_ready({cb}) + Registers a callback to be executed each time a server is + ready to be initiated. + + When called, all currently installed servers will be + considered ready to be initiated and will each + individually be invoked on {cb}. + + Parameters: ~ + {cb} (function) Function to be invoked when a server is ready to + be initiated. + + Return: ~ + Returns a function which when called will de-register the + cb} from any future dispatches. + ============================================================================== Lua module: nvim-lsp-installer.server *lsp_installer.server* diff --git a/lua/nvim-lsp-installer.lua b/lua/nvim-lsp-installer.lua index eae7363e..881db323 100644 --- a/lua/nvim-lsp-installer.lua +++ b/lua/nvim-lsp-installer.lua @@ -1,4 +1,5 @@ local notify = require "nvim-lsp-installer.notify" +local dispatcher = require "nvim-lsp-installer.dispatcher" local M = {} @@ -103,4 +104,20 @@ function M.register(server) _SERVERS[server.name] = server end +function M.on_server_ready(cb) + dispatcher.register_server_ready_callback(cb) + for _, server in pairs(M.get_installed_servers()) do + dispatcher.dispatch_server_ready(server) + 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) +function M.lsp_attach_proxy() + -- As of writing, if the lspconfig server provides a filetypes setting, it uses FileType as trigger, otherwise it uses BufReadPost + local cur_bufnr = vim.fn.bufnr "%" + vim.cmd [[ bufdo do FileType | do BufReadPost ]] + vim.cmd(("buffer %s"):format(cur_bufnr)) -- restore buffer +end + return M diff --git a/lua/nvim-lsp-installer/dispatcher.lua b/lua/nvim-lsp-installer/dispatcher.lua new file mode 100644 index 00000000..c8ec34a7 --- /dev/null +++ b/lua/nvim-lsp-installer/dispatcher.lua @@ -0,0 +1,21 @@ +local M = {} + +local registered_callbacks = {} + +function M.dispatch_server_ready(server) + for _, callback in pairs(registered_callbacks) do + callback(server) + end +end + +local idx = 0 +function M.register_server_ready_callback(callback) + local key = idx + 1 + registered_callbacks[("%d"):format(key)] = callback + return function () + table.remove(registered_callbacks, key) + end +end + + +return M diff --git a/lua/nvim-lsp-installer/server.lua b/lua/nvim-lsp-installer/server.lua index 8b72ce53..edc80253 100644 --- a/lua/nvim-lsp-installer/server.lua +++ b/lua/nvim-lsp-installer/server.lua @@ -1,4 +1,5 @@ local notify = require "nvim-lsp-installer.notify" +local dispatcher = require "nvim-lsp-installer.dispatcher" local fs = require "nvim-lsp-installer.fs" local path = require "nvim-lsp-installer.path" @@ -83,6 +84,7 @@ function M.Server:install() pcall(self.uninstall, self) else notify(("Successfully installed %s."):format(self.name)) + dispatcher.dispatch_server_ready(self) end end) end diff --git a/plugin/nvim-lsp-installer.vim b/plugin/nvim-lsp-installer.vim index a915207c..6e9e192d 100644 --- a/plugin/nvim-lsp-installer.vim +++ b/plugin/nvim-lsp-installer.vim @@ -40,5 +40,7 @@ command! -nargs=1 -complete=custom,s:LspUninstallCompletion LspUninstall exe s:L command! LspUninstallAll call s:LspUninstallAll() command! LspPrintInstalled call s:LspPrintInstalled() +autocmd User LspAttachBuffers lua require"nvim-lsp-installer".lsp_attach_proxy() + let &cpo = s:save_cpo unlet s:save_cpo |
