From 6d8a3502331af43bc356102ae4e72ff24d6b8c2c Mon Sep 17 00:00:00 2001 From: MagicDuck Date: Fri, 27 Sep 2024 21:06:51 -0700 Subject: [PATCH 1/2] adding more defensive logic around aborting search --- lua/grug-far/engine/ripgrep/search.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lua/grug-far/engine/ripgrep/search.lua b/lua/grug-far/engine/ripgrep/search.lua index 4b0058ba..f63298e9 100644 --- a/lua/grug-far/engine/ripgrep/search.lua +++ b/lua/grug-far/engine/ripgrep/search.lua @@ -166,6 +166,18 @@ local function run_search_with_replace(params) local abortSearch = nil local effectiveArgs = nil local processingQueue = nil + local has_finished = false + local on_finish = function(...) + if not has_finished then + has_finished = true + params.on_finish(...) + end + end + local on_fetch_chunk = function(...) + if not has_finished then + params.on_fetch_chunk(...) + end + end local abort = function() if processingQueue then @@ -174,6 +186,7 @@ local function run_search_with_replace(params) if abortSearch then abortSearch() end + on_finish(nil, nil) end local searchArgs = argUtils.stripReplaceArgs(params.args) @@ -185,7 +198,7 @@ local function run_search_with_replace(params) if #json_line > 0 then local success, entry = pcall(vim.json.decode, json_line) if not success then - params.on_fetch_chunk({ + on_fetch_chunk({ lines = vim .iter(vim.split(data, '\n')) :map(utils.getLineWithoutCarriageReturn) @@ -207,12 +220,13 @@ local function run_search_with_replace(params) on_finish = function(status, errorMessage, results) if status == 'success' then if results then - params.on_fetch_chunk(results) + on_fetch_chunk(results) end on_done() + return else abort() - params.on_finish(status, errorMessage) + on_finish(status, errorMessage) end end, }) @@ -231,11 +245,11 @@ local function run_search_with_replace(params) if status == 'success' then processingQueue:on_finish(function() processingQueue:stop() - params.on_finish(status, errorMessage) + on_finish(status, errorMessage) end) else processingQueue:stop() - params.on_finish(status, errorMessage) + on_finish(status, errorMessage) end end, }) From 7954e956bb11c47d9a1d671e17508139960a13b9 Mon Sep 17 00:00:00 2001 From: MagicDuck Date: Fri, 27 Sep 2024 21:12:55 -0700 Subject: [PATCH 2/2] moving history window close before setting inputs --- lua/grug-far.lua | 2 ++ lua/grug-far/actions/historyOpen.lua | 8 +++++--- lua/grug-far/farBuffer.lua | 7 ++++++- lua/grug-far/replacementInterpreter.lua | 4 ++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lua/grug-far.lua b/lua/grug-far.lua index 951e706c..be70d36f 100644 --- a/lua/grug-far.lua +++ b/lua/grug-far.lua @@ -130,6 +130,7 @@ local contextCount = 0 ---@field highlightRegions LangRegions ---@field normalModeSearch boolean ---@field searchAgain boolean +---@field searchDisabled boolean ---@class GrugFarAction ---@field text string @@ -187,6 +188,7 @@ local function createContext(options) highlightResults = {}, normalModeSearch = options.normalModeSearch, searchAgain = false, + searchDisabled = false, }, } end diff --git a/lua/grug-far/actions/historyOpen.lua b/lua/grug-far/actions/historyOpen.lua index 7ef9aa96..e531fd43 100644 --- a/lua/grug-far/actions/historyOpen.lua +++ b/lua/grug-far/actions/historyOpen.lua @@ -58,8 +58,10 @@ local function pickHistoryEntry(historyWin, historyBuf, buf, context) return end + closeHistoryWindow(historyWin) + + context.state.searchDisabled = true context.engine = engine.getEngine(entry.engine) - replacementInterpreter.setReplacementInterpreter(buf, context, entry.replacementInterpreter) inputs.fill(context, buf, { search = entry.search, replacement = entry.replacement, @@ -67,8 +69,8 @@ local function pickHistoryEntry(historyWin, historyBuf, buf, context) flags = entry.flags, paths = entry.paths, }, true) - - closeHistoryWindow(historyWin) + context.state.searchDisabled = false + replacementInterpreter.setReplacementInterpreter(buf, context, entry.replacementInterpreter) end --- set up key maps for history buffer diff --git a/lua/grug-far/farBuffer.lua b/lua/grug-far/farBuffer.lua index 7a76c7ef..f33a0ae3 100644 --- a/lua/grug-far/farBuffer.lua +++ b/lua/grug-far/farBuffer.lua @@ -265,8 +265,13 @@ function M.createBuffer(win, context) local debouncedSearch = utils.debounce(vim.schedule_wrap(search), context.options.debounceMs) local function searchOnChange() - -- only re-issue search when inputs have changed local state = context.state + + if state.searchDisabled then + return + end + + -- only re-issue search when inputs have changed if vim.deep_equal(state.inputs, state.lastInputs) then return end diff --git a/lua/grug-far/replacementInterpreter.lua b/lua/grug-far/replacementInterpreter.lua index 198fdc4a..7918d639 100644 --- a/lua/grug-far/replacementInterpreter.lua +++ b/lua/grug-far/replacementInterpreter.lua @@ -1,4 +1,5 @@ local treesitter = require('grug-far/render/treesitter') +local resultsList = require('grug-far/render/resultsList') local M = {} ---@class GrugFarReplacementInterpreter @@ -30,6 +31,9 @@ function M.setReplacementInterpreter(buf, context, type) return end + -- clear results as it can be slow to clear sytnax highlight otherwise + resultsList.clear(buf, context) + -- clear old syntax highlighting treesitter.clear(buf)