From 9c444599c006c9de3509026a311cc50fcef8048f Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Mon, 10 Oct 2022 19:23:12 +0800 Subject: [PATCH] feat(flygrep): support filter mode --- lua/spacevim/plugin/flygrep.lua | 42 +++++++++++++++++++++++++++++++++ lua/spacevim/plugin/search.lua | 5 ++++ 2 files changed, 47 insertions(+) diff --git a/lua/spacevim/plugin/flygrep.lua b/lua/spacevim/plugin/flygrep.lua index 20bafb775..56ed4021e 100644 --- a/lua/spacevim/plugin/flygrep.lua +++ b/lua/spacevim/plugin/flygrep.lua @@ -55,6 +55,7 @@ local preview_able = false local grep_history = {} local previewd_bufnrs = {} local preview_win_id = -1 +local filter_file = '' @@ -463,6 +464,47 @@ local function toggle_preview() mpt._build_prompt() end +local function get_filter_cmd(expr) + local cmd = {grep_exe} + append(cmd, require('spacevim.plugin.search').getFopt(grep_exe)) + append(cmd, {expr, filter_file}) + return cmd +end + +local function filter_timer(...) + local cmd = get_filter_cmd(vim.fn.join(vim.fn.split(grep_expr), '.*')) + grepid = jobstart(cmd, { + on_stdout = grep_stdout, + on_exit = grep_exit + }) +end + +local function filter(expr) + mpt._build_prompt() + if expr == '' then + return + end + pcall(vim.fn.matchdelete, hi_id) + vim.cmd('hi def link FlyGrepPattern MoreMsg') + hi_id = matchadd('FlyGrepPattern', expr_to_pattern(expr), 2) + grep_expr = expr + grep_timer_id = timer_start(200, filter_timer, {['repeat'] = 1}) +end + +local function start_filter() + mode = 'f' + update_statusline() + mpt._handle_fly = filter + mpt._clear_prompt() + filter_file = vim.fn.tempname() + local context = vim.api.nvim_buf_get_lines(buffer_id, 0, -1, false) + local ok, rst = pcall(vim.fn.writefile, context, filter_file, 'b') + if not ok then + logger.info('Failed to write filter content to temp file') + end + mpt._build_prompt() +end + mpt._function_key = { [Key.t('')] = next_item, [Key.t('')] = next_item, diff --git a/lua/spacevim/plugin/search.lua b/lua/spacevim/plugin/search.lua index 8ed6aa9aa..67bda1c49 100644 --- a/lua/spacevim/plugin/search.lua +++ b/lua/spacevim/plugin/search.lua @@ -129,5 +129,10 @@ function M.default_tool() end +function M.getFopt(exe) + local key = search_tools.namespace[exe] + return search_tools[key]['default_fopts'] +end + return M