Skip to content

Commit

Permalink
feat: implemented LspRestart with re-attach
Browse files Browse the repository at this point in the history
  • Loading branch information
konradmalik committed Oct 29, 2023
1 parent 7f3c9b4 commit bc98bc7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 28 deletions.
94 changes: 73 additions & 21 deletions config/native/lua/konrad/lsp/commands.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
local lsp = require("konrad.lsp")
local popup = require("plenary.popup")

local function create_window()
local config = {}
---@param args string
---@return number?
local function parse_int_arg(args)
local arguments = vim.split(args, "%s")
for _, v in pairs(arguments) do
if v:find("^[0-9]+$") then
return tonumber(v)
end
end
return nil
end

---@param filter table?
local function restart_servers(filter)
local clients = vim.lsp.get_clients(filter)
local detach_clients = {}
for _, client in ipairs(clients) do
detach_clients[client.id] = { client, client.attached_buffers }
vim.lsp.stop_client(client.id)
end

local timer = vim.loop.new_timer()
timer:start(
500,
100,
vim.schedule_wrap(function()
for old_client_id, tuple in pairs(detach_clients) do
local client, attached_buffers = unpack(tuple)
if client.is_stopped() then
for buf in pairs(attached_buffers) do
lsp.start_and_attach(client.config, buf)
end
detach_clients[old_client_id] = nil
end
end

if next(detach_clients) == nil and not timer:is_closing() then
timer:close()
end
end)
)
end

---@param config table?
---@return table
local function create_window(config)
config = config or {}
local width = config.width or 100
local height = config.height or 40
local borderchars = config.borderchars or { "", "", "", "", "", "", "", "" }
Expand All @@ -25,17 +71,15 @@ end
vim.api.nvim_create_user_command("LspInfo", function()
local replacement = {}
for i, client in ipairs(vim.lsp.get_clients()) do
local name = client.name
local cmd = table.concat(client.config.cmd, ",")
local root_dir = client.config.root_dir
local buffers = table.concat(vim.tbl_keys(client.attached_buffers), ",")
replacement[i] = string.format(
"id: %s, name: %s, cmd: %s, root_dir: %s, buffers: %s",
client.id,
name,
cmd,
root_dir,
buffers
if i > 1 then
table.insert(replacement, "---------------")
end
table.insert(replacement, string.format("Client: %s (%s)", client.name, client.id))
table.insert(replacement, string.format("Root Dir: %s", client.config.root_dir))
table.insert(replacement, string.format("Command: %s", table.concat(client.config.cmd, " ")))
table.insert(
replacement,
string.format("Attached Bufs: [ %s ]", table.concat(vim.tbl_keys(client.attached_buffers), ", "))
)
end
local info = create_window()
Expand All @@ -46,13 +90,7 @@ end, {
})

vim.api.nvim_create_user_command("LspStop", function(info)
local server_id
local arguments = vim.split(info.args, "%s")
for _, v in pairs(arguments) do
if v:find("^[0-9]+$") then
server_id = tonumber(v)
end
end
local server_id = parse_int_arg(info.args)
if server_id then
vim.notify("stoping server with id: " .. server_id)
vim.lsp.stop_client(server_id)
Expand All @@ -62,5 +100,19 @@ vim.api.nvim_create_user_command("LspStop", function(info)
end
end, {
desc = "Stops specified LSP by id",
nargs = 1,
nargs = "?",
})

vim.api.nvim_create_user_command("LspRestart", function(info)
local server_id = parse_int_arg(info.args)
if server_id then
vim.notify("restarting server with id: " .. server_id)
restart_servers({ id = server_id })
else
vim.notify("restarting all lsp servers")
restart_servers()
end
end, {
desc = "Restarts specified LSPs by id",
nargs = "?",
})
12 changes: 5 additions & 7 deletions config/native/lua/konrad/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@ local M = {}

---starts if needed and attaches to the current buffer
---@param config table
---@return integer
M.start_and_attach = function(config)
---@param bufnr integer? buffer to attach to
M.start_and_attach = function(config, bufnr)
initialize_once()

local made_config = require("konrad.lsp.configs").make_config(config)
local client_id = vim.lsp.start(made_config)
local target_buf = bufnr or 0
local client_id = vim.lsp.start(made_config, { bufnr = target_buf })
if not client_id then
vim.notify("cannot start lsp: " .. config.cmd, vim.log.levels.ERROR)
return 0
vim.notify("cannot start lsp: " .. config.cmd[1], vim.log.levels.ERROR)
end
vim.lsp.buf_attach_client(vim.api.nvim_get_current_buf(), client_id)
return client_id
end

return M

0 comments on commit bc98bc7

Please sign in to comment.