Skip to content

Commit

Permalink
Merge pull request #1590 from NeogitOrg/development
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey authored Dec 6, 2024
2 parents 11e14b6 + ed01faa commit a38945a
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/neogit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ NeogitDiffContext
NeogitDiffAdd
NeogitDiffDelete
NeogitDiffHeader
NeogitActiveItem Highlight of current commit-ish open

SIGNS FOR LINE HIGHLIGHTING CURRENT CONTEXT
These are essentially an accented version of the above highlight groups. Only
Expand Down
13 changes: 9 additions & 4 deletions lua/neogit/buffers/commit_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ function M:close()
M.instance = nil
end

---@return string
function M.current_oid()
if M.is_open() then
return M.instance.commit_info.oid
else
return "null-oid"
end
end

---Opens the CommitViewBuffer if it isn't open or performs the given action
---which is passed the window id of the commit view buffer
---@param commit_id string commit
Expand Down Expand Up @@ -145,10 +154,6 @@ end
function M:open(kind)
kind = kind or config.values.commit_view.kind

if M.is_open() then
M.instance:close()
end

M.instance = self

self.buffer = Buffer.create {
Expand Down
8 changes: 7 additions & 1 deletion lua/neogit/buffers/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,13 @@ M.CommitEntry = Component.new(function(commit, remotes, args)
}
),
details,
}, { oid = commit.oid, foldable = args.details == true, folded = true, remote = info.remotes[1] })
}, {
item = commit,
oid = commit.oid,
foldable = args.details == true,
folded = true,
remote = info.remotes[1],
})
end)

M.CommitGraph = Component.new(function(commit, padding)
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/buffers/log_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function M:open()
context_highlight = false,
header = self.header,
scroll_header = false,
active_item_highlight = true,
status_column = not config.values.disable_signs and "" or nil,
mappings = {
v = {
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/buffers/reflog_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function M:open(_)
scroll_header = true,
status_column = not config.values.disable_signs and "" or nil,
context_highlight = true,
active_item_highlight = true,
mappings = {
v = {
[popups.mapping_for("CherryPickPopup")] = popups.open("cherry_pick", function(p)
Expand Down
5 changes: 4 additions & 1 deletion lua/neogit/buffers/reflog_view/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ M.Entry = Component.new(function(entry, total)
{ date, "Special" },
},
}),
}, { oid = entry.oid })
}, {
oid = entry.oid,
item = entry,
})
end)

---@param entries ReflogEntry[]
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/buffers/stash_list_view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function M:open()
scroll_header = true,
kind = config.values.stash.kind,
context_highlight = true,
active_item_highlight = true,
mappings = {
v = {
[popups.mapping_for("CherryPickPopup")] = function()
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/buffers/stash_list_view/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ M.Stash = Component.new(function(stash)
{ config.values.log_date_format ~= nil and stash.date or stash.rel_date, "Special" },
},
}),
}, { oid = label })
}, { oid = label, item = stash })
end)

---@param stashes StashItem[]
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/buffers/status/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function M:open(kind)
disable_line_numbers = config.values.disable_line_numbers,
disable_relative_line_numbers = config.values.disable_relative_line_numbers,
foldmarkers = not config.values.disable_signs,
active_item_highlight = true,
on_detach = function()
Watcher.instance(self.root):unregister(self)

Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/buffers/status/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ local SectionItemStash = Component.new(function(item)
text.highlight("NeogitSubtleText")(name),
text.highlight("NeogitSubtleText")(": "),
text(item.message),
}, { yankable = name, item = item })
}, { yankable = item.oid, item = item })
end)

local SectionItemCommit = Component.new(function(item)
Expand Down
42 changes: 38 additions & 4 deletions lua/neogit/lib/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ end
---@field status_column string|nil
---@field load boolean|nil
---@field context_highlight boolean|nil
---@field active_item_highlight boolean|nil
---@field open boolean|nil
---@field disable_line_numbers boolean|nil
---@field disable_relative_line_numbers boolean|nil
Expand Down Expand Up @@ -675,6 +676,11 @@ function Buffer.create(config)
buffer:set_filetype(config.filetype)
end

if config.status_column then
buffer:set_buffer_option("statuscolumn", config.status_column)
buffer:set_buffer_option("signcolumn", "no")
end

if config.user_mappings then
logger.debug("[BUFFER:" .. buffer.handle .. "] Building user key-mappings")

Expand Down Expand Up @@ -826,13 +832,41 @@ function Buffer.create(config)
})
end

