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 3 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
41 changes: 18 additions & 23 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 @@ -17,30 +18,24 @@ local subcommand_tbl = {
return
end

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
end
roslyn_emitter:on_stopped(function()
local attached_buffers = vim.tbl_keys(client.attached_buffers)
for _, buffer in ipairs(attached_buffers) do
vim.api.nvim_exec_autocmds("FileType", { group = "Roslyn", buffer = buffer })
end
end)

if not timer:is_closing() then
timer:close()
end
end)
)
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

-- local timer = vim.uv.new_timer()
-- timer:start(
-- 200,
-- 0,
-- vim.schedule_wrap(function()
-- vim.api.nvim_feedkeys("j", "n", false)
-- vim.api.nvim_feedkeys("k", "n", false)
-- timer:close()
-- end)
-- )
end,
},
stop = {
Expand Down
43 changes: 43 additions & 0 deletions lua/roslyn/event_emitter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local M = {}

function M:new()
local obj = {
_events = {},
}
setmetatable(obj, self)
self.__index = self
return obj
end

function M:on(event, callback)
if not self._events[event] then
self._events[event] = {}
end
table.insert(self._events[event], callback)
end

function M:emit(event, ...)
if self._events[event] then
for _, callback in ipairs(self._events[event]) do
callback(...)
end
end
end

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

function M:clear(event)
self._events[event] = nil
end

return M
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
20 changes: 20 additions & 0 deletions lua/roslyn/roslyn_emitter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local EventEmitter = require "roslyn.event_emitter"
Copy link
Owner

Choose a reason for hiding this comment

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

I think I would prefer if we didn't have this file. I think instead we can just use the interface directly which you created in event_emitter.lua

We should then add some emmylua hints to which events is available to on and emit.

I think for now it is okay to just have stopped as an event, and then we can just add started or other potential events when needed

Copy link
Author

@GustavEikaas GustavEikaas Mar 8, 2025

Choose a reason for hiding this comment

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

Did some refactorings to make a concrete implementation with the things we need. Please re-review this file and see if it looks okay to you. Trying to keep the exposed signature small and succinct.

Also added emmylua hints and examples

local RoslynEmitter = EventEmitter:new()

function RoslynEmitter:on_stopped(callback)
self:on("stopped", callback)
end

function RoslynEmitter:on_started(callback)
self:on("started", callback)
end

function RoslynEmitter:emit_stopped(...)
self:emit("stopped", ...)
end

function RoslynEmitter:emit_started(...)
self:emit("started", ...)
end

return RoslynEmitter
Loading