Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stevenxxiu opened this issue Jan 14, 2024 · 0 comments
Open

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

stevenxxiu opened this issue Jan 14, 2024 · 0 comments

Comments

@stevenxxiu
Copy link

stevenxxiu commented Jan 14, 2024

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.

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

No branches or pull requests

1 participant