sindrets / diffview.nvim

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

[Feature Request] Open a file with `xdg-open` #456

Open stevenxxiu opened 5 months ago

stevenxxiu commented 5 months ago

I'm seeking to use Neovim as my Git UI.

In the log view, gf opens the file in Neovim. But I'd like to have another keybind to open files that aren't supported by Neovim, say image files.

I copied some code from https://github.com/sindrets/diffview.nvim/blob/3dc498c9777fe79156f3d32dddd483b8b3dbd95f/lua/diffview/actions.lua#L37 to cobble something together that works.

In the following I use go to open a file with xdg-open. However as you can see there's a lot of copy paste:

local function goto_file_open()
  local lazy = require('diffview.lazy')
  local lib = lazy.require('diffview.lib')
  local utils = lazy.require('diffview.utils')
  local DiffView = lazy.access('diffview.scene.views.diff.diff_view', 'DiffView')
  local FileHistoryView = lazy.access('diffview.scene.views.file_history.file_history_view', 'FileHistoryView')

  local pl = lazy.access(utils, 'path')

  local view = lib.get_current_view()

  if view and not (view:instanceof(DiffView.__get()) or view:instanceof(FileHistoryView.__get())) then
    return
  end

  local file = view:infer_cur_file()
  if file then
    -- Ensure file exists
    if not pl:readable(file.absolute_path) then
      utils.err(string.format("File does not exist on disk: '%s'", pl:relative(file.absolute_path, '.')))
      return
    end
    vim.fn.jobstart({ 'xdg-open', file.absolute_path })
  end
end

return {
  'sindrets/diffview.nvim',
  event = 'VeryLazy',
  opts = {
    enhanced_diff_hl = true,
    keymaps = {
      view = {
        { 'n', 'go', goto_file_open },
      },
      file_panel = {
        { 'n', 'go', goto_file_open },
      },
      file_history_panel = {
        { 'n', 'go', goto_file_open },
      },
    },
  },
  ...
}

Could you expose prepare_goto_file()? And add this too if this seems good.