Skip to content

Commit

Permalink
refactor: move Buffer:set_line_highlights() ANSI highlights to `Buf…
Browse files Browse the repository at this point in the history
…fer: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.
  • Loading branch information
stevenxxiu committed Dec 12, 2024
1 parent 6036128 commit bc6ab47
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
17 changes: 13 additions & 4 deletions lua/neogit/buffers/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,19 @@ local HunkLine = Component.new(function(line)
end)

M.Hunk = Component.new(function(props)
return col.tag("Hunk")({
text.line_hl("NeogitHunkHeader")(props.header),
col.tag("HunkContent")(map(props.content, HunkLine)),
}, { foldable = true, folded = props.folded or false, context = true, hunk = props.hunk })
return col.tag("Hunk")(
{
text.line_hl("NeogitHunkHeader")(props.header),
col.tag("HunkContent")(map(props.content, HunkLine)),
},
{
foldable = true,
folded = props.folded or false,
context = true,
hunk = props.hunk,
ansi_hl = config.values.log_pager ~= nil,
}
)
end)

M.List = Component.new(function(props)
Expand Down
36 changes: 7 additions & 29 deletions lua/neogit/lib/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,16 @@ function Buffer:set_extmarks(extmarks)
end

function Buffer:set_line_highlights(highlights)
local line_ansi_colorized_map = {}

for _, hl in ipairs(highlights) do
local line_nr, hl_group = unpack(hl)
if hl_group == "NeogitDiffContext" then
line_ansi_colorized_map[line_nr] = true
else
self:add_line_highlight(unpack(hl))
end
end

local line_ansi_colorized = {}
for k in pairs(line_ansi_colorized_map) do
table.insert(line_ansi_colorized, k)
self:add_line_highlight(unpack(hl))
end
table.sort(line_ansi_colorized)
end

local start_line_nr, prev_line_nr
for _, line_nr in ipairs(line_ansi_colorized) do
if start_line_nr == nil then
start_line_nr = line_nr
end
if prev_line_nr ~= nil and line_nr ~= prev_line_nr + 1 then
local text = self:get_lines(start_line_nr, prev_line_nr + 1, false)
vim.g.baleia.buf_set_lines(self.handle, start_line_nr, prev_line_nr + 1, false, text)
start_line_nr = line_nr
end
prev_line_nr = line_nr
end
if start_line_nr ~= nil then
local text = self:get_lines(start_line_nr, prev_line_nr + 1, false)
vim.g.baleia.buf_set_lines(self.handle, start_line_nr, prev_line_nr + 1, false, text)
function Buffer:set_ansi_highlights(highlights)
for _, hl in ipairs(highlights) do
local first_line, last_line = unpack(hl)
local text = self:get_lines(first_line, last_line, false)
vim.g.baleia.buf_set_lines(self.handle, first_line, last_line, false, text)
end
end

Expand Down
1 change: 1 addition & 0 deletions lua/neogit/lib/ui/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local default_component_options = {

---@class ComponentOptions
---@field line_hl string
---@field ansi_hl boolean
---@field highlight string
---@field align_right integer|nil
---@field padding_left integer
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/lib/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ function Ui:update()
self.buf:set_highlights(renderer.buffer.highlight)
self.buf:set_extmarks(renderer.buffer.extmark)
self.buf:set_line_highlights(renderer.buffer.line_highlight)
self.buf:set_ansi_highlights(renderer.buffer.ansi_highlight)
self.buf:set_folds(renderer.buffer.fold)

self.statuscolumn = {}
Expand Down
9 changes: 9 additions & 0 deletions lua/neogit/lib/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ end
---@field line string[]
---@field highlight table[]
---@field line_highlight table[]
---@field ansi_highlight table[]
---@field extmark table[]
---@field fold table[]

Expand Down Expand Up @@ -93,6 +94,7 @@ function Renderer:new(layout, buffer)
line = {},
highlight = {},
line_highlight = {},
ansi_highlight = {},
extmark = {},
fold = {},
},
Expand Down Expand Up @@ -194,6 +196,13 @@ function Renderer:_render_child(child)
table.insert(self.buffer.line_highlight, { #self.buffer.line - 1, line_hl })
end

if child.options.ansi_hl then
table.insert(self.buffer.ansi_highlight, {
#self.buffer.line - (child.position.row_end - child.position.row_start),
#self.buffer.line,
})
end

if child.options.virtual_text then
table.insert(self.buffer.extmark, {
self.namespace,
Expand Down

0 comments on commit bc6ab47

Please sign in to comment.