Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Alexander Courtis edited this pageJun 21, 2025 ·4 revisions

Please share your custom decorators.

See:help nvim-tree-decorators for documentation and example:help nvim-tree-decorator-example

See_meta/api_decorator.lua fornvim_tree.api.decorator.UserDecorator class documentation.See_meta/api.lua fornvim_tree.api.Node classes documentation.

Quickfix Decorator

@b0o

Highlights files which are in your quickfix list.2024-12-01_17-39-39_region

---@class (exact)QuickfixDecorator:nvim_tree.api.decorator.UserDecorator---@fieldprivateqf_icon nvim_tree.api.HighlightedStringlocalQuickfixDecorator=require('nvim-tree.api').decorator.UserDecorator:extend()localaugroup=vim.api.nvim_create_augroup('nvim-tree-quickfix-decorator', {clear=true })localautocmds_setup=falselocalfunctionsetup_autocmds()ifautocmds_setupthenreturnendautocmds_setup=truevim.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,  })endfunctionQuickfixDecorator:new()self.enabled=trueself.highlight_range='all'self.icon_placement='signcolumn'self.qf_icon= {str='',hl= {'QuickFixLine'} }self:define_sign(self.qf_icon)setup_autocmds()end---Helper function to check if a node is in quickfix list---@paramnodenvim_tree.api.Node---@returnbooleanlocalfunctionis_qf_item(node)ifnode.name=='..'ornode.type=='directory'thenreturnfalseendlocalbufnr=vim.fn.bufnr(node.absolute_path)returnbufnr~=-1andvim.iter(vim.fn.getqflist()):any(function(qf)returnqf.bufnr==bufnrend)end---Return quickfix icons for the node---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString[]?iconsfunctionQuickfixDecorator:icons(node)ifis_qf_item(node)thenreturn {self.qf_icon }endreturnnilend---Return highlight group for the node---@paramnodenvim_tree.api.Node---@returnstring?highlight_groupfunctionQuickfixDecorator:highlight_group(node)ifis_qf_item(node)thenreturn'QuickFixLine'endreturnnilendreturnQuickfixDecorator

Reference Example

A decorator class for nodes named "example", overridind all builtin decoratorsexcept for Cut.

  • Highlights node name withIncSearch
  • Creates two icons"1" and"2" placed after the node name, highlighted withDiffAdd andDiffText
  • Replaces the node icon with"N", highlighted withError

Create a class file~/.config/nvim/lua/my-decorator.lua

Require and register it during setup:

localMyDecorator=require("my-decorator")require("nvim-tree").setup({renderer= {decorators= {"Git","Open","Hidden","Modified","Bookmark","Diagnostics","Copied",MyDecorator,"Cut",    },  },})

Contents ofmy-decorator.lua:

---@class (exact)MyDecorator:nvim_tree.api.decorator.UserDecorator---@fieldprivatemy_icon1 nvim_tree.api.HighlightedString---@fieldprivatemy_icon2 nvim_tree.api.HighlightedString---@fieldprivatemy_icon_node nvim_tree.api.HighlightedString---@fieldprivatemy_highlight_group stringlocalMyDecorator=require("nvim-tree.api").decorator.UserDecorator:extend()---Mandatory constructor  :new()  will be called once per tree render, with no arguments.functionMyDecorator:new()self.enabled=trueself.highlight_range="name"self.icon_placement="after"-- create your icons and highlights once, applied to every nodeself.my_icon1= {str="1",hl= {"DiffAdd"} }self.my_icon2= {str="2",hl= {"DiffText"} }self.my_icon_node= {str="N",hl= {"Error"} }self.my_highlight_group="IncSearch"-- Define the icon signs only once-- Only needed if you are using icon_placement = "signcolumn"-- self:define_sign(self.my_icon1)-- self:define_sign(self.my_icon2)end---Override node icon---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString?icon_nodefunctionMyDecorator:icon_node(node)ifnode.name=="example"thenreturnself.my_icon_nodeelsereturnnilendend---Return two icons for DecoratorIconPlacement "after"---@paramnodenvim_tree.api.Node---@returnnvim_tree.api.HighlightedString[]?iconsfunctionMyDecorator:icons(node)ifnode.name=="example"thenreturn {self.my_icon1,self.my_icon2, }elsereturnnilendend---Exactly one highlight group for DecoratorHighlightRange "name"---@paramnodenvim_tree.api.Node---@returnstring?highlight_groupfunctionMyDecorator:highlight_group(node)ifnode.name=="example"thenreturnself.my_highlight_groupelsereturnnilendendreturnMyDecorator
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp