From cc74396e8ff9117deb127171912bacb2dba846e5 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Tue, 5 Oct 2021 08:43:28 -0700 Subject: [PATCH] added 'args' provider --- README.md | 12 ++++++++-- doc/fzf-lua.txt | 12 ++++++++-- lua/fzf-lua/actions.lua | 10 +++++++++ lua/fzf-lua/config.lua | 9 ++++++++ lua/fzf-lua/init.lua | 1 + lua/fzf-lua/providers/files.lua | 39 +++++++++++++++++++++++++++++++++ lua/fzf-lua/utils.lua | 8 +++++++ 7 files changed, 87 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8f64c85..8526157 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ vim.api.nvim_set_keymap('n', '', | `lines` | open buffers lines | | `blines` | current buffer lines | | `tabs` | open tabs | +| `args` | argument list | ### Search | Command | List | @@ -345,7 +346,7 @@ require'fzf-lua'.setup { ["ctrl-t"] = actions.file_tabedit, ["alt-q"] = actions.file_sel_to_qf, -- custom actions are available too - ["ctrl-y"] = function(selected) print(selected[2]) end, + ["ctrl-y"] = function(selected) print(selected[1]) end, } }, git = { @@ -411,6 +412,13 @@ require'fzf-lua'.setup { file_icons = true, -- show file icons? color_icons = true, -- colorize file|git icons }, + args = { + prompt = 'Args❯ ', + actions = { + -- added on top of regular file actions + ["ctrl-x"] = actions.arg_del, + } + }, oldfiles = { prompt = 'History❯ ', cwd_only = false, @@ -444,7 +452,7 @@ require'fzf-lua'.setup { live_preview = true, -- apply the colorscheme on preview? actions = { ["default"] = actions.colorscheme, - ["ctrl-y"] = function(selected) print(selected[2]) end, + ["ctrl-y"] = function(selected) print(selected[1]) end, }, winopts = { win_height = 0.55, diff --git a/doc/fzf-lua.txt b/doc/fzf-lua.txt index acff045..5aa7094 100644 --- a/doc/fzf-lua.txt +++ b/doc/fzf-lua.txt @@ -160,6 +160,7 @@ BUFFERS AND FILES *fzf-lua-buffers-and-files* | `lines` | open buffers lines | | `blines` | current buffer lines | | `tabs` | open tabs | +| `args` | argument list | SEARCH *fzf-lua-search* @@ -379,7 +380,7 @@ Consult the list below for available settings: ["ctrl-t"] = actions.file_tabedit, ["alt-q"] = actions.file_sel_to_qf, -- custom actions are available too - ["ctrl-y"] = function(selected) print(selected[2]) end, + ["ctrl-y"] = function(selected) print(selected[1]) end, } }, git = { @@ -445,6 +446,13 @@ Consult the list below for available settings: file_icons = true, -- show file icons? color_icons = true, -- colorize file|git icons }, + args = { + prompt = 'Args❯ ', + actions = { + -- added on top of regular file actions + ["ctrl-x"] = actions.arg_del, + } + }, oldfiles = { prompt = 'History❯ ', cwd_only = false, @@ -478,7 +486,7 @@ Consult the list below for available settings: live_preview = true, -- apply the colorscheme on preview? actions = { ["default"] = actions.colorscheme, - ["ctrl-y"] = function(selected) print(selected[2]) end, + ["ctrl-y"] = function(selected) print(selected[1]) end, }, winopts = { win_height = 0.55, diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 6ae23f6..b6f9d67 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -340,4 +340,14 @@ M.git_buf_vsplit = function(selected, opts) M.git_buf_edit(selected, opts) end +M.arg_add = function(selected, opts) + local vimcmd = "argadd" + M.vimcmd_file(vimcmd, selected, opts) +end + +M.arg_del = function(selected, opts) + local vimcmd = "argdel" + M.vimcmd_file(vimcmd, selected, opts) +end + return M diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 1874d86..79f0189 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -213,6 +213,15 @@ M.globals.grep = { rg_opts = "--column --line-number --no-heading --color=always --smart-case", actions = M.globals.files.actions, } +M.globals.args = { + previewer = function() return M.globals.default_previewer end, + prompt = 'Args> ', + file_icons = true and M._has_devicons, + color_icons = true, + git_icons = true, + actions = M.globals.files.actions, + } +M.globals.args.actions["ctrl-x"] = actions.arg_del M.globals.oldfiles = { previewer = function() return M.globals.default_previewer end, prompt = 'History> ', diff --git a/lua/fzf-lua/init.lua b/lua/fzf-lua/init.lua index 2040185..4571ae0 100644 --- a/lua/fzf-lua/init.lua +++ b/lua/fzf-lua/init.lua @@ -58,6 +58,7 @@ end M.fzf_files = require'fzf-lua.core'.fzf_files M.files = require'fzf-lua.providers.files'.files M.files_resume = require'fzf-lua.providers.files'.files_resume +M.args = require'fzf-lua.providers.files'.args M.grep = require'fzf-lua.providers.grep'.grep M.live_grep = require'fzf-lua.providers.grep'.live_grep_native M.live_grep_native = require'fzf-lua.providers.grep'.live_grep_native diff --git a/lua/fzf-lua/providers/files.lua b/lua/fzf-lua/providers/files.lua index 8c9e854..fc3fa89 100644 --- a/lua/fzf-lua/providers/files.lua +++ b/lua/fzf-lua/providers/files.lua @@ -71,4 +71,43 @@ M.files_resume = function(opts) return core.fzf_files(opts) end +M.args = function(opts) + opts = config.normalize_opts(opts, config.globals.args) + if not opts then return end + + local entries = vim.fn.execute("args") + entries = utils.strsplit(entries, "%s\n") + -- remove the current file indicator + -- remove all non-files + local args = {} + for _, s in ipairs(entries) do + if s:match('^%[') then + s = s:gsub('^%[', ''):gsub('%]$', '') + end + local st = vim.loop.fs_stat(s) + if opts.files_only == false or + st and st.type == 'file' then + table.insert(args, s) + end + end + entries = nil + + opts.fzf_fn = function (cb) + for _, x in ipairs(args) do + x = core.make_entry_file(opts, x) + if x then + cb(x, function(err) + if err then return end + -- close the pipe to fzf, this + -- removes the loading indicator in fzf + cb(nil, function() end) + end) + end + end + utils.delayed_cb(cb) + end + + return core.fzf_files(opts) +end + return M diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index 20e5a80..d1a3ef2 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -22,6 +22,14 @@ M._if = function(bool, a, b) end end +M.strsplit = function(inputstr, sep) + local t={} + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + table.insert(t, str) + end + return t +end + function M.round(num, limit) if not num then return nil end if not limit then limit = 0.5 end