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

fix: roslyn restart command not working #162

Merged
merged 6 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 11 additions & 20 deletions lua/roslyn/commands.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local roslyn_emitter = require("roslyn.roslyn_emitter")
-- Huge credits to mrcjkb
-- https://github.com/mrcjkb/rustaceanvim/blob/2fa45427c01ded4d3ecca72e357f8a60fd8e46d4/lua/rustaceanvim/commands/init.lua
local M = {}
Expand All @@ -19,28 +20,18 @@ local subcommand_tbl = {

local attached_buffers = vim.tbl_keys(client.attached_buffers)

-- TODO: Change this to `client:request` when minimal version is `0.11`
---@diagnostic disable-next-line: missing-parameter
client.stop()

local timer = vim.uv.new_timer()
timer:start(
500,
100,
vim.schedule_wrap(function()
-- TODO: Change this to `client:request` when minimal version is `0.11`
---@diagnostic disable-next-line: missing-parameter
if client.is_stopped() then
for _, buffer in ipairs(attached_buffers) do
vim.api.nvim_exec_autocmds("FileType", { group = "Roslyn", buffer = buffer })
end
local function restart_lsp(remove_listener)
for _, buffer in ipairs(attached_buffers) do
if vim.api.nvim_buf_is_valid(buffer) then
vim.api.nvim_exec_autocmds("FileType", { group = "Roslyn", buffer = buffer })
end
end
remove_listener()
end

if not timer:is_closing() then
timer:close()
end
end)
)
roslyn_emitter:on_stopped(restart_lsp)

client.stop(true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to force it? It works for me without, and it prints out this message of the server quit with exit code x which would be nice to avoid if possible

end,
},
stop = {
Expand Down
2 changes: 2 additions & 0 deletions lua/roslyn/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local roslyn_emitter = require("roslyn.roslyn_emitter")
local M = {}

---@param bufnr integer
Expand Down Expand Up @@ -108,6 +109,7 @@ function M.start(bufnr, root_dir, on_init)
config.on_exit = function(code, signal, client_id)
vim.g.roslyn_nvim_selected_solution = nil
vim.schedule(function()
roslyn_emitter:emit_stopped()
vim.notify("Roslyn server stopped", vim.log.levels.INFO, { title = "roslyn.nvim" })
end)
if roslyn_config.config.on_exit then
Expand Down
64 changes: 64 additions & 0 deletions lua/roslyn/roslyn_emitter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local M = {
events = {},
}

local function on(event, callback)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually first thinking about making this and emit public and have the type of event be literal "stopped" to get some typing when using it. Then we could just add a new literal string as alternative to event to get hints if we add more events, instead of creating new methods.

However, I don't have that strong opinions on that after some thinking and am okay with this as well. What do you think?

if not M.events[event] then
M.events[event] = {}
end
table.insert(M.events[event], callback)
return function()
M:off(event, callback)
end
end

local function emit(event, ...)
if M.events[event] then
for _, callback in ipairs(M.events[event]) do
callback(...)
end
end
end

local function off(event, callback)
if not M.events[event] then
return
end
for i, cb in ipairs(M.events[event]) do
if cb == callback then
table.remove(M.events[event], i)
break
end
end
end

---@param callback fun(remove_listener: fun()) # Callback function that is invoked when the Roslyn server is stopped. Accepts a function parameter for removing the event listener.
---@return fun() # Returns a cleanup function for removing the listener manually.
---```lua
--- local remove_listener = M:on_stopped(function(remove_listener2)
--- --For oneshot jobs
--- remove_listener2()
--- end)
---
--- remove_listener()
---```
function M:on_stopped(callback)
local wrapped
wrapped = function()
callback(function()
off("stopped", wrapped)
end)
end

on("stopped", wrapped)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just return on here since that is also returning a a cleanup function?

return function()
off("stopped", wrapped)
end
end

--- Emits a stop event notifying all the M:on_stopped subscribers
function M:emit_stopped(...)
emit("stopped", ...)
end

return M
Loading