Skip to content

Commit 24f48e5

Browse files
committed
feat: allow running commands for a specific runner
1 parent 9024195 commit 24f48e5

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

lua/static/init.lua

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
local M = {}
22

33
local actions = { 'build', 'serve', 'prod' }
4+
local runners = require('static.runners')
5+
local runnerNames = vim.tbl_keys(runners.runners)
46

57
local execute = function(args)
68
-- action == build, serve, prod
79
local arguments = args.fargs
8-
local action = arguments[1]
9-
-- remove the first argument which is the action
10-
table.remove(arguments, 1)
10+
local runnerName
11+
local runner
12+
local action
1113

12-
local _, runner = require('static.runners').find()
14+
-- If the second argument is the name of a runner
15+
-- e.g. :Static astro build
16+
if vim.tbl_contains(runnerNames, arguments[1]) then
17+
runnerName = arguments[1]
18+
action = arguments[2]
19+
20+
table.remove(arguments, 2)
21+
table.remove(arguments, 1)
22+
else
23+
action = arguments[1]
24+
table.remove(arguments, 1)
25+
end
26+
27+
_, runner = runners.find(runnerName)
1328

1429
if not runner then
1530
vim.notify('Could not determine the correct runner.', vim.log.levels.WARN)
1631
return
1732
end
1833

34+
if not action then
35+
vim.notify('Could not determine the action.', vim.log.levels.WARN)
36+
return
37+
end
38+
1939
local command = runner[action]
2040

2141
if command == nil then
@@ -34,9 +54,35 @@ function M.setup(opts)
3454
config.setup(opts)
3555

3656
vim.api.nvim_create_user_command('Static', execute, {
37-
nargs = '*',
38-
complete = function()
39-
return actions
57+
nargs = '+',
58+
complete = function(_, line)
59+
local parts = vim.split(vim.trim(line), '%s+')
60+
61+
if parts[1]:find('Static') then
62+
table.remove(parts, 1)
63+
end
64+
65+
if line:sub(-1) == ' ' then
66+
parts[#parts + 1] = ''
67+
end
68+
69+
table.remove(parts, 1)
70+
71+
if #parts > 0 then
72+
return actions
73+
end
74+
75+
local merged = {}
76+
77+
for _, value in pairs(actions) do
78+
table.insert(merged, value)
79+
end
80+
81+
for _, value in pairs(runnerNames) do
82+
table.insert(merged, value)
83+
end
84+
85+
return merged
4086
end,
4187
})
4288
end

lua/static/runners.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ M.runners = {
5959
},
6060
}
6161

62-
M.find = function()
62+
M.find = function(runnerName)
63+
if runnerName then
64+
return runnerName, M.runners[runnerName]
65+
end
66+
6367
for name, runner in pairs(M.runners) do
6468
local r = vim.fs.find(runner.file, {
6569
path = runner.path,

0 commit comments

Comments
 (0)