Skip to content

Commit

Permalink
Merge pull request #54 from Klafyvel/feat-graceful-exit
Browse files Browse the repository at this point in the history
feat: Cleanly exit previous session when re-creating a buffer.
  • Loading branch information
Klafyvel authored Dec 8, 2024
2 parents 7e2b192 + 2f0ccc8 commit 25d3c0a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
17 changes: 16 additions & 1 deletion lua/smuggler/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function M.buffer(bufnbr, force, settings)
if not closed and not force then
return current_config
elseif not closed and force then
current_config.socket:close()
M.terminate(current_config)
end
end
local bufname = vim.fn.bufname(bufnbr)
Expand Down Expand Up @@ -152,6 +152,17 @@ function M.buffer(bufnbr, force, settings)
buffer.update_diagnostic_display_event.clear()
end
end)
nio.run(function()
buffer.stopped_event.wait()
log.debug("Closing buffer", buffer.number)
ui.hide_chunk_highlights(buffer.number)
ui.hide_evaluation_results(buffer.number)
ui.reset_diagnostics(buffer.number)
ui.disable_autocommands(buffer.number)
if buffer.socket ~= nil then
buffer.socket:close()
end
end)
nio.run(function()
buffer.socket_chosen_event.wait()
protocol.runclient(buffer.number)
Expand All @@ -160,6 +171,10 @@ function M.buffer(bufnbr, force, settings)
return buffer
end

function M.terminate(buffer)
buffer.stopped_event.set()
end

function M.chunk(linestart, linestop, colstart, colstop, valid)
if valid == nil then
valid = true
Expand Down
35 changes: 24 additions & 11 deletions lua/smuggler/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ M.PROTOCOL_VERSION = vim.version.parse("0.5.x")

local uv = vim.loop
local nio = require("nio")
local config = require("smuggler.config")
local snitch = require("smuggler.snitch")
local buffers = require("smuggler.buffers")
local run = require("smuggler.run")
local log = require("smuggler.log")
local mpack = require("smuggler.partial_mpack")
Expand All @@ -16,6 +14,7 @@ function M.serialize_requests(bufnbr)
local bufconfig = run.buffers[bufnbr]
local queue = bufconfig.outgoing_queue
local handle = bufconfig.socket
log.trace("Starting request serialization thread.")
while not bufconfig.stopped_event.is_set() do
local data = queue.get()
if data.payload == nil then
Expand Down Expand Up @@ -79,7 +78,7 @@ function M.deserialize_answers(bufnbr)
end)
end

function format_version(ver)
function M.format_version(ver)
return string.format("%d.%d.%d", ver.major, ver.minor, ver.patch)
end

Expand All @@ -93,22 +92,23 @@ function M.verify_protocol(handshake)
if vim.version.gt(M.PROTOCOL_VERSION, serverprotocolversion) then
log.fatal(
"The server uses version: "
.. format_version(serverprotocolversion)
.. M.format_version(serverprotocolversion)
.. " and we expect: "
.. format_version(M.PROTOCOL_VERSION)
.. M.format_version(M.PROTOCOL_VERSION)
.. ". Consider upgrading REPLSmuggler.jl."
)
success = false
elseif vim.version.lt(M.PROTOCOL_VERSION, serverprotocolversion) then
log.fatal(
"The server uses version: "
.. format_version(serverprotocolversion)
.. M.format_version(serverprotocolversion)
.. " and we expect: "
.. format_version(M.PROTOCOL_VERSION)
.. M.format_version(M.PROTOCOL_VERSION)
.. ". Consider upgrading nvim-smuggler."
)
success = false
end
log.trace("Protocol verification success status:", success)
return success
end

