Skip to content

Commit 2593a45

Browse files
committed
refactor: ANSI colorize continuous chunks at once
- This improves the speed. - If not done, for large diffs we can get the error `PANIC: unprotected error in call to Lua API (EMFILE: too many open files)`.
1 parent f9b71f1 commit 2593a45

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

lua/neogit/lib/buffer.lua

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,38 @@ function Buffer:set_extmarks(extmarks)
139139
end
140140

141141
function Buffer:set_line_highlights(highlights)
142-
local line_ansi_colorized = {}
142+
local line_ansi_colorized_map = {}
143143

144144
for _, hl in ipairs(highlights) do
145145
local line_nr, hl_group = unpack(hl)
146146
if hl_group == "NeogitDiffContext" then
147-
if not line_ansi_colorized[line_nr] then
148-
local text = self:get_line(line_nr + 1)
149-
vim.g.baleia.buf_set_lines(self.handle, line_nr, line_nr + 1, false, text)
150-
line_ansi_colorized[line_nr] = true
151-
end
147+
line_ansi_colorized_map[line_nr] = true
148+
else
149+
self:add_line_highlight(unpack(hl))
152150
end
151+
end
153152

154-
if not line_ansi_colorized[line_nr] then
155-
self:add_line_highlight(unpack(hl))
153+
local line_ansi_colorized = {}
154+
for k in pairs(line_ansi_colorized_map) do
155+
table.insert(line_ansi_colorized, k)
156+
end
157+
table.sort(line_ansi_colorized)
158+
159+
local start_line_nr, prev_line_nr
160+
for _, line_nr in ipairs(line_ansi_colorized) do
161+
if start_line_nr == nil then
162+
start_line_nr = line_nr
156163
end
164+
if prev_line_nr ~= nil and line_nr ~= prev_line_nr + 1 then
165+
local text = self:get_lines(start_line_nr, prev_line_nr + 1, false)
166+
vim.g.baleia.buf_set_lines(self.handle, start_line_nr, prev_line_nr + 1, false, text)
167+
start_line_nr = line_nr
168+
end
169+
prev_line_nr = line_nr
170+
end
171+
if start_line_nr ~= nil then
172+
local text = self:get_lines(start_line_nr, prev_line_nr + 1, false)
173+
vim.g.baleia.buf_set_lines(self.handle, start_line_nr, prev_line_nr + 1, false, text)
157174
end
158175
end
159176

0 commit comments

Comments
 (0)