nvim-tree.lua icon indicating copy to clipboard operation
nvim-tree.lua copied to clipboard

Tap into nvim-tree's git status

Open cevhyruz opened this issue 2 years ago • 3 comments

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.

cevhyruz avatar Aug 21 '22 07:08 cevhyruz

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.

alex-courtis avatar Aug 22 '22 01:08 alex-courtis

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)

cevhyruz avatar Aug 22 '22 05:08 cevhyruz

Im having a bit of error here explorer being nil.

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

alex-courtis avatar Aug 22 '22 06:08 alex-courtis

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.

hermitmaster avatar Nov 18 '22 16:11 hermitmaster

~~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

alex-courtis avatar Nov 19 '22 02:11 alex-courtis

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.

alex-courtis avatar Nov 19 '22 02:11 alex-courtis

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.

alex-courtis avatar Nov 19 '22 04:11 alex-courtis