Skip to content

Commit

Permalink
feat!: stop lsp clients plugin (#70)
Browse files Browse the repository at this point in the history
The plugin `delete_buffers` now won't stop lsp clients, there is separate `stop_lsp_clients` plugin. Users that had `delete_buffers` set to true and want to keep the previous behavior should also set `stop_lsp_clients` to true.
  • Loading branch information
josh-nz authored Jul 9, 2024
1 parent 460d534 commit 26afec8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ require('possession').setup {
dapui = true,
neotest = true,
delete_buffers = false,
stop_lsp_clients = false,
},
telescope = {
previewer = {
Expand Down
4 changes: 4 additions & 0 deletions doc/possession.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ Closes and restores dapui view in a tab.

Closes and restores the summary window.

*possession-stop-lsp-clients*

Stops all attached language server protocol clients.

*possession-telescope*

telescope~
Expand Down
1 change: 1 addition & 0 deletions lua/possession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ local function defaults()
dapui = true,
neotest = true,
delete_buffers = false,
stop_lsp_clients = false,
},
telescope = {
previewer = {
Expand Down
1 change: 1 addition & 0 deletions lua/possession/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local plugins = {
'dapui',
'dap',
'delete_buffers',
'stop_lsp_clients',
}

local function req(plugin)
Expand Down
10 changes: 10 additions & 0 deletions lua/possession/plugins/stop_lsp_clients.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local M = {}

local utils = require('possession.utils')

function M.before_load(_, _, plugin_data)
utils.stop_lsp_clients()
return plugin_data
end

return M
1 change: 1 addition & 0 deletions lua/possession/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ function M.close(force)
end

utils.delete_all_buffers(force)
utils.stop_lsp_clients()
state.session_name = nil
end

Expand Down
8 changes: 5 additions & 3 deletions lua/possession/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,22 @@ local function lsp_get_clients(filter)
end
end

--- Stops all attached LSP clients
function M.stop_lsp_clients()
vim.lsp.stop_client(lsp_get_clients())
end

--- Delete all open buffers, avoiding potential errors
---@param force? boolean delete buffers with unsaved changes
function M.delete_all_buffers(force)
-- Deleting the current buffer before deleting other buffers will cause autocmd "BufEnter" to be triggered.
-- Lspconfig will use the invalid buffer handler in vim.schedule.
-- So make sure the current buffer is the last loaded one to delete.
local current_buffer = vim.api.nvim_get_current_buf()
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buffer) and current_buffer ~= buffer then
vim.api.nvim_buf_delete(buffer, { force = force })
end
end
vim.api.nvim_buf_delete(current_buffer, { force = force })
vim.lsp.stop_client(lsp_get_clients())
end

---@param mod string
Expand Down

0 comments on commit 26afec8

Please sign in to comment.