Skip to content

Commit 1417701

Browse files
committed
perf(grep): move match parsing to resolve and fix an issue with .* results. Closes #2308
1 parent 80286bf commit 1417701

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

lua/snacks/picker/source/grep.lua

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local M = {}
22

33
local uv = vim.uv or vim.loop
44

5-
local MATCH_SEP = "__snacks__"
5+
local MATCH_SEP = "󰄊󱥳󱥰"
66

77
---@param opts snacks.picker.grep.Config
88
---@param filter snacks.picker.Filter
@@ -131,7 +131,6 @@ function M.grep(opts, ctx)
131131
end
132132
local file = item.text:sub(1, file_sep - 1)
133133
local rest = item.text:sub(file_sep + 1)
134-
item.text = file .. ":" .. rest
135134
---@type string?, string?, string?
136135
local line, col, text = rest:match("^(%d+):(%d+):(.*)$")
137136
if not (line and col and text) then
@@ -140,38 +139,36 @@ function M.grep(opts, ctx)
140139
end
141140
return false
142141
end
142+
item.text = file .. ":" .. rest:gsub(MATCH_SEP, "")
143143

144144
-- indices of matches
145-
local positions = {} ---@type number[]
146145
local from = tonumber(col)
147146
item.pos = { tonumber(line), from - 1 }
148147

149-
local offset = 0
150-
local in_match = false
151-
while from < #text do
152-
local idx = text:find(MATCH_SEP, from, true)
153-
if not idx then
154-
break
155-
end
156-
157-
if in_match then
158-
for i = from, idx - 1 do
159-
positions[#positions + 1] = i - offset
148+
item.resolve = function()
149+
local positions = {} ---@type number[]
150+
local offset = 0
151+
local in_match = false
152+
while from < #text do
153+
local idx = text:find(MATCH_SEP, from, true)
154+
if not idx then
155+
break
156+
end
157+
if in_match then
158+
for i = from, idx - 1 do
159+
positions[#positions + 1] = i - offset
160+
end
161+
item.end_pos = item.end_pos or { item.pos[1], idx - offset - 1 }
160162
end
161-
item.end_pos = item.end_pos or { item.pos[1], idx - offset - 1 }
163+
in_match = not in_match
164+
offset = offset + #MATCH_SEP
165+
from = idx + #MATCH_SEP
162166
end
163-
in_match = not in_match
164-
165-
offset = offset + #MATCH_SEP
166-
from = idx + #MATCH_SEP
167+
item.positions = #positions > 0 and positions or nil
168+
item.line = text:gsub(MATCH_SEP, "")
167169
end
168170

169171
item.file = file
170-
if #positions > 0 then
171-
text = text:gsub(MATCH_SEP, "")
172-
item.positions = positions
173-
end
174-
item.line = text
175172
end,
176173
},
177174
}, ctx)

0 commit comments

Comments
 (0)