feat: Sloth command has now completion
parent
459eadfae4
commit
f50f314c98
|
|
@ -8,13 +8,69 @@ local commands = {
|
||||||
version = require 'sloth-flake.command.version',
|
version = require 'sloth-flake.command.version',
|
||||||
}
|
}
|
||||||
|
|
||||||
function sloth_cmd(param)
|
local function parse_line(line, cursor_pos)
|
||||||
|
local raw_args = vim.split(line, ' +', { trimempty = true })
|
||||||
|
local parse_pos = 1
|
||||||
|
local args = vim.iter(raw_args):map(function(arg)
|
||||||
|
local start, stop = string.find(line, arg, parse_pos, { plain = true })
|
||||||
|
parse_pos = stop
|
||||||
|
return {
|
||||||
|
arg = arg,
|
||||||
|
start = start,
|
||||||
|
stop = stop,
|
||||||
|
}
|
||||||
|
end):totable()
|
||||||
|
parse_pos = 1
|
||||||
|
local arg_idx = 1
|
||||||
|
vim.iter(args):find(function(arg)
|
||||||
|
if cursor_pos < arg.start then
|
||||||
|
arg_idx = arg_idx - 1
|
||||||
|
return true
|
||||||
|
elseif cursor_pos <= arg.stop then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
arg_idx = arg_idx + 1
|
||||||
|
return false
|
||||||
|
end)
|
||||||
|
arg_idx = arg_idx < 1 and 1 or arg_idx
|
||||||
|
if arg_idx > #args then
|
||||||
|
args[#args + 1] = {
|
||||||
|
arg = "",
|
||||||
|
start = line:len(),
|
||||||
|
stop = line:len(),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
line = line,
|
||||||
|
args = args,
|
||||||
|
arg_idx = arg_idx,
|
||||||
|
pos = cursor_pos,
|
||||||
|
in_arg_pos = args[arg_idx] and cursor_pos - args[arg_idx].start + 1,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
-- print(vim.inspect(parse_line('Sloth ', 6)))
|
||||||
|
|
||||||
|
local function sloth_cmd_complete(arg_lead, cmd_line, cursor_pos)
|
||||||
|
local parsed_line = parse_line(cmd_line, cursor_pos)
|
||||||
|
local arg = parsed_line.args[parsed_line.arg_idx]
|
||||||
|
if parsed_line.arg_idx == 2 then
|
||||||
|
return vim.iter(commands):map(function(name, command)
|
||||||
|
return vim.startswith(name, arg.arg) and name or nil
|
||||||
|
end):totable()
|
||||||
|
elseif parsed_line.arg_idx > 2 then
|
||||||
|
local cmd = parsed_line.args[2].arg
|
||||||
|
local command = commands[cmd]
|
||||||
|
return command and command.complete(parsed_line)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function sloth_cmd(param)
|
||||||
local args = param.fargs
|
local args = param.fargs
|
||||||
local cmd = args[1] or "list";
|
local cmd = args[1] or "list";
|
||||||
table.remove(args, 1)
|
table.remove(args, 1)
|
||||||
local fn = commands[cmd]
|
local command = commands[cmd]
|
||||||
if fn then
|
if command then
|
||||||
fn(args)
|
command.cmd(args)
|
||||||
else
|
else
|
||||||
vim.api.nvim_err_writeln(string.format([[No Sloth subcommand "%s"]], cmd))
|
vim.api.nvim_err_writeln(string.format([[No Sloth subcommand "%s"]], cmd))
|
||||||
end
|
end
|
||||||
|
|
@ -23,6 +79,7 @@ end
|
||||||
function M.register()
|
function M.register()
|
||||||
vim.api.nvim_create_user_command('Sloth', sloth_cmd, {
|
vim.api.nvim_create_user_command('Sloth', sloth_cmd, {
|
||||||
nargs = '*',
|
nargs = '*',
|
||||||
|
complete = sloth_cmd_complete
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,46 @@
|
||||||
local Dep = require 'sloth-flake.dep'
|
local Dep = require 'sloth-flake.dep'
|
||||||
local utils = require 'sloth-flake.utils'
|
local utils = require 'sloth-flake.utils'
|
||||||
|
|
||||||
return function(args)
|
local filters = {
|
||||||
local filter = args[1] or "all"
|
all = {},
|
||||||
local deps = vim.iter(Dep.all()):map(function(_, dep)
|
loaded = {},
|
||||||
return dep.name
|
notloaded = {},
|
||||||
end)
|
}
|
||||||
if filter == "all" then
|
|
||||||
-- Nothing to do
|
return {
|
||||||
elseif filter == "loaded" then
|
complete = function(line)
|
||||||
deps = deps:filter(function(dep)
|
if line.arg_idx == 3 then
|
||||||
return Dep.get(dep).is_loaded
|
local prefix = line.args[3].arg
|
||||||
|
return vim.iter(vim.tbl_keys(filters)):filter(function(name)
|
||||||
|
return vim.startswith(name, prefix)
|
||||||
|
end):totable()
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
cmd = function(args)
|
||||||
|
local filter = args[1] or "all"
|
||||||
|
local deps = vim.iter(Dep.all()):map(function(_, dep)
|
||||||
|
return dep.name
|
||||||
end)
|
end)
|
||||||
elseif filter == "notloaded" then
|
if filter == "all" then
|
||||||
deps = deps:filter(function(dep)
|
-- Nothing to do
|
||||||
return not Dep.get(dep).is_loaded
|
elseif filter == "loaded" then
|
||||||
end)
|
deps = deps:filter(function(dep)
|
||||||
else
|
return Dep.get(dep).is_loaded
|
||||||
utils.error([[No Sloth list filter "%s".]], cmd)
|
end)
|
||||||
utils.error("Filters are: all, loaded, notloaded")
|
elseif filter == "notloaded" then
|
||||||
return
|
deps = deps:filter(function(dep)
|
||||||
end
|
return not Dep.get(dep).is_loaded
|
||||||
deps = deps:totable()
|
end)
|
||||||
table.sort(deps)
|
else
|
||||||
for _, dep in ipairs(deps) do
|
utils.error([[No Sloth list filter "%s".]], cmd)
|
||||||
print(string.format("- %s", dep))
|
utils.error("Filters are: all, loaded, notloaded")
|
||||||
end
|
return
|
||||||
end
|
end
|
||||||
|
deps = deps:totable()
|
||||||
|
table.sort(deps)
|
||||||
|
for _, dep in ipairs(deps) do
|
||||||
|
print(string.format("- %s", dep))
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,32 @@
|
||||||
local Dep = require 'sloth-flake.dep'
|
local Dep = require 'sloth-flake.dep'
|
||||||
local utils = require 'sloth-flake.utils'
|
local utils = require 'sloth-flake.utils'
|
||||||
|
|
||||||
return function(plugins)
|
return {
|
||||||
if #plugins == 0 then
|
complete = function(line)
|
||||||
utils.error("You should at least give a plugin to load!")
|
local previous_deps = vim.iter(line.args):enumerate():map(function(i, arg)
|
||||||
return
|
if i < 3 or i > line.arg_idx then return end
|
||||||
end
|
return arg.arg
|
||||||
for _, plugin in ipairs(plugins) do
|
end):totable()
|
||||||
local dep = Dep.get(plugin)
|
|
||||||
if dep ~= nil then
|
local prefix = line.args[line.arg_idx].arg
|
||||||
dep:load()
|
return vim.iter(Dep.all()):filter(function(name, dep)
|
||||||
|
return vim.startswith(name, prefix) and not dep.is_loaded
|
||||||
|
and not vim.list_contains(previous_deps, name)
|
||||||
|
end):map(function(name)
|
||||||
|
return name
|
||||||
|
end):totable()
|
||||||
|
end,
|
||||||
|
|
||||||
|
cmd = function(plugins)
|
||||||
|
if #plugins == 0 then
|
||||||
|
utils.error("You should at least give a plugin to load!")
|
||||||
|
return
|
||||||
end
|
end
|
||||||
end
|
for _, plugin in ipairs(plugins) do
|
||||||
end
|
local dep = Dep.get(plugin)
|
||||||
|
if dep ~= nil then
|
||||||
|
dep:load()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
return function()
|
return {
|
||||||
local version = require('sloth-flake.version')
|
complete = function() end,
|
||||||
print(string.format('Sloth v%s', version()))
|
cmd = function()
|
||||||
end
|
local version = require('sloth-flake.version')
|
||||||
|
print(string.format('Sloth v%s', version()))
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue