Skip to content

Commit

Permalink
feat(#2948): decorator classes specified by prefix rather than suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Dec 1, 2024
1 parent 4bf4a85 commit 3b60fa7
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 109 deletions.
8 changes: 4 additions & 4 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ Whether to show the destination of the symlink.
*nvim-tree.renderer.decorators*
Highlighting and icons for the nodes, in increasing order of precedence.
Uses strings to specify builtin decorators otherwise specify your
`nvim_tree.api.decorator.DecoratorUser` class.
`nvim_tree.api.decorator.UserDecorator` class.
Type: `nvim_tree.api.decorator.Name[]`, Default: >lua
{
"Git",
Expand Down Expand Up @@ -2777,7 +2777,7 @@ Decorators may:
- Set highlight group for the name or icons
- Override node icon

Create your decorator class via `api.decorator.DecoratorUser:extend()` and add it
Create your decorator class via `api.decorator.UserDecorator:extend()` and add it
to |nvim-tree.renderer.decorators|

e.g. default decorators with an user decorator being overridden only by Cut: >lua
Expand All @@ -2798,9 +2798,9 @@ See `api_decorator.lua` for decorator class definition and full documentation.
Example decorator: >lua

---Create your decorator class
---@class (exact) MyDecorator: nvim_tree.api.decorator.DecoratorUser
---@class (exact) MyDecorator: nvim_tree.api.decorator.UserDecorator
---@field private my_icon nvim_tree.api.HighlightedString
local MyDecorator = require("nvim-tree.api").decorator.DecoratorUser:extend()
local MyDecorator = require("nvim-tree.api").decorator.UserDecorator:extend()

---Mandatory constructor :new() will be called once per tree render, with no arguments.
function MyDecorator:new()
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
special_files = { "Cargo.toml", "Makefile", "README.md", "readme.md" },
hidden_display = "none",
symlink_destination = true,
decorators = { "Git", "Opened", "Hidden", "Modified", "Bookmarks", "Diagnostics", "Copied", "Cut", },
decorators = { "Git", "Open", "Hidden", "Modified", "Bookmark", "Diagnostics", "Copied", "Cut", },
highlight_git = "none",
highlight_diagnostics = "none",
highlight_opened_files = "none",
Expand Down
14 changes: 7 additions & 7 deletions lua/nvim-tree/_meta/api_decorator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ local nvim_tree = {}
---@alias nvim_tree.api.decorator.IconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"

---Names of builtin decorators or your decorator classes. Builtins are ordered lowest to highest priority.
---@alias nvim_tree.api.decorator.Name "Git" | "Opened" | "Hidden" | "Modified" | "Bookmarks" | "Diagnostics" | "Copied" | "Cut" | nvim_tree.api.decorator.DecoratorUser
---@alias nvim_tree.api.decorator.Name "Git" | "Opened" | "Hidden" | "Modified" | "Bookmarks" | "Diagnostics" | "Copied" | "Cut" | nvim_tree.api.decorator.UserDecorator

---Custom decorator, see :help nvim-tree-decorators
---
---@class (exact) nvim_tree.api.decorator.DecoratorUser
---@class (exact) nvim_tree.api.decorator.UserDecorator
---@field protected enabled boolean
---@field protected highlight_range nvim_tree.api.decorator.HighlightRange
---@field protected icon_placement nvim_tree.api.decorator.IconPlacement

---Abstract: no-args constructor must be implemented and will be called once per tree render.
---Must set all fields.
---
function nvim_tree.api.decorator.DecoratorUser:new() end
function nvim_tree.api.decorator.UserDecorator:new() end

---Abstract: optionally implement to set the node's icon
---
---@param node nvim_tree.api.Node
---@return nvim_tree.api.HighlightedString? icon_node
function nvim_tree.api.decorator.DecoratorUser:icon_node(node) end
function nvim_tree.api.decorator.UserDecorator:icon_node(node) end

---Abstract: optionally implement to provide icons and the highlight groups for your icon_placement.
---
---@param node nvim_tree.api.Node
---@return nvim_tree.api.HighlightedString[]? icons
function nvim_tree.api.decorator.DecoratorUser:icons(node) end
function nvim_tree.api.decorator.UserDecorator:icons(node) end

---Abstract: optionally implement to provide one highlight group to apply to your highlight_range.
---
---@param node nvim_tree.api.Node
---@return string? highlight_group
function nvim_tree.api.decorator.DecoratorUser:highlight_group(node) end
function nvim_tree.api.decorator.UserDecorator:highlight_group(node) end

---Define a sign. This should be called in the constructor.
---
---@protected
---@param icon nvim_tree.api.HighlightedString?
function nvim_tree.api.decorator.DecoratorUser:define_sign(icon) end
function nvim_tree.api.decorator.UserDecorator:define_sign(icon) end
6 changes: 3 additions & 3 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local notify = require("nvim-tree.notify")
local DirectoryNode = require("nvim-tree.node.directory")
local FileLinkNode = require("nvim-tree.node.file-link")
local RootNode = require("nvim-tree.node.root")
local DecoratorUser = require("nvim-tree.renderer.decorator.user")
local UserDecorator = require("nvim-tree.renderer.decorator.user")

local Api = {
tree = {},
Expand Down Expand Up @@ -314,7 +314,7 @@ Api.commands.get = wrap(function()
end)

---Create a decorator class by calling :extend()
---@type nvim_tree.api.decorator.DecoratorUser
Api.decorator.DecoratorUser = DecoratorUser --[[@as nvim_tree.api.decorator.DecoratorUser]]
---@type nvim_tree.api.decorator.UserDecorator
Api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]

return Api
50 changes: 25 additions & 25 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ 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 DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
local DecoratorModified = require("nvim-tree.renderer.decorator.modified")
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
local DecoratorUser = require("nvim-tree.renderer.decorator.user")
local BookmarkDecorator = require("nvim-tree.renderer.decorator.bookmarks")
local CopiedDecorator = require("nvim-tree.renderer.decorator.copied")
local CutDecorator = require("nvim-tree.renderer.decorator.cut")
local DiagnosticsDecorator = require("nvim-tree.renderer.decorator.diagnostics")
local GitDecorator = require("nvim-tree.renderer.decorator.git")
local HiddenDecorator = require("nvim-tree.renderer.decorator.hidden")
local ModifiedDecorator = require("nvim-tree.renderer.decorator.modified")
local OpenDecorator = require("nvim-tree.renderer.decorator.opened")
local UserDecorator = require("nvim-tree.renderer.decorator.user")

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

Expand All @@ -23,14 +23,14 @@ local pad = require("nvim-tree.renderer.components.padding")
-- Builtin Decorators
---@type table<nvim_tree.api.decorator.Name, Decorator>
local BUILTIN_DECORATORS = {
Git = DecoratorGit,
Opened = DecoratorOpened,
Hidden = DecoratorHidden,
Modified = DecoratorModified,
Bookmarks = DecoratorBookmarks,
Diagnostics = DecoratorDiagnostics,
Copied = DecoratorCopied,
Cut = DecoratorCut,
Git = GitDecorator,
Open = OpenDecorator,
Hidden = HiddenDecorator,
Modified = ModifiedDecorator,
Bookmark = BookmarkDecorator,
Diagnostics = DiagnosticsDecorator,
Copied = CopiedDecorator,
Cut = CutDecorator,
}

---@class (exact) AddHighlightArgs
Expand Down Expand Up @@ -83,8 +83,8 @@ function Builder:new(args)
---@type Decorator
builtin = BUILTIN_DECORATORS[d]

---@type DecoratorUser
user = d.as and d:as(DecoratorUser)
---@type UserDecorator
user = d.as and d:as(UserDecorator)

if builtin then
table.insert(self.decorators, builtin({ explorer = self.explorer }))
Expand Down Expand Up @@ -164,18 +164,18 @@ function Builder:format_line(indent_markers, arrows, icon, name, node)
add_to_end(line, { icon })

for _, d in ipairs(self.decorators) do
add_to_end(line, d:icons_before(not d:is(DecoratorUser) and node or api_node))
add_to_end(line, d:icons_before(not d:is(UserDecorator) and node or api_node))
end

add_to_end(line, { name })

for _, d in ipairs(self.decorators) do
add_to_end(line, d:icons_after(not d:is(DecoratorUser) and node or api_node))
add_to_end(line, d:icons_after(not d:is(UserDecorator) and node or api_node))
end

local rights = {}
for _, d in ipairs(self.decorators) do
add_to_end(rights, d:icons_right_align(not d:is(DecoratorUser) and node or api_node))
add_to_end(rights, d:icons_right_align(not d:is(UserDecorator) and node or api_node))
end
if #rights > 0 then
self.extmarks[self.index] = rights
Expand All @@ -194,7 +194,7 @@ function Builder:build_signs(node)
local d, sign_name
for i = #self.decorators, 1, -1 do
d = self.decorators[i]
sign_name = d:sign_name(not d:is(DecoratorUser) and node or api_node)
sign_name = d:sign_name(not d:is(UserDecorator) and node or api_node)
if sign_name then
self.signs[self.index] = sign_name
break
Expand Down Expand Up @@ -251,9 +251,9 @@ function Builder:icon_name_decorated(node)
local hl_icon, hl_name
for _, d in ipairs(self.decorators) do
-- maybe overridde icon
icon = d:icon_node((not d:is(DecoratorUser) and node or api_node)) or icon
icon = d:icon_node((not d:is(UserDecorator) and node or api_node)) or icon

hl_icon, hl_name = d:highlight_group_icon_name((not d:is(DecoratorUser) and node or api_node))
hl_icon, hl_name = d:highlight_group_icon_name((not d:is(UserDecorator) and node or api_node))

table.insert(icon_groups, hl_icon)
table.insert(name_groups, hl_name)
Expand Down
16 changes: 8 additions & 8 deletions lua/nvim-tree/renderer/decorator/bookmarks.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local Decorator = require("nvim-tree.renderer.decorator")

---@class (exact) DecoratorBookmarks: Decorator
---@class (exact) BookmarkDecorator: Decorator
---@field private explorer Explorer
---@field private icon HighlightedString?
local DecoratorBookmarks = Decorator:extend()
local BookmarkDecorator = Decorator:extend()

---@class DecoratorBookmarks
---@overload fun(args: DecoratorArgs): DecoratorBookmarks
---@class BookmarkDecorator
---@overload fun(args: DecoratorArgs): BookmarkDecorator

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

self.enabled = true
Expand All @@ -29,7 +29,7 @@ end
---Bookmark icon: renderer.icons.show.bookmarks and node is marked
---@param node Node
---@return HighlightedString[]? icons
function DecoratorBookmarks:icons(node)
function BookmarkDecorator:icons(node)
if self.explorer.marks:get(node) then
return { self.icon }
end
Expand All @@ -38,10 +38,10 @@ end
---Bookmark highlight: renderer.highlight_bookmarks and node is marked
---@param node Node
---@return string? highlight_group
function DecoratorBookmarks:highlight_group(node)
function BookmarkDecorator:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.marks:get(node) then
return "NvimTreeBookmarkHL"
end
end

return DecoratorBookmarks
return BookmarkDecorator
14 changes: 7 additions & 7 deletions lua/nvim-tree/renderer/decorator/copied.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local Decorator = require("nvim-tree.renderer.decorator")

---@class (exact) DecoratorCopied: Decorator
---@class (exact) CopiedDecorator: Decorator
---@field private explorer Explorer
local DecoratorCopied = Decorator:extend()
local CopiedDecorator = Decorator:extend()

---@class DecoratorCopied
---@overload fun(args: DecoratorArgs): DecoratorCopied
---@class CopiedDecorator
---@overload fun(args: DecoratorArgs): CopiedDecorator

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

self.enabled = true
Expand All @@ -20,10 +20,10 @@ end
---Copied highlight: renderer.highlight_clipboard and node is copied
---@param node Node
---@return string? highlight_group
function DecoratorCopied:highlight_group(node)
function CopiedDecorator:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.clipboard:is_copied(node) then
return "NvimTreeCopiedHL"
end
end

return DecoratorCopied
return CopiedDecorator
14 changes: 7 additions & 7 deletions lua/nvim-tree/renderer/decorator/cut.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local Decorator = require("nvim-tree.renderer.decorator")

---@class (exact) DecoratorCut: Decorator
---@class (exact) CutDecorator: Decorator
---@field private explorer Explorer
local DecoratorCut = Decorator:extend()
local CutDecorator = Decorator:extend()

---@class DecoratorCut
---@overload fun(args: DecoratorArgs): DecoratorCut
---@class CutDecorator
---@overload fun(args: DecoratorArgs): CutDecorator

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

self.enabled = true
Expand All @@ -20,10 +20,10 @@ end
---Cut highlight: renderer.highlight_clipboard and node is cut
---@param node Node
---@return string? highlight_group
function DecoratorCut:highlight_group(node)
function CutDecorator:highlight_group(node)
if self.highlight_range ~= "none" and self.explorer.clipboard:is_cut(node) then
return "NvimTreeCutHL"
end
end

return DecoratorCut
return CutDecorator
16 changes: 8 additions & 8 deletions lua/nvim-tree/renderer/decorator/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ local ICON_KEYS = {
["hint"] = vim.diagnostic.severity.HINT,
}

---@class (exact) DecoratorDiagnostics: Decorator
---@class (exact) DiagnosticsDecorator: Decorator
---@field private explorer Explorer
---@field private diag_icons HighlightedString[]?
local DecoratorDiagnostics = Decorator:extend()
local DiagnosticsDecorator = Decorator:extend()

---@class DecoratorDiagnostics
---@overload fun(args: DecoratorArgs): DecoratorDiagnostics
---@class DiagnosticsDecorator
---@overload fun(args: DecoratorArgs): DiagnosticsDecorator

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

self.enabled = true
Expand All @@ -62,7 +62,7 @@ end
---Diagnostic icon: diagnostics.enable, renderer.icons.show.diagnostics and node has status
---@param node Node
---@return HighlightedString[]? icons
function DecoratorDiagnostics:icons(node)
function DiagnosticsDecorator:icons(node)
if node and self.diag_icons then
local diag_status = diagnostics.get_diag_status(node)
local diag_value = diag_status and diag_status.value
Expand All @@ -76,7 +76,7 @@ end
---Diagnostic highlight: diagnostics.enable, renderer.highlight_diagnostics and node has status
---@param node Node
---@return string? highlight_group
function DecoratorDiagnostics:highlight_group(node)
function DiagnosticsDecorator:highlight_group(node)
if self.highlight_range == "none" then
return nil
end
Expand All @@ -102,4 +102,4 @@ function DecoratorDiagnostics:highlight_group(node)
end
end

return DecoratorDiagnostics
return DiagnosticsDecorator
Loading

0 comments on commit 3b60fa7

Please sign in to comment.