From eb9dec7dcd0c1798314f4b547f4930c9b6e428be Mon Sep 17 00:00:00 2001 From: bhagwan Date: Thu, 22 Jul 2021 12:53:16 -0700 Subject: [PATCH] added provider for builtin methods --- README.md | 1 + lua/fzf-lua/actions.lua | 26 ++++++++++++++++++++------ lua/fzf-lua/config.lua | 11 +++++++++++ lua/fzf-lua/core.lua | 6 +++--- lua/fzf-lua/init.lua | 13 ++++++++++++- lua/fzf-lua/providers/buffers.lua | 2 +- lua/fzf-lua/providers/colorschemes.lua | 14 ++++++++++---- lua/fzf-lua/providers/helptags.lua | 2 +- lua/fzf-lua/providers/lsp.lua | 2 +- lua/fzf-lua/providers/manpages.lua | 2 +- 10 files changed, 61 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index e274309..f8a71a7 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ nnoremap lua require('fzf-lua').files() |`help_tags`|help tags| |`man_pages`|man pages| |`colorschemes`|color schemes| +|`builtin`|fzf-lua builtin methods| ## LSP Commands diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 8b97007..8ac55de 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -10,14 +10,19 @@ M.expect = function(actions) end end if #keys > 0 then - return table.concat(keys, ',') + return string.format("--expect=%s", table.concat(keys, ',')) end return nil end -M.act = function(actions, action, selected) - if not actions or not action then return end - if #action == 0 then action = "default" end +M.act = function(actions, selected) + if not actions or not selected then return end + local action = "default" + -- if there are no actions besides default + -- the table will contain the results directly + -- otherwise 'selected[1]` will contain the keybind + -- empty string in selected[1] represents default + if #selected>1 and #selected[1]>0 then action = selected[1] end if actions[action] then actions[action](selected) end @@ -107,8 +112,17 @@ M.buf_del = function(selected) end M.colorscheme = function(selected) - if not selected or #selected < 2 then return end - vim.cmd("colorscheme " .. selected[2]) + if not selected then return end + local colorscheme = selected[1] + if #selected>1 then colorscheme = selected[2] end + vim.cmd("colorscheme " .. colorscheme) +end + +M.run_builtin = function(selected) + if not selected then return end + local method = selected[1] + if #selected>1 then method = selected[2] end + vim.cmd(string.format("lua require'fzf-lua'.%s()", method)) end M.help = function(selected) diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 34fc479..6e57217 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -200,6 +200,17 @@ M.lsp = { }, } +M.builtin = { + prompt = 'Builtin> ', + winopts = { + win_height = 0.65, + win_width = 0.50, + }, + actions = { + ["default"] = actions.run_builtin, + }, +} + -- toggle preview -- toggle preview text wrap -- | page down|up diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 869d9e8..99861f3 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -51,7 +51,7 @@ M.build_fzf_cli = function(opts) [[ %s --layout=%s --bind=%s --prompt=%s]] .. [[ --preview-window='%s%s' --preview=%s]] .. [[ --height=100%% --ansi --info=inline]] .. - [[ --expect=%s %s %s]], + [[ %s %s %s]], opts.fzf_args or cfg.fzf_args or '', opts.fzf_layout or cfg.fzf_layout, utils._if(opts.fzf_binds, opts.fzf_binds, @@ -60,7 +60,7 @@ M.build_fzf_cli = function(opts) utils._if(opts.preview_window, opts.preview_window, cfg.preview_window()), utils._if(#opts.preview_offset>0, ":"..opts.preview_offset, ''), utils._if(opts.preview, opts.preview, M.preview_cmd(opts, cfg)), - utils._if(opts.actions, actions.expect(opts.actions), 'ctrl-s,ctrl-v,ctrl-t'), + utils._if(actions.expect(opts.actions), actions.expect(opts.actions), ''), utils._if(opts.nomulti, '--no-multi', '--multi'), utils._if(opts.cli_args, opts.cli_args, '') ) @@ -208,7 +208,7 @@ M.fzf_files = function(opts) -- dump fails after fzf for some odd reason -- functions are still valid as can seen by pairs -- _G.dump(opts.actions) - actions.act(opts.actions, selected[1], selected) + actions.act(opts.actions, selected) end)() diff --git a/lua/fzf-lua/init.lua b/lua/fzf-lua/init.lua index 87508de..911e744 100644 --- a/lua/fzf-lua/init.lua +++ b/lua/fzf-lua/init.lua @@ -163,11 +163,15 @@ function M.setup(opts) setopts(config.helptags, opts.helptags, { prompt = "string", }) + setopts(config.builtin, opts.builtin, { + prompt = "string", + }) -- table overrides without losing defaults for _, k in ipairs({ "git", "files", "oldfiles", "buffers", - "grep", "quickfix", "loclist", + "grep", "quickfix", "loclist", "lsp", "colorschemes", "helptags", "manpages", + "builtin", }) do setopt_tbl(config[k], opts[k], "actions") setopt_tbl(config[k], opts[k], "winopts") @@ -216,4 +220,11 @@ M.lsp_code_actions = require'fzf-lua.providers.lsp'.code_actions M.lsp_document_diagnostics = require'fzf-lua.providers.lsp'.diagnostics M.lsp_workspace_diagnostics = require'fzf-lua.providers.lsp'.workspace_diagnostics +M.builtin = function(self) + return require'fzf-lua.providers.module'.metatable({ + metatable = M, + metatable_exclude = { ["setup"]=0, ["fzf_files"]=0 }, + }) +end + return M diff --git a/lua/fzf-lua/providers/buffers.lua b/lua/fzf-lua/providers/buffers.lua index c507b95..f88c57b 100644 --- a/lua/fzf-lua/providers/buffers.lua +++ b/lua/fzf-lua/providers/buffers.lua @@ -140,7 +140,7 @@ M.buffers = function(opts) end end - actions.act(opts.actions, selected[1], selected) + actions.act(opts.actions, selected) end)() end diff --git a/lua/fzf-lua/providers/colorschemes.lua b/lua/fzf-lua/providers/colorschemes.lua index 5cecd4d..0c14cf9 100644 --- a/lua/fzf-lua/providers/colorschemes.lua +++ b/lua/fzf-lua/providers/colorschemes.lua @@ -37,20 +37,26 @@ M.colorschemes = function(opts) local current_background = vim.o.background local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color')) + -- must add ':nohidden' or fzf ignore the preview action + -- disabling our live preview of colorschemes opts.preview = prev_act - opts.preview_window = opts.preview_window or 'right:0' + opts.preview_window = opts.preview_window or 'nohidden:right:0' opts.nomulti = utils._if(opts.nomulti~=nil, opts.nomulti, true) local selected = fzf.fzf(colors, core.build_fzf_cli(opts), config.winopts(opts.winopts)) - if not selected then + -- reset color scheme if live_preview is enabled + -- and nothing or non-default action was selected + if opts.live_preview and (not selected or #selected[1]>0) then vim.o.background = current_background vim.cmd("colorscheme " .. current_colorscheme) vim.o.background = current_background - else - actions.act(opts.actions, selected[1], selected) + end + + if selected then + actions.act(opts.actions, selected) end if opts.post_reset_cb then diff --git a/lua/fzf-lua/providers/helptags.lua b/lua/fzf-lua/providers/helptags.lua index 6fd6241..dae9d97 100644 --- a/lua/fzf-lua/providers/helptags.lua +++ b/lua/fzf-lua/providers/helptags.lua @@ -113,7 +113,7 @@ M.helptags = function(opts) if not selected then return end - actions.act(opts.actions, selected[1], selected) + actions.act(opts.actions, selected) end)() diff --git a/lua/fzf-lua/providers/lsp.lua b/lua/fzf-lua/providers/lsp.lua index c556c63..81a81ca 100644 --- a/lua/fzf-lua/providers/lsp.lua +++ b/lua/fzf-lua/providers/lsp.lua @@ -266,7 +266,7 @@ M.code_actions = function(opts) if not selected then return end - actions.act(opts.actions, selected[1], selected) + actions.act(opts.actions, selected) end)() diff --git a/lua/fzf-lua/providers/manpages.lua b/lua/fzf-lua/providers/manpages.lua index 8538b0d..c38241a 100644 --- a/lua/fzf-lua/providers/manpages.lua +++ b/lua/fzf-lua/providers/manpages.lua @@ -50,7 +50,7 @@ M.manpages = function(opts) end end - actions.act(opts.actions, selected[1], selected) + actions.act(opts.actions, selected) end)()