Skip to content

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

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

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading