Skip to content

Commit

Permalink
feat(#2948): add decorator registry and order
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 24, 2024
1 parent 919a0a3 commit 129c349
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 94 deletions.
1 change: 0 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
},
},
renderer = {
user_decorators = {},
add_trailing = false,
group_empty = false,
full_name = false,
Expand Down
1 change: 0 additions & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
},
renderer = {
user_decorators = {},
add_trailing = false,
group_empty = false,
full_name = false,
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local events = require("nvim-tree.events")
local help = require("nvim-tree.help")
local keymap = require("nvim-tree.keymap")
local notify = require("nvim-tree.notify")
local decorator_registry = require("nvim-tree.renderer.decorator.registry")

local DirectoryNode = require("nvim-tree.node.directory")
local FileLinkNode = require("nvim-tree.node.file-link")
Expand Down Expand Up @@ -312,6 +313,10 @@ Api.commands.get = wrap(function()
return require("nvim-tree.commands").get()
end)

-- TODO provide a registration convenience to hide classic
-- TODO add doc
Api.decorator.BaseDecorator = require("nvim-tree.renderer.decorator.user") --[[@as nvim_tree.api.decorator.BaseDecorator ]]
Api.decorator.register = decorator_registry.register
Api.decorator.unregister = decorator_registry.unregister

return Api
33 changes: 11 additions & 22 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
local decorator_registry = require("nvim-tree.renderer.decorator.registry")
local notify = require("nvim-tree.notify")
local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")

local Class = require("nvim-tree.classic")
local DirectoryNode = require("nvim-tree.node.directory")

local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
local DecoratorDiagnostics = require("nvim-tree.renderer.decorator.diagnostics")
local DecoratorGit = require("nvim-tree.renderer.decorator.git")
local DecoratorModified = require("nvim-tree.renderer.decorator.modified")
local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
local DecoratorUser = require("nvim-tree.renderer.decorator.user")

local pad = require("nvim-tree.renderer.components.padding")

Expand Down Expand Up @@ -56,21 +49,17 @@ function Builder:new(args)
self.signs = {}
self.extmarks = {}
self.virtual_lines = {}
self.decorators = {
-- priority order
DecoratorCut(self.explorer),
DecoratorCopied(self.explorer),
DecoratorDiagnostics(self.explorer),
DecoratorBookmarks(self.explorer),
DecoratorModified(self.explorer),
DecoratorHidden(self.explorer),
DecoratorOpened(self.explorer),
DecoratorGit(self.explorer),
}
self.hidden_display = Builder:setup_hidden_display_function(self.explorer.opts)

for _, user_decorator in ipairs(args.explorer.opts.renderer.user_decorators) do
table.insert(self.decorators, user_decorator.class())
-- lowest priority is registered first
self.decorators = {}
local decorator_args = { explorer = self.explorer }
for _, d in ipairs(decorator_registry.registered) do
if d:is(DecoratorUser) then
table.insert(self.decorators, 1, d())
else
table.insert(self.decorators, 1, d(decorator_args))
end
end
end

Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/bookmarks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ local Decorator = require("nvim-tree.renderer.decorator")
---@field private explorer Explorer
---@field private icon HighlightedString?
local DecoratorBookmarks = Decorator:extend()
DecoratorBookmarks.name = "Bookmarks"

---@class DecoratorBookmarks
---@overload fun(explorer: Explorer): DecoratorBookmarks

---@protected
---@param explorer Explorer
function DecoratorBookmarks:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorBookmarks:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_bookmarks or "none",
icon_placement = self.explorer.opts.renderer.icons.bookmarks_placement or "none",
}

DecoratorBookmarks.super.new(self, args)
DecoratorBookmarks.super.new(self, a)

if self.explorer.opts.renderer.icons.show.bookmarks then
self.icon = {
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/copied.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ local Decorator = require("nvim-tree.renderer.decorator")
---@class (exact) DecoratorCopied: Decorator
---@field private explorer Explorer
local DecoratorCopied = Decorator:extend()
DecoratorCopied.name = "Copied"

---@class DecoratorCopied
---@overload fun(explorer: Explorer): DecoratorCopied

---@protected
---@param explorer Explorer
function DecoratorCopied:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorCopied:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
}

DecoratorCopied.super.new(self, args)
DecoratorCopied.super.new(self, a)
end

---Copied highlight: renderer.highlight_clipboard and node is copied
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/cut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ local Decorator = require("nvim-tree.renderer.decorator")
---@class (exact) DecoratorCut: Decorator
---@field private explorer Explorer
local DecoratorCut = Decorator:extend()
DecoratorCut.name = "Cut"

---@class DecoratorCut
---@overload fun(explorer: Explorer): DecoratorCut

