-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommands.lua
More file actions
412 lines (385 loc) · 11.4 KB
/
commands.lua
File metadata and controls
412 lines (385 loc) · 11.4 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
---@enum (key) CompleteTypes
local complete_types = { ---@diagnostic disable-line:unused-local
arglist = 1,
breakpoint = 1,
buffer = 1,
color = 1,
command = 1,
compiler = 1,
diff_buffer = 1,
dir = 1,
dir_in_path = 1,
environment = 1,
event = 1,
expression = 1,
file = 1,
file_in_path = 1,
filetype = 1,
['function'] = 1,
help = 1,
highlight = 1,
history = 1,
keymap = 1,
locale = 1,
lua = 1,
mapclear = 1,
mapping = 1,
menu = 1,
messages = 1,
option = 1,
packadd = 1,
retab = 1,
runtime = 1,
scriptnames = 1,
shellcmd = 1,
shellcmdline = 1,
sign = 1,
syntax = 1,
syntime = 1,
tag = 1,
tag_listfiles = 1,
user = 1,
var = 1,
}
---@alias ProjectCmdFun fun(ctx?: vim.api.keyset.create_user_command.command_args)
---@alias CompletorFunc fun(lead: string, line: string, pos: integer): completions: string[]
---@alias Project.CMD
---|{ desc: string, name: string, bang: boolean, complete?: (CompletorFunc)|CompleteTypes, nargs?: string|integer }
---|ProjectCmdFun
---@class Project.Commands.Spec
---@field bang? boolean
---@field callback ProjectCmdFun
---@field complete? CompleteTypes|CompletorFunc
---@field desc string
---@field name string
---@field nargs? string|integer
local WARN = vim.log.levels.WARN
local ERROR = vim.log.levels.ERROR
local Util = require('project.util')
local Popup = require('project.popup')
local History = require('project.util.history')
local Api = require('project.api')
local Log = require('project.util.log')
local Config = require('project.config')
---@class Project.Commands
local Commands = {}
Commands.cmds = {} ---@type table<string, Project.CMD>
---@param specs Project.Commands.Spec[]
function Commands.new(specs)
Util.validate({ specs = { specs, { 'table' } } })
if vim.tbl_isempty(specs) or not vim.islist(specs) then
error('Invalid command spec!', ERROR)
end
for _, spec in ipairs(specs) do
Util.validate({
name = { spec.name, { 'string' } },
desc = { spec.desc, { 'string' } },
callback = { spec.callback, { 'function' } },
bang = { spec.bang, { 'boolean', 'nil' }, true },
nargs = { spec.nargs, { 'string', 'number', 'nil' }, true },
complete = { spec.complete, { 'string', 'function', 'nil' }, true },
})
local name = spec.name
local bang = spec.bang ~= nil and spec.bang or false
local T = { name = name, desc = spec.desc, bang = bang }
local opts = { desc = spec.desc, bang = bang }
if spec.nargs ~= nil then
T.nargs = spec.nargs
opts.nargs = spec.nargs
end
if spec.complete then
T.complete = spec.complete
opts.complete = spec.complete
end
Commands.cmds[name] = setmetatable({}, {
__index = function(_, k) ---@param k string
return T[k]
end,
__tostring = function()
return T.desc
end,
__call = function(_, ctx) ---@param ctx? vim.api.keyset.create_user_command.command_args
if ctx then
spec.callback(ctx)
return
end
spec.callback()
end,
})
vim.api.nvim_create_user_command(name, function(ctx)
Commands.cmds[name](ctx)
end, opts)
end
end
function Commands.create_user_commands()
Commands.new({
{
name = 'Project',
desc = 'Run the main project.nvim UI',
bang = true,
nargs = '*',
complete = function(_, line)
local args = vim.split(line, '%s+', { trimempty = false })
if args[1]:sub(-1) == '!' and #args == 1 then
return {}
end
if #args == 2 then
if args[2] == '' then
local res = Popup.open_menu.choices_list(false)
for i, v in ipairs(res) do
if v == 'Exit' then
table.remove(res, i)
break
end
end
table.sort(res)
return res
end
local res = {}
for _, choice in ipairs(Popup.open_menu.choices_list(false)) do
if vim.startswith(choice, args[2]) and choice ~= 'Exit' then
table.insert(res, choice)
end
end
table.sort(res)
return res
end
return {}
end,
callback = function(ctx)
Popup.open_menu(ctx)
end,
},
{
name = 'ProjectAdd',
desc = 'Prompt to add the current directory to the project history',
bang = true,
nargs = '*',
complete = 'file',
callback = function(ctx)
if ctx and vim.tbl_isempty(ctx.fargs) then
local opts = { prompt = 'Input a valid path to the project:', completion = 'dir' } ---@type vim.ui.input.Opts
if ctx.bang ~= nil and ctx.bang then
local bufnr = vim.api.nvim_get_current_buf()
opts.default = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ':p:h')
end
vim.ui.input(opts, Popup.prompt_project)
return
end
local session = History.session_projects
local msg = ''
for _, input in ipairs(ctx.fargs) do
input = Util.rstrip('/', (vim.fn.fnamemodify(input, ':p')))
if Util.dir_exists(input) then
if Api.current_project ~= input and not vim.list_contains(session, input) then
Api.set_pwd(input, 'command')
History.write_history()
else
msg = ('%s%sAlready added `%s`!'):format(msg, msg == '' and '' or '\n', input)
end
else
msg = ('%s%s`%s` is not a directory!'):format(msg, msg == '' and '' or '\n', input)
end
end
vim.notify(msg, WARN)
end,
},
{
name = 'ProjectConfig',
desc = 'Prints out the current configuratiion for `project.nvim`',
bang = true,
callback = function(ctx)
if ctx and ctx.bang ~= nil and ctx.bang then
vim.print(Config.get_config())
return
end
Config.toggle_win()
end,
},
{
name = 'ProjectDelete',
desc = 'Deletes the projects given as args, assuming they are valid. No args open a popup',
bang = true,
nargs = '*',
complete = function(_, line)
local args = vim.split(line, '%s+', { trimempty = false })
if args[1]:sub(-1) == '!' and #args == 1 then
return {}
end
local recents = Util.reverse(History.get_recent_projects(true)) ---@type string[]
if args[#args] == '' then
return recents
end
local res = {} ---@type string[]
for _, proj in ipairs(recents) do
if vim.startswith(proj, args[#args]) then
table.insert(res, proj)
end
end
return res
end,
callback = function(ctx)
if not ctx or vim.tbl_isempty(ctx.fargs) then
Popup.delete_menu()
return
end
local recent = History.get_recent_projects()
if not recent then
Log.error('(:ProjectDelete): No recent projects!')
vim.notify('(:ProjectDelete): No recent projects!', ERROR)
return
end
local force = ctx.bang ~= nil and ctx.bang or false
local msg
for _, v in ipairs(ctx.fargs) do
v = Util.strip({ '"', "'" }, v)
local path = vim.fn.fnamemodify(v, ':p')
if path:sub(-1) == '/' then
path = path:sub(1, path:len() - 1)
end
if not (force or vim.list_contains(recent, path) or path ~= '') then
msg = ('(:ProjectDelete): Could not delete `%s`, aborting'):format(path)
Log.error(msg)
vim.notify(msg, ERROR)
return
end
if vim.list_contains(recent, path) then
History.delete_project(path)
end
end
end,
},
{
name = 'ProjectExport',
desc = 'Export project.nvim history to JSON file',
bang = true,
nargs = '*',
complete = function(_, line)
local args = vim.split(line, '%s+', { trimempty = false })
if args[1]:sub(-1) == '!' and #args == 1 then
return {}
end
if #args == 2 then
-- Thanks to @TheLeoP for the advice!
-- https://www.reddit.com/r/neovim/comments/1pvl1tb/comment/nvwzvvu/
return vim.fn.getcompletion(args[2], 'file', true)
end
if #args == 3 then
---@type string[]
local nums = vim.tbl_map(function(value) ---@param value integer
return tostring(value)
end, Util.range(0, 32))
if args[3] == '' then
return nums
end
local res = {} ---@type string[]
for _, num in ipairs(nums) do
if vim.startswith(num, args[3]) then
table.insert(res, num)
end
end
return res
end
return {}
end,
callback = function(ctx)
if not ctx or #ctx.fargs > 2 then
vim.notify('Usage: `:ProjectExport[!] </path/to/file[.json]> [<INDENT>]`', WARN)
return
end
if vim.tbl_isempty(ctx.fargs) then
Popup.gen_export_prompt()
return
end
History.export_history_json(
ctx.fargs[1],
#ctx.fargs == 2 and tonumber(ctx.fargs[2]) or nil,
ctx.bang
)
end,
},
{
name = 'ProjectHealth',
desc = 'Run checkhealth for project.nvim',
callback = function()
vim.cmd.checkhealth('project')
end,
},
{
name = 'ProjectHistory',
desc = 'Manage project history',
bang = true,
nargs = '*',
complete = function(_, line)
local args = vim.split(line, '%s+', { trimempty = false })
if (args[1]:sub(-1) == '!' and #args == 1) or #args > 2 then
return {}
end
local res = {}
for _, choice in ipairs({ 'clear' }) do
if vim.startswith(choice, args[2]) then
table.insert(res, choice)
end
end
return res
end,
callback = function(ctx)
if vim.tbl_isempty(ctx.fargs) then
History.toggle_win()
return
end
if ctx.fargs[1] ~= 'clear' or #ctx.fargs > 1 then
vim.notify('Usage: `:ProjectHistory[!] [clear]`', WARN)
return
end
History.clear_historyfile(ctx.bang)
end,
},
{
name = 'ProjectImport',
desc = 'Import project history from JSON file',
bang = true,
nargs = '*',
complete = 'file',
callback = function(ctx)
if vim.tbl_isempty(ctx.fargs) then
Popup.gen_import_prompt()
return
end
vim.print(ctx.fargs)
if #ctx.fargs == 1 then
History.import_history_json(ctx.fargs[1], ctx.bang)
end
vim.notify('Usage: `:ProjectImport[!] </path/to/file[.json]>`', WARN)
end,
},
{
name = 'ProjectRecents',
desc = 'Opens a menu to select a project from your history',
callback = function()
Popup.recents_menu()
end,
},
{
name = 'ProjectRoot',
desc = 'Sets the current project root to the current cwd',
bang = true,
callback = function(ctx)
Api.on_buf_enter()
if ctx and ctx.bang then
vim.notify(vim.fn.getcwd(0, 0))
end
end,
},
{
name = 'ProjectSession',
desc = 'Opens a menu to switch between sessions',
bang = true,
callback = function(ctx)
Popup.session_menu(ctx)
end,
},
})
end
return Commands
-- vim: set ts=2 sts=2 sw=2 et ai si sta: