ibhagwan / fzf-lua

Improved fzf.vim written in lua
MIT License
2.22k stars 144 forks source link

Feature: make setup_commands.cb_create public #1261

Closed Grueslayer closed 3 months ago

Grueslayer commented 3 months ago

Have you RTFM'd?

Feature Request

I used a plugin which gave me GBranches and would like to comfig that with fzf-lua.

Nicest way is to create a command with vim.api.nvim_create_user_command by using your wrapper function cb_create. But as long this is local (in a local function) I could not access it.

Maybe you can create it as a public funtion in a better context?

ibhagwan commented 3 months ago

If you’re talking about this https://github.com/ibhagwan/fzf-lua/blob/c5eb3b051b1e38f037ffdf388406edc93465baff/lua/fzf-lua/profiles/fzf-vim.lua#L12

Profiles are sample configs meant to be used as “baseline”, this is just a small function I created for the fzf.vim profile, just copy it from the profile, it’s less than 20 lines.

You can also call :FzfLua setup_fzfvim_cmds and you’ll get all of fzf.vim’s commands.

Grueslayer commented 3 months ago

Hi, yes I can copy it but I don't really like redundance. You've put some effort and ideas to this and we all can benefit from it (and maybe your future improvements) when make this a public function (i see no value to hide this).

If you make it like this

diff --git a/lua/fzf-lua/profiles/fzf-vim.lua b/lua/fzf-lua/profiles/fzf-vim.lua
index e581f7b..882e733 100644
--- a/lua/fzf-lua/profiles/fzf-vim.lua
+++ b/lua/fzf-lua/profiles/fzf-vim.lua
@@ -1,5 +1,6 @@
 local fzf_lua = require("fzf-lua")
 local actions = fzf_lua.actions
+local utils = fzf_lua.utils

 local function cmd_exists(cmd)
   local ret = vim.fn.exists(":" .. cmd)
@@ -9,61 +10,29 @@ local function cmd_exists(cmd)
 end

 local function setup_commands(no_override, prefix)