Expand Down Expand Up @@ -161,22 +161,34 @@ function M.runclient(bufnbr)
end)
nio.run(function()
log.trace("Waiting for session initialization before serialization starts.")
bufconfig.session_initialized_event.wait()
nio.first({ bufconfig.session_initialized_event.wait, bufconfig.stopped_event.wait })
if bufconfig.stopped_event.is_set() then
return
end
M.serialize_requests(bufnbr)
end)
nio.run(function()
log.trace("Waiting for session initialization before session configuration.")
bufconfig.session_initialized_event.wait()
nio.first({ bufconfig.session_initialized_event.wait, bufconfig.stopped_event.wait })
if bufconfig.stopped_event.is_set() then
return
end
M.configure_session(bufnbr)
end)
nio.run(function()
log.trace("Waiting for session connection before deserialization starts.")
bufconfig.session_connected_event.wait()
nio.first({ bufconfig.session_connected_event.wait, bufconfig.stopped_event.wait })
if bufconfig.stopped_event.is_set() then
return
end
M.deserialize_answers(bufnbr)
end)
nio.run(function()
log.trace("Waiting for session connection before requests treatement starts.")
bufconfig.session_connected_event.wait()
nio.first({ bufconfig.session_connected_event.wait, bufconfig.stopped_event.wait })
if bufconfig.stopped_event.is_set() then
return
end
M.treat_incoming(bufnbr)
end)
end
Expand Down Expand Up @@ -216,6 +228,7 @@ function M.exit(bufnbr)
end

function M.configure_session(bufnbr, settings)
log.trace("Configuring session for buffer", bufnbr)
if bufnbr == nil then
bufnbr = vim.api.nvim_get_current_buf()
end
Expand Down
23 changes: 12 additions & 11 deletions lua/smuggler/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ end
function ui.hide_chunk_highlights(bufnbr)
bufnbr = (bufnbr == nil) and vim.api.nvim_get_current_buf() or bufnbr
local namespace = nio.api.nvim_create_namespace("smuggler")
for i, chunk in pairs(run.buffers[bufnbr].evaluated_chunks) do
for _, chunk in pairs(run.buffers[bufnbr].evaluated_chunks) do
if chunk.extmark ~= nil then
local extmark = vim.api.nvim_buf_get_extmark_by_id(bufnbr, namespace, chunk.extmark, { details = true })
if #extmark == 0 then -- failed to retrieve the extmark.
Expand All @@ -185,15 +185,8 @@ function ui.hide_chunk_highlights(bufnbr)
.. ". That's likely a bug in nvim-smuggler"
)
else
vim.api.nvim_buf_set_extmark(bufnbr, namespace, extmark[1], extmark[2], {
id = chunk.extmark,
end_row = extmark[3].end_row,
end_col = extmark[3].end_col,
sign_text = "",
sign_hl_group = "",
end_right_gravity = true,
right_gravity = false,
})
vim.api.nvim_buf_del_extmark(bufnbr, namespace, chunk.extmark)
chunk.extmark = nil
end
end
end
Expand Down Expand Up @@ -249,7 +242,7 @@ end
function ui.place_chunk_highlights(bufnbr)
if config.ui.show_eval then
bufnbr = (bufnbr == nil) and vim.api.nvim_get_current_buf() or bufnbr
chunks = run.buffers[bufnbr].evaluated_chunks
local chunks = run.buffers[bufnbr].evaluated_chunks
for _, chunk in pairs(chunks) do
ui.highlight_chunk(bufnbr, chunk)
end
Expand Down Expand Up @@ -455,6 +448,14 @@ function ui.hide_diagnostics(bufnbr)
buffer.diagnostics_shown = false
end

function ui.reset_diagnostics(bufnbr)
bufnbr = (bufnbr == nil) and vim.api.nvim_get_current_buf() or bufnbr
local buffer = run.buffers[bufnbr]
local namespace = nio.api.nvim_create_namespace("smuggler")
vim.diagnostic.reset(namespace, bufnbr)
buffer.diagnostics_shown = false
end

function ui.make_quickfix_list(diagnostic)
local list = {}
for stackidx, stackrow in ipairs(diagnostic.stacktrace) do
Expand Down

0 comments on commit 25d3c0a

Please sign in to comment.