From fe0b3d1184372cadb3f8d416c232a75f4d072ee7 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Fri, 16 Jul 2021 10:39:02 -0700 Subject: [PATCH] escape special chars before search so we can grep [(...)] --- lua/fzf-lua/providers/grep.lua | 8 ++++++++ lua/fzf-lua/utils.lua | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index f1cc122..d04cf25 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -46,6 +46,10 @@ M.grep = function(opts) "rg_opts", "grep_opts", }) + if opts.search and #opts.search>0 then + opts.search = utils.rg_escape(opts.search) + end + if opts.repeat_last_search == true then opts.search = config.grep.last_search end @@ -103,6 +107,10 @@ M.live_grep = function(opts) "rg_opts", "grep_opts", }) + if opts.search and #opts.search>0 then + opts.search = utils.rg_escape(opts.search) + end + -- resetting last_search will return -- {q} placeholder in our command opts.last_search = opts.search diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index c10ae41..9ba6df7 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -48,6 +48,14 @@ function M.is_git_repo() return M._if(M.shell_error(), false, true) end +function M.rg_escape(str) + -- [(~'"\/$?'`*&&||;[]<>)] + -- escape "\~$?*|[()" + return str:gsub("[\\~$?*|\\[()]", function(x) + return '\\' .. x + end) +end + M.read_file = function(filepath) local fd = vim.loop.fs_open(filepath, "r", 438) if fd == nil then return '' end