---@protected
---@param explorer Explorer
function DecoratorCut:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorCut:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
}

DecoratorCut.super.new(self, args)
DecoratorCut.super.new(self, a)
end

---Cut highlight: renderer.highlight_clipboard and node is cut
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ local ICON_KEYS = {
---@field private explorer Explorer
---@field private diag_icons HighlightedString[]?
local DecoratorDiagnostics = Decorator:extend()
DecoratorDiagnostics.name = "Diagnostics"

---@class DecoratorDiagnostics
---@overload fun(explorer: Explorer): DecoratorDiagnostics

---@protected
---@param explorer Explorer
function DecoratorDiagnostics:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorDiagnostics:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_diagnostics or "none",
icon_placement = self.explorer.opts.renderer.icons.diagnostics_placement or "none",
}

DecoratorDiagnostics.super.new(self, args)
DecoratorDiagnostics.super.new(self, a)

if not self.enabled then
return
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@ local DirectoryNode = require("nvim-tree.node.directory")
---@field private icons_by_status GitIconsByStatus?
---@field private icons_by_xy GitIconsByXY?
local DecoratorGit = Decorator:extend()
DecoratorGit.name = "Git"

---@class DecoratorGit
---@overload fun(explorer: Explorer): DecoratorGit

---@protected
---@param explorer Explorer
function DecoratorGit:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorGit:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = self.explorer.opts.git.enable,
highlight_range = self.explorer.opts.renderer.highlight_git or "none",
icon_placement = self.explorer.opts.renderer.icons.git_placement or "none",
}

DecoratorGit.super.new(self, args)
DecoratorGit.super.new(self, a)

if not self.enabled then
return
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/hidden.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ local DirectoryNode = require("nvim-tree.node.directory")
---@field private explorer Explorer
---@field private icon HighlightedString?
local DecoratorHidden = Decorator:extend()
DecoratorHidden.name = "Hidden"

---@class DecoratorHidden
---@overload fun(explorer: Explorer): DecoratorHidden

---@protected
---@param explorer Explorer
function DecoratorHidden:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorHidden:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_hidden or "none",
icon_placement = self.explorer.opts.renderer.icons.hidden_placement or "none",
}

DecoratorHidden.super.new(self, args)
DecoratorHidden.super.new(self, a)

if self.explorer.opts.renderer.icons.show.hidden then
self.icon = {
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/renderer/decorator/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ local Class = require("nvim-tree.classic")

---Abstract Decorator
---@class (exact) Decorator: Class
---@field name string for registry
---@field protected enabled boolean
---@field protected highlight_range DecoratorHighlightRange
---@field protected icon_placement DecoratorIconPlacement
local Decorator = Class:extend()

---@class (exact) DecoratorArgs
---@field explorer Explorer
---
---@class (exact) AbstractDecoratorArgs
---@field enabled boolean
---@field highlight_range DecoratorHighlightRange
---@field icon_placement DecoratorIconPlacement

---@protected
---@param args DecoratorArgs
---@param args AbstractDecoratorArgs
function Decorator:new(args)
if args then
self.enabled = args.enabled
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/modified.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ local DirectoryNode = require("nvim-tree.node.directory")
---@field private explorer Explorer
---@field private icon HighlightedString?
local DecoratorModified = Decorator:extend()
DecoratorModified.name = "Modified"

---@class DecoratorModified
---@overload fun(explorer: Explorer): DecoratorModified

---@protected
---@param explorer Explorer
function DecoratorModified:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorModified:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_modified or "none",
icon_placement = self.explorer.opts.renderer.icons.modified_placement or "none",
}

DecoratorModified.super.new(self, args)
DecoratorModified.super.new(self, a)

if not self.enabled then
return
Expand Down
13 changes: 7 additions & 6 deletions lua/nvim-tree/renderer/decorator/opened.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ local Decorator = require("nvim-tree.renderer.decorator")
---@field private explorer Explorer
---@field private icon HighlightedString|nil
local DecoratorOpened = Decorator:extend()
DecoratorOpened.name = "Opened"

---@class DecoratorOpened
---@overload fun(explorer: Explorer): DecoratorOpened

---@protected
---@param explorer Explorer
function DecoratorOpened:new(explorer)
self.explorer = explorer
---@param args DecoratorArgs
function DecoratorOpened:new(args)
self.explorer = args.explorer

---@type DecoratorArgs
local args = {
---@type AbstractDecoratorArgs
local a = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_opened_files or "none",
icon_placement = "none",
}

DecoratorOpened.super.new(self, args)
DecoratorOpened.super.new(self, a)
end

---Opened highlight: renderer.highlight_opened_files and node has an open buffer
Expand Down
Loading

0 comments on commit 129c349

Please sign in to comment.