-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpopup.lua
More file actions
481 lines (440 loc) · 13.7 KB
/
popup.lua
File metadata and controls
481 lines (440 loc) · 13.7 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
---@class Project.Popup.SelectChoices
---@field choices fun(): choices_dict: table<string, function>
---@field choices_list fun(): choices: string[]
---@class Project.Popup.SelectSpec: Project.Popup.SelectChoices
---@field callback ProjectCmdFun
local MODSTR = 'project.popup'
local ERROR = vim.log.levels.ERROR
local WARN = vim.log.levels.WARN
local Util = require('project.utils.util')
local uv = vim.uv or vim.loop
---@param path string
---@param hidden boolean
---@return boolean available
local function hidden_avail(path, hidden)
Util.validate({
path = { path, { 'string' } },
hidden = { hidden, { 'boolean' } },
})
local fd = Util.executable('fd') and 'fd' or (Util.executable('fdfind') and 'fdfind' or '')
if fd == '' then
error(('(%s.hidden_avail): `fd`/`fdfind` not found in found PATH!'):format(MODSTR), ERROR)
end
local cmd = { fd, '-Iad1' }
if hidden then
table.insert(cmd, '-H')
end
local out = vim.system(cmd, { text = true, cwd = vim.g.project_nvim_cwd }):wait(10000).stdout
if not out then
return false
end
local ret = false
local nodes = vim.split(out, '\n', { plain = true, trimempty = true })
vim.tbl_map(function(value)
if value == path or vim.startswith(value, path) then
ret = true
end
end, nodes)
return ret
end
---@param proj string
---@param only_cd boolean
---@param ran_cd boolean
local function open_node(proj, only_cd, ran_cd)
Util.validate({
proj = { proj, { 'string' } },
only_cd = { only_cd, { 'boolean' } },
ran_cd = { ran_cd, { 'boolean' } },
})
if not ran_cd then
if not require('project.api').set_pwd(proj, 'prompt') then
vim.notfy('(open_node): Unsucessful `set_pwd`!', ERROR)
return
end
if only_cd then
return
end
ran_cd = not ran_cd
vim.g.project_nvim_cwd = proj
end
local dir = uv.fs_scandir(proj)
if not dir then
vim.notify(('(%s.open_node): NO DIR `%s`!'):format(MODSTR, proj), ERROR)
return
end
local hidden = require('project.config').options.show_hidden
local ls = {}
while true do
local node = uv.fs_scandir_next(dir)
if not node then
break
end
node = proj .. '/' .. node
if uv.fs_stat(node) then
local hid = Util.is_hidden(node)
if (hidden and hid) or hidden_avail(node, hidden) then
table.insert(ls, node)
end
end
end
table.insert(ls, 'Exit')
vim.ui.select(ls, {
prompt = 'Select a file:',
format_item = function(item) ---@param item string
if item == 'Exit' then
return item
end
item = Util.rstrip('/', vim.fn.fnamemodify(item, ':p'))
return vim.fn.isdirectory(item) == 1 and (item .. '/') or item
end,
}, function(item) ---@param item string
if not item or vim.list_contains({ '', 'Exit' }, item) then
return
end
item = Util.rstrip('\\', Util.rstrip('/', vim.fn.fnamemodify(item, ':p')))
local stat = uv.fs_stat(item)
if not stat then
return
end
if stat.type == 'file' then
vim.g.project_nvim_cwd = ''
vim.cmd.edit(item)
return
end
if stat.type == 'directory' then
vim.g.project_nvim_cwd = item
open_node(item, false, ran_cd)
end
end)
end
---@class Project.Popup
local M = {}
---@class Project.Popup.Select
M.select = {}
---@param bang boolean
---@overload fun()
function M.gen_import_prompt(bang)
Util.validate({ bang = { bang, { 'boolean', 'nil' }, true } })
bang = bang ~= nil and bang or false
vim.ui.input({ prompt = 'Input the import file:' }, function(input) ---@param input? string
if not input or input == '' then
return
end
require('project.utils.history').import_history_json(input, bang)
end)
end
---@param bang boolean
---@overload fun()
function M.gen_export_prompt(bang)
Util.validate({ bang = { bang, { 'boolean', 'nil' }, true } })
bang = bang ~= nil and bang or false
vim.ui.input({ prompt = 'Input the export file:' }, function(input) ---@param input? string
if not input or input == '' then
return
end
vim.ui.input(
{ prompt = 'Select your indent level (default: 0):', default = '0' },
function(indent)
if not indent or indent == '' then
return
end
require('project.utils.history').export_history_json(input, indent, bang)
end
)
end)
end
---@param opts Project.Popup.SelectSpec
---@return Project.Popup.SelectChoices|ProjectCmdFun
function M.select.new(opts)
Util.validate({
opts = { opts, { 'table' } },
opts_choices = { opts.choices, { 'function' } },
opts_choices_list = { opts.choices_list, { 'function' } },
opts_callback = { opts.callback, { 'function' } },
})
if vim.tbl_isempty(opts) then
error(('(%s.select.new): Empty args for constructor!'):format(MODSTR), ERROR)
end
local T = setmetatable({ ---@type Project.Popup.SelectChoices|ProjectCmdFun
choices = opts.choices,
choices_list = opts.choices_list,
}, {
---@param t Project.Popup.SelectChoices|ProjectCmdFun
---@param k string
__index = function(t, k)
return rawget(t, k)
end,
__call = function(_, ctx) ---@param ctx? vim.api.keyset.create_user_command.command_args
if not ctx then
opts.callback()
return
end
opts.callback(ctx)
end,
})
return T
end
---@param input string
---@overload fun()
function M.prompt_project(input)
Util.validate({ input = { input, { 'string', 'nil' }, true } })
if not input or input == '' then
return
end
local Path = require('project.utils.path')
local original_input = input
input = Util.rstrip('/', vim.fn.fnamemodify(input, ':p'))
if not (Path.exists(input) and Path.exists(vim.fn.fnamemodify(input, ':p:h'))) then
vim.notify(('Invalid path `%s`'):format(original_input), ERROR)
return
end
if not Util.dir_exists(input) then
input = Util.rstrip('/', vim.fn.fnamemodify(input, ':p:h'))
if not Util.dir_exists(input) then
vim.notify('Path is not a directory, and parent could not be retrieved!', ERROR)
return
end
end
local Api = require('project.api')
local session = require('project.utils.history').session_projects
if Api.current_project == input or vim.list_contains(session, input) then
vim.notify('Already added that directory!', WARN)
return
end
Api.set_pwd(input, 'prompt')
require('project.utils.history').write_history()
end
M.delete_menu = M.select.new({
callback = function()
local choices_list = M.delete_menu.choices_list()
vim.ui.select(choices_list, {
prompt = 'Select a project to delete:',
format_item = function(item) ---@param item string
if vim.list_contains(require('project.utils.history').session_projects, item) then
return '* ' .. item
end
return item
end,
}, function(item)
if not item then
return
end
if not vim.list_contains(choices_list, item) then
vim.notify('Bad selection!', ERROR)
return
end
local choice = M.delete_menu.choices()[item]
if not (choice and vim.is_callable(choice)) then
vim.notify('Bad selection!', ERROR)
return
end
choice()
end)
end,
choices_list = function()
local recents = Util.reverse(require('project.utils.history').get_recent_projects()) ---@type string[]
table.insert(recents, 'Exit')
return recents
end,
choices = function()
local T = {} ---@type table<string, function>
for _, proj in ipairs(require('project.utils.history').get_recent_projects()) do
T[proj] = function()
require('project.utils.history').delete_project(proj)
end
end
T.Exit = function() end
return T
end,
})
M.recents_menu = M.select.new({
callback = function()
local choices_list = M.recents_menu.choices_list()
vim.ui.select(choices_list, {
prompt = 'Select a project:',
format_item = function(item) ---@param item string
local curr = require('project.api').current_project or ''
return item == curr and '* ' .. item or item
end,
}, function(item)
if not item then
return
end
if not vim.list_contains(choices_list, item) then
vim.notify('Bad selection!', ERROR)
return
end
local choice = M.recents_menu.choices()[item]
if not (choice and vim.is_callable(choice)) then
vim.notify('Bad selection!', ERROR)
return
end
choice(item, false, false)
end)
end,
choices_list = function()
local choices_list = vim.deepcopy(require('project.utils.history').get_recent_projects())
if require('project.config').options.telescope.sort == 'newest' then
choices_list = Util.reverse(choices_list)
end
table.insert(choices_list, 'Exit')
return choices_list
end,
choices = function()
local choices = {} ---@type table<string, function>
for _, s in ipairs(M.recents_menu.choices_list()) do
choices[s] = s ~= 'Exit' and open_node or function(_, _, _) end
end
return choices
end,
})
M.open_menu = M.select.new({
callback = function(ctx)
if ctx and ctx.fargs and not vim.tbl_isempty(ctx.fargs) then
local fargs = table.concat(
vim.tbl_map(function(value) ---@param value string
return Util.strip({ "'", '"' }, value)
end, ctx.fargs),
' '
)
local choices = M.open_menu.choices()
if not vim.list_contains(vim.tbl_keys(choices), fargs) then
return
end
choices[fargs]()
return
end
local choices_list = M.open_menu.choices_list()
vim.ui.select(choices_list, { prompt = 'Select an operation:' }, function(item)
if not item then
return
end
if not vim.list_contains(choices_list, item) then
vim.notify('Bad selection!', ERROR)
return
end
local choice = M.open_menu.choices()[item]
if not (choice and vim.is_callable(choice)) then
vim.notify('Bad selection!', ERROR)
return
end
choice()
end)
end,
choices = function()
local Config = require('project.config')
local res = { ---@type table<string, ProjectCmdFun>
['Project Session'] = M.session_menu,
['New Project'] = require('project.commands').cmds.ProjectAdd,
['Open Recent Project'] = M.recents_menu,
['Delete A Project'] = M.delete_menu,
['Show Config'] = require('project.commands').cmds.ProjectConfig,
['Open History'] = vim.cmd.ProjectHistory,
['Export History (JSON)'] = M.gen_export_prompt,
['Import History (JSON)'] = M.gen_import_prompt,
['Open Help Docs'] = function()
vim.cmd.help('project-nvim')
end,
['Run Checkhealth'] = vim.cmd.ProjectHealth or function()
vim.cmd.checkhealth('project')
end,
['Go To Source Code'] = function()
vim.ui.open('https://github.com/DrKJeff16/project.nvim')
end,
Exit = function() end,
}
if vim.g.project_telescope_loaded == 1 then
res['Open Telescope Picker'] = require('telescope._extensions.projects').projects
end
if Config.options.fzf_lua.enabled then
res['Open Fzf-Lua Picker'] = require('project.extensions.fzf-lua').run_fzf_lua
end
if Config.options.log.enabled then
res['Open Log'] = require('project.utils.log').open_win
res['Clear Log'] = require('project.utils.log').clear_log
end
return res
end,
choices_list = function()
local Config = require('project.config')
local res_list = {
'Project Session',
'New Project',
'Open Recent Project',
'Delete A Project',
'Run Checkhealth',
'Show Config',
'Open History',
'Export History (JSON)',
'Import History (JSON)',
'Open Help Docs',
'Go To Source Code',
'Exit',
}
if vim.g.project_telescope_loaded == 1 then
table.insert(res_list, #res_list - 5, 'Open Telescope Picker')
end
if Config.options.fzf_lua.enabled then
table.insert(res_list, #res_list - 5, 'Open Fzf-Lua Picker')
end
if Config.options.log.enabled then
table.insert(res_list, #res_list - 5, 'Open Log')
table.insert(res_list, #res_list - 5, 'Clear Log')
end
return res_list
end,
})
M.session_menu = M.select.new({
callback = function(ctx)
local only_cd = (ctx and ctx.bang ~= nil and ctx.bang) and ctx.bang or false
local choices_list = M.session_menu.choices_list()
if #choices_list == 1 then
vim.notify('No sessions available!', WARN)
return
end
vim.ui.select(choices_list, {
prompt = 'Select a project from your session:',
format_item = function(item) ---@param item string
return vim.fn.isdirectory(item) == 1 and (item .. '/') or item
end,
}, function(item)
if not item then
return
end
if not vim.list_contains(choices_list, item) then
vim.notify('Bad selection!', ERROR)
return
end
local choice = M.session_menu.choices()[item]
if not (choice and vim.is_callable(choice)) then
vim.notify('Bad selection!', ERROR)
return
end
choice(item, only_cd, false)
end)
end,
choices = function()
local sessions = require('project.utils.history').session_projects
local choices = { Exit = function(_, _, _) end }
if vim.tbl_isempty(sessions) then
return choices
end
for _, proj in ipairs(sessions) do
choices[proj] = open_node
end
return choices
end,
choices_list = function()
local choices = vim.deepcopy(require('project.utils.history').session_projects)
table.insert(choices, 'Exit')
return choices
end,
})
local Popup = setmetatable(M, { ---@type Project.Popup
__index = M,
__newindex = function()
vim.notify('Project.Popup is Read-Only!', ERROR)
end,
})
return Popup
-- vim: set ts=2 sts=2 sw=2 et ai si sta: