From 974941811c22694ed63157a962b6fce7a9353075 Mon Sep 17 00:00:00 2001 From: Stephan Badragan Date: Tue, 7 May 2024 12:54:54 -0700 Subject: [PATCH] fixing error when no matches --- lua/grug-far/actions/replace.lua | 4 +++- lua/grug-far/actions/search.lua | 2 +- lua/grug-far/render/resultsList.lua | 6 ++++-- lua/grug-far/rg/fetchWithRg.lua | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lua/grug-far/actions/replace.lua b/lua/grug-far/actions/replace.lua index 7e82b1fb..9989bc2e 100644 --- a/lua/grug-far/actions/replace.lua +++ b/lua/grug-far/actions/replace.lua @@ -121,10 +121,12 @@ local function replace(params) end) local reportError = function(errorMessage) + vim.api.nvim_buf_set_option(buf, 'modifiable', true) + state.status = 'error' state.progressCount = nil state.actionMessage = getActionMessage(errorMessage) - resultsList.appendError(buf, context, errorMessage) + resultsList.setError(buf, context, errorMessage) renderResultsHeader(buf, context) end diff --git a/lua/grug-far/actions/search.lua b/lua/grug-far/actions/search.lua index 53181076..78ffb083 100644 --- a/lua/grug-far/actions/search.lua +++ b/lua/grug-far/actions/search.lua @@ -43,7 +43,7 @@ local function search(params) state.progressCount = nil if status == 'error' then state.stats = nil - resultsList.appendError(buf, context, errorMessage) + resultsList.setError(buf, context, errorMessage) end renderResultsHeader(buf, context) diff --git a/lua/grug-far/render/resultsList.lua b/lua/grug-far/render/resultsList.lua index cd005f4c..2af20dbc 100644 --- a/lua/grug-far/render/resultsList.lua +++ b/lua/grug-far/render/resultsList.lua @@ -18,10 +18,12 @@ function M.appendResultsChunk(buf, context, data) end end -function M.appendError(buf, context, error) +function M.setError(buf, context, error) + M.clear(buf, context) + local startLine = context.state.headerRow + 1 - local err_lines = vim.split(error, '\n') + local err_lines = vim.split((error and #error > 0) and error or 'Unexpected error!', '\n') vim.api.nvim_buf_set_lines(buf, startLine, startLine, false, err_lines) for i = startLine, startLine + #err_lines do diff --git a/lua/grug-far/rg/fetchWithRg.lua b/lua/grug-far/rg/fetchWithRg.lua index 1c5e0fd2..6a68b780 100644 --- a/lua/grug-far/rg/fetchWithRg.lua +++ b/lua/grug-far/rg/fetchWithRg.lua @@ -24,6 +24,10 @@ local function fetchWithRg(params) stdout:close() stderr:close() handle:close() + + 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)