-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathinit.lua
350 lines (309 loc) · 9.55 KB
/
init.lua
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
local NuiTree = require("nui.tree")
local NuiLine = require("nui.line")
local common = require("dbee.ui.common")
local menu = require("dbee.ui.drawer.menu")
local convert = require("dbee.ui.drawer.convert")
local expansion = require("dbee.ui.drawer.expansion")
-- action function of drawer nodes
---@alias drawer_node_action fun(cb: fun(), select: menu_select, input: menu_input)
-- A single line in drawer tree
---@class DrawerUINode: NuiTree.Node
---@field id string unique identifier
---@field name string display name
---@field type ""|"table"|"view"|"column"|"history"|"note"|"connection"|"database_switch"|"add"|"edit"|"remove"|"help"|"source"|"separator" type of node
---@field action_1? drawer_node_action primary action if function takes a second selection parameter, pick_items get picked before the call
---@field action_2? drawer_node_action secondary action if function takes a second selection parameter, pick_items get picked before the call
---@field action_3? drawer_node_action tertiary action if function takes a second selection parameter, pick_items get picked before the call
---@field lazy_children? fun():DrawerUINode[] lazy loaded child nodes
---@class DrawerUI
---@field private tree NuiTree
---@field private handler Handler
---@field private editor EditorUI
---@field private result ResultUI
---@field private mappings key_mapping[]
---@field private candies table<string, Candy> map of eye-candy stuff (icons, highlight)
---@field private disable_help boolean show help or not
---@field private winid? integer
---@field private bufnr integer
---@field private current_conn_id? connection_id current active connection
---@field private current_note_id? note_id current active note
---@field private window_options table<string, any> a table of window options.
---@field private buffer_options table<string, any> a table of buffer options.
local DrawerUI = {}
---@param handler Handler
---@param editor EditorUI
---@param result ResultUI
---@param opts? drawer_config
---@return DrawerUI
function DrawerUI:new(handler, editor, result, opts)
opts = opts or {}
if not handler then
error("no Handler provided to Drawer")
end
if not editor then
error("no Editor provided to Drawer")
end
if not result then
error("no Result provided to Drawer")
end
local candies = {}
if not opts.disable_candies then
candies = opts.candies or {}
end
local current_conn = handler:get_current_connection() or {}
local current_note = editor:get_current_note() or {}
-- class object
local o = {
handler = handler,
editor = editor,
result = result,
mappings = opts.mappings or {},
candies = candies,
disable_help = opts.disable_help or false,
current_conn_id = current_conn.id,
current_note_id = current_note.id,
window_options = vim.tbl_extend("force", {
wrap = false,
winfixheight = true,
winfixwidth = true,
number = false,
relativenumber = false,
spell = false,
}, opts.window_options or {}),
buffer_options = vim.tbl_extend("force", {
buflisted = false,
bufhidden = "delete",
buftype = "nofile",
swapfile = false,
filetype = "dbee",
}, opts.buffer_options or {}),
}
setmetatable(o, self)
self.__index = self
-- create a buffer for drawer and configure it
o.bufnr = common.create_blank_buffer("dbee-drawer", o.buffer_options)
common.configure_buffer_mappings(o.bufnr, o:get_actions(), opts.mappings)
-- create tree
o.tree = o:create_tree(o.bufnr)
-- listen to events
handler:register_event_listener("current_connection_changed", function(data)
o:on_current_connection_changed(data)
end)
editor:register_event_listener("current_note_changed", function(data)
o:on_current_note_changed(data)
end)
return o
end
-- event listener for current connection change
---@private
---@param data { conn_id: connection_id }
function DrawerUI:on_current_connection_changed(data)
if self.current_conn_id == data.conn_id then
return
end
self.current_conn_id = data.conn_id
self:refresh()
end
-- event listener for current note change
---@private
---@param data { note_id: note_id }
function DrawerUI:on_current_note_changed(data)
if self.current_note_id == data.note_id then
return
end
self.current_note_id = data.note_id
self:refresh()
end
---@private
---@param bufnr integer
---@return NuiTree tree
function DrawerUI:create_tree(bufnr)
return NuiTree {
bufnr = bufnr,
prepare_node = function(node)
local line = NuiLine()
if node.type == "separator" then
return line
end
line:append(string.rep(" ", node:get_depth() - 1))
if node:has_children() or node.lazy_children then
local candy = self.candies["node_closed"] or { icon = ">", icon_highlight = "NonText" }
if node:is_expanded() then
candy = self.candies["node_expanded"] or { icon = "v", icon_highlight = "NonText" }
end
line:append(candy.icon .. " ", candy.icon_highlight)
else
line:append(" ")
end
---@type Candy
local candy
-- special icons for nodes without type
if not node.type or node.type == "" then
if node:has_children() then
candy = self.candies["none_dir"]
else
candy = self.candies["none"]
end
else
candy = self.candies[node.type] or {}
end
candy = candy or {}
if candy.icon then
line:append(" " .. candy.icon .. " ", candy.icon_highlight)
end
-- apply a special highlight for active connection and active note
if node.id == self.current_conn_id or self.current_note_id == node.id then
line:append(string.gsub(node.name, "\n", " "), candy.icon_highlight)
else
line:append(string.gsub(node.name, "\n", " "), candy.text_highlight)
end
return line
end,
get_node_id = function(node)
if node.id then
return node.id
end
return tostring(math.random())
end,
}
end
---@private
---@return table<string, fun()>
function DrawerUI:get_actions()
local function collapse_node(node)
if node:collapse() then
self.tree:render()
end
end
local function expand_node(node)
local expanded = node:is_expanded()
-- if function for getting layout exist, call it
if not expanded and type(node.lazy_children) == "function" then
self.tree:set_nodes(node.lazy_children(), node.id)
end
node:expand()
self.tree:render()
end
-- wrapper for actions (e.g. action_1, action_2, action_3)
---@param action drawer_node_action
local function perform_action(action)
if type(action) ~= "function" then
return
end
action(function()
self:refresh()
end, function(opts)
opts = opts or {}
menu.select {
relative_winid = self.winid,
title = opts.title or "",
mappings = self.mappings,
items = opts.items or {},
on_confirm = opts.on_confirm,
on_yank = opts.on_yank,
}
end, function(opts)
menu.input {
relative_winid = self.winid,
title = opts.title or "",
mappings = self.mappings,
default_value = opts.default or "",
on_confirm = opts.on_confirm,
}
end)
end
return {
refresh = function()
self:refresh()
end,
action_1 = function()
local node = self.tree:get_node() --[[@as DrawerUINode]]
if not node then
return
end
perform_action(node.action_1)
end,
action_2 = function()
local node = self.tree:get_node() --[[@as DrawerUINode]]
if not node then
return
end
perform_action(node.action_2)
end,
action_3 = function()
local node = self.tree:get_node() --[[@as DrawerUINode]]
if not node then
return
end
perform_action(node.action_3)
end,
collapse = function()
local node = self.tree:get_node()
if not node then
return
end
collapse_node(node)
end,
expand = function()
local node = self.tree:get_node()
if not node then
return
end
expand_node(node)
end,
toggle = function()
local node = self.tree:get_node()
if not node then
return
end
if node:is_expanded() then
collapse_node(node)
else
expand_node(node)
end
end,
}
end
---Triggers an in-built action.
---@param action string
function DrawerUI:do_action(action)
local act = self:get_actions()[action]
if not act then
error("unknown action: " .. action)
end
act()
end
---Refreshes the tree.
function DrawerUI:refresh()
-- assemble tree layout
---@type DrawerUINode[]
local nodes = {}
local editor_nodes = convert.editor_nodes(self.editor, self.current_conn_id, function()
self:refresh()
end)
for _, ly in ipairs(editor_nodes) do
table.insert(nodes, ly)
end
table.insert(nodes, convert.separator_node())
for _, ly in ipairs(convert.handler_nodes(self.handler, self.result)) do
table.insert(nodes, ly)
end
if not self.disable_help then
table.insert(nodes, convert.separator_node())
table.insert(nodes, convert.help_node(self.mappings))
end
local exp = expansion.get(self.tree)
self.tree:set_nodes(nodes)
expansion.set(self.tree, exp)
self.tree:render()
end
---@param winid integer
function DrawerUI:show(winid)
self.winid = winid
-- set buffer to window
vim.api.nvim_win_set_buf(self.winid, self.bufnr)
-- configure window options (needs to be set after setting the buffer to window)
common.configure_window_options(self.winid, self.window_options)
self:refresh()
end
return DrawerUI