Skip to content

Commit 5dabe87

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 hunk line numbers.
1 parent 092d2dc commit 5dabe87

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
@@ -146,38 +146,16 @@ function Buffer:set_extmarks(extmarks)
146146
end
147147

148148
function Buffer:set_line_highlights(highlights)
149-
local line_ansi_colorized_map = {}
150-
151149
for _, hl in ipairs(highlights) do
152-
local line_nr, hl_group = unpack(hl)
153-
if hl_group == "NeogitDiffContext" then
154-
line_ansi_colorized_map[line_nr] = true
155-
else
156-
self:add_line_highlight(unpack(hl))
157-
end
158-
end
159-
160-
local line_ansi_colorized = {}
161-
for k in pairs(line_ansi_colorized_map) do
162-
table.insert(line_ansi_colorized, k)
150+
self:add_line_highlight(unpack(hl))
163151
end
164-
table.sort(line_ansi_colorized)
152+
end
165153

166-
local start_line_nr, prev_line_nr
167-
for _, line_nr in ipairs(line_ansi_colorized) do
168-
if start_line_nr == nil then
169-
start_line_nr = line_nr
170-
end
171-
if prev_line_nr ~= nil and line_nr ~= prev_line_nr + 1 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)
174-
start_line_nr = line_nr
175-
end
176-
prev_line_nr = line_nr
177-
end
178-
if start_line_nr ~= nil then
179-
local text = self:get_lines(start_line_nr, prev_line_nr + 1, false)
180-
vim.g.baleia.buf_set_lines(self.handle, start_line_nr, prev_line_nr + 1, false, text)
154+
function Buffer:set_ansi_highlights(highlights)
155+
for _, hl in ipairs(highlights) do
156+
local first_line, last_line = unpack(hl)
157+
local text = self:get_lines(first_line, last_line, false)
158+
vim.g.baleia.buf_set_lines(self.handle, first_line, last_line, false, text)
181159
end
182160
end
183161

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
@@ -679,6 +679,7 @@ function Ui:update()
679679
self.buf:set_highlights(renderer.buffer.highlight)
680680
self.buf:set_extmarks(renderer.buffer.extmark)
681681
self.buf:set_line_highlights(renderer.buffer.line_highlight)
682+
self.buf:set_ansi_highlights(renderer.buffer.ansi_highlight)
682683
self.buf:set_folds(renderer.buffer.fold)
683684

684685
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)