sindrets / diffview.nvim

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

[Question] How to open entry of the file panel via lua/command? #464

Closed Otterpatsch closed 4 months ago

Otterpatsch commented 7 months ago

Hello there, First of all i very much like your plugin. So much that im trying to use it within the plugin im trying to create (bitbucket.nvim basically just to review PRs of bitbuckets repos).

So i want to jump to a file of the file panel given its relative(or absolut if needed) file path image

For example i would like to jump to the file to which a comment is connected image

I tried to dig into your code but as im just picked up lua like 4 days ago im struggle a bit. select_entry = function() So how to i "fake" a item for the linked function? Or is there maybe another function which does that?

jakubbortlik commented 5 months ago

Hi @Otterpatsch, not sure if this is still relevant, but you can take inspiration from how gitlab.nvim does this: You can focus the file panel and then use the diffview.lib.get_current_view() to get the current view and then you should be able to view:set_file(file).

sindrets commented 4 months ago

You would be reaching into the internal API for this, which could change at any point. But I believe you could do this (untested):

local DiffView = require("diffview.scene.views.diff.DiffView").DiffView
local async = require("diffview.async")

async.void(function()
  local view = require("diffview.lib").get_current_view()

  if view and DiffView:ancestorof(view) then
    --- @cast view DiffView
    async.await(view:set_file_by_path("some/file/path"))
  end
end)()

The file path here would be relative to the repository top-level. That can be found from view.adapter.ctx.toplevel.