if config.status_column then
vim.opt_local.statuscolumn = config.status_column
vim.opt_local.signcolumn = "no"
if config.active_item_highlight then
logger.debug("[BUFFER:" .. buffer.handle .. "] Setting up active item decorations")
buffer:create_namespace("ActiveItem")
buffer:set_decorations("ActiveItem", {
on_start = function()
return buffer:exists() and buffer:is_valid()
end,
on_win = function()
buffer:clear_namespace("ActiveItem")

local active_oid = require("neogit.buffers.commit_view").current_oid()
local item = buffer.ui:find_component_by_oid(active_oid)
if item and item.first and item.last then
for line = item.first, item.last do
buffer:add_line_highlight(line - 1, "NeogitActiveItem", {
priority = 200,
namespace = "ActiveItem",
})
end
end
end,
})

-- The decoration provider will not quite update in time, leaving two lines highlighted unless we use an autocmd too
api.nvim_create_autocmd("WinLeave", {
buffer = buffer.handle,
group = buffer.autocmd_group,
callback = function()
buffer:clear_namespace("ActiveItem")
end,
})
end

if config.foldmarkers then
vim.opt_local.signcolumn = "auto"
buffer:set_buffer_option("signcolumn", "auto")

logger.debug("[BUFFER:" .. buffer.handle .. "] Setting up foldmarkers")
buffer:create_namespace("FoldSigns")
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/lib/git/reflog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end

function M.list(refname, options)
local format = table.concat({
"%h", -- Full Hash
"%H", -- Full Hash
"%aN", -- Author Name
"%gd", -- Reflog Name
"%gs", -- Reflog Subject
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/lib/git/stash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function M.register(meta)
idx = idx,
name = line,
message = message,
oid = git.rev_parse.oid("stash@{" .. idx .. "}"),
}

-- These calls can be somewhat expensive, so lazy load them
Expand Down
1 change: 1 addition & 0 deletions lua/neogit/lib/hl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ function M.setup(config)
NeogitTagDistance = { fg = palette.cyan },
NeogitFloatHeader = { bg = palette.bg0, bold = palette.bold },
NeogitFloatHeaderHighlight = { bg = palette.bg2, fg = palette.cyan, bold = palette.bold },
NeogitActiveItem = { bg = palette.bg_orange, fg = palette.bg0, bold = palette.bold },
}

for group, hl in pairs(hl_store) do
Expand Down
2 changes: 2 additions & 0 deletions lua/neogit/lib/ui/component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ local default_component_options = {
---@field highlight fun(hl_group:string): self
---@field line_hl fun(hl_group:string): self
---@field padding_left fun(string): self
---@field first integer|nil first line component appears rendered in buffer
---@field last integer|nil last line component appears rendered in buffer
---@operator call: Component
local Component = {}

Expand Down
6 changes: 6 additions & 0 deletions lua/neogit/lib/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ function Ui:_find_component_by_index(line, f)
end
end

---@param oid string
---@return Component|nil
function Ui:find_component_by_oid(oid)
return self.node_index:find_by_oid(oid)
end

---@return Component|nil
function Ui:get_cursor_context(line)
local cursor = line or vim.api.nvim_win_get_cursor(0)[1]
Expand Down
12 changes: 12 additions & 0 deletions lua/neogit/lib/ui/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
---@class RendererIndex
---@field index table
---@field items table
---@field oid_index table
local RendererIndex = {}
RendererIndex.__index = RendererIndex

Expand All @@ -12,6 +13,12 @@ function RendererIndex:find_by_line(line)
return self.index[line] or {}
end

---@param oid string
---@return Component|nil
function RendererIndex:find_by_oid(oid)
return self.oid_index[oid]
end

---@param node Component
function RendererIndex:add(node)
if not self.index[node.position.row_start] then
Expand All @@ -38,11 +45,16 @@ function RendererIndex:add_item(item, first, last)
item.first = first
item.last = last
table.insert(self.items[#self.items].items, item)

if item.oid then
self.oid_index[item.oid] = item
end
end

function RendererIndex.new()
return setmetatable({
index = {},
oid_index = {},
items = {
{ items = {} }, -- First section
},
Expand Down

0 comments on commit a38945a

Please sign in to comment.