-  local cb_create = function(provider, arg, altmap)
-    local function fzflua_opts(o)
-      local ret = {}
-      -- fzf.vim's bang version of the commands opens fullscreen
-      if o.bang then ret.winopts = { fullscreen = true } end
-      return ret
-    end
-    return function(o)
-      local prov = provider
-      local opts = fzflua_opts(o) -- setup bang!
-      if type(o.fargs[1]) == "string" then
-        local farg = o.fargs[1]
-        for c, p in pairs(altmap or {}) do
-          -- fzf.vim hijacks the first character of the arg
-          -- to setup special commands postfixed with `?:/`
-          -- "GFiles?", "History:" and "History/"
-          if farg:sub(1, 1) == c then
-            prov = p
-            -- we still allow using args with alt
-            -- providers by removing the "?:/" prefix
-            farg = #farg > 1 and vim.trim(farg:sub(2))
-            break
-          end
-        end
-        if arg and farg and #farg > 0 then
-          opts[arg] = vim.trim(farg)
-        end
-      end
-      fzf_lua[prov](opts)
-    end
-  end
-
   local cmds = {
-    ["Files"] = cb_create("files", "cwd"),
-    ["GFiles"] = cb_create("git_files", "cwd", { ["?"] = "git_status" }),
-    ["Buffers"] = cb_create("buffers"),
-    ["Colors"] = cb_create("colorschemes"),
-    ["Rg"] = cb_create("grep", "search"),
-    ["RG"] = cb_create("live_grep", "search"),
-    ["Lines"] = cb_create("lines", "query"),
-    ["BLines"] = cb_create("blines", "query"),
-    ["Tags"] = cb_create("tags", "query"),
-    ["BTags"] = cb_create("btags", "query"),
-    ["Changes"] = cb_create("changes"),
-    ["Marks"] = cb_create("marks"),
-    ["Jumps"] = cb_create("jumps"),
-    ["History"] = cb_create("oldfiles", "query", {
+    ["Files"] = utils.create_user_command_callback("files", "cwd"),
+    ["GFiles"] = utils.create_user_command_callback("git_files", "cwd", { ["?"] = "git_status" }),
+    ["Buffers"] = utils.create_user_command_callback("buffers"),
+    ["Colors"] = utils.create_user_command_callback("colorschemes"),
+    ["Rg"] = utils.create_user_command_callback("grep", "search"),
+    ["RG"] = utils.create_user_command_callback("live_grep", "search"),
+    ["Lines"] = utils.create_user_command_callback("lines", "query"),
+    ["BLines"] = utils.create_user_command_callback("blines", "query"),
+    ["Tags"] = utils.create_user_command_callback("tags", "query"),
+    ["BTags"] = utils.create_user_command_callback("btags", "query"),
+    ["Changes"] = utils.create_user_command_callback("changes"),
+    ["Marks"] = utils.create_user_command_callback("marks"),
+    ["Jumps"] = utils.create_user_command_callback("jumps"),
+    ["History"] = utils.create_user_command_callback("oldfiles", "query", {
       [":"] = "command_history",
       ["/"] = "search_history",
     }),
-    ["Commits"] = cb_create("git_commits", "query"),
-    ["BCommits"] = cb_create("git_bcommits", "query"),
-    ["Maps"] = cb_create("keymaps", "query"),
-    ["Helptags"] = cb_create("help_tags", "query"),
-    ["Filetypes"] = cb_create("filetypes", "query"),
+    ["Commits"] = utils.create_user_command_callback("git_commits", "query"),
+    ["BCommits"] = utils.create_user_command_callback("git_bcommits", "query"),
+    ["Maps"] = utils.create_user_command_callback("keymaps", "query"),
+    ["Helptags"] = utils.create_user_command_callback("help_tags", "query"),
+    ["Filetypes"] = utils.create_user_command_callback("filetypes", "query"),
   }

   for cmd, cb in pairs(cmds) do
diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua
index d368cd3..8407bbf 100644
--- a/lua/fzf-lua/utils.lua
+++ b/lua/fzf-lua/utils.lua
@@ -1158,4 +1158,37 @@ function M.windows_pipename()
   return ([[\\.\pipe\%s]]):format(tmpname)
 end

+function M.create_user_command_callback(provider, arg, altmap)
+  local function fzflua_opts(o)
+    local ret = {}
+    -- fzf.vim's bang version of the commands opens fullscreen
+    if o.bang then ret.winopts = { fullscreen = true } end
+    return ret
+  end
+  return function(o)
+    local fzf_lua = require("fzf-lua")
+    local prov = provider
+    local opts = fzflua_opts(o) -- setup bang!
+    if type(o.fargs[1]) == "string" then
+      local farg = o.fargs[1]
+      for c, p in pairs(altmap or {}) do
+        -- fzf.vim hijacks the first character of the arg
+        -- to setup special commands postfixed with `?:/`
+        -- "GFiles?", "History:" and "History/"
+        if farg:sub(1, 1) == c then
+          prov = p
+          -- we still allow using args with alt
+          -- providers by removing the "?:/" prefix
+          farg = #farg > 1 and vim.trim(farg:sub(2))
+          break
+        end
+      end
+      if arg and farg and #farg > 0 then
+        opts[arg] = vim.trim(farg)
+      end
+    end
+    fzf_lua[prov](opts)
+  end
+end
+
 return M

I can easily add my own commands (which isn't part of the fzf-vim profile) with

      vim.api.nvim_create_user_command('GBranches', require('fzf-lua').utils.create_user_command_callback("git_branches", "query"), { bang = true, nargs = "?" })
ibhagwan commented 3 months ago

Would you like to submit a PR for that?

Grueslayer commented 3 months ago

Yap..