Skip to content

Commit 5bfc499

Browse files
committed
refactor: move Buffer:set_line_highlights() ANSI highlights to Buffer:set_ansi_highlights(), to avoid finding hunk line numbers manually with the hard-coded NeogitDiffContext
- Add `ansi_hl` to `ComponentOptions`, to indicate whether the component needs ANSI highlighting. - Add `ansi_highlight` to `RendererBuffer`, to pass the line numbers.
1 parent 6036128 commit 5bfc499

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

lua/neogit/buffers/common.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ M.Hunk = Component.new(function(props)
8383
return col.tag("Hunk")({
8484
text.line_hl("NeogitHunkHeader")(props.header),
8585
col.tag("HunkContent")(map(props.content, HunkLine)),
86-
}, { foldable = true, folded = props.folded or false, context = true, hunk = props.hunk })
86+
}, {
87+
ansi_hl = config.values.log_pager ~= nil,
88+
foldable = true,
89+
folded = props.folded or false,
90+
context = true,
91+
hunk = props.hunk,
92+
})
8793
end)
8894

8995
M.List = Component.new(function(props)

lua/neogit/lib/buffer.lua

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

141141
function Buffer:set_line_highlights(highlights)
142-
local line_ansi_colorized_map = {}
143-
144142
for _, hl in ipairs(highlights) do
145-
local line_nr, hl_group = unpack(hl)
146-
if hl_group == "NeogitDiffContext" then
147-
line_ansi_colorized_map[line_nr] = true
148-
else
149-
self:add_line_highlight(unpack(hl))
150-
end
151-
end
152-
153-
local line_ansi_colorized = {}
154-
for k in pairs(line_ansi_colorized_map) do
155-
table.insert(line_ansi_colorized, k)
143+
self:add_line_highlight(unpack(hl))
156144
end
157-
table.sort(line_ansi_colorized)
145+
end
158146

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
163-
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)
147+
function Buffer:set_ansi_highlights(highlights)
148+
for _, hl in ipairs(highlights) do
149+
local first_line, last_line = unpack(hl)
150+
local text = self:get_lines(first_line, last_line, false)
151+
vim.g.baleia.buf_set_lines(self.handle, first_line, last_line, false, text)
174152
end
175153
end
176154

lua/neogit/lib/ui/component.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local default_component_options = {
1313

1414
---@class ComponentOptions
1515
---@field line_hl string
16+
---@field ansi_hl boolean
1617
---@field highlight string
1718
---@field align_right integer|nil
1819
---@field padding_left integer

lua/neogit/lib/ui/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ function Ui:update()
686686
self.buf:set_highlights(renderer.buffer.highlight)
687687
self.buf:set_extmarks(renderer.buffer.extmark)
688688
self.buf:set_line_highlights(renderer.buffer.line_highlight)
689+
self.buf:set_ansi_highlights(renderer.buffer.ansi_highlight)
689690
self.buf:set_folds(renderer.buffer.fold)
690691

691692
self.statuscolumn = {}

lua/neogit/lib/ui/renderer.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ end
6565
---@field line string[]
6666
---@field highlight table[]
6767
---@field line_highlight table[]
68+
---@field ansi_highlight table[]
6869
---@field extmark table[]
6970
---@field fold table[]
7071

@@ -93,6 +94,7 @@ function Renderer:new(layout, buffer)
9394
line = {},
9495
highlight = {},
9596
line_highlight = {},
97+
ansi_highlight = {},
9698
extmark = {},
9799
fold = {},
98100
},
@@ -194,6 +196,13 @@ function Renderer:_render_child(child)
194196
table.insert(self.buffer.line_highlight, { #self.buffer.line - 1, line_hl })
195197
end
196198

199+
if child.options.ansi_hl then
200+
table.insert(self.buffer.ansi_highlight, {
201+
#self.buffer.line - (child.position.row_end - child.position.row_start),
202+
#self.buffer.line,
203+
})
204+
end
205+
197206
if child.options.virtual_text then
198207
table.insert(self.buffer.extmark, {
199208
self.namespace,

0 commit comments

Comments
 (0)