Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LspconfigServerLifeCycle): stop/start server upon demand #2609

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (\<c-x\>\<c-o\>).

```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 {}
Expand Down
22 changes: 22 additions & 0 deletions doc/lspconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,28 @@ contained in `:LspInfo`:
- `:LspRestart <client_id>` 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*

Expand Down
56 changes: 56 additions & 0 deletions plugin/lspconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,59 @@ api.nvim_create_user_command('LspLog', function()
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
-- See :h lspconfig-lifecycle
local lspSLifeCycle = vim.g.lspconfigServerLifeCycle
if lspSLifeCycle and type(lspSLifeCycle) == 'table' then
local defaultTimeOut = 1000 * 60 * 30 -- 30 minutes
local timeout = lspSLifeCycle.timeout or defaultTimeOut
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
_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