Skip to content

Commit

Permalink
fixing buf update on search when finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan authored and MagicDuck committed May 18, 2024
1 parent 0578379 commit b702d58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lua/grug-far/actions/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ local fetchResults = require('grug-far/rg/fetchResults')
local renderResultsHeader = require('grug-far/render/resultsHeader')
local resultsList = require('grug-far/render/resultsList')

-- TODO (sbadragan): problem when you search if you go: -> ge -> g , it gets stuck
-- TODO (sbadragan): show progress when searching, helps with big searches
local function search(params)
local buf = params.buf
local context = params.context
local state = context.state
local isFinished;

if state.abortSearch then
state.abortSearch();
Expand All @@ -22,13 +21,15 @@ local function search(params)
state.actionMessage = nil
renderResultsHeader(buf, context)
resultsList.clear(buf, context)
P('starting search')
end)

state.abortSearch = fetchResults({
state.abortSearch, isFinished = fetchResults({
inputs = state.inputs,
options = context.options,
on_fetch_chunk = vim.schedule_wrap(function(data)
if isFinished() then
return
end
state.status = 'progress'
state.progressCount = state.progressCount + 1
state.stats = {
Expand All @@ -40,12 +41,12 @@ local function search(params)
resultsList.appendResultsChunk(buf, context, data)
end),
on_finish = vim.schedule_wrap(function(status, errorMessage)
P('finish search with status ' .. (status or 'nil'))
state.status = status
if status == 'error' then
state.stats = nil
resultsList.setError(buf, context, errorMessage)
elseif status == nil then
-- was aborted
state.stats = nil
state.actionMessage = nil
end
Expand Down
3 changes: 2 additions & 1 deletion lua/grug-far/opts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ M.defaultOptions = {
search = "ex: foo foo([a-z0-9]*) fun\\(",
replacement = "ex: bar ${1}_foo $$MY_ENV_VAR ",
filesFilter = "ex: *.lua *.{css,js} **/docs/*.md",
flags = "ex: --help --hidden (-.) <relative-file-path> --ignore-case (-i) --multiline (-U) --fixed-strings (-F)",
flags =
"ex: --help --ignore-case (-i) <relative-file-path> --replace= (empty replace) --multiline (-U)",
},

-- strings to auto-fill in each input area at start
Expand Down
24 changes: 14 additions & 10 deletions lua/grug-far/rg/fetchWithRg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ local function fetchWithRg(params)
local on_fetch_chunk = params.on_fetch_chunk
local on_finish = params.on_finish
local args = params.args
local isFinished = false
local finished = false
local errorMessage = ''

local function isFinished()
return finished
end

if not args then
finished = true
on_finish(nil, nil)
return
return nil, isFinished
end

local stdout = uv.new_pipe()
Expand All @@ -31,11 +36,11 @@ local function fetchWithRg(params)
code
-- signal
)
if isFinished then
if finished then
return
end

isFinished = true
finished = true
closeHandle(stdout)
closeHandle(stderr)
closeHandle(handle)
Expand All @@ -49,23 +54,22 @@ local function fetchWithRg(params)
end)

local on_abort = function()
if isFinished then
if finished then
return
end

isFinished = true
finished = true
closeHandle(stdout)
closeHandle(stderr)
closeHandle(handle)
P('terminating search')
uv.kill(pid, uv.constants.SIGTERM)

on_finish(nil, nil);
end

local lastLine = ''
uv.read_start(stdout, function(err, data)
if isFinished then
if finished then
return
end

Expand Down Expand Up @@ -94,7 +98,7 @@ local function fetchWithRg(params)
end)

uv.read_start(stderr, function(err, data)
if isFinished then
if finished then
return
end

Expand All @@ -108,7 +112,7 @@ local function fetchWithRg(params)
end
end)

return on_abort
return on_abort, isFinished
end

return fetchWithRg

0 comments on commit b702d58

Please sign in to comment.