-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
332 lines (288 loc) · 7.78 KB
/
init.lua
File metadata and controls
332 lines (288 loc) · 7.78 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
local config = require("peekstack.config")
local keymaps = require("peekstack.ui.stack_view.keymaps")
local renderer = require("peekstack.ui.stack_view.render")
local M = {}
---@type table<integer, PeekstackStackViewState>
local states = {}
---@type integer?
local tab_cleanup_group = nil
---@param s PeekstackStackViewState
local function cleanup_state(s)
if s.help_winid and vim.api.nvim_win_is_valid(s.help_winid) then
pcall(vim.api.nvim_win_close, s.help_winid, true)
end
s.help_winid = nil
s.help_bufnr = nil
if s.help_augroup then
pcall(vim.api.nvim_del_augroup_by_id, s.help_augroup)
s.help_augroup = nil
end
if s.autoclose_group then
pcall(vim.api.nvim_del_augroup_by_id, s.autoclose_group)
s.autoclose_group = nil
end
end
local function cleanup_invalid_states()
for tabpage, s in pairs(states) do
if not vim.api.nvim_tabpage_is_valid(tabpage) then
cleanup_state(s)
states[tabpage] = nil
end
end
end
---Setup stack view autocmds.
function M.setup()
if tab_cleanup_group then
return
end
tab_cleanup_group = vim.api.nvim_create_augroup("PeekstackStackViewTabCleanup", { clear = true })
vim.api.nvim_create_autocmd("TabClosed", {
group = tab_cleanup_group,
callback = function()
cleanup_invalid_states()
end,
})
end
---@return PeekstackStackViewState
local function get_state()
local tabpage = vim.api.nvim_get_current_tabpage()
if not states[tabpage] then
states[tabpage] = {
bufnr = nil,
winid = nil,
root_winid = nil,
line_to_id = {},
render_keys = {},
filter = nil,
header_lines = 0,
help_bufnr = nil,
help_winid = nil,
help_augroup = nil,
autoclose_group = nil,
autoclose_suspended = 0,
preview_ts_cache = {},
}
end
return states[tabpage]
end
---@param s PeekstackStackViewState
---@return boolean
local function is_open(s)
return s.winid ~= nil and vim.api.nvim_win_is_valid(s.winid)
end
---@param s PeekstackStackViewState
---@return boolean
local function is_ready(s)
return s.bufnr ~= nil and s.winid ~= nil and vim.api.nvim_buf_is_valid(s.bufnr) and vim.api.nvim_win_is_valid(s.winid)
end
---@param s PeekstackStackViewState
local function focus_stack_view(s)
if s.winid and vim.api.nvim_win_is_valid(s.winid) then
vim.api.nvim_set_current_win(s.winid)
end
end
---@param s PeekstackStackViewState
---@return boolean
local function should_autoclose(s)
if s.autoclose_suspended and s.autoclose_suspended > 0 then
return false
end
if not is_open(s) then
return false
end
local current = vim.api.nvim_get_current_win()
if s.winid and current == s.winid then
return false
end
if s.help_winid and vim.api.nvim_win_is_valid(s.help_winid) and current == s.help_winid then
return false
end
return true
end
---@param s PeekstackStackViewState
local function render_state(s)
renderer.render(s, is_ready)
end
---@return PeekstackStackViewKeymapDeps
local function keymap_deps()
return {
render = function(s)
render_state(s)
end,
toggle = function()
M.toggle()
end,
is_open = function(s)
return is_open(s)
end,
focus_stack_view = function(s)
focus_stack_view(s)
end,
}
end
---@param s PeekstackStackViewState
local function reset_open_state(s)
if s.autoclose_group then
pcall(vim.api.nvim_del_augroup_by_id, s.autoclose_group)
end
s.autoclose_group = nil
if s.winid and vim.api.nvim_win_is_valid(s.winid) then
pcall(vim.api.nvim_win_close, s.winid, true)
end
s.winid = nil
s.bufnr = nil
s.root_winid = nil
s.render_keys = {}
s.autoclose_suspended = 0
s.help_augroup = nil
s.preview_ts_cache = {}
end
---Find a non-floating window to use as root.
---@return integer
local function find_root_winid()
local winid = vim.api.nvim_get_current_win()
local cfg = vim.api.nvim_win_get_config(winid)
if cfg.relative == "" then
return winid
end
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
local c = vim.api.nvim_win_get_config(w)
if c.relative == "" then
return w
end
end
return winid
end
---@return integer
local function editor_lines()
return math.max(vim.o.lines - vim.o.cmdheight, 1)
end
---@return PeekstackRenderWinOpts
local function stack_view_win_config()
local columns = vim.o.columns
local lines = editor_lines()
local position = config.get().ui.stack_view.position or "right"
local width = math.max(30, math.floor(columns * 0.3))
width = math.min(width, columns)
local height = math.max(6, lines - 2)
height = math.min(height, lines)
local row = 0
local col = math.max(columns - width, 0)
if position == "left" then
col = 0
elseif position == "bottom" then
width = columns
height = math.max(6, math.floor(lines * 0.3))
height = math.min(height, lines)
row = math.max(lines - height, 0)
col = 0
end
return {
relative = "editor",
row = row,
col = col,
width = width,
height = height,
style = "minimal",
border = "rounded",
focusable = true,
zindex = 100,
title = "Stack View",
title_pos = "center",
}
end
---Open the stack view.
function M.open()
M.setup()
local s = get_state()
if is_open(s) then
vim.api.nvim_set_current_win(s.winid)
render_state(s)
return
end
s.autoclose_suspended = 0
s.root_winid = find_root_winid()
s.bufnr = vim.api.nvim_create_buf(false, true)
s.winid = vim.api.nvim_open_win(s.bufnr, true, stack_view_win_config())
s.render_keys = {}
vim.wo[s.winid].cursorline = true
vim.wo[s.winid].winhighlight = "CursorLine:PeekstackStackViewCursorLine"
vim.api.nvim_win_set_var(s.winid, "peekstack_root_winid", s.root_winid)
require("peekstack.core.stack")._register_stack_view_win(s.winid)
local fs = require("peekstack.util.fs")
fs.configure_buffer(s.bufnr)
vim.bo[s.bufnr].modifiable = false
vim.bo[s.bufnr].filetype = "peekstack-stack"
local group_name = string.format("PeekstackStackViewAutoClose:%d", s.bufnr)
local au_group = vim.api.nvim_create_augroup(group_name, { clear = true })
s.autoclose_group = au_group
vim.api.nvim_create_autocmd("WinLeave", {
group = au_group,
buffer = s.bufnr,
callback = function()
vim.schedule(function()
if not should_autoclose(s) then
return
end
keymaps.close_help(s, nil, keymap_deps())
reset_open_state(s)
end)
end,
})
vim.api.nvim_create_autocmd("CursorMoved", {
group = au_group,
buffer = s.bufnr,
callback = function()
keymaps.ensure_non_header_cursor(s)
end,
})
keymaps.apply(s, keymap_deps())
render_state(s)
end
---Toggle the stack view (open if closed, close if open).
function M.toggle()
local s = get_state()
if is_open(s) then
keymaps.close_help(s, nil, keymap_deps())
reset_open_state(s)
return
end
M.open()
end
---Re-render all open stack views (called on push/close events).
function M.refresh_all()
for _, s in pairs(states) do
if is_open(s) and s.bufnr and vim.api.nvim_buf_is_valid(s.bufnr) then
render_state(s)
end
end
end
---Resize and re-render all open stack views (called on VimResized/WinResized).
function M.resize_all()
for _, s in pairs(states) do
if is_open(s) and s.winid and vim.api.nvim_win_is_valid(s.winid) then
vim.api.nvim_win_set_config(s.winid, stack_view_win_config())
render_state(s)
end
end
end
---Get stack view state (for testing).
---@return PeekstackStackViewState
function M._get_state()
return get_state()
end
---Get stack view state count (for testing).
---@return integer
function M._state_count()
local count = 0
for _ in pairs(states) do
count = count + 1
end
return count
end
---Render stack view state (for testing).
---@param s PeekstackStackViewState
function M._render(s)
render_state(s)
end
return M