-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.lua
More file actions
205 lines (197 loc) · 5.74 KB
/
events.lua
File metadata and controls
205 lines (197 loc) · 5.74 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
local state = require("peekstack.core.stack.state")
local common = require("peekstack.core.stack.common")
local M = {}
local layout, popup, feedback, history, user_events
local function deps()
if not layout then
layout = require("peekstack.core.layout")
popup = require("peekstack.core.popup")
feedback = require("peekstack.ui.feedback")
history = require("peekstack.core.history")
user_events = require("peekstack.core.user_events")
end
end
---@param stack PeekstackStackModel
---@param idx integer
---@param item PeekstackPopupModel
---@param opts? { close_window?: boolean, highlight_origin?: boolean }
local function remove_stack_popup(stack, idx, item, opts)
opts = opts or {}
if stack.zoomed_id == item.id then
stack.zoomed_id = nil
end
if opts.highlight_origin ~= false then
feedback.highlight_origin(item.origin)
end
common.emit_popup_event("PeekstackClose", item, stack.root_winid)
history.push_entry(stack, history.build_entry(item, idx))
user_events.emit("PeekstackHistoryPush", {
popup_id = item.id,
location = item.location,
root_winid = stack.root_winid,
})
state.unindex_popup(item)
table.remove(stack.popups, idx)
if opts.close_window ~= false and item.winid and vim.api.nvim_win_is_valid(item.winid) then
popup.close(item)
end
end
---@param id integer
---@param item PeekstackPopupModel
---@param opts? { close_window?: boolean }
local function remove_ephemeral(id, item, opts)
opts = opts or {}
if opts.close_window ~= false and item.winid and vim.api.nvim_win_is_valid(item.winid) then
popup.close(item)
end
state.unregister_ephemeral(id)
user_events.emit(
"PeekstackClose",
user_events.build_popup_data(item, item.origin and item.origin.winid or 0, { ephemeral = true })
)
end
---@param winid integer
function M.handle_win_closed(winid)
deps()
if state.suppress_win_events then
return
end
if state.stack_view_wins[winid] then
state.stack_view_wins[winid] = nil
return
end
for id, item in pairs(state.ephemerals) do
if item.winid == winid then
user_events.emit("PeekstackClose", user_events.build_popup_data(item, item.origin and item.origin.winid or 0))
state.unregister_ephemeral(id)
end
end
for root_winid, stack in pairs(state.stacks) do
if stack.root_winid == winid then
for idx = #stack.popups, 1, -1 do
local item = stack.popups[idx]
common.emit_popup_event("PeekstackClose", item, root_winid)
history.push_entry(stack, history.build_entry(item, idx))
table.remove(stack.popups, idx)
state.unindex_popup(item)
popup.close(item)
end
state.stacks[root_winid] = nil
else
local removed = false
local focused_removed = false
for idx = #stack.popups, 1, -1 do
local item = stack.popups[idx]
if item.winid == winid then
if stack.focused_id == item.id then
focused_removed = true
end
if stack.zoomed_id == item.id then
stack.zoomed_id = nil
end
common.emit_popup_event("PeekstackClose", item, root_winid)
feedback.highlight_origin(item.origin)
table.remove(stack.popups, idx)
state.unindex_popup(item)
popup.close(item)
removed = true
end
end
if removed then
if focused_removed then
if #stack.popups > 0 then
stack.focused_id = stack.popups[#stack.popups].id
else
stack.focused_id = nil
end
end
layout.reflow(stack)
end
end
end
end
---@param bufnr integer
function M.handle_buf_wipeout(bufnr)
deps()
if state.suppress_win_events then
return
end
for id, item in pairs(state.ephemerals) do
if item.bufnr == bufnr then
remove_ephemeral(id, item, { close_window = false })
end
end
for _, stack in pairs(state.stacks) do
local removed = false
local focused_removed = false
for idx = #stack.popups, 1, -1 do
local item = stack.popups[idx]
if item.bufnr == bufnr then
if stack.focused_id == item.id then
focused_removed = true
end
remove_stack_popup(stack, idx, item, { close_window = false })
removed = true
end
end
if removed then
if focused_removed then
if #stack.popups > 0 then
stack.focused_id = stack.popups[#stack.popups].id
else
stack.focused_id = nil
end
end
layout.reflow(stack)
end
end
end
---@param bufnr integer
function M.handle_origin_wipeout(bufnr)
deps()
local function should_close_for_origin(item)
if not (item.origin and item.origin.bufnr == bufnr) then
return false
end
if item.origin_is_popup == true then
return false
end
if vim.api.nvim_buf_is_valid(bufnr) then
local ft = vim.bo[bufnr].filetype
if ft == "peekstack-stack" or ft == "peekstack-stack-help" then
return false
end
end
return true
end
for id, item in pairs(state.ephemerals) do
if should_close_for_origin(item) then
remove_ephemeral(id, item)
end
end
for _, stack in pairs(state.stacks) do
local removed = false
local focused_removed = false
for idx = #stack.popups, 1, -1 do
local item = stack.popups[idx]
if should_close_for_origin(item) then
if stack.focused_id == item.id then
focused_removed = true
end
remove_stack_popup(stack, idx, item)
removed = true
end
end
if removed then
if focused_removed then
if #stack.popups > 0 then
stack.focused_id = stack.popups[#stack.popups].id
else
stack.focused_id = nil
end
end
layout.reflow(stack)
end
end
end
return M