-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.lua
More file actions
149 lines (130 loc) · 3.83 KB
/
events.lua
File metadata and controls
149 lines (130 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local config = require("peekstack.config")
local stack = require("peekstack.core.stack")
local timer_util = require("peekstack.util.timer")
local M = {}
---@type uv.uv_timer_t?
local reflow_timer = nil
local REFLOW_DEBOUNCE_MS = 80
---@type table<integer, boolean>
local popup_cursor_buffers = {}
local function reset_reflow_timer()
local store = timer_util.get_store()
timer_util.close(store.reflow)
timer_util.close(reflow_timer)
store.reflow = nil
reflow_timer = nil
end
local function debounced_reflow()
if reflow_timer then
reflow_timer:stop()
else
reflow_timer = vim.uv.new_timer()
timer_util.get_store().reflow = reflow_timer
end
reflow_timer:start(REFLOW_DEBOUNCE_MS, 0, function()
reflow_timer:stop()
vim.schedule(function()
stack.reflow_all()
require("peekstack.ui.stack_view").resize_all()
end)
end)
end
---@param group integer
---@param bufnr integer
local function ensure_popup_cursor_tracking(group, bufnr)
if popup_cursor_buffers[bufnr] then
return
end
popup_cursor_buffers[bufnr] = true
vim.api.nvim_create_autocmd("CursorMoved", {
group = group,
buffer = bufnr,
callback = function()
local winid = vim.api.nvim_get_current_win()
if vim.w[winid].peekstack_popup_id == nil then
return
end
stack.touch(winid)
end,
})
end
---Close all ephemeral popups across all stacks and reflow
local function close_ephemeral_popups()
stack.close_ephemerals()
end
---Check if a window is a floating popup
---@param winid integer
---@return boolean
local function is_floating_window(winid)
local win_cfg = vim.api.nvim_win_get_config(winid)
return win_cfg.relative ~= ""
end
function M.setup()
reset_reflow_timer()
popup_cursor_buffers = {}
local group = vim.api.nvim_create_augroup("PeekstackEvents", { clear = true })
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
callback = function(args)
local winid = tonumber(args.match)
if winid then
stack.handle_win_closed(winid)
end
end,
})
vim.api.nvim_create_autocmd("BufWipeout", {
group = group,
callback = function(args)
stack.handle_buf_wipeout(args.buf)
stack.handle_origin_wipeout(args.buf)
popup_cursor_buffers[args.buf] = nil
end,
})
vim.api.nvim_create_autocmd({ "VimResized", "WinResized" }, {
group = group,
callback = debounced_reflow,
})
vim.api.nvim_create_autocmd("WinEnter", {
group = group,
callback = function()
local winid = vim.api.nvim_get_current_win()
if vim.w[winid].peekstack_popup_id ~= nil then
local bufnr = vim.api.nvim_win_get_buf(winid)
ensure_popup_cursor_tracking(group, bufnr)
end
if is_floating_window(winid) then
stack.touch(winid)
local layout = require("peekstack.core.layout")
local s, _ = stack.find_by_winid(winid)
if s then
layout.update_focus_zindex(s, winid)
end
end
end,
})
local current_winid = vim.api.nvim_get_current_win()
if vim.w[current_winid].peekstack_popup_id ~= nil then
ensure_popup_cursor_tracking(group, vim.api.nvim_win_get_buf(current_winid))
end
local cfg = config.get()
local close_events = cfg.ui.quick_peek and cfg.ui.quick_peek.close_events
or { "CursorMoved", "InsertEnter", "BufLeave", "WinLeave" }
for _, event in ipairs(close_events) do
vim.api.nvim_create_autocmd(event, {
group = group,
callback = close_ephemeral_popups,
})
end
vim.api.nvim_create_autocmd("User", {
group = group,
pattern = { "PeekstackPush", "PeekstackClose" },
callback = function()
require("peekstack.ui.stack_view").refresh_all()
end,
})
if cfg.ui.popup.auto_close and cfg.ui.popup.auto_close.enabled then
local cleanup = require("peekstack.core.cleanup")
cleanup.start()
end
end
return M