-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.lua
More file actions
331 lines (279 loc) · 10.3 KB
/
ui.lua
File metadata and controls
331 lines (279 loc) · 10.3 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
local notify = require("peekstack.util.notify")
local shared = require("peekstack.config.validate.shared")
local M = {}
---@type string[]
local KNOWN_LAYOUT_STYLES = { "stack", "cascade", "single" }
---@type string[]
local KNOWN_BUFFER_MODES = { "copy", "source" }
---@type string[]
local KNOWN_RESTORE_POSITIONS = { "top", "original" }
---@type string[]
local KNOWN_PATH_BASES = { "repo", "cwd", "absolute" }
---@type string[]
local KNOWN_STACK_VIEW_POSITIONS = { "left", "right", "bottom" }
---@type PeekstackConfigFieldRule[]
local UI_PATH_RULES = {
{ key = "base", validate = shared.field_enum(KNOWN_PATH_BASES), require_truthy = true },
{ key = "max_width", validate = shared.validate_non_negative_number },
}
---@type PeekstackConfigFieldRule[]
local STACK_VIEW_RULES = {
{ key = "position", validate = shared.field_enum(KNOWN_STACK_VIEW_POSITIONS) },
}
---@type PeekstackConfigFieldRule[]
local POPUP_RULES = {
{ key = "editable", validate = shared.field_type("boolean") },
{ key = "buffer_mode", validate = shared.field_enum(KNOWN_BUFFER_MODES), require_truthy = true },
}
---@type PeekstackConfigFieldRule[]
local POPUP_SOURCE_RULES = {
{ key = "prevent_auto_close_if_modified", validate = shared.field_type("boolean") },
{ key = "confirm_on_close", validate = shared.field_type("boolean") },
}
---@type PeekstackConfigFieldRule[]
local POPUP_HISTORY_RULES = {
{ key = "max_items", validate = shared.field_number_range({ min = 1 }) },
{ key = "restore_position", validate = shared.field_enum(KNOWN_RESTORE_POSITIONS), require_truthy = true },
}
---@type PeekstackConfigFieldRule[]
local INLINE_PREVIEW_RULES = {
{ key = "enabled", validate = shared.field_type("boolean") },
{ key = "max_lines", validate = shared.field_number_range({ min = 1 }) },
{ key = "hl_group", validate = shared.field_type("string") },
{ key = "close_events", validate = shared.field_event_list() },
}
---@type PeekstackConfigFieldRule[]
local QUICK_PEEK_RULES = {
{ key = "close_events", validate = shared.field_event_list() },
}
---@type PeekstackConfigFieldRule[]
local POPUP_AUTO_CLOSE_RULES = {
{ key = "enabled", validate = shared.field_type("boolean") },
{ key = "idle_ms", validate = shared.field_number_range({ min = 1 }) },
{ key = "check_interval_ms", validate = shared.field_number_range({ min = 1 }) },
{ key = "ignore_pinned", validate = shared.field_type("boolean") },
}
---@type PeekstackConfigFieldRule[]
local FEEDBACK_RULES = {
{ key = "highlight_origin_on_close", validate = shared.field_type("boolean") },
}
---@type PeekstackConfigFieldRule[]
local PROMOTE_RULES = {
{ key = "close_popup", validate = shared.field_type("boolean") },
}
---@type PeekstackConfigFieldRule[]
local TITLE_RULES = {
{ key = "enabled", validate = shared.field_type("boolean") },
{ key = "format", validate = shared.field_type("string") },
}
---@type PeekstackConfigFieldRule[]
local TITLE_CONTEXT_RULES = {
{ key = "enabled", validate = shared.field_type("boolean") },
{ key = "max_depth", validate = shared.field_number_range({ min = 1 }) },
{ key = "separator", validate = shared.field_type("string") },
}
---@type PeekstackConfigFieldRule[]
local TITLE_ICON_RULES = {
{ key = "enabled", validate = shared.field_type("boolean") },
}
---@type PeekstackConfigFieldRule[]
local LAYOUT_RULES = {
{ key = "style", validate = shared.field_enum(KNOWN_LAYOUT_STYLES), require_truthy = true },
{ key = "max_ratio", validate = shared.field_ratio() },
}
---@type PeekstackConfigFieldRule[]
local LAYOUT_MIN_SIZE_RULES = {
{ key = "w", validate = shared.field_number_range({ min = 1 }) },
{ key = "h", validate = shared.field_number_range({ min = 1 }) },
}
---@type PeekstackConfigFieldRule[]
local LAYOUT_SHRINK_RULES = {
{ key = "w", validate = shared.field_number_range({ min = 0 }) },
{ key = "h", validate = shared.field_number_range({ min = 0 }) },
}
---@type PeekstackConfigFieldRule[]
local LAYOUT_OFFSET_RULES = {
{ key = "row", validate = shared.field_number_range({ min = 0 }) },
{ key = "col", validate = shared.field_number_range({ min = 0 }) },
}
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_keys(ui, defaults)
if ui.keys == nil then
return
end
local keys = shared.ensure_table_field(ui, "keys", "ui.keys", defaults.keys)
if not keys then
return
end
for name, val in pairs(keys) do
if type(val) ~= "string" then
notify.warn(
string.format(
"ui.keys.%s must be a string, got %s. Falling back to %s",
name,
type(val),
tostring(defaults.keys[name])
)
)
keys[name] = defaults.keys[name]
end
end
end
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_popup(ui, defaults)
if ui.popup == nil then
return
end
local popup = shared.ensure_table_field(ui, "popup", "ui.popup", defaults.popup)
if not popup then
return
end
shared.apply_rules(popup, "ui.popup", defaults.popup, POPUP_RULES)
if popup.source ~= nil then
local source = shared.ensure_table_field(popup, "source", "ui.popup.source", defaults.popup.source)
if source then
shared.apply_rules(source, "ui.popup.source", defaults.popup.source, POPUP_SOURCE_RULES)
end
end
if popup.history ~= nil then
local history = shared.ensure_table_field(popup, "history", "ui.popup.history", defaults.popup.history)
if history then
shared.apply_rules(history, "ui.popup.history", defaults.popup.history, POPUP_HISTORY_RULES)
end
end
if popup.auto_close ~= nil then
local auto_close = shared.ensure_table_field(popup, "auto_close", "ui.popup.auto_close", defaults.popup.auto_close)
if auto_close then
shared.apply_rules(auto_close, "ui.popup.auto_close", defaults.popup.auto_close, POPUP_AUTO_CLOSE_RULES)
end
end
end
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_path(ui, defaults)
if ui.path == nil then
return
end
local path = shared.ensure_table_field(ui, "path", "ui.path", defaults.path)
if path then
shared.apply_rules(path, "ui.path", defaults.path, UI_PATH_RULES)
end
end
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_stack_view(ui, defaults)
if ui.stack_view == nil then
return
end
local stack_view = shared.ensure_table_field(ui, "stack_view", "ui.stack_view", defaults.stack_view)
if stack_view then
shared.apply_rules(stack_view, "ui.stack_view", defaults.stack_view, STACK_VIEW_RULES)
end
end
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_preview(ui, defaults)
if ui.inline_preview ~= nil then
local inline_preview = shared.ensure_table_field(ui, "inline_preview", "ui.inline_preview", defaults.inline_preview)
if inline_preview then
shared.apply_rules(inline_preview, "ui.inline_preview", defaults.inline_preview, INLINE_PREVIEW_RULES)
end
end
if ui.quick_peek ~= nil then
local quick_peek = shared.ensure_table_field(ui, "quick_peek", "ui.quick_peek", defaults.quick_peek)
if quick_peek then
shared.apply_rules(quick_peek, "ui.quick_peek", defaults.quick_peek, QUICK_PEEK_RULES)
end
end
end
---@param ui table
---@param defaults PeekstackConfigTitle
local function validate_title(ui, defaults)
if ui.title == nil then
return
end
local title = shared.ensure_table_field(ui, "title", "ui.title", defaults)
if not title then
return
end
shared.apply_rules(title, "ui.title", defaults, TITLE_RULES)
if title.icons ~= nil and type(title.icons) ~= "table" then
notify.warn("ui.title.icons must be a table, got " .. type(title.icons) .. ". Falling back to defaults")
title.icons = vim.deepcopy(defaults.icons)
return
end
if type(title.icons) == "table" then
local icons = title.icons
shared.apply_rules(icons, "ui.title.icons", defaults.icons, TITLE_ICON_RULES)
if icons.map ~= nil and type(icons.map) ~= "table" then
notify.warn("ui.title.icons.map must be a table, got " .. type(icons.map) .. ". Falling back to defaults")
icons.map = vim.deepcopy(defaults.icons.map)
end
end
if title.context ~= nil then
local context = shared.ensure_table_field(title, "context", "ui.title.context", defaults.context)
if context then
shared.apply_rules(context, "ui.title.context", defaults.context, TITLE_CONTEXT_RULES)
end
end
end
---@param ui table
---@param defaults PeekstackConfigUI
local function validate_layout(ui, defaults)
if ui.layout == nil then
return
end
local layout = shared.ensure_table_field(ui, "layout", "ui.layout", defaults.layout)
if not layout then
return
end
shared.apply_rules(layout, "ui.layout", defaults.layout, LAYOUT_RULES)
if layout.min_size ~= nil then
local min_size = shared.ensure_table_field(layout, "min_size", "ui.layout.min_size", defaults.layout.min_size)
if min_size then
shared.apply_rules(min_size, "ui.layout.min_size", defaults.layout.min_size, LAYOUT_MIN_SIZE_RULES)
end
end
if layout.shrink ~= nil then
local shrink = shared.ensure_table_field(layout, "shrink", "ui.layout.shrink", defaults.layout.shrink)
if shrink then
shared.apply_rules(shrink, "ui.layout.shrink", defaults.layout.shrink, LAYOUT_SHRINK_RULES)
end
end
if layout.offset ~= nil then
local offset = shared.ensure_table_field(layout, "offset", "ui.layout.offset", defaults.layout.offset)
if offset then
shared.apply_rules(offset, "ui.layout.offset", defaults.layout.offset, LAYOUT_OFFSET_RULES)
end
end
end
---@param cfg table
---@param defaults PeekstackConfig
function M.validate(cfg, defaults)
local ui = shared.as_table(cfg.ui)
if not ui then
return
end
validate_keys(ui, defaults.ui)
validate_popup(ui, defaults.ui)
validate_path(ui, defaults.ui)
validate_stack_view(ui, defaults.ui)
validate_preview(ui, defaults.ui)
validate_title(ui, defaults.ui.title)
validate_layout(ui, defaults.ui)
if ui.feedback ~= nil then
local feedback = shared.ensure_table_field(ui, "feedback", "ui.feedback", defaults.ui.feedback)
if feedback then
shared.apply_rules(feedback, "ui.feedback", defaults.ui.feedback, FEEDBACK_RULES)
end
end
if ui.promote ~= nil then
local promote = shared.ensure_table_field(ui, "promote", "ui.promote", defaults.ui.promote)
if promote then
shared.apply_rules(promote, "ui.promote", defaults.ui.promote, PROMOTE_RULES)
end
end
end
return M