diffview-plus.nvim icon indicating copy to clipboard operation
diffview-plus.nvim copied to clipboard

[Request] :DiffviewGraph

Open 3dyuval opened this issue 5 months ago • 13 comments

First of all I really appreciate the effort everyone has put into this. I was so relieved all bugs finally went away. So I was thinking - why not introduce new once? (😂)

Here's my idea. Neogit has a nice surface-level integration with Diffview, and seeing them together just hits my sweet spot. So much so I started wondering how to get them play nicely. I landed on: Neogit's graph view + Diffview's layouts = 🔥

For example, here are two Neovims side by side. On the left is Diffview's DiffviewFileHistory; on the right is :Neogit log (Kitty terminal).

Image

Considerations

I think this belongs in diffview-plus itself rather than tying the two plugins closer together. Diffview's state is straightforward, and most of the selection logic already exists in the file-history view.

  • cmd name? :DiffviewLog was my first choice but seeing it already exists. (debug log) we either add new command or a flag on it. Something like :DiffviewLogPretty is really a mouthful, no sure about it. For now lets leave it at :DiffviewGraph
  • Where the panel lives? The graph itself pinned either to the left, bottom (and or right, right?). Diffview already supports placing panels in any of those.
  • Selecting multiple commits: I'd want to select a range and have them highlight together; most of that already exists in the file-history panel.
  • No hard dependency on Neogit: the graph drawing would be included in diffview-plus directly, so users wouldn't need Neogit installed.

Architecture

  1. Saving/restoring the view across sessions - right now a "graph" view can't be stored, as its own type. So we need to add it as one.
  2. How the commit list maps to diffs - Diffview is built around "the files a commit changed" whereas a graph is about how commits connect. So the graph display would need to sit on top of Diffview's existing model rather than replace it.

What do you think? Love to hear your thoughts.

3dyuval avatar Jun 14 '26 13:06 3dyuval

Have you considered gitgraph.nvim? It also has diffview integration and multiple commit selection, without the heavy dependency of neogit.

I don't know if it's possible to save/restore the graph across sessions, but it should be possible to regenerate it using an autocmd on SessionLoadPost.

Implementing a "commit graph" feature is a serious endeavour for a problem which has already been solved in the Neovim ecosystem. I don't see the need to maintain yet another implementation.

dlyongemallo avatar Jun 14 '26 23:06 dlyongemallo

Nice. I didn't knew it existed. So there is a clean seperation. The dependency is reversed though in my mind. Diffview already does all heavy lifting with views, windows, querying, state... Gitgraph is a view.

Diffview could use it to render a stateful Graph panel. Users could opt-in with deps = "isakbm/gitgraph.nvim"

3dyuval avatar Jun 16 '26 10:06 3dyuval

That is still a huge maintenance burden. gitgraph.nvim has no releases or public API, which means that the change you're suggesting effectively ties the maintenance of diffview+ to an external repo's internal API. The project's own README says NOTE: this project is still very WIP and there is no help documentation aside from this README.md. To have the degree of integration that you want, gitgraph.nvim should at least have a public API contract which is promised to be stable.

Furthermore, doing what you ask will introduce a feature which is git-only, regressing the degree of feature-parity for multi-VCS support.

I also don't see why this plugin should tie itself to one way of integrating commit graphs with neovim, when there are others (like Neogit, or vim-flog).

What are you trying to do that you can't already do with scripting using diffview+'s public API, which integrating so closely with a commit graph plugin would buy you?

dlyongemallo avatar Jun 16 '26 11:06 dlyongemallo

Yes I agree, its a shot in the dark. Its a huge endevour.

I'm trying to push the limits of both plugins by writing a RECIPE-GitGraph.nvim.md. I'm curious how far a userspace config can go, some gaps are:

gitgraph.nvim doesn't has a stable public core api. Its exposing a draw, Nothing for working with items via callbacks. (Ideally, something like a snacks api.)

diffview-plus.nvim doesn't expose view_opened and file_open_post hooks. Ideally for having q forquit, <CR> for prev and next by emitting so global keymaps_context (per this naive recipe)

