refactor(git): rework module around clearer Status and Repo split
This commit is contained in:
+75
-28
@@ -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,
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
+1
-3
@@ -53,9 +53,7 @@ local highlights = {
|
||||
DiffAdd = { bg = "#1a2f22" },
|
||||
DiffChange = { bg = "#15304a" },
|
||||
DiffDelete = { bg = "#311c1e" },
|
||||
-- GitDeleted = { fg = c.red },
|
||||
-- GitUnstaged = { fg = c.yellow },
|
||||
-- GitUntracked = { fg = c.green },
|
||||
Changed = { fg = c.yellow },
|
||||
}
|
||||
for kind, color in pairs(completion_kind_colors) do
|
||||
highlights["LspKind" .. kind] = { fg = color }
|
||||
|
||||
Reference in New Issue
Block a user