diff --git a/lua/greyjoy/callback.lua b/lua/greyjoy/callback.lua new file mode 100644 index 0000000..5e009c2 --- /dev/null +++ b/lua/greyjoy/callback.lua @@ -0,0 +1,55 @@ +local utils = require("greyjoy.utils") +local greyjoy = require("greyjoy") + +local greycall = {} + +greycall.translate = function(obj) + local orig_command = nil + local commandinput = table.concat(obj.command, " ") + if greyjoy.overrides[commandinput] then + orig_command = obj.command + obj.command = utils.str_to_array(greyjoy.overrides[commandinput]) + obj.name = greyjoy.overrides[commandinput] + end + obj.orig_command = orig_command + return obj +end + +greycall.extensions = function(arg, callback, callbackoptions) + local bufname = vim.api.nvim_buf_get_name(0) + local filetype = vim.bo.filetype + local pluginname = arg or "" + local fileobj = utils.new_file_obj(greyjoy.patterns, bufname, filetype) + local rootdir = fileobj.rootdir + for plugin, obj in pairs(greyjoy.extensions) do + local plugin_obj = obj + local plugin_type = plugin_obj.type + + if + pluginname == "" + or pluginname == plugin + or greyjoy.__in_group(pluginname, plugin) + then + local output = {} + if plugin_type == "global" then + output = plugin_obj.parse(fileobj) + callback(output, callbackoptions) + else + if rootdir then + for _, file in pairs(plugin_obj.files) do + if utils.file_exists(rootdir .. "/" .. file) then + local fileinfo = { + filename = file, + filepath = rootdir, + } + output = plugin_obj.parse(fileinfo) + callback(output, callbackoptions) + end + end + end + end + end + end +end + +return greycall diff --git a/lua/greyjoy/fzf.lua b/lua/greyjoy/fzf.lua index edf769f..3d4f179 100644 --- a/lua/greyjoy/fzf.lua +++ b/lua/greyjoy/fzf.lua @@ -2,31 +2,20 @@ local greyfzf = {} local utils = require("greyjoy.utils") local greyjoy = require("greyjoy") +local callback = require("greyjoy.callback") -local translate = function(obj) - local orig_command = nil - local commandinput = table.concat(obj.command, " ") - if greyjoy.overrides[commandinput] then - orig_command = obj.command - obj.command = utils.str_to_array(greyjoy.overrides[commandinput]) - obj.name = greyjoy.overrides[commandinput] - end - obj.orig_command = orig_command - return obj -end - -local fzfadddata = function(co, fzf_cb, content, count, output) +local fzfcallback = function(output, callbackoptions) for _, result in ipairs(output) do - local translated = translate(result) - table.insert(content, translated) - count = count + 1 + local translated = callback.translate(result) + table.insert(callbackoptions.content, translated) + callbackoptions.count = callbackoptions.count + 1 - local key = count .. ":" .. translated.name - content[key] = translated + local key = callbackoptions.count .. ":" .. translated.name + callbackoptions.content[key] = translated vim.schedule(function() - fzf_cb(key, function() - coroutine.resume(co) + callbackoptions.fzf_cb(key, function() + coroutine.resume(callbackoptions.co) end) end) coroutine.yield() @@ -34,12 +23,6 @@ local fzfadddata = function(co, fzf_cb, content, count, output) end greyfzf.run = function(arg) - local bufname = vim.api.nvim_buf_get_name(0) - local filetype = vim.bo.filetype - local pluginname = arg or "" - local fileobj = utils.new_file_obj(greyjoy.patterns, bufname, filetype) - local rootdir = fileobj.rootdir - local fzf = require("fzf-lua") local content = {} local count = 0 @@ -48,43 +31,13 @@ greyfzf.run = function(arg) coroutine.wrap(function() local co = coroutine.running() - for plugin, obj in pairs(greyjoy.extensions) do - local plugin_obj = obj - local plugin_type = plugin_obj.type + callback.extensions(arg, fzfcallback, { + co = co, + fzf_cb = fzf_cb, + content = content, + count = count, + }) - if - pluginname == "" - or pluginname == plugin - or greyjoy.__in_group(pluginname, plugin) - then - local output = {} - if plugin_type == "global" then - output = plugin_obj.parse(fileobj) - fzfadddata(co, fzf_cb, content, count, output) - else - if rootdir then - for _, file in pairs(plugin_obj.files) do - if - utils.file_exists(rootdir .. "/" .. file) - then - local fileinfo = { - filename = file, - filepath = rootdir, - } - output = plugin_obj.parse(fileinfo) - fzfadddata( - co, - fzf_cb, - content, - count, - output - ) - end - end - end - end - end - end fzf_cb() end)() end, { diff --git a/lua/greyjoy/telescope.lua b/lua/greyjoy/telescope.lua index 3539cf4..4594c22 100644 --- a/lua/greyjoy/telescope.lua +++ b/lua/greyjoy/telescope.lua @@ -8,6 +8,7 @@ local actions = require("telescope.actions") local utils = require("greyjoy.utils") local greyjoy = require("greyjoy") +local callback = require("greyjoy.callback") local all_results = {} local opts = {} @@ -54,8 +55,6 @@ local translate = function(obj) end greytelescope.run = function(arg) - local bufname = vim.api.nvim_buf_get_name(0) - local filetype = vim.bo.filetype all_results = {} local picker = pickers.new(opts, { @@ -70,7 +69,7 @@ greytelescope.run = function(arg) end end, }, - attach_mappings = function(prompt_bufnr, map) + attach_mappings = function(_, map) map( "i", greyjoy.ui.telescope.keys.select, @@ -130,41 +129,14 @@ greytelescope.run = function(arg) picker:refresh(generate_new_finder(), opts) end - local pluginname = arg or "" - local fileobj = utils.new_file_obj(greyjoy.patterns, bufname, filetype) - local rootdir = fileobj.rootdir - - for plugin, obj in pairs(greyjoy.extensions) do - local plugin_obj = obj - local plugin_type = plugin_obj.type - - if - pluginname == "" - or pluginname == plugin - or greyjoy.__in_group(pluginname, plugin) - then - vim.schedule(function() - if plugin_type == "global" then - local output = plugin_obj.parse(fileobj) - handle_new_results(output) - else - if rootdir then - for _, file in pairs(plugin_obj.files) do - if utils.file_exists(rootdir .. "/" .. file) then - local fileinfo = { - filename = file, - filepath = rootdir, - } - local output = plugin_obj.parse(fileinfo) - handle_new_results(output) - end - end - end - end - end) - end + local function telescopecallback(output) + vim.schedule(function() + handle_new_results(output) + end) end + callback.extensions(arg, telescopecallback, {}) + picker:find() end