I will try to explore CDDiffView after discovering it now.

What I'm going after is replicating the experience I had with Webstorm. The VCS panel over there is unparallel to anything I've seen.

With diffview split system, I can jump between changes, conflicts, files, and its smooth again thanks to your hard work.

But given I'm a frontend dev that likes changing stuff just to see where they break, I figured, if I could hook into a some window I could replicate the Webstorm experience. This included things like filtering by author, by repo, by hash, and the graph changes instantly, everything is very clear and intuitive.

3dyuval avatar Jun 18 '26 14:06 3dyuval

diffview-plus.nvim doesn't expose view_opened

Yes, it does. See :h diffview-config-hooks and :h diffview-user-autocmds.

and file_open_post hooks. Ideally for having q forquit, <CR> for prev and next by emitting so global keymaps_context (per this naive recipe)

Those bindings are already the defaults and are configurable using the keymaps config. See :h diffview-config-keymaps. You can use the diff_buf_win_enter and diff_buf_read hooks for per-file q/<CR> setup.

dlyongemallo avatar Jun 18 '26 20:06 dlyongemallo

Thank you so much for this. I've done my digging, I'd like to hear your thoughts. What I'm trying to achieve is having some way of allowing plugins to take over a diffview split. Thats the easiest and most meaningful solution.

I've been working in front end for quite a while now and seen my share of interface design patterns. There's so much you can do with just a render callback. Its like getting given the keys but the tank is half empty.

A more meaningful affordance would be "Heres a view. Do what you wanna, just make sure you close the light on your way out..." Something like this, what do you think?

EXTENDING LAYOUTS                               *diffview-extending-layouts*

diffview-plus lets a plugin render its own content inside a view and have that view restored on session reload. diffview keeps the window geometry
(|diffview-config| winopts), the buffer, and the layout; you supply the
content and, optionally, a way to rebuild the view.

This is done by:
1. renderer that draws the content
2. registry entry that restores the view when a session is reloaded.
                                              *diffview.extend.register*
require("diffview.extend").register({spec})
        Register an `extended view`. Called once, from your plugin's `setup`.
        {spec} is a table; `kind` and `render` are required.

        Spec: ~
            {kind} (`string`)
                Required. The identity of the extended view.
           {extends} (`string`)
                Optional. The base view this extends, one of `file_history` or `diffview`.
            {render} (`fun(comp, entry, ctx)`)
                Required. Draw the content for {entry} onto the render
                component {comp}. Called on open and on every refresh.

            {restore} (`fun(lib, entry)`)
                Optional. Re-open the view when the session is restored, using
                the recorded state. When omitted, the view is not restored.

        Example: >lua
            require("diffview.extend").register({
              kind    = "graph_file_history",
              extends = "file_history",
              render  = function(comp, entry, ctx) ... end,
              restore = function(lib, entry) ... end,
            })
<

3dyuval avatar Jun 19 '26 15:06 3dyuval

This gives you the render (drawing) part of the integration, but what about the interactions such as keymaps and previewing/opening diffs from the graph? And the maintenance burden is probably even worse: it exposes RenderComponent and the session restore ViewEntry, which makes internal components public. Also, render and restore are not enough: you'd also need to have an API to capture the current state of the session as well.

I specifically don't want to freeze the session restore internal API and make it a public contract, as it's intended to be changed to use SessionWritePre when that's out on stable nvim (see #217).

And do you actually need to draw onto diffview+'s components? What you're asking for isn't just a feature, but a category change of this plugin from "a diff viewer" to "a plugin-hosting platform which happens to show diffs". But why not just use any one of the many plugins which already exist to render window content or manage layout, e.g., nui.nvim, snacks.nvim, edgy.nvim, etc., and any number of session persistence managers.

What you're asking for is out of scope for this plugin. Potentially, we could make changes to the diffview+ API to better interact with plugins which render content or manage layout, but diffview+ itself should not be turned into such a plugin.

dlyongemallo avatar Jun 20 '26 05:06 dlyongemallo

