Skip to content

Commit 091924d

Browse files
committed
fix(grep): explain ignore file failures
1 parent 70c25a0 commit 091924d

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

lua/peekstack/providers/grep.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@ local notify = require("peekstack.util.notify")
44

55
local M = {}
66

7+
---@param text string?
8+
---@return string
9+
local function compact_message(text)
10+
local message = (text or ""):gsub("%s+", " "):gsub("^%s+", ""):gsub("%s+$", "")
11+
if message == "" then
12+
return "unknown error"
13+
end
14+
return message
15+
end
16+
17+
---@param stderr string?
18+
---@return boolean
19+
local function is_ignore_file_error(stderr)
20+
local message = compact_message(stderr):lower()
21+
return message:find(".gitignore", 1, true) ~= nil
22+
or message:find(".ignore", 1, true) ~= nil
23+
or message:find(".rgignore", 1, true) ~= nil
24+
or (message:find("ignore", 1, true) ~= nil and message:find("glob", 1, true) ~= nil)
25+
end
26+
27+
---@param stderr string?
28+
---@return string
29+
local function format_failure_message(stderr)
30+
local message = compact_message(stderr)
31+
if is_ignore_file_error(stderr) then
32+
return "rg failed; check .gitignore/.ignore patterns or encoding: " .. message
33+
end
34+
return "rg failed: " .. message
35+
end
36+
737
---@param line string
838
---@return string?, integer?, integer?, string?
939
local function parse_rg_line(line)
@@ -56,7 +86,7 @@ function M.search(_, cb)
5686
vim.system({ "rg", "--vimgrep", "--max-count=1000", "--", query }, { text = true }, function(result)
5787
vim.schedule(function()
5888
if result.code ~= 0 and result.code ~= 1 then
59-
notify.warn("rg failed: " .. (result.stderr or "unknown error"))
89+
notify.warn(format_failure_message(result.stderr))
6090
cb({})
6191
return
6292
end
@@ -68,5 +98,6 @@ end
6898

6999
---Expose parser for tests.
70100
M._parse_output = parse_rg_output
101+
M._format_failure_message = format_failure_message
71102

72103
return M

tests/grep_provider_spec.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
describe("peekstack.providers.grep", function()
22
local grep = require("peekstack.providers.grep")
3+
local original_notify
4+
local original_system
5+
local original_input
6+
local notifications
7+
8+
before_each(function()
9+
original_notify = vim.notify
10+
original_system = vim.system
11+
original_input = vim.ui.input
12+
notifications = {}
13+
vim.notify = function(msg, level)
14+
table.insert(notifications, { msg = msg, level = level })
15+
end
16+
end)
17+
18+
after_each(function()
19+
vim.notify = original_notify
20+
vim.system = original_system
21+
vim.ui.input = original_input
22+
end)
323

424
it("parses vimgrep output with Unix paths", function()
525
local output = "/tmp/sample.lua:3:5:hello"
@@ -23,4 +43,41 @@ describe("peekstack.providers.grep", function()
2343
assert.equals("hit", items[1].text)
2444
assert.is_true(items[1].uri:find("sample.lua", 1, true) ~= nil)
2545
end)
46+
47+
it("formats ignore-file failures with a targeted hint", function()
48+
local message = grep._format_failure_message("error reading .gitignore: invalid UTF-8")
49+
50+
assert.equals(
51+
"rg failed; check .gitignore/.ignore patterns or encoding: error reading .gitignore: invalid UTF-8",
52+
message
53+
)
54+
end)
55+
56+
it("warns with the ignore hint when rg reports ignore file issues", function()
57+
local items = nil
58+
59+
vim.ui.input = function(_, cb)
60+
cb("sample")
61+
end
62+
vim.system = function(_, _, cb)
63+
cb({
64+
code = 2,
65+
stdout = "",
66+
stderr = "error reading .ignore: invalid UTF-8",
67+
})
68+
end
69+
70+
grep.search({}, function(result)
71+
items = result
72+
end)
73+
74+
vim.wait(100, function()
75+
return items ~= nil
76+
end)
77+
78+
assert.equals(0, #items)
79+
assert.equals(1, #notifications)
80+
assert.equals(vim.log.levels.WARN, notifications[1].level)
81+
assert.is_true(notifications[1].msg:find("check .gitignore/.ignore patterns or encoding", 1, true) ~= nil)
82+
end)
2683
end)

0 commit comments

Comments
 (0)