Skip to content

Commit

Permalink
feat(#2948): add UserDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 22, 2024
1 parent 099de58 commit 24eb27e
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 87 deletions.
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/bookmarks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ local DecoratorBookmarks = Decorator:extend()
function DecoratorBookmarks:new(explorer)
self.explorer = explorer

DecoratorBookmarks.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_bookmarks or "none",
icon_placement = self.explorer.opts.renderer.icons.bookmarks_placement or "none",
})
---@type DecoratorArgs
local args = {
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)

if self.explorer.opts.renderer.icons.show.bookmarks then
self.icon = {
Expand All @@ -41,7 +44,7 @@ end
---@param node Node
---@return string|nil group
function DecoratorBookmarks:calculate_highlight(node)
if self.range ~= "none" and self.explorer.marks:get(node) then
if self.highlight_range ~= "none" and self.explorer.marks:get(node) then
return "NvimTreeBookmarkHL"
end
end
Expand Down
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/copied.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ local DecoratorCopied = Decorator:extend()
function DecoratorCopied:new(explorer)
self.explorer = explorer

DecoratorCopied.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
})
---@type DecoratorArgs
local args = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
}

DecoratorCopied.super.new(self, args)
end

---Copied highlight: renderer.highlight_clipboard and node is copied
---@param node Node
---@return string|nil group
function DecoratorCopied:calculate_highlight(node)
if self.range ~= "none" and self.explorer.clipboard:is_copied(node) then
if self.highlight_range ~= "none" and self.explorer.clipboard:is_copied(node) then
return "NvimTreeCopiedHL"
end
end
Expand Down
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/cut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ local DecoratorCut = Decorator:extend()
function DecoratorCut:new(explorer)
self.explorer = explorer

DecoratorCut.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
})
---@type DecoratorArgs
local args = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_clipboard or "none",
icon_placement = "none",
}

DecoratorCut.super.new(self, args)
end

---Cut highlight: renderer.highlight_clipboard and node is cut
---@param node Node
---@return string|nil group
function DecoratorCut:calculate_highlight(node)
if self.range ~= "none" and self.explorer.clipboard:is_cut(node) then
if self.highlight_range ~= "none" and self.explorer.clipboard:is_cut(node) then
return "NvimTreeCutHL"
end
end
Expand Down
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ local DecoratorDiagnostics = Decorator:extend()
function DecoratorDiagnostics:new(explorer)
self.explorer = explorer

DecoratorDiagnostics.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_diagnostics or "none",
icon_placement = self.explorer.opts.renderer.icons.diagnostics_placement or "none",
})
---@type DecoratorArgs
local args = {
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)

if not self.enabled then
return
Expand Down Expand Up @@ -83,7 +86,7 @@ end
---@param node Node
---@return string|nil group
function DecoratorDiagnostics:calculate_highlight(node)
if not node or not self.enabled or self.range == "none" then
if not node or not self.enabled or self.highlight_range == "none" then
return nil
end

Expand Down
19 changes: 11 additions & 8 deletions lua/nvim-tree/renderer/decorator/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ local DecoratorGit = Decorator:extend()
function DecoratorGit:new(explorer)
self.explorer = explorer

DecoratorGit.super.new(self, {
enabled = self.explorer.opts.git.enable,
hl_pos = self.explorer.opts.renderer.highlight_git or "none",
icon_placement = self.explorer.opts.renderer.icons.git_placement or "none",
})
---@type DecoratorArgs
local args = {
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)

if not self.enabled then
return
end

if self.range ~= "none" then
if self.highlight_range ~= "none" then
self:build_file_folder_hl_by_xy()
end

Expand Down Expand Up @@ -161,7 +164,7 @@ function DecoratorGit:calculate_icons(node)
for _, s in pairs(git_xy) do
local icons = self.icons_by_xy[s]
if not icons then
if self.range == "none" then
if self.highlight_range == "none" then
notify.warn(string.format("Unrecognized git state '%s'", git_xy))
end
return nil
Expand Down Expand Up @@ -207,7 +210,7 @@ end
---@param node Node
---@return string|nil group
function DecoratorGit:calculate_highlight(node)
if not node or not self.enabled or self.range == "none" then
if not node or not self.enabled or self.highlight_range == "none" then
return nil
end

Expand Down
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/hidden.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ local DecoratorHidden = Decorator:extend()
function DecoratorHidden:new(explorer)
self.explorer = explorer

DecoratorHidden.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_hidden or "none",
icon_placement = self.explorer.opts.renderer.icons.hidden_placement or "none",
})
---@type DecoratorArgs
local args = {
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)

if self.explorer.opts.renderer.icons.show.hidden then
self.icon = {
Expand All @@ -42,7 +45,7 @@ end
---@param node Node
---@return string|nil group
function DecoratorHidden:calculate_highlight(node)
if not self.enabled or self.range == "none" or not node:is_dotfile() then
if not self.enabled or self.highlight_range == "none" or not node:is_dotfile() then
return nil
end

Expand Down
56 changes: 26 additions & 30 deletions lua/nvim-tree/renderer/decorator/init.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
local Class = require("nvim-tree.classic")

---@alias DecoratorRange "none" | "icon" | "name" | "all"
---@alias DecoratorIconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"

---Abstract Decorator
---@class (exact) Decorator: Class
---@field protected enabled boolean
---@field protected range DecoratorRange
---@field protected highlight_range DecoratorHighlightRange
---@field protected icon_placement DecoratorIconPlacement
local Decorator = Class:extend()

---@class (exact) DecoratorArgs
---@field enabled boolean
---@field hl_pos DecoratorRange
---@field icon_placement DecoratorIconPlacement

---@protected
---@param args DecoratorArgs
function Decorator:new(args)
self.enabled = args.enabled
self.range = args.hl_pos
self.icon_placement = args.icon_placement
if args then
self.enabled = args.enabled
self.highlight_range = args.highlight_range
self.icon_placement = args.icon_placement
else
self.enabled = false
end
end

---Maybe highlight groups
---@param node Node
---@return string|nil icon highlight group
---@return string|nil name highlight group
---@return string? icon highlight group
---@return string? name highlight group
function Decorator:groups_icon_name(node)
local icon_hl, name_hl

if self.enabled and self.range ~= "none" then
if self.enabled and self.highlight_range ~= "none" then
local hl = self:calculate_highlight(node)

if self.range == "all" or self.range == "icon" then
if self.highlight_range == "all" or self.highlight_range == "icon" then
icon_hl = hl
end
if self.range == "all" or self.range == "name" then
if self.highlight_range == "all" or self.highlight_range == "name" then
name_hl = hl
end
end
Expand All @@ -46,7 +42,7 @@ end

---Maybe icon sign
---@param node Node
---@return string|nil name
---@return string? name
function Decorator:sign_name(node)
if not self.enabled or self.icon_placement ~= "signcolumn" then
return
Expand All @@ -60,7 +56,7 @@ end

---Icons when "before"
---@param node Node
---@return HighlightedString[]|nil icons
---@return HighlightedString[]? icons
function Decorator:icons_before(node)
if not self.enabled or self.icon_placement ~= "before" then
return
Expand All @@ -71,7 +67,7 @@ end

---Icons when "after"
---@param node Node
---@return HighlightedString[]|nil icons
---@return HighlightedString[]? icons
function Decorator:icons_after(node)
if not self.enabled or self.icon_placement ~= "after" then
return
Expand All @@ -82,7 +78,7 @@ end

---Icons when "right_align"
---@param node Node
---@return HighlightedString[]|nil icons
---@return HighlightedString[]? icons
function Decorator:icons_right_align(node)
if not self.enabled or self.icon_placement ~= "right_align" then
return
Expand All @@ -93,23 +89,23 @@ end

---Maybe icons, optionally implemented
---@protected
---@param _ Node
---@return HighlightedString[]|nil icons
function Decorator:calculate_icons(_)
return nil
---@param node Node
---@return HighlightedString[]? icons
function Decorator:calculate_icons(node)
self:nop(node)
end

---Maybe highlight group, optionally implemented
---@protected
---@param _ Node
---@return string|nil group
function Decorator:calculate_highlight(_)
return nil
---@param node Node
---@return string? group
function Decorator:calculate_highlight(node)
self:nop(node)
end

---Define a sign
---@protected
---@param icon HighlightedString|nil
---@param icon HighlightedString?
function Decorator:define_sign(icon)
if icon and #icon.hl > 0 then
local name = icon.hl[1]
Expand Down
13 changes: 13 additions & 0 deletions lua/nvim-tree/renderer/decorator/meta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---@meta

---Highlight group range as per nvim-tree.renderer.highlight_*
---@alias DecoratorHighlightRange "none" | "icon" | "name" | "all"

---Icon position as per renderer.icons.*_placement
---@alias DecoratorIconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"

---Decorator Constructor Arguments
---@class (exact) DecoratorArgs
---@field enabled boolean
---@field highlight_range DecoratorHighlightRange
---@field icon_placement DecoratorIconPlacement
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/modified.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ local DecoratorModified = Decorator:extend()
function DecoratorModified:new(explorer)
self.explorer = explorer

DecoratorModified.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_modified or "none",
icon_placement = self.explorer.opts.renderer.icons.modified_placement or "none",
})
---@type DecoratorArgs
local args = {
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)

if not self.enabled then
return
Expand Down Expand Up @@ -48,7 +51,7 @@ end
---@param node Node
---@return string|nil group
function DecoratorModified:calculate_highlight(node)
if not self.enabled or self.range == "none" or not buffers.is_modified(node) then
if not self.enabled or self.highlight_range == "none" or not buffers.is_modified(node) then
return nil
end

Expand Down
15 changes: 9 additions & 6 deletions lua/nvim-tree/renderer/decorator/opened.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ local DecoratorOpened = Decorator:extend()
function DecoratorOpened:new(explorer)
self.explorer = explorer

DecoratorOpened.super.new(self, {
enabled = true,
hl_pos = self.explorer.opts.renderer.highlight_opened_files or "none",
icon_placement = "none",
})
---@type DecoratorArgs
local args = {
enabled = true,
highlight_range = self.explorer.opts.renderer.highlight_opened_files or "none",
icon_placement = "none",
}

DecoratorOpened.super.new(self, args)
end

---Opened highlight: renderer.highlight_opened_files and node has an open buffer
---@param node Node
---@return string|nil group
function DecoratorOpened:calculate_highlight(node)
if self.range ~= "none" and buffers.is_opened(node) then
if self.highlight_range ~= "none" and buffers.is_opened(node) then
return "NvimTreeOpenedHL"
end
end
Expand Down
Loading

0 comments on commit 24eb27e

Please sign in to comment.