diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 2297d82685d..1791e9f33c9 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -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"` diff --git a/lua/nvim-tree/actions/node/system-open.lua b/lua/nvim-tree/actions/node/system-open.lua index fa3a9d846dd..6393f1f9267 100644 --- a/lua/nvim-tree/actions/node/system-open.lua +++ b/lua/nvim-tree/actions/node/system-open.lua @@ -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 @@ -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