ibhagwan / fzf-lua

Improved fzf.vim written in lua
MIT License
2.37k stars 151 forks source link

Feature: Add action toggle_fixed_strings #1502

Closed hernancerm closed 1 month ago

hernancerm commented 1 month ago

Problem

Solution

··
  grep = {
    actions = {
      ["ctrl-g"] = { actions.grep_lgrep },
      ["ctrl-x"] = { actions.toggle_fixed_strings } -- Toggle between regex/literal search with <C-x>.
    },
  }
··

Other solutions I considered

Thanks.

ibhagwan commented 1 month ago

Ty @hernancerm but I don’t wish to add actions for every single flag possible, you can achieve this customization easily for any flag as below:


require("fzf-lua").setup({
  grep = {
    actions = {
      ["alt-g"] = {
        fn = function(_, opts)
          require("fzf-lua").actions.toggle_flag(_, vim.tbl_extend("force", opts, {
            toggle_flag = "--ignore-case"
          }))
        end,
        desc = "toggle-ignore-case",
        header = "Toggle Ignore Case",
      },
    },
  },
})
hernancerm commented 1 month ago

I figured that might be a concern, I understand. Thank you for the snippet! The only thing it's missing is alternating the header between "Respect special regex chars" and "Disable special regex chars" depending on the presence of the flag. I'll try to tweak what you provided to get this behavior.

ibhagwan commented 1 month ago

I figured that might be a concern, I understand. Thank you for the snippet! The only thing it's missing is alternating the header between "Respect special regex chars" and "Disable special regex chars" depending on the presence of the flag. I'll try to tweak what you provided to get this behavior.

Change the header to a function and you can have that too:

    header = function(o)
      local flag = o.toggle_hidden_flag or "--hidden"
      if o.cmd and o.cmd:match(utils.lua_regex_escape(flag)) then
        return "Exclude hidden files"
      else
        return "Include hidden files"
      end
    end,
hernancerm commented 1 month ago

That works like a charm. Thanks for the follow up @ibhagwan!

For future reference, here is the full action mapping I'm using to toggle --fixed-strings on grep:

["ctrl-x"] = {
  fn = function(_, opts)
    actions.toggle_flag(_, vim.tbl_extend("force", opts, {
      toggle_flag = "--fixed-strings"
    }))
  end,
  desc = "toggle-fixed-strings",
  header = function(o)
    local flag = "--fixed-strings"
    if o.cmd and o.cmd:match(utils.lua_regex_escape(flag)) then
      return "Respect regex chars"
    else
      return "Disable regex chars"
    end
  end,
}
ibhagwan commented 1 month ago

Ty for the update and snippet @hernancerm!