feat: Sloth command has now completion

main
LeMarsu 2024-06-05 01:54:41 +02:00
parent 459eadfae4
commit f50f314c98
4 changed files with 138 additions and 44 deletions

View File

@ -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

View File

@ -1,7 +1,23 @@
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 = {
all = {},
loaded = {},
notloaded = {},
}
return {
complete = function(line)
if line.arg_idx == 3 then
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 filter = args[1] or "all"
local deps = vim.iter(Dep.all()):map(function(_, dep) local deps = vim.iter(Dep.all()):map(function(_, dep)
return dep.name return dep.name
@ -26,4 +42,5 @@ return function(args)
for _, dep in ipairs(deps) do for _, dep in ipairs(deps) do
print(string.format("- %s", dep)) print(string.format("- %s", dep))
end end
end end,
}

View File

@ -1,7 +1,23 @@
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 {
complete = function(line)
local previous_deps = vim.iter(line.args):enumerate():map(function(i, arg)
if i < 3 or i > line.arg_idx then return end
return arg.arg
end):totable()
local prefix = line.args[line.arg_idx].arg
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 if #plugins == 0 then
utils.error("You should at least give a plugin to load!") utils.error("You should at least give a plugin to load!")
return return
@ -12,4 +28,5 @@ return function(plugins)
dep:load() dep:load()
end end
end end
end end,
}

View File

@ -1,4 +1,7 @@
return function() return {
complete = function() end,
cmd = function()
local version = require('sloth-flake.version') local version = require('sloth-flake.version')
print(string.format('Sloth v%s', version())) print(string.format('Sloth v%s', version()))
end end,
}