-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto.lua
More file actions
222 lines (188 loc) · 5 KB
/
auto.lua
File metadata and controls
222 lines (188 loc) · 5 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
local config = require("peekstack.config")
local fs = require("peekstack.util.fs")
local persist = require("peekstack.persist")
local stack = require("peekstack.core.stack")
local timer_util = require("peekstack.util.timer")
local M = {}
---@type uv.uv_timer_t?
local save_timer = nil
---@type integer?
local pending_root_winid = nil
---@type string?
local last_restored_repo = nil
---@return boolean
local function is_enabled()
local cfg = config.get()
if type(cfg.persist.auto) ~= "table" then
return false
end
return cfg.persist.enabled and cfg.persist.auto.enabled or false
end
---@return string
local function resolve_session_name()
local cfg = config.get()
if type(cfg.persist.auto) == "table" and cfg.persist.auto.session_name then
return cfg.persist.auto.session_name
end
return "auto"
end
---@param winid? integer
---@return integer?
local function normalize_root_winid(winid)
if winid and type(winid) == "number" and vim.api.nvim_win_is_valid(winid) then
return winid
end
return nil
end
---@return integer
local function resolve_root_winid()
local winid = vim.api.nvim_get_current_win()
local bufnr = vim.api.nvim_win_get_buf(winid)
if vim.bo[bufnr].filetype == "peekstack-stack" then
local ok, root_winid = pcall(vim.api.nvim_win_get_var, winid, "peekstack_root_winid")
if ok and type(root_winid) == "number" and vim.api.nvim_win_is_valid(root_winid) then
return root_winid
end
end
return winid
end
---@param root_winid? integer
---@param opts? { sync?: boolean }
---@return boolean
local function save_session(root_winid, opts)
if not is_enabled() then
return false
end
if not fs.repo_root() then
return false
end
persist.save_current(resolve_session_name(), {
root_winid = normalize_root_winid(root_winid),
silent = true,
sync = opts and opts.sync or false,
})
return true
end
---@return boolean
function M.maybe_restore()
if not is_enabled() then
return false
end
local cfg = config.get()
if not cfg.persist.auto.restore then
return false
end
local repo_root = fs.repo_root()
if not repo_root then
return false
end
if last_restored_repo == repo_root then
return false
end
if cfg.persist.auto.restore_if_empty then
local root_winid = resolve_root_winid()
if #stack.list(root_winid) > 0 then
return false
end
end
last_restored_repo = repo_root
persist.restore(resolve_session_name(), { silent = true })
return true
end
---@param opts? { root_winid?: integer }
---@return boolean
function M.schedule_save(opts)
if not is_enabled() then
return false
end
local cfg = config.get()
if not cfg.persist.auto.save then
return false
end
if not fs.repo_root() then
return false
end
local debounce_ms = tonumber(cfg.persist.auto.debounce_ms) or 1000
local root_winid = normalize_root_winid(opts and opts.root_winid or nil)
if root_winid then
pending_root_winid = root_winid
end
if save_timer then
save_timer:stop()
else
save_timer = vim.uv.new_timer()
timer_util.get_store().persist_auto = save_timer
end
save_timer:start(debounce_ms, 0, function()
save_timer:stop()
local target_winid = pending_root_winid
pending_root_winid = nil
vim.schedule(function()
save_session(target_winid)
end)
end)
return true
end
---@param opts? { root_winid?: integer }
---@return boolean
function M.save_on_leave(opts)
if not is_enabled() then
return false
end
local cfg = config.get()
if not cfg.persist.auto.save_on_leave then
return false
end
local root_winid = normalize_root_winid(opts and opts.root_winid or nil) or pending_root_winid
pending_root_winid = nil
if save_timer then
save_timer:stop()
end
return save_session(root_winid, { sync = true })
end
function M.setup()
timer_util.close(save_timer)
timer_util.get_store().persist_auto = nil
save_timer = nil
pending_root_winid = nil
local group = vim.api.nvim_create_augroup("PeekstackPersistAuto", { clear = true })
if not is_enabled() then
return
end
local cfg = config.get()
if cfg.persist.auto.restore then
vim.api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, {
group = group,
callback = function()
M.maybe_restore()
end,
})
end
if cfg.persist.auto.save then
vim.api.nvim_create_autocmd("User", {
group = group,
pattern = { "PeekstackPush", "PeekstackClose", "PeekstackRestorePopup" },
callback = function(args)
local root_winid = args.data and args.data.root_winid or nil
M.schedule_save({ root_winid = root_winid })
end,
})
end
if cfg.persist.auto.save_on_leave then
vim.api.nvim_create_autocmd("VimLeavePre", {
group = group,
callback = function()
M.save_on_leave()
end,
})
end
end
---Reset internal state (for testing).
function M._reset()
last_restored_repo = nil
pending_root_winid = nil
timer_util.close(save_timer)
timer_util.get_store().persist_auto = nil
save_timer = nil
end
return M