nvim-tree.lua
nvim-tree.lua copied to clipboard
Tap into nvim-tree's git status
Hi! I'm wondering if it is currently possible to tap into the file tree's git status?
What I want to do is colorize the filename in my statusline/winbar with a highlight group from git status of nvim-tree so the filename will be colored depending on the status of the specific file.
I figure I can do my own, but I think there would be less friction if I can just tap into the existing nvim-tree git integration.
We don't have official API for that, however you can retrieve all the nodes in the tree. Something like:
local explorer = require("nvim-tree.core").get_explorer()
for _, node in pairs(explorer.nodes) do
log.line("dev", "%s", vim.inspect(node))
end
Caveat: this is not API and the contents of nodes may change.
Im having a bit of error here explorer
being nil
.
Caveat: this is not API and the contents of nodes may change.
Will there plans to support an official API for this? Something like
local hl_group, symbol = get_file_git_status(filename)
Im having a bit of error here
explorer
beingnil
.
You'll need to open the tree first.
Will there plans to support an official API for this? Something like
Currently the API is focused on control rather than information.
Events provide information although not the information you need. I suspect an event may be a good fit.
Perhaps you could draft an event that suits your use case: :help nvim-tree-events
I'm looking for a similar feature as well. I'm working on a plugin for lualine and wanted to put the git status summary in the statusline. There are alternative solutions, I just though this would be a good option.
~~You can reach in and get the all the nodes:~~
~~local log = require "nvim-tree.log"~~ ~~local core = require "nvim-tree.core"~~ ~~log.line("dev", "all nodes %s", vim.inspect(core.get_explorer().nodes))~~
~~git_status
contains the first two characters from git --no-optional-locks status --porcelain=v1 --ignored=matching -u
~~
~~Usual disclaimer: this is not API and subject to change at any time.~~
Edit: added api.tree.get_nodes
Events are useful, however it does seem that the ability to fetch all nodes via API would be useful.
Idea: API to retrieve a sanitised / cloned map of all nodes, similar to nvim-tree.sort_by
function.
API added.
Please update to latest and try out:
vim.inspect(require("nvim-tree.api").tree.get_nodes())
This isn't set in stone; we can add to this.