refactor(git): rework module around clearer Status and Repo split

This commit is contained in:
2026-05-06 00:45:56 +02:00
parent 1b43fa6a1c
commit 80d6d465cf
17 changed files with 821 additions and 775 deletions
+75 -28
View File
@@ -41,29 +41,88 @@ end)
---@class nvim_tree.api.decorator.UserDecorator
local Decorator = require("nvim-tree.api").Decorator
---@class GitIgnoreDecorator: nvim_tree.api.decorator.UserDecorator
local GitIgnoreDecorator = Decorator:extend()
local repo = require("git.repo")
function GitIgnoreDecorator:new()
repo.on("refresh", function()
require("nvim-tree.api").tree.reload()
end)
do
local events = require("nvim-tree.api").events
local function on_change(data)
if not data then
return
end
local seen = {}
local function fire(path)
if not path then
return
end
local r = repo.find(path)
if r and not seen[r] then
seen[r] = true
r:refresh()
end
end
fire(data.fname)
fire(data.folder_name)
fire(data.old_name)
fire(data.new_name)
end
events.subscribe(events.Event.FileCreated, on_change)
events.subscribe(events.Event.FileRemoved, on_change)
events.subscribe(events.Event.FolderCreated, on_change)
events.subscribe(events.Event.FolderRemoved, on_change)
events.subscribe(events.Event.NodeRenamed, on_change)
end
---@class ow.GitDecorator: nvim_tree.api.Decorator
local GitDecorator = Decorator:extend()
function GitDecorator:new()
self.enabled = true
self.icon_placement = "after"
self.highlight_range = "name"
self.icon_placement = "none"
self.file_hl = "NvimTreeGitFileIgnoredHL"
self.folder_hl = "NvimTreeGitFolderIgnoredHL"
end
---@param node Node
---@return string? highlight_group
function GitIgnoreDecorator:highlight_group(node)
local status = node.git_status
if not status then
---@return ow.Git.Status.Entry[]?
local function entries_for(node)
local r = repo.find(node.absolute_path)
if not r then
return
end
local rel = vim.fs.relpath(r.worktree, node.absolute_path)
return rel and r.status.entries[rel]
end
if status.file == "!!" then
return self.file_hl
elseif status.dir and status.dir.direct == "!!" then
return self.folder_hl
---@param node Node
---@return { str: string, hl: string[] }[]?
function GitDecorator.icons(_, node)
local list = entries_for(node)
if not list then
return
end
local out = {}
for _, entry in ipairs(list) do
if entry.kind ~= "ignored" then
table.insert(out, { str = entry.char, hl = { entry.hl } })
end
end
return out
end
---@param node Node
---@return string?
function GitDecorator.highlight_group(_, node)
local list = entries_for(node)
if not list then
return
end
for _, entry in ipairs(list) do
if entry.kind == "ignored" then
return entry.hl
end
end
end
@@ -120,8 +179,7 @@ require("nvim-tree").setup({
end,
special_files = {},
decorators = {
"Git",
GitIgnoreDecorator,
GitDecorator,
"Open",
"Modified",
"Bookmark",
@@ -140,7 +198,6 @@ require("nvim-tree").setup({
},
},
icons = {
git_placement = "after",
diagnostics_placement = "signcolumn",
bookmarks_placement = "after",
symlink_arrow = " -> ",
@@ -157,15 +214,6 @@ require("nvim-tree").setup({
},
glyphs = {
modified = "*",
git = {
unstaged = "M",
staged = "M",
unmerged = "!",
renamed = "R",
untracked = "?",
deleted = "D",
ignored = " ",
},
},
},
},
@@ -186,7 +234,6 @@ require("nvim-tree").setup({
enable = true,
},
filters = {
git_ignored = false,
custom = { "^\\.git$" },
},
live_filter = {
@@ -219,7 +266,7 @@ require("nvim-tree").setup({
},
sync_root_with_cwd = true,
git = {
show_on_open_dirs = false,
enable = false,
},
})