diff --git a/site-modules/core/files/vim/init.vim b/site-modules/core/files/vim/init.vim index e0267db..7c861ec 100644 --- a/site-modules/core/files/vim/init.vim +++ b/site-modules/core/files/vim/init.vim @@ -70,6 +70,15 @@ noremap ; gcc inoremap jj :w nnoremap + +" Add mapping to "Run" tasks from the taskfile +" +" This needs to be added here so its defined before the Ferret plugin is +" loaded. Ferret adds a mapping to r only if one is not defined. If we +" do this in the taksfile plugin it will be too late and the Ferret mapping +" will only be added. +nmap r :IvyTaskfile + " Use the CommandTWildIgnore insted of the wildignore so I can still get " completion for :e in folders like vendor or node_modules but, they will be " ignored from command-t file searches. diff --git a/site-modules/core/files/vim/plugin/taskfile.lua b/site-modules/core/files/vim/plugin/taskfile.lua new file mode 100644 index 0000000..d913fc0 --- /dev/null +++ b/site-modules/core/files/vim/plugin/taskfile.lua @@ -0,0 +1,42 @@ +local libivy = require "ivy.libivy" + +local get_tasks = function() + local task_output, _ = vim.fn.system { "task", "-j", "--list" } + local output = vim.json.decode(task_output) + + return output.tasks +end + +vim.api.nvim_create_user_command("IvyTaskfile", function() + local task_list = get_tasks() + vim.ivy.run("Taskfile", function(input) + local tasks = {} + for _, task in ipairs(task_list) do + local content = task.name .. " » " .. task.desc + local score = libivy.ivy_match(input, content) + if score > -200 then + table.insert(tasks, { content = content, score = score }) + end + end + + table.sort(tasks, function(a, b) + return a.score < b.score + end) + + return tasks + end, function(item) + local name, _ = unpack(vim.split(item, " » ")) + vim.fn.jobstart({ "task", name }, { + on_stdout = function(_, data) + print(table.concat(data, "\n")) + end, + on_exit = function(_, data) + if data ~= 0 then + print("Task " .. name .. " failed") + else + print("Task " .. name .. " succeeded") + end + end, + }) + end) +end, {})