Skip to content

feat(view): expose revision and file context to diff buffer hooks - #373

Open
tummetott wants to merge 2 commits into
dlyongemallo:mainfrom
tummetott:feat/diff-buf-ctx
Open

tummetott wants to merge 2 commits into
dlyongemallo:mainfrom
tummetott:feat/diff-buf-ctx

Conversation

@tummetott

Copy link
Copy Markdown

The diff_buf_read and diff_buf_win_enter hooks only report the window's layout position (symbol, layout_name).

This PR extends the hook context with:

  • rev: type ("LOCAL", "COMMIT", "STAGE" or "CUSTOM"), commit, stage and tracks_head
  • path: the file path relative to toplevel
  • toplevel: the repository root
  • adapter: "git", "hg", "jj" or "p4" (nil outside a repository)

The DiffviewDiffBufRead and DiffviewDiffBufWinEnter User autocmds carry the same fields in data, together with bufnr and winid. The existing fields are unchanged.

This is especially useful for winbar and statusline plugins, which can now label each diff window with the revision it shows (LOCAL, INDEX, HEAD, a commit hash) directly from the hook:

hooks = {
  diff_buf_win_enter = function(bufnr, winid, ctx)
    vim.w[winid].diffview_ctx = ctx
  end,
}

A statusline component then reads vim.w.diffview_ctx when it draws.

The `diff_buf_read` and `diff_buf_win_enter` hooks only reported the
window's layout position, so consumers such as statusline and winbar
plugins had to walk Diffview internals to learn which revision a buffer
shows.

Extend the hook context with the buffer's revision (`type`, `commit`,
`stage`, `tracks_head`), its repo-relative `path`, the repository
`toplevel` and the `adapter` name. The `DiffviewDiffBufRead` and
`DiffviewDiffBufWinEnter` User autocmds carry the same fields in
`data`, together with `bufnr` and `winid`.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Hooks and User autocmds expose absolute paths where paths relative to toplevel are documented, leaving the exported path contract inconsistent for arbitrary-file and directory-diff views.

1 open issue
What changed in this PR

This PR exposes revision and file context to diff-buffer hooks and User autocmds, allowing winbar and statusline integrations to identify what each diff window displays.

Changes:

  • Adds revision, path, repository root, and adapter fields to the shared hook context.
  • Includes context and buffer/window identifiers in User autocmd data.
  • Documents the context and adds tests for commit and index-stage revisions.
File Description
lua/​diffview/​tests/​functional/​window_spec.lua Tests commit and index-stage context fields.
lua/​diffview/​scene/​window.lua Builds shared diff-buffer context for hooks.
lua/​diffview/​init.lua Adds context data to User autocmds.
lua/​diffview/​config.lua Defines types for the expanded hook context.
doc/​diffview.txt Documents context fields and autocmd payloads.

Review effort: Balanced

Comment thread lua/diffview/scene/window.lua Resolved
Views without a VCS use absolute file paths, and their files can lie
outside any single directory, so there is no root that `path` is
relative to. Report `toplevel` as nil there, alongside `adapter`, and
document that `path` is absolute in that case.
@tummetott

Copy link
Copy Markdown
Author

Can this be merged?

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.

2 participants