From 37ea63c26bf8d91e3d92c30d45e19b41c229f500 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Mon, 13 Dec 2021 14:57:12 -0800 Subject: [PATCH] grep|live_grep: added support for multiprocess search resume --- lua/fzf-lua/make_entry.lua | 36 ++++++++++++++++++++++++++++ lua/fzf-lua/providers/grep.lua | 43 ++++++++++++++++------------------ 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/lua/fzf-lua/make_entry.lua b/lua/fzf-lua/make_entry.lua index 79e39ef..433c7a8 100644 --- a/lua/fzf-lua/make_entry.lua +++ b/lua/fzf-lua/make_entry.lua @@ -48,6 +48,36 @@ local function load_config_section(s, datatype) end end +local function set_config_section(s, data) + if M._fzf_lua_server then + -- save config in our running instance + local ok, errmsg = pcall(function() + local chan_id = vim.fn.sockconnect("pipe", M._fzf_lua_server, { rpc = true }) + vim.rpcrequest(chan_id, "nvim_exec_lua", ([[ + local data = select(1, ...) + require'fzf-lua'.config.%s = data + ]]):format(s), { data }) + vim.fn.chanclose(chan_id) + end) + if not ok then + io.stderr:write(("Error setting remote config section '%s': %s\n") + :format(s, errmsg)) + end + return ok + elseif config then + local keys = utils.strsplit(s, '.') + local iter = config + for i=1,#keys do + iter = iter[keys[i]] + if not iter then break end + if i == #keys-1 then + iter[keys[i+1]] = data + return iter + end + end + end +end + -- Setup the terminal colors codes for nvim-web-devicons colors local setup_devicon_term_hls = function() local function hex(hexstr) @@ -156,6 +186,12 @@ M.preprocess = function(opts) return i and vim.v.argv[i+6] or nil end + -- save our last search argument for resume + if opts.cmd:match("{argv1}") then + set_config_section('globals.grep._last_search', + { query = argv(1), no_esc = true }) + end + -- did the caller request rg with glob support? -- mannipulation needs to be done before the argv hack if opts.rg_glob then diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index e796c59..f44c05e 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -3,7 +3,17 @@ local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" -local last_search = {} +local function get_last_search() + local last_search = config.globals.grep._last_search or {} + return last_search.query, last_search.no_esc +end + +local function set_last_search(query, no_esc) + config.globals.grep._last_search = { + query = query, + no_esc = no_esc + } +end local M = {} @@ -58,8 +68,7 @@ M.grep = function(opts) local no_esc = false if opts.continue_last_search or opts.repeat_last_search then - no_esc = last_search.no_esc - opts.search = last_search.query + opts.search, no_esc = get_last_search() end -- if user did not provide a search term @@ -78,9 +87,7 @@ M.grep = function(opts) -- save the search query so the use can -- call the same search again - last_search = {} - last_search.no_esc = no_esc or opts.no_esc - last_search.query = opts.search + set_last_search(opts.search, no_esc or opts.no_esc) opts.cmd = get_grep_cmd(opts, opts.search, no_esc) local contents = core.mt_cmd_wrapper(opts) @@ -96,17 +103,14 @@ M.live_grep = function(opts) local no_esc = false if opts.continue_last_search or opts.repeat_last_search then - no_esc = last_search.no_esc - opts.search = last_search.query + opts.search, no_esc = get_last_search() end opts.query = opts.search or '' if opts.search and #opts.search>0 then -- save the search query so the use can -- call the same search again - last_search = {} - last_search.no_esc = true - last_search.query = opts.search + set_last_search(opts.search, true) -- escape unless the user requested not to if not (no_esc or opts.no_esc) then opts.query = utils.rg_escape(opts.search) @@ -118,9 +122,7 @@ M.live_grep = function(opts) opts._reload_command = function(query) if query and not (opts.save_last_search == false) then - last_search = {} - last_search.no_esc = true - last_search.query = query + set_last_search(query, true) end -- can be nill when called as fzf initial command query = query or '' @@ -176,17 +178,14 @@ M.live_grep_mt = function(opts) local no_esc = false if opts.continue_last_search or opts.repeat_last_search then - no_esc = last_search.no_esc - opts.search = last_search.query + opts.search, no_esc = get_last_search() end local query = opts.search or '' if opts.search and #opts.search>0 then -- save the search query so the use can -- call the same search again - last_search = {} - last_search.no_esc = no_esc or opts.no_esc - last_search.query = opts.search + set_last_search(opts.search, no_esc or opts.no_esc) -- escape unless the user requested not to if not (no_esc or opts.no_esc) then query = utils.rg_escape(opts.search) @@ -237,10 +236,8 @@ M.live_grep_mt = function(opts) else opts.fzf_fn = {} if opts.exec_empty_query or (opts.search and #opts.search > 0) then - -- must empty opts.cmd first - opts.cmd = nil - opts.cmd = get_grep_cmd(opts , opts.search, false) - opts.fzf_fn = core.mt_cmd_wrapper(opts) + opts.fzf_fn = initial_command:gsub(placeholder, + vim.fn.shellescape(query)) end opts.fzf_opts['--phony'] = '' opts.fzf_opts['--query'] = vim.fn.shellescape(query)