Skip to content

Commit

Permalink
fix(ui): special handling for floats closed before VimEnter. Seems th…
Browse files Browse the repository at this point in the history
…at WinClosed events dont execute before that. Fixes #1390
  • Loading branch information
folke committed Mar 27, 2024
1 parent d37a76b commit eefb897
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 26 deletions.
30 changes: 30 additions & 0 deletions lua/lazy/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ function M.throttle(ms, fn)
end
end

--- Creates a weak reference to an object.
--- Calling the returned function will return the object if it has not been garbage collected.
---@generic T: table
---@param obj T
---@return T|fun():T?
function M.weak(obj)
local weak = { _obj = obj }
---@return table<any, any>
local function get()
local ret = rawget(weak, "_obj")
return ret == nil and error("Object has been garbage collected", 2) or ret
end
local mt = {
__mode = "v",
__call = function(t)
return rawget(t, "_obj")
end,
__index = function(_, k)
return get()[k]
end,
__newindex = function(_, k, v)
get()[k] = v
end,
__pairs = function()
return pairs(get())
end,
}
return setmetatable(weak, mt)
end

---@class LazyCmdOptions: LazyFloatOptions
---@field cwd? string
---@field env? table<string,string>
Expand Down
69 changes: 50 additions & 19 deletions lua/lazy/view/float.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local ViewConfig = require("lazy.view.config")
---@field win_opts LazyWinOpts
---@field backdrop_buf number
---@field backdrop_win number
---@field id number
---@overload fun(opts?:LazyFloatOptions):LazyFloat
local M = {}

Expand All @@ -33,6 +34,12 @@ setmetatable(M, {
end,
})

local _id = 0
local function next_id()
_id = _id + 1
return _id
end

---@param opts? LazyFloatOptions
function M.new(opts)
local self = setmetatable({}, { __index = M })
Expand All @@ -42,6 +49,7 @@ end
---@param opts? LazyFloatOptions
function M:init(opts)
require("lazy.view.colors").setup()
self.id = next_id()
self.opts = vim.tbl_deep_extend("force", {
size = Config.options.ui.size,
style = "minimal",
Expand All @@ -65,8 +73,13 @@ function M:init(opts)
title_pos = self.opts.title and self.opts.title_pos or nil,
}
self:mount()
self:on_key(ViewConfig.keys.close, self.close)
self:on({ "WinLeave", "BufDelete", "BufHidden" }, self.close, { once = false })
self:on("VimEnter", function()
vim.schedule(function()
if not self:win_valid() then
self:close()
end
end)
end, { buffer = false })
return self
end

Expand Down Expand Up @@ -138,7 +151,13 @@ function M:mount()

self:layout()
self.win = vim.api.nvim_open_win(self.buf, true, self.win_opts)
self:on("WinClosed", function()
self:close()
self:augroup(true)
end, { win = true })
self:focus()
self:on_key(ViewConfig.keys.close, self.close)
self:on({ "BufDelete", "BufHidden" }, self.close)

if vim.bo[self.buf].buftype == "" then
vim.bo[self.buf].buftype = "nofile"
Expand Down Expand Up @@ -185,27 +204,38 @@ function M:mount()
})
end

---@param clear? boolean
function M:augroup(clear)
return vim.api.nvim_create_augroup("trouble.window." .. self.id, { clear = clear == true })
end

---@param events string|string[]
---@param fn fun(self?):boolean?
---@param opts? table
---@param fn fun(self:LazyFloat, event:{buf:number}):boolean?
---@param opts? vim.api.keyset.create_autocmd | {buffer: false, win?:boolean}
function M:on(events, fn, opts)
if type(events) == "string" then
events = { events }
opts = opts or {}
if opts.win then
opts.pattern = self.win .. ""
opts.win = nil
elseif opts.buffer == nil then
opts.buffer = self.buf
elseif opts.buffer == false then
opts.buffer = nil
end
for _, e in ipairs(events) do
local event, pattern = e:match("(%w+) (%w+)")
event = event or e
vim.api.nvim_create_autocmd(
event,
vim.tbl_extend("force", {
pattern = pattern,
buffer = (not pattern) and self.buf or nil,
callback = function()
return fn(self)
end,
}, opts or {})
)
if opts.pattern then
opts.buffer = nil
end
local _self = Util.weak(self)
opts.callback = function(e)
local this = _self()
if not this then
-- delete the autocmd
return true
end
return fn(this, e)
end
opts.group = self:augroup()
vim.api.nvim_create_autocmd(events, opts)
end

---@param key string
Expand All @@ -223,6 +253,7 @@ end

---@param opts? {wipe:boolean}
function M:close(opts)
self:augroup(true)
local buf = self.buf
local win = self.win