From ca4dc395d87719b3837c64e4c20fb464c6b43c80 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sun, 6 Mar 2022 12:15:51 -0800 Subject: [PATCH] removed deprecated providers, 'resume' to accept options --- lua/fzf-lua/core.lua | 15 +-- lua/fzf-lua/init.lua | 10 +- lua/fzf-lua/providers/grep.lua | 119 +++++++++++++---------- lua/fzf-lua/providers/tags.lua | 166 +-------------------------------- 4 files changed, 84 insertions(+), 226 deletions(-) diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 64e9b84..0d8f118 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -10,12 +10,12 @@ local make_entry = require "fzf-lua.make_entry" local M = {} -M.fzf_resume = function() +M.fzf_resume = function(opts) if not config.__resume_data or not config.__resume_data.opts then utils.info("No resume data available, is 'global_resume' enabled?") return end - local opts = config.__resume_data.opts + opts = vim.tbl_deep_extend("force", config.__resume_data.opts, opts or {}) local last_query = config.__resume_data.last_query if last_query and #last_query>0 then last_query = vim.fn.shellescape(last_query) @@ -26,11 +26,13 @@ M.fzf_resume = function() last_query = false end opts.__resume = true - opts.fzf_opts['--query'] = last_query if opts.__FNCREF__ then -- HACK for 'live_grep' and 'lsp_live_workspace_symbols' - opts.__FNCREF__({ continue_last_search = true }) + opts.cmd = nil + opts.continue_last_search = true + opts.__FNCREF__(opts) else + opts.fzf_opts['--query'] = last_query M.fzf_wrap(opts, config.__resume_data.contents)() end end @@ -355,12 +357,11 @@ M.mt_cmd_wrapper = function(opts) return '{'..str..'}' end - if not opts.force_multiprocess and - not opts.requires_processing and + if not opts.requires_processing and not opts.git_icons and not opts.file_icons then -- command does not require any processing return opts.cmd - elseif opts.multiprocess or opts.force_multiprocess then + elseif opts.multiprocess then local fn_preprocess = opts._fn_preprocess_str or [[return require("make_entry").preprocess]] local fn_transform = opts._fn_transform_str or [[return require("make_entry").file]] -- replace all below 'fn.shellescape' with our version diff --git a/lua/fzf-lua/init.lua b/lua/fzf-lua/init.lua index c20c0a7..4d3f388 100644 --- a/lua/fzf-lua/init.lua +++ b/lua/fzf-lua/init.lua @@ -80,12 +80,10 @@ M.resume = require'fzf-lua.core'.fzf_resume M.files = require'fzf-lua.providers.files'.files M.args = require'fzf-lua.providers.files'.args M.grep = require'fzf-lua.providers.grep'.grep -M.live_grep = require'fzf-lua.providers.grep'.live_grep_mt -M.live_grep_old = require'fzf-lua.providers.grep'.live_grep_st +M.live_grep = require'fzf-lua.providers.grep'.live_grep M.live_grep_native = require'fzf-lua.providers.grep'.live_grep_native M.live_grep_resume = require'fzf-lua.providers.grep'.live_grep_resume -M.live_grep_glob = require'fzf-lua.providers.grep'.live_grep_glob_mt -M.live_grep_glob_old = require'fzf-lua.providers.grep'.live_grep_glob +M.live_grep_glob = require'fzf-lua.providers.grep'.live_grep_glob M.grep_last = require'fzf-lua.providers.grep'.grep_last M.grep_cword = require'fzf-lua.providers.grep'.grep_cword M.grep_cWORD = require'fzf-lua.providers.grep'.grep_cWORD @@ -113,11 +111,9 @@ M.tags = require'fzf-lua.providers.tags'.tags M.btags = require'fzf-lua.providers.tags'.btags M.tags_grep = require'fzf-lua.providers.tags'.grep M.tags_grep_cword = require'fzf-lua.providers.tags'.grep_cword -M.tags_grep_CWORD = require'fzf-lua.providers.tags'.grep_cWORD +M.tags_grep_cWORD = require'fzf-lua.providers.tags'.grep_cWORD M.tags_grep_visual = require'fzf-lua.providers.tags'.grep_visual M.tags_live_grep = require'fzf-lua.providers.tags'.live_grep -M.tags_old = require'fzf-lua.providers.tags'.tags_old -M.btags_old = require'fzf-lua.providers.tags'.btags_old M.jumps = require'fzf-lua.providers.nvim'.jumps M.changes = require'fzf-lua.providers.nvim'.changes M.tagstack = require'fzf-lua.providers.nvim'.tagstack diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index 0db3fa6..47a883b 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -115,6 +115,8 @@ M.live_grep_st = function(opts) opts = config.normalize_opts(opts, config.globals.grep) if not opts then return end + assert(not opts.multiprocess) + local no_esc = false if opts.continue_last_search or opts.repeat_last_search then opts.search, no_esc = get_last_search() @@ -163,41 +165,15 @@ M.live_grep_st = function(opts) core.fzf_files(opts) end -M.live_grep_native = function(opts) - - -- backward compatibility, by setting git|files icons to false - -- we forces mt_cmd_wrapper to pipe the command as is so fzf - -- runs the command directly in the 'change:reload' event - opts = opts or {} - opts.git_icons = false - opts.file_icons = false - opts.__FNCREF__ = utils.__FNCREF__() - return M.live_grep_mt(opts) -end - -M.live_grep_glob_mt = function(opts) - - if vim.fn.executable("rg") ~= 1 then - utils.warn("'--glob|iglob' flags requires 'rg' (https://github.com/BurntSushi/ripgrep)") - return - end - - -- 'rg_glob = true' enables the glob processsing in - -- 'make_entry.preprocess', only supported with multiprocess - opts = opts or {} - opts.rg_glob = true - opts.force_multiprocess = true - opts.__FNCREF__ = utils.__FNCREF__() - return M.live_grep_mt(opts) -end -- multi threaded (multi-process actually) version M.live_grep_mt = function(opts) opts = config.normalize_opts(opts, config.globals.grep) - if not opts then return end + assert(opts.multiprocess) + local no_esc = false if opts.continue_last_search or opts.repeat_last_search then opts.search, no_esc = get_last_search() @@ -217,13 +193,6 @@ M.live_grep_mt = function(opts) -- search query in header line opts = core.set_header(opts, 2) - -- since the introduction of 'libuv.spawn_stdio' with '--headless' - -- we can now run the command externally with minimal overhead - if not opts.multiprocess and not opts.force_multiprocess then - opts.git_icons = false - opts.file_icons = false - end - -- signal to preprocess we are looking to replace {argvz} opts.argv_expr = true @@ -287,18 +256,7 @@ M.live_grep_mt = function(opts) opts.search = nil end -M.live_grep_resume = function(opts) - if not opts then opts = {} end - if not opts.search then - opts.continue_last_search = - (opts.continue_last_search == nil and - opts.repeat_last_search == nil and true) or - (opts.continue_last_search or opts.repeat_last_search) - end - return M.live_grep_mt(opts) -end - -M.live_grep_glob = function(opts) +M.live_grep_glob_st = function(opts) if not opts then opts = {} end if vim.fn.executable("rg") ~= 1 then utils.warn("'--glob|iglob' flags requires 'rg' (https://github.com/BurntSushi/ripgrep)") @@ -337,10 +295,73 @@ M.live_grep_glob = function(opts) :format(o.rg_opts, glob_arg, search_query, search_path) return cmd end - opts.__FNCREF__ = utils.__FNCREF__() return M.live_grep_st(opts) end +M.live_grep_glob_mt = function(opts) + + if vim.fn.executable("rg") ~= 1 then + utils.warn("'--glob|iglob' flags requires 'rg' (https://github.com/BurntSushi/ripgrep)") + return + end + + -- 'rg_glob = true' enables the glob processsing in + -- 'make_entry.preprocess', only supported with multiprocess + opts = opts or {} + opts.rg_glob = true + opts.requires_processing = true + return M.live_grep_mt(opts) +end + +M.live_grep_native = function(opts) + + -- backward compatibility, by setting git|files icons to false + -- we forces mt_cmd_wrapper to pipe the command as is so fzf + -- runs the command directly in the 'change:reload' event + opts = opts or {} + opts.git_icons = false + opts.file_icons = false + opts.__FNCREF__ = utils.__FNCREF__() + return M.live_grep_mt(opts) +end + +M.live_grep = function(opts) + opts = config.normalize_opts(opts, config.globals.grep) + if not opts then return end + + opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__() + + if opts.multiprocess then + return M.live_grep_mt(opts) + else + return M.live_grep_st(opts) + end +end + +M.live_grep_glob = function(opts) + opts = config.normalize_opts(opts, config.globals.grep) + if not opts then return end + + opts.__FNCREF__ = opts.__FNCREF__ or utils.__FNCREF__() + + if opts.multiprocess then + return M.live_grep_glob_mt(opts) + else + return M.live_grep_glob_st(opts) + end +end + +M.live_grep_resume = function(opts) + if not opts then opts = {} end + if not opts.search then + opts.continue_last_search = + (opts.continue_last_search == nil and + opts.repeat_last_search == nil and true) or + (opts.continue_last_search or opts.repeat_last_search) + end + return M.live_grep(opts) +end + M.grep_last = function(opts) if not opts then opts = {} end opts.continue_last_search = true @@ -389,7 +410,7 @@ M.grep_curbuf = function(opts) if #opts.filename > 0 and vim.loop.fs_stat(opts.filename) then opts.filename = path.relative(opts.filename, vim.loop.cwd()) if opts.lgrep then - return M.live_grep_mt(opts) + return M.live_grep(opts) else opts.search = '' return M.grep(opts) diff --git a/lua/fzf-lua/providers/tags.lua b/lua/fzf-lua/providers/tags.lua index e30a101..cc650da 100644 --- a/lua/fzf-lua/providers/tags.lua +++ b/lua/fzf-lua/providers/tags.lua @@ -6,164 +6,6 @@ local make_entry = require "fzf-lua.make_entry" local M = {} -local grep_cmd = nil - -local get_grep_cmd = function() - if vim.fn.executable("rg") == 1 then - return {"rg", "--line-number"} - end - return {"grep", "-n", "-P"} -end - -local fzf_tags = function(opts) - opts.ctags_file = opts.ctags_file and vim.fn.expand(opts.ctags_file) or "tags" - - if not vim.loop.fs_open(opts.ctags_file, "r", 438) then - utils.info("Tags file does not exists. Create one with ctags -R") - return - end - - -- get these here before we open fzf - local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) - local current_file = vim.api.nvim_buf_get_name(0) - - local contents = function (cb) - - --[[ local read_line = function(file) - local line - local handle = io.open(file, "r") - if handle then - line = handle and handle:read("*line") - handle:close() - end - return line - end - - local _file2ff = {} - local string_byte = string.byte - local fileformat = function(file) - local ff = _file2ff[file] - if ff then return ff end - local line = read_line(file) - -- dos ends with \13 - -- mac ends with \13\13 - -- unix ends with \62 - -- char(13) == ^M - if line and string_byte(line, #line) == 13 then - if #line>1 and string_byte(line, #line-1) == 13 then - ff = 'mac' - else - ff = 'dos' - end - else - ff = 'unix' - end - _file2ff[file] = ff - return ff - end --]] - - local getlinenumber = function(t) - if not grep_cmd then grep_cmd = get_grep_cmd() end - local line = 1 - local filepath = path.join({cwd, t.file}) - local pattern = utils.rg_escape(t.text:match("/^?(.*)/")) - if not pattern or not filepath then return line end - -- ctags uses '$' at the end of short patterns - -- 'rg|grep' does not match these properly when - -- 'fileformat' isn't set to 'unix', when set to - -- 'dos' we need to prepend '$' with '\r$' with 'rg' - -- it is simpler to just ignore it compleley. - --[[ local ff = fileformat(filepath) - if ff == 'dos' then - pattern = pattern:gsub("\\%$$", "\\r%$") - else - pattern = pattern:gsub("\\%$$", "%$") - end --]] - -- equivalent pattern to `rg --crlf` - -- see discussion in #219 - pattern = pattern:gsub("\\%$$", "\\r??%$") - local cmd = utils.tbl_deep_clone(grep_cmd) - table.insert(cmd, pattern) - table.insert(cmd, filepath) - local out = utils.io_system(cmd) - if not utils.shell_error() then - line = out:match("[^:]+") - end - -- if line == 1 then print(cmd) end - return line - end - - local add_tag = function(t, fzf_cb, co, no_line) - local line = not no_line and getlinenumber(t) - local tag = string.format("%s%s: %s %s", - core.make_entry_file(opts, t.file), - not line and "" or ":"..utils.ansi_codes.green(tostring(line)), - utils.ansi_codes.magenta(t.name), - utils.ansi_codes.green(t.text)) - fzf_cb(tag, function() - coroutine.resume(co) - end) - end - - coroutine.wrap(function () - local co = coroutine.running() - local lines = vim.split(utils.read_file(opts.ctags_file), '\n', true) - for _, line in ipairs(lines) do - if not line:match'^!_TAG_' then - local name, file, text = line:match("^(.*)\t(.*)\t(/.*/)") - if name and file and text then - if not opts.current_buffer_only or - current_file == path.join({cwd, file}) then - -- without vim.schedule `add_tag` would crash - -- at any `vim.fn...` call - vim.schedule(function() - add_tag({ - name = name, - file = file, - text = text, - }, cb, co, - -- unless we're using native previewer - -- do not need to extract the line number - not opts.previewer - or opts.previewer == 'builtin' - or type(opts.previewer) == 'table') - end) - -- pause here until we call coroutine.resume() - coroutine.yield() - end - end - end - end - -- done, we can't call utils.delayed_cb here - -- because sleep() messes up the coroutine - -- cb(nil, function() coroutine.resume(co) end) - utils.delayed_cb(cb, function() coroutine.resume(co) end) - coroutine.yield() - end)() - end - - -- signal actions this is a ctag - opts._ctag = true - opts = core.set_header(opts, 2) - opts = core.set_fzf_field_index(opts) - return core.fzf_files(opts, contents) -end - -M.tags_old = function(opts) - opts = config.normalize_opts(opts, config.globals.tags) - if not opts then return end - return fzf_tags(opts) -end - -M.btags_old = function(opts) - opts = config.normalize_opts(opts, config.globals.btags) - if not opts then return end - opts.fzf_opts = vim.tbl_extend("keep", - opts.fzf_opts or {}, config.globals.blines.fzf_opts) - opts.current_buffer_only = true - return fzf_tags(opts) -end - local function get_tags_cmd(opts) local query = nil local cmd = "grep" @@ -171,10 +13,8 @@ local function get_tags_cmd(opts) cmd = "rg" end if opts.search and #opts.search>0 then - if not opts.no_esc then - opts.search = utils.rg_escape(opts.search) - end - query = vim.fn.shellescape(opts.search) + query = vim.fn.shellescape(opts.no_esc and opts.search or + utils.rg_escape(opts.search)) elseif opts._curr_file and #opts._curr_file>0 then query = vim.fn.shellescape(opts._curr_file) else @@ -207,7 +47,7 @@ local function tags(opts) if opts.lgrep then -- live_grep requested by caller ('tags_live_grep') - opts.prompt = '*' .. opts.prompt + opts.prompt = opts.prompt:match("^*") and opts.prompt or '*' .. opts.prompt opts.filename = opts._ctags_file if opts.multiprocess then return require'fzf-lua.providers.grep'.live_grep_mt(opts)