diff --git a/lua/tiny-inline-diagnostic/diagnostic.lua b/lua/tiny-inline-diagnostic/diagnostic.lua index b4c006d..41d1480 100644 --- a/lua/tiny-inline-diagnostic/diagnostic.lua +++ b/lua/tiny-inline-diagnostic/diagnostic.lua @@ -137,16 +137,20 @@ end local function apply_virtual_texts(opts, event) extmarks.clear(event.buf) - if not M.enabled then + + -- stylua: ignore + if not M.enabled + or not vim.diagnostic.is_enabled() + or not vim.api.nvim_buf_is_valid(event.buf) then return end - if not vim.diagnostic.is_enabled() then + local ok, diagnostics = pcall(vim.diagnostic.get, event.buf) + + if not ok then return end - local diagnostics = vim.diagnostic.get(event.buf) - if vim.tbl_isempty(diagnostics) then return end @@ -185,6 +189,16 @@ local function apply_virtual_texts(opts, event) end end +local function detach(buf) + timers.close(buf) + + print("buf", buf) + + if attached_buffers[buf] then + attached_buffers[buf] = nil + end +end + --- Set diagnostic autocmds. --- This function creates an autocmd for the `LspAttach` event. --- @param opts table - The table of options, which includes the `clear_on_insert` option and the signs to use for the virtual texts. @@ -234,15 +248,7 @@ function M.set_diagnostic_autocmds(opts) group = autocmd_ns, buffer = event.buf, callback = function() - if not vim.api.nvim_buf_is_valid(event.buf) then - return - end - - timers.close(event.buf) - - if attached_buffers[event.buf] then - attached_buffers[event.buf] = nil - end + detach(event.buf) end, }) @@ -260,6 +266,8 @@ function M.set_diagnostic_autocmds(opts) callback = function() if vim.api.nvim_buf_is_valid(event.buf) then vim.api.nvim_exec_autocmds("User", { pattern = "TinyDiagnosticEvent" }) + else + detach(event.buf) end end, desc = "Handle window resize event, force diagnostics update to fit new window width.", @@ -271,6 +279,8 @@ function M.set_diagnostic_autocmds(opts) callback = function() if vim.api.nvim_buf_is_valid(event.buf) then vim.api.nvim_exec_autocmds("User", { pattern = "TinyDiagnosticEventThrottled" }) + else + detach(event.buf) end end, desc = "Show diagnostics on cursor move, throttled.", @@ -283,6 +293,8 @@ function M.set_diagnostic_autocmds(opts) if vim.api.nvim_buf_is_valid(event.buf) then M.disable() extmarks.clear(event.buf) + else + detach(event.buf) end end, }) @@ -293,6 +305,8 @@ function M.set_diagnostic_autocmds(opts) callback = function() if vim.api.nvim_buf_is_valid(event.buf) then M.enable() + else + detach(event.buf) end end, }) diff --git a/lua/tiny-inline-diagnostic/timer.lua b/lua/tiny-inline-diagnostic/timer.lua index bcb5f97..9760cf2 100644 --- a/lua/tiny-inline-diagnostic/timer.lua +++ b/lua/tiny-inline-diagnostic/timer.lua @@ -3,32 +3,32 @@ local M = {} M.timers_by_buffer = {} function M.close_timers() - for _, timer in pairs(M.timers_by_buffer) do - if timer then - timer:close() - end - end + for _, timer in pairs(M.timers_by_buffer) do + if timer then + timer:close() + end + end end function M.close(buf) - if M.timers_by_buffer[buf] then - M.timers_by_buffer[buf]:close() - M.timers_by_buffer[buf] = nil - end + if M.timers_by_buffer[buf] then + M.timers_by_buffer[buf]:close() + M.timers_by_buffer[buf] = nil + end end function M.set_timers() - if not vim.tbl_isempty(M.timers_by_buffer) then - M.close_timers() - end + if not vim.tbl_isempty(M.timers_by_buffer) then + M.close_timers() + end - M.timers_by_buffer = {} + M.timers_by_buffer = {} end function M.add(buf, timer) - if not M.timers_by_buffer[buf] then - M.timers_by_buffer[buf] = timer - end + if not M.timers_by_buffer[buf] then + M.timers_by_buffer[buf] = timer + end end return M