diff --git a/lua/grug-far/actions/search.lua b/lua/grug-far/actions/search.lua index 7fc449f1..19ed1974 100644 --- a/lua/grug-far/actions/search.lua +++ b/lua/grug-far/actions/search.lua @@ -2,6 +2,8 @@ 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 @@ -20,6 +22,7 @@ local function search(params) state.actionMessage = nil renderResultsHeader(buf, context) resultsList.clear(buf, context) + P('starting search') end) state.abortSearch = fetchResults({ @@ -37,12 +40,14 @@ local function search(params) resultsList.appendResultsChunk(buf, context, data) end), on_finish = vim.schedule_wrap(function(status, errorMessage) - state.abortSearch = nil - + 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 + state.stats = nil + state.actionMessage = nil end renderResultsHeader(buf, context) diff --git a/lua/grug-far/rg/fetchWithRg.lua b/lua/grug-far/rg/fetchWithRg.lua index 4290ffe4..f2f3632d 100644 --- a/lua/grug-far/rg/fetchWithRg.lua +++ b/lua/grug-far/rg/fetchWithRg.lua @@ -1,15 +1,21 @@ local utils = require('grug-far/utils') local uv = vim.loop +local function closeHandle(handle) + if handle and not handle:is_closing() then + handle:close() + end +end + local function fetchWithRg(params) local on_fetch_chunk = params.on_fetch_chunk local on_finish = params.on_finish local args = params.args - local isAborted = false + local isFinished = false local errorMessage = '' if not args then - on_finish(nil) + on_finish(nil, nil) return end @@ -23,43 +29,43 @@ local function fetchWithRg(params) args = args }, function( code - --signal + -- signal ) - if not stdout:is_closing() then - stdout:close() - end - if not stderr:is_closing() then - stderr:close() - end - if handle and not handle:is_closing() then - handle:close() + if isFinished then + return end + isFinished = true + closeHandle(stdout) + closeHandle(stderr) + closeHandle(handle) + if code > 0 and #errorMessage == 0 then errorMessage = 'no matches' end local isSuccess = code == 0 and #errorMessage == 0 + on_finish(isSuccess and 'success' or 'error', errorMessage); end) - -- TODO (sbadragan): problem here in that we don't seem to be immediately aborting searches local on_abort = function() - isAborted = true - if not stdout:is_closing() then - stdout:close() - end - if not stderr:is_closing() then - stderr:close() - end - if handle and not handle:is_closing() then - handle:close() + if isFinished then + return end + + isFinished = 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 isAborted then + if isFinished then return end @@ -88,7 +94,7 @@ local function fetchWithRg(params) end) uv.read_start(stderr, function(err, data) - if isAborted then + if isFinished then return end