From 6ab1cce1dd1c17c1739c0f0d1d57f39fb50279f0 Mon Sep 17 00:00:00 2001 From: Hinell Date: Fri, 12 May 2023 20:05:15 +0300 Subject: [PATCH] feat(LspconfigServerLifeCycle): add configuration option; add docs; disable by default --- README.md | 4 ++ doc/lspconfig.txt | 22 +++++++++ plugin/lspconfig.lua | 107 ++++++++++++++++++++++--------------------- 3 files changed, 82 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 64a13b46ee..63187503d7 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ See [server_configurations.md](doc/server_configurations.md) (`:help lspconfig-a nvim-lspconfig does not set keybindings or enable completion by default. The following example configuration provides suggested keymaps for the most commonly used language server functions, and manually triggered completion with omnifunc (\\). ```lua +-- Uncomment to automatically start/stop servers when nvim is idle (non-focused) +-- See :h lspconfig-lifecycle for details +-- vim.g.lspconfigServerLifeCycle = { timeout = 1000 * 60 * 30 } + -- Setup language servers. local lspconfig = require('lspconfig') lspconfig.pyright.setup {} diff --git a/doc/lspconfig.txt b/doc/lspconfig.txt index cff149d6b6..3eb6c92372 100644 --- a/doc/lspconfig.txt +++ b/doc/lspconfig.txt @@ -472,6 +472,28 @@ contained in `:LspInfo`: - `:LspRestart ` restarts the client with the given client id, and will attempt to reattach to all previously attached buffers. +============================================================================== +SERVER LIFECYCLE MANAGEMENT *lspconfig-lifecycle* + +`lspconfig` may automatically stop or restart workspace LSP servers by using +`:LspStart` / `:LspStop` commands. When `nvim` loses focus it will automatically +stop buffer-bound LSP server upon clearing timeout threshold and restart +otherwise. + +Disabled by default. To turn it on set the following global config option. +Make sure it's set before |load-plugins| cycle and any |lspconfig-setup|. + +> + -- Table is expected + vim.g.lspconfigServerLifeCycle = { + -- ms before server is stopped (30 minutes by default) + timeout = 1000 * 60 * 30 + } + ... + local lspconfig = require('lspconfig') + .... + + ============================================================================== EXAMPLE KEYBINDINGS *lspconfig-keybindings* diff --git a/plugin/lspconfig.lua b/plugin/lspconfig.lua index 2b6944a98f..855b5e9604 100644 --- a/plugin/lspconfig.lua +++ b/plugin/lspconfig.lua @@ -61,7 +61,7 @@ end -- Called from plugin/lspconfig.vim because it requires knowing that the last -- script in scriptnames to be executed is lspconfig. api.nvim_create_user_command('LspInfo', function() - require 'lspconfig.ui.lspinfo'() + require 'lspconfig.ui.lspinfo' () end, { desc = 'Displays attached, active, and configured language servers', }) @@ -151,53 +151,58 @@ end, { desc = 'Opens the Nvim LSP client log.', }) ---- Start/stop servers when nvim session looses focus and restart them on demand. --- This features is motivated by the fact that some servers may gain serious --- memory footprint and may incur performance issues. Added on May 12, 2023 -vim.api.nvim_create_augroup('LspconfigServerLifeCycle', { clear = true }) -vim.api.nvim_create_autocmd({ - 'FocusGained', -}, { - pattern = '*', - group = 'LspconfigServerLifeCycle', - desc = 'Lspconfig: restart halted lsp servers for given', - callback = function() - if _G.nvimLspconfigTimer then - _G.nvimLspconfigTimer:stop() - _G.nvimLspconfigTimer:close() - _G.nvimLspconfigTimer = nil - end - if #vim.lsp.get_active_clients() <= 1 then - vim.cmd 'LspStart' - end - end, -}) - -vim.api.nvim_create_autocmd({ - 'FocusLost', -}, { - pattern = '*', - group = 'LspconfigServerLifeCycle', - desc = 'Lspconfig: halt lsp servers when focus is lost', - callback = function() - if not _G.nvimLspconfigTimer and #vim.lsp.get_active_clients() > 0 then - local timeout = 1000 * 60 * 5 -- 5 minutes - _G.nvimLspconfigTimer = vim.loop.new_timer() - _G.nvimLspconfigTimer:start( - timeout, - 0, - vim.schedule_wrap(function() - local activeServers = #vim.lsp.get_active_clients() - vim.cmd 'LspStop' - vim.notify( - ('[lspconfig]: nvim has lost focus, stop current language servers. Number of servers left: %s'):format( - activeServers - ), - vim.log.levels.INFO - ) - _G.nvimLspconfigTimer = nil - end) - ) - end - end, -}) +if vim.g.lspconfigServerLifeCycle and type(vim.g.lspconfigServerLifeCycle) == 'table' then + local defaultTimeOut = 1000 * 60 * 30 -- 30 minutes + vim.g.lspconfigServerLifeCycle.timeout = vim.g.lspconfigServerLifeCycle.timeout or defaultTimeOut + + --- Start/stop servers when nvim session looses focus and restart them on demand. + -- This features is motivated by the fact that some servers may gain serious + -- memory footprint and may incur performance issues. Added on May 12, 2023 + vim.api.nvim_create_augroup('lspconfigServerLifeCycle', { clear = true }) + vim.api.nvim_create_autocmd({ + 'FocusGained', + }, { + pattern = '*', + group = 'lspconfigServerLifeCycle', + desc = 'Lspconfig: restart halted lsp servers for given', + callback = function() + if _G.nvimLspconfigTimer then + _G.nvimLspconfigTimer:stop() + _G.nvimLspconfigTimer:close() + _G.nvimLspconfigTimer = nil + end + if #vim.lsp.get_active_clients() <= 1 then + vim.cmd 'LspStart' + end + end, + }) + + vim.api.nvim_create_autocmd({ + 'FocusLost', + }, { + pattern = '*', + group = 'lspconfigServerLifeCycle', + desc = 'Lspconfig: halt lsp servers when focus is lost', + callback = function() + if not _G.nvimLspconfigTimer and #vim.lsp.get_active_clients() > 0 then + local timeout = vim.g.lspconfigServerLifeCycle.timeout + _G.nvimLspconfigTimer = vim.loop.new_timer() + _G.nvimLspconfigTimer:start( + timeout, + 0, + vim.schedule_wrap(function() + local activeServers = #vim.lsp.get_active_clients() + vim.cmd 'LspStop' + vim.notify( + ('[lspconfig]: nvim has lost focus, stop current language servers. Number of servers left: %s'):format( + activeServers + ), + vim.log.levels.INFO + ) + _G.nvimLspconfigTimer = nil + end) + ) + end + end, + }) +end