Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding more defensive logic around aborting search #279

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/grug-far.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ local contextCount = 0
---@field highlightRegions LangRegions
---@field normalModeSearch boolean
---@field searchAgain boolean
---@field searchDisabled boolean

---@class GrugFarAction
---@field text string
Expand Down Expand Up @@ -187,6 +188,7 @@ local function createContext(options)
highlightResults = {},
normalModeSearch = options.normalModeSearch,
searchAgain = false,
searchDisabled = false,
},
}
end
Expand Down
8 changes: 5 additions & 3 deletions lua/grug-far/actions/historyOpen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ 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,
filesFilter = entry.filesFilter,
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
Expand Down
24 changes: 19 additions & 5 deletions lua/grug-far/engine/ripgrep/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand Down
7 changes: 6 additions & 1 deletion lua/grug-far/farBuffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lua/grug-far/replacementInterpreter.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local treesitter = require('grug-far/render/treesitter')
local resultsList = require('grug-far/render/resultsList')
local M = {}

---@class GrugFarReplacementInterpreter
Expand Down Expand Up @@ -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)

Expand Down
Loading