In your screenshot, you show a side-by-side using kitty terminal. In fact, why don't you just use kitty (or another terminal multiplexer like tmux) to drive the UI, instead of cramming everything inside neovim?

This has the additional advantage that you are not restricted to using neovim plugins for your commit graph or fancy git view, but allows for TUIs like tig, which already does graph drawing, filtering, and author/range browsing, etc., out of the box. You can wire up the two halves using neovim's RPC.

For example. launch the diffview+ instance with a socket (nvim --listen /tmp/diff.sock), then send commands from the graph pane (nvim --server /tmp/diff.sock --remote-send ":DiffviewOpen <hash><CR>"), using tig bindings or whatever.

It seems that you're stuck on methodology ("shove everything into neovim, and in particular into diffview") when your actual goal ("replicate the webstorm experience") is already possible using composition with existing tools.

dlyongemallo avatar Jun 21 '26 07:06 dlyongemallo

When zooming out, your perspective does makes a lot of sense. It already composed seamlessly with existing tools, fair. I'm not ruling out tig/RPC.

So its out of scope, especially with #217. The one thing I'd like to zoom into is composition inside Neovim. It's just isn't possible with the current view. It takes over the tabpage, and it's what pushed me toward render/restore rather than composition.

And you're right that render and restore aren't enough - it'd also need keymaps, previewing/opening diffs, and capturing session state, which means:

  • expose internal components,
  • couple session-restore to public api

A smaller, concrete, middleground version of these two extremes we're discussing witout internal API and no session would be something like

        --no-panel
                Opens a view without file panel (history panel for
                |:DiffviewFileHistory|). Subtractive only: the freed column is
                left as plain tabpage space for another window. Unlike the
                `toggle_files` keymap, it is set at open time and survives
                refresh and layout cycling.

3dyuval avatar Jun 21 '26 10:06 3dyuval

Can you just use file_panel = { show = false} (same for file_history_panel) in your config? (See :h diffview-config-file_panel and :h diffview-config-file_history_panel.)

Is the reason that you can't do this because you want to show the file/file history panels normally, but want a per-open override for when you start with a git commit graph UI?

dlyongemallo avatar Jun 21 '26 18:06 dlyongemallo

Unlike the toggle_files keymap, it is set at open time and survives refresh and layout cycling.

Wait, what did you mean by this? In what way does toggle_files not survive refresh and layout cycling?

dlyongemallo avatar Jun 22 '26 03:06 dlyongemallo

You can file_panel.show = false(globally). :DiffviewToggleFiles does survive refresh/layout cycling, I misspoke. Let me accurate; :DiffviewFileHistory is blocked for composition because of this cobmination:

  1. You can't disable panel per call e.g. :DiffviewFileHistory --no-file-panel
  2. Can't set global or per call file_history.tab_behavior = "current"
  3. Can't reuse previous state with :DiffviewToggle; https://github.com/dlyongemallo/diffview-plus.nvim/blob/de8bf7bc1b7382890de7b0623023e322c7a8ce7e/lua/diffview/actions.lua#L330

3dyuval avatar Jun 22 '26 15:06 3dyuval

  1. I've implemented the --no-panel flag in #254, which seems useful independent of your request.
  2. This is a request to redesign the entire lifecycle management model of the plugin.
  3. The inability to reuse the previous state is intentional and correct: if a user toggles from "open" to "close", the expected behaviour is that the view is closed, not that it is in some hidden state waiting to be restored.

It seems in any case that :DiffviewToggle is the wrong tool to reach for. If you go with the multiplexer path, the diff lives in its own pane anyway, and if you want it out of the way, you'd use the multiplexer's hide/zoom pane feature to deal with it. The neovim process continues to run, so there's no dispose, and there's nothing to reuse. Even in the scenario where there was a tab_behavior = "current" flag, the diff would be a persistent window in the same tabpage as the graph, and you'd want to show/hide that window, not destroy/restore it.

dlyongemallo avatar Jun 23 '26 13:06 dlyongemallo