Skip to content

feat(#2948): add custom decorators, :help nvim-tree-decorators #2996

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

Merged
merged 30 commits into from
Dec 7, 2024

Conversation

alex-courtis
Copy link
Member

@alex-courtis alex-courtis commented Nov 9, 2024

fixes #2948

fixes #902

fixes #284

@alex-courtis alex-courtis changed the title feat(#2948): add UserDecorator, proof of concept feat(#2948): add user decorators Nov 24, 2024
Copy link

@b0o b0o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, awesome work! One thing is that I am getting some LuaLS type checker warnings when defining my decorator:

---@class (exact) DecoratorQuickfix: nvim_tree.api.decorator.UserDecorator
---@field private qf_icon nvim_tree.api.HighlightedString
local DecoratorQuickfix = require('nvim-tree.api').decorator.UserDecorator:extend()
-- ^ Undefined field `extend`.

local augroup = vim.api.nvim_create_augroup('nvim-tree-decorator-quickfix', { clear = true })

local autocmds_setup = false
local function setup_autocmds()
  if autocmds_setup then
    return
  end
  autocmds_setup = true
  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

function DecoratorQuickfix:new()
  self.enabled = true
  self.highlight_range = 'all'
  self.icon_placement = 'signcolumn'
  self.qf_icon = { str = '', hl = { 'QuickFixLine' } }
  self:define_sign(self.qf_icon)
  -- ^ Undefined field `define_sign`.
  setup_autocmds()
end

---Helper function to check if a node is in quickfix list
---@param node nvim_tree.api.Node
---@return boolean
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

---Return quickfix icons for the node
---@param node nvim_tree.api.Node
---@return nvim_tree.api.HighlightedString[]? icons
function DecoratorQuickfix:icons(node)
  if is_qf_item(node) then
    return { self.qf_icon }
  end
  return nil
end

---Return highlight group for the node
---@param node nvim_tree.api.Node
---@return string? highlight_group
function DecoratorQuickfix:highlight_group(node)
  if is_qf_item(node) then
    return 'QuickFixLine'
  end
  return nil
end

return DecoratorQuickfix

@alex-courtis
Copy link
Member Author

LuaLS type checker warnings when defining my decorator:

Many thanks! It seems that luals needs the actual class table to exist. Hopefully that resolves the warnings.

I'll dogfood this for a week before releasing a new minor version.

I like QF; I'll be using that one. I've created a Decorators wiki page and I'd be most grateful if you shared it there.

@alex-courtis alex-courtis changed the title feat(#2948): add user decorators feat(#2948): user decorators Dec 2, 2024
@b0o
Copy link

b0o commented Dec 2, 2024

Types are working now, and I added to the wiki!

@alex-courtis
Copy link
Member Author

Types are working now, and I added to the wiki!

Lovely, thank you!

I added your name back if that's OK - everyone gets credited in the wiki...

@alex-courtis alex-courtis changed the title feat(#2948): user decorators feat(#2948): add custom decorators :help nvim-tree-decorators Dec 7, 2024
@alex-courtis alex-courtis changed the title feat(#2948): add custom decorators :help nvim-tree-decorators feat(#2948): add custom decorators, :help nvim-tree-decorators Dec 7, 2024
@alex-courtis alex-courtis merged commit 7a4ff1a into master Dec 7, 2024
9 checks passed
@alex-courtis alex-courtis deleted the 2948-add-user-decorators branch December 7, 2024 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] User Decorators Set folder icon depending on it's name make the filename color like icon's color.
2 participants