Skip to content

Commit

Permalink
feat(#2430): use vim.ui.open as default system_open, for neovim 0.10+ (
Browse files Browse the repository at this point in the history
…#2912)

* feat(#2430): use vim.ui.open as default system_open, for neovim 0.10+

* feat(#2430): use vim.ui.open as default system_open, for neovim 0.10+
  • Loading branch information
alex-courtis authored Sep 14, 2024
1 parent a4dd5ad commit 03f737e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
9 changes: 8 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1198,11 +1198,18 @@ Takes the `BufEnter` event as an argument. see |autocmd-events|

Open a file or directory in your preferred application.

|vim.ui.open| was introduced in neovim 0.10 and is the default.

Once nvim-tree minimum neovim version is updated to 0.10, these options will
no longer be necessary and will be removed.

*nvim-tree.system_open.cmd*
The open command itself.
Type: `string`, Default: `""`

Leave empty for OS specific default:
neovim >= 0.10 defaults to |vim.ui.open|

neovim < 0.10 defaults to:
UNIX: `"xdg-open"`
macOS: `"open"`
Windows: `"cmd"`
Expand Down
43 changes: 32 additions & 11 deletions lua/nvim-tree/actions/node/system-open.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local utils = require "nvim-tree.utils"
local M = {}

---@param node Node
function M.fn(node)
local function user(node)
if #M.config.system_open.cmd == 0 then
require("nvim-tree.utils").notify.warn "Cannot open file with system application. Unrecognized platform."
return
Expand Down Expand Up @@ -49,20 +49,41 @@ function M.fn(node)
vim.loop.unref(process.handle)
end

---@param node Node
local function native(node)
local _, err = vim.ui.open(node.link_to or node.absolute_path)

-- err only provided on opener executable not found hence logging path is not useful
if err then
notify.warn(err)
end
end

---@param node Node
function M.fn(node)
M.open(node)
end

-- TODO always use native once 0.10 is the minimum neovim version
function M.setup(opts)
M.config = {}
M.config.system_open = opts.system_open or {}

if #M.config.system_open.cmd == 0 then
if utils.is_windows then
M.config.system_open = {
cmd = "cmd",
args = { "/c", "start", '""' },
}
elseif utils.is_macos then
M.config.system_open.cmd = "open"
elseif utils.is_unix then
M.config.system_open.cmd = "xdg-open"
if vim.fn.has "nvim-0.10" == 1 and #M.config.system_open.cmd == 0 then
M.open = native
else
M.open = user
if #M.config.system_open.cmd == 0 then
if utils.is_windows then
M.config.system_open = {
cmd = "cmd",
args = { "/c", "start", '""' },
}
elseif utils.is_macos then
M.config.system_open.cmd = "open"
elseif utils.is_unix then
M.config.system_open.cmd = "xdg-open"
end
end
end
end
Expand Down

0 comments on commit 03f737e

Please sign in to comment.