Skip to content

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

@stevenxxiu

Description

@stevenxxiu

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

---@return FileEntry?
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions