Skip to content

Commit

Permalink
simplifying errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 6, 2024
1 parent 42d3fcd commit 9a4ca40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
30 changes: 11 additions & 19 deletions lua/grug-far/render/resultsList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local function asyncFetchResultList(params)
local on_start = params.on_start
local on_fetch_chunk = vim.schedule_wrap(params.on_fetch_chunk)
local on_finish = vim.schedule_wrap(params.on_finish)
local on_error = vim.schedule_wrap(params.on_error)
local inputs = params.inputs
local context = params.context

Expand All @@ -20,11 +19,10 @@ local function asyncFetchResultList(params)
inputs = inputs,
options = context.options,
on_fetch_chunk = on_fetch_chunk,
on_finish = function(isSuccess)
on_finish = function(status, errorMessage)
context.state.abortFetch = nil
on_finish(isSuccess)
on_finish(status, errorMessage)
end,
on_error = on_error
})
end

Expand All @@ -47,16 +45,14 @@ local function bufAppendResultsChunk(buf, context, data)
end

local function bufAppendErrorChunk(buf, context, error)
local lastErrorLine = context.state.lastErrorLine
local startLine = context.state.headerRow + 1

local err_lines = vim.split(error, '\n')
vim.api.nvim_buf_set_lines(buf, lastErrorLine, lastErrorLine, false, err_lines)
vim.api.nvim_buf_set_lines(buf, startLine, startLine, false, err_lines)

for i = lastErrorLine, lastErrorLine + #err_lines do
for i = startLine, startLine + #err_lines do
vim.api.nvim_buf_add_highlight(buf, context.namespace, 'DiagnosticError', i, 0, -1)
end

context.state.lastErrorLine = lastErrorLine + #err_lines
end

local function renderResultsList(buf, context, inputs)
Expand All @@ -74,7 +70,6 @@ local function renderResultsList(buf, context, inputs)
-- remove all lines after heading and add one blank line
local headerRow = state.headerRow
vim.api.nvim_buf_set_lines(buf, headerRow, -1, false, { "" })
state.lastErrorLine = headerRow + 1
end,
on_fetch_chunk = function(data)
state.status = 'progress'
Expand All @@ -87,17 +82,14 @@ local function renderResultsList(buf, context, inputs)

bufAppendResultsChunk(buf, context, data)
end,
on_error = function(error)
state.status = 'error'
state.progressCount = nil
state.stats = nil
renderResultsHeader(buf, context)

bufAppendErrorChunk(buf, context, error)
end,
on_finish = function(status)
on_finish = function(status, errorMessage)
state.status = status
state.progressCount = nil
if status == 'error' then
state.stats = nil
bufAppendErrorChunk(buf, context, errorMessage)
end

renderResultsHeader(buf, context)
end,
context = context
Expand Down
12 changes: 6 additions & 6 deletions lua/grug-far/rg/fetchResults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ local uv = vim.loop
local function fetchResults(params)
local on_fetch_chunk = params.on_fetch_chunk
local on_finish = params.on_finish
local on_error = params.on_error
local inputs = params.inputs
local options = params.options
local isAborted = false
local errorMessage = ''

local args = getArgs(inputs, options)
if not args then
Expand All @@ -28,8 +28,8 @@ local function fetchResults(params)
stdout:close()
stderr:close()
handle:close()
local isSuccess = code == 0
on_finish(isSuccess and 'success' or 'error');
local isSuccess = code == 0 and #errorMessage == 0
on_finish(isSuccess and 'success' or 'error', errorMessage);
end)

local on_abort = function()
Expand All @@ -46,7 +46,7 @@ local function fetchResults(params)
end

if err then
on_error('rg fetcher: error reading from rg stdout!')
errorMessage = errorMessage .. '\nrg fetcher: error reading from rg stdout!'
return
end

Expand All @@ -61,12 +61,12 @@ local function fetchResults(params)
end

if err then
on_error('rg fetcher: error reading from rg stderr!')
errorMessage = errorMessage .. '\nrg fetcher: error reading from rg stderr!'
return
end

if data then
on_error(data)
errorMessage = errorMessage .. data
end
end)

Expand Down

0 comments on commit 9a4ca40

Please sign in to comment.