-
-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] User Decorators #2948
Comments
That's a great idea! There's nothing blocking us from doing this: Add a Instantiate them with highest priority. Make the Add help entries that just direct to the Add |
|
Safer approach: Create a This class will be similar to
|
Challenges:
|
Migrating to classic to allow simple class instantiation and privacy: #2991 |
A proof-of-concept for user decorators has been created: #2996 I'd be most grateful if you tested this, instructions to follow. cd /path/to/nvim-tree.lua
git pull 2948-add-user-decorators
git checkout When you're finished testing: git checkout master |
Please note that this is a proof of concept for now. TODO:
Example decorator class to highlight and add an icon to files named "example": local UserDecorator = require("nvim-tree.renderer.decorator.user")
---A string with one or more highlight groups applied to it
---@class (exact) HighlightedString
---@field str string
---@field hl string[] highlight groups applied in order
---Custom user decorators must inherit from UserDecorator
---@class (exact) UserDecoratorExample: UserDecorator
---@field private my_icon HighlightedString
local UserDecoratorExample = UserDecorator:extend()
---Constructor will be called once per tree render, with no arguments.
function UserDecoratorExample:new()
UserDecoratorExample.super.new(self, {
enabled = true,
hl_pos = "name",
icon_placement = "right_align",
})
-- create your icon once, for convenience
self.my_icon = { str = "E", hl = { "ExampleIcon" } }
-- Define the icon sign only once
-- Only needed if you are using icon_placement = "signcolumn"
-- self:define_sign(self.my_icon)
end
---@param node Node
---@return HighlightedString[]|nil icons
function UserDecoratorExample:calculate_icons(node)
if node.name == "example" then
return { self.my_icon }
else
return nil
end
end
---@param node Node
---@return string|nil group
function UserDecoratorExample:calculate_highlight(node)
if node.name == "example" then
return "ExampleHighlight"
else
return nil
end
end Registering it: require("nvim-tree").setup({
renderer = {
user_decorators = {
{
class = UserDecoratorExample,
},
},
},
}) |
@b0o and @DMyashkov I would be most grateful for your testing and feedback. I'm open to any and all ideas and changes. |
@b0o you won't have access to explorer, however you will be able to use API: |
Sorry for the delay. This is really awesome! I've converted my quickfix decorator and it seems to be working great: local UserDecorator = require 'nvim-tree.renderer.decorator.user'
---A string with one or more highlight groups applied to it
---@class (exact) HighlightedString
---@field str string
---@field hl string[] highlight groups applied in order
---Quickfix decorator
---@class (exact) DecoratorQuickfix: UserDecorator
---@field private qf_icon HighlightedString
local DecoratorQuickfix = UserDecorator:extend()
local augroup = vim.api.nvim_create_augroup('nvim-tree-decorator-quickfix', { clear = true })
function DecoratorQuickfix:new()
DecoratorQuickfix.super.new(self, {
enabled = true,
hl_pos = 'name',
icon_placement = 'signcolumn',
})
self.qf_icon = { str = '', hl = { 'QuickFixLine' } }
self:define_sign(self.qf_icon)
vim.api.nvim_create_autocmd('QuickfixCmdPost', {
group = augroup,
callback = function()
require('nvim-tree.api').tree.reload()
end,
})
vim.api.nvim_create_autocmd('FileType', {
pattern = 'qf',
group = augroup,
callback = function(evt)
vim.api.nvim_create_autocmd('TextChanged', {
buffer = evt.buf,
group = augroup,
callback = function()
require('nvim-tree.api').tree.reload()
end,
})
end,
})
end
---@param node Node
local function is_qf_item(node)
if node.name == '..' or node.type == 'directory' then
return false
end
local bufnr = vim.fn.bufnr(node.absolute_path)
return bufnr ~= -1 and vim.iter(vim.fn.getqflist()):any(function(qf)
return qf.bufnr == bufnr
end)
end
---@param node Node
---@return HighlightedString[]|nil icons
---@diagnostic disable-next-line: duplicate-set-field
function DecoratorQuickfix:calculate_icons(node)
if is_qf_item(node) then
return { self.qf_icon }
end
return nil
end
---@param node Node
---@return string|nil group
---@diagnostic disable-next-line: duplicate-set-field
function DecoratorQuickfix:calculate_highlight(node)
if is_qf_item(node) then
return 'QuickFixLine'
end
return nil
end
return DecoratorQuickfix Only issue is that this line seems to be causing this error:
Thank you @alex-courtis, this opens up a lot of cool possibilities for other custom decorators! |
Great news; let's make this official.
I recommend you don't do that inside the constructor, as it will be called every time the tree is rendered. We'll end up with thousands of events all firing at the same time. You could move all the create_au* to your init.lua. It would be safest to do it after nvim-tree setup.
Apologies, POC was really rushed and I didn't commit the example. |
I'll get to this one, however I am pressed for time right now. #2954 requires attention first. |
This was a great opportunity to start fully documenting API! I'd be most grateful for a review of:
It would be fantastic if you could update your decorator and tested with latest:
|
I would love to have an API for defining custom decorators. My use case is to add an icon/highlight for nodes which are in the quickfix list. I've come up with a hacky way to accomplish this, by overriding the bookmarks decorator:
It would be nice if there was an official way to do this.
The text was updated successfully, but these errors were encountered: