sindrets / diffview.nvim

Single tabpage interface for easily cycling through diffs for all modified files for any git rev.
Other
3.6k stars 101 forks source link

Extension points for diffview #389

Closed dsych closed 1 year ago

dsych commented 1 year ago

Hey! diffview is awesome at what it does and I would like to leverage it as part of a plugin I am writing instead of re-inventing the wheel. What I am looking for is some way to hook into diffview to be able to enhance the current layout with additional keymaps, signs virtual lines etc.

I tried creating an autocmd for diffview:// file name, but it proved insufficient for me, since the path contains references to .git repo and other git related stuff, which I don't really want to parse out. I need to file name because of the way I designed the data structure containing additional information i.e. there is a mapping from file path to some arbitrary data, so that every time a new file diff is loaded, i could efficiently look up the related data and use it to enhance the view.

Here is how I see it playing out:

  1. Register a callback for a new diff view getting opened. Either view diffview api or through BufEnter
  2. The callback receives a reference to the current View object that contains the system file path to the file current displayed in the view.
  3. I use the fs file path to cross-reference it with my own data structure to retrieve related info and use that info to add more mappings, signs, virtual lines etc.

I found https://github.com/sindrets/diffview.nvim/issues/329, which talks about require("diffview.lib").get_current_view() that looks like what I need. However, I wasn't able to figure out how to get the fs filepath out of it.

Any pointers are appreciated.

dsych commented 1 year ago

Just found require("diffview.lib").get_current_view():infer_cur_file().path, which is probably the answer. But do let me know if there is a better way

sindrets commented 1 year ago

Thank you for taking an interest in the project.

Just found require("diffview.lib").get_current_view():infer_cur_file().path, which is probably the answer. But do let me know if there is a better way

Not quite. infer_cur_file() is contextual, and will infer the file according to the rules described under :h diffview-file-inference.

What you want is probably:

require("diffview.lib").get_current_view().cur_entry.absolute_path