-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.lua
More file actions
203 lines (183 loc) · 5.78 KB
/
commands.lua
File metadata and controls
203 lines (183 loc) · 5.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
local M = {}
local notify = require("peekstack.util.notify")
local loaded = false
local COMMAND_NAMES = {
"PeekstackStack",
"PeekstackSaveSession",
"PeekstackRestoreSession",
"PeekstackListSessions",
"PeekstackDeleteSession",
"PeekstackRestorePopup",
"PeekstackRestoreAllPopups",
"PeekstackHistory",
"PeekstackCloseAll",
"PeekstackQuickPeek",
"PeekstackToggleVisibility",
}
---@param session PeekstackSession|table
---@return integer
local function session_item_count(session)
local items = session and session.items
if type(items) ~= "table" then
return 0
end
return #items
end
---@param session PeekstackSession|table
---@return string
local function session_updated_at_text(session)
local meta = session and session.meta
local updated_at = type(meta) == "table" and meta.updated_at or nil
if type(updated_at) ~= "number" then
return "unknown"
end
return vim.fn.strftime("%Y-%m-%d %H:%M:%S", updated_at)
end
---@return string[]
local function list_session_names()
local persist = require("peekstack.persist")
local names = vim.tbl_keys(persist.list_sessions())
table.sort(names)
return names
end
function M.setup()
if loaded then
return
end
loaded = true
vim.api.nvim_create_user_command("PeekstackStack", function()
require("peekstack.ui.stack_view").open()
end, {})
vim.api.nvim_create_user_command("PeekstackSaveSession", function(opts)
local name = opts.args and opts.args ~= "" and opts.args or nil
local cfg = require("peekstack.config").get()
if not name and cfg.persist.session and cfg.persist.session.prompt_if_missing then
vim.ui.input(
{ prompt = "Session name: ", default = cfg.persist.session.default_name or "default" },
function(input)
if input and input ~= "" then
require("peekstack.persist").save_current(input)
end
end
)
return
end
require("peekstack.persist").save_current(name)
end, { nargs = "?" })
vim.api.nvim_create_user_command("PeekstackRestoreSession", function(opts)
local name = opts.args and opts.args ~= "" and opts.args or nil
require("peekstack.persist").restore(name)
end, {
nargs = "?",
complete = list_session_names,
})
vim.api.nvim_create_user_command("PeekstackListSessions", function()
local persist = require("peekstack.persist")
persist.list_sessions({
on_done = function(sessions)
local names = vim.tbl_keys(sessions)
table.sort(names)
if #names == 0 then
notify.info("No saved sessions")
return
end
vim.ui.select(names, { prompt = "Select a session" }, function(selected)
if not selected then
return
end
local session = sessions[selected]
local info = string.format(
"%s: %d items (updated: %s)",
selected,
session_item_count(session),
session_updated_at_text(session)
)
vim.ui.select({ "Restore", "Info only" }, { prompt = info }, function(action)
if action == "Restore" then
persist.restore(selected)
end
end)
end)
end,
})
end, {})
vim.api.nvim_create_user_command("PeekstackDeleteSession", function(opts)
local name = opts.args
if not name or name == "" then
notify.warn("Usage: PeekstackDeleteSession <name>")
return
end
vim.ui.select({ "Yes", "No" }, { prompt = "Delete session '" .. name .. "'?" }, function(choice)
if choice == "Yes" then
require("peekstack.persist").delete_session(name)
end
end)
end, {
nargs = 1,
complete = list_session_names,
})
vim.api.nvim_create_user_command("PeekstackRestorePopup", function()
local restored = require("peekstack.core.stack").restore_last()
if not restored then
notify.info("No closed popups to restore")
end
end, {})
vim.api.nvim_create_user_command("PeekstackRestoreAllPopups", function()
local restored = require("peekstack.core.stack").restore_all()
if #restored == 0 then
notify.info("No closed popups to restore")
end
end, {})
vim.api.nvim_create_user_command("PeekstackHistory", function()
local stack = require("peekstack.core.stack")
local loc = require("peekstack.core.location")
local history = stack.history_list()
if #history == 0 then
notify.info("No history entries")
return
end
local items = {}
for i = #history, 1, -1 do
local entry = history[i]
local label = entry.title or loc.display_text(entry.location, 0)
table.insert(items, { idx = i, label = label, entry = entry })
end
vim.ui.select(items, {
prompt = "Popup History",
format_item = function(item)
return item.label
end,
}, function(selected)
if not selected then
return
end
stack.restore_from_history(selected.idx)
end)
end, {})
vim.api.nvim_create_user_command("PeekstackCloseAll", function()
require("peekstack.core.stack").close_all()
end, {})
vim.api.nvim_create_user_command("PeekstackToggleVisibility", function()
local toggled = require("peekstack.core.stack").toggle_visibility()
if not toggled then
notify.info("No popups in the current stack")
end
end, {})
vim.api.nvim_create_user_command("PeekstackQuickPeek", function(opts)
local provider = opts.args and opts.args ~= "" and opts.args or "lsp.definition"
require("peekstack").peek(provider, { mode = "quick" })
end, {
nargs = "?",
complete = function()
return require("peekstack").list_providers()
end,
})
end
---Reset command registration state (for tests).
function M._reset()
loaded = false
for _, name in ipairs(COMMAND_NAMES) do
pcall(vim.api.nvim_del_user_command, name)
end
end
return M