-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommands.lua
More file actions
286 lines (264 loc) · 8.24 KB
/
commands.lua
File metadata and controls
286 lines (264 loc) · 8.24 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
---@alias ProjectCmdFun fun()|fun(ctx: vim.api.keyset.create_user_command.command_args)
---@alias CompletorFun fun(a?: string, l?: string, p?: integer): string[]
---@alias Project.CMD
---|{ desc: string, name: string, bang: boolean, complete?: string|CompletorFun, nargs?: string|integer }
---|ProjectCmdFun
---@class Project.Commands.Spec
---@field callback ProjectCmdFun
---@field name string
---@field desc string
---@field complete? string|CompletorFun
---@field bang? boolean
---@field nargs? string|integer
local MODSTR = 'project.commands'
local INFO = vim.log.levels.INFO
local ERROR = vim.log.levels.ERROR
local Util = require('project.utils.util')
local Popup = require('project.popup')
local History = require('project.utils.history')
local Api = require('project.api')
local Log = require('project.utils.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) then
error(('(%s.new): Empty command spec!'):format(MODSTR), ERROR)
end
if not vim.islist(specs) then
error(('(%s.new): Spec is not a list!'):format(MODSTR), 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',
callback = function(ctx)
Popup.open_menu(ctx)
end,
desc = 'Run the main project.nvim UI',
nargs = '*',
bang = true,
complete = function(_, line)
local args = vim.split(line, '%s+', { trimempty = false })
if #args == 2 then
---@type string[]
local list = vim.tbl_map(function(value) ---@param value string
return ('"%s"'):format(value)
end, Popup.open_menu.choices_list())
for i, v in ipairs(list) do
if v == '"Exit"' then
table.remove(list, i)
break
end
end
return list
end
return {}
end,
},
{
name = 'ProjectAdd',
callback = function(ctx)
local opts = { prompt = 'Input a valid path to the project:', completion = 'dir' }
if ctx and 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)
end,
desc = 'Prompt to add the current directory to the project history',
bang = true,
},
{
name = 'ProjectExportJSON',
desc = 'Export project.nvim history to JSON file',
callback = function(ctx)
if not (ctx and #ctx.fargs <= 2) then
vim.notify('Usage\n :ProjectExportJSON </path/to/file[.json]> [<INDENT>]', INFO)
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,
bang = true,
complete = function(_, line) ---@param line string
local args = vim.split(line, '%s+', { trimempty = false })
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
local tbl = Util.range(0, 32)
local res = vim.tbl_map(function(value) ---@param value integer
return tostring(value)
end, tbl)
table.sort(res)
return res
end
return {}
end,
nargs = '*',
},
{
name = 'ProjectImportJSON',
desc = 'Import project history from JSON file',
callback = function(ctx)
if not (ctx and #ctx.fargs <= 1) then
vim.notify('Usage\n :ProjectImportJSON </path/to/file[.json]>', INFO)
return
end
if vim.tbl_isempty(ctx.fargs) then
Popup.gen_import_prompt()
return
end
History.import_history_json(ctx.fargs[1], ctx.bang)
end,
bang = true,
nargs = '?',
complete = 'file',
},
{
name = 'ProjectConfig',
callback = function(ctx)
if ctx and ctx.bang ~= nil and ctx.bang then
vim.print(Config.get_config())
return
end
Config.toggle_win()
end,
desc = 'Prints out the current configuratiion for `project.nvim`',
bang = true,
},
{
name = 'ProjectDelete',
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,
desc = 'Deletes the projects given as args, assuming they are valid. No args open a popup',
nargs = '*',
bang = true,
complete = function()
return History.get_recent_projects(true)
end,
},
{
name = 'ProjectHealth',
callback = function()
vim.cmd.checkhealth('project')
end,
desc = 'Run checkhealth for project.nvim',
},
{
name = 'ProjectHistory',
callback = function()
History.toggle_win()
end,
desc = 'Run project.nvim through Fzf-Lua (assuming you have it installed)',
bang = true,
},
{
name = 'ProjectRecents',
callback = function()
Popup.recents_menu()
end,
desc = 'Opens a menu to select a project from your history',
},
{
name = 'ProjectRoot',
callback = function(ctx)
Api.on_buf_enter(ctx.bang ~= nil and ctx.bang or false)
end,
desc = 'Sets the current project root to the current CWD',
bang = true,
},
{
name = 'ProjectSession',
callback = function(ctx)
Popup.session_menu(ctx)
end,
desc = 'Opens a menu to switch between sessions',
bang = true,
},
})
end
return Commands
-- vim: set ts=2 sts=2 sw=2 et ai si sta: