Skip to content

Commit

Permalink
Merge pull request #48 from desdic/fzf
Browse files Browse the repository at this point in the history
feat: Add support for fzf
  • Loading branch information
desdic authored Dec 2, 2024
2 parents 2b68c88 + 8b614dc commit e51ffaf
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

[![Greyjoy.nvim demo](http://img.youtube.com/vi/9AcNjkqROIM/0.jpg)](http://www.youtube.com/watch?v=9AcNjkqROIM "Greyjoy.nvim demo")

Greyjoy per default uses vim.ui.select so the settings from (telescope, dressing etc.) menu will reflect it. But there is also a telescope only version that uses async which makes the UI feel faster.
Greyjoy per default uses vim.ui.select so the settings from (telescope, dressing etc.) menu will reflect it. But there is also support for telescope (requires [telescope](https://github.com/nvim-telescope/telescope.nvim)) and a fzf (requires [fzf-lua](https://github.com/ibhagwan/fzf-lua))

Integration with [toggleterm](https://github.com/akinsho/toggleterm.nvim) is also provided.

Expand Down
6 changes: 6 additions & 0 deletions doc/greyjoy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ Default options:
edit = "<C-e>", -- CTRL-e
},
},
fzf = {
keys = {
select = "enter", -- enter as fzf wants it
edit = "ctrl-e", -- <C-e> as fzf wants it
},
},
},
toggleterm = {
-- default_group_id can be a number or a function that takes a string as parameter.
Expand Down
55 changes: 55 additions & 0 deletions lua/greyjoy/callback.lua
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lua/greyjoy/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ local defaults = {
edit = "<C-e>", -- CTRL-e
},
},
fzf = {
keys = {
select = "enter", -- enter as fzf wants it
edit = "ctrl-e", -- <C-e> as fzf wants it
},
},
},
toggleterm = {
-- default_group_id can be a number or a function that takes a string as parameter.
Expand Down
83 changes: 83 additions & 0 deletions lua/greyjoy/fzf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
local greyfzf = {}

local utils = require("greyjoy.utils")
local greyjoy = require("greyjoy")
local callback = require("greyjoy.callback")

local fzfcallback = function(output, callbackoptions)
for _, result in ipairs(output) do
local translated = callback.translate(result)
table.insert(callbackoptions.content, translated)
callbackoptions.count = callbackoptions.count + 1

local key = callbackoptions.count .. ":" .. translated.name
callbackoptions.content[key] = translated

vim.schedule(function()
callbackoptions.fzf_cb(key, function()
coroutine.resume(callbackoptions.co)
end)
end)
coroutine.yield()
end
end

greyfzf.run = function(arg)
local fzf = require("fzf-lua")
local content = {}
local count = 0

fzf.fzf_exec(function(fzf_cb)
coroutine.wrap(function()
local co = coroutine.running()

callback.extensions(arg, fzfcallback, {
co = co,
fzf_cb = fzf_cb,
content = content,
count = count,
})

fzf_cb()
end)()
end, {
prompt = "Run> ",
actions = {
[greyjoy.ui.fzf.keys.select] = {
fn = function(selected)
local cmd = content[selected[1]]
greyjoy.execute(cmd)
end,
silent = true,
},
[greyjoy.ui.fzf.keys.edit] = {
fn = function(selected)
local command = content[selected[1]]

local commandinput = table.concat(command.command, " ")

vim.ui.input(
{ prompt = "Edit", default = commandinput },
function(input)
if input then
if command.orig_command then
local tmp =
table.concat(command.orig_command, " ")
greyjoy.overrides[tmp] = input
else
greyjoy.overrides[commandinput] = input
end

command.command = utils.str_to_array(input)
greyjoy.execute(command)
end
end
)
end,
silent = true,
},
},
})
end

return greyfzf
7 changes: 7 additions & 0 deletions lua/greyjoy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,11 @@ if has_telescope then
end, { nargs = "*", desc = "Run greyjoy via telescope" })
end

local has_fzf, _ = pcall(require, "fzf-lua")
if has_fzf then
vim.api.nvim_create_user_command("GreyjoyFzf", function(args)
require("greyjoy.fzf").run(args.args)
end, { nargs = "*", desc = "Run greyjoy via fzf" })
end

return greyjoy
44 changes: 8 additions & 36 deletions lua/greyjoy/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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, {
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e51ffaf

Please sign in to comment.