nvim-tree.lua/lua/nvim-tree/actions/node/system-open.lua
Azad 13f967f8e7
chore: add type annotations and resolve LSP warnings (#2555)
* chore: add type annotations to (almost) all functions

* stylua

* Add classes for symlink nodes

* Replace deprecated `@vararg`

* Move node classes to `node` module

* Fix `Symlink*` classes

* add vim and libuv runtime for luals, qualify libuv types

* add scripts/luals-check, not quite ready for CI

* additional nil checks for git/init.lua and git/runner.lua

* additional nil checks for nvim-tree.lua

* wrap vim.cmd-as-a-function calls inside functions

* vim.tbl_filter predicate returns booleans

* Revert "add scripts/luals-check, not quite ready for CI"

This reverts commit c70229cad9.

* Add `MinimalNode` class in `marks` module

* Fix various LSP warnings

* stylua

* Fix `Explorer` class, update related annotations and add necessary checks

* Add missing annotations to `live-filter`

* Add temporary aliases for `uv.*` types

* Resolve remaining LSP warnings

* Revert changes not related to internal types

* Minor adjustments

* Update doc comments style

* Minor adjustments (pt. 2)

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
2023-12-09 11:34:35 +11:00

71 lines
1.7 KiB
Lua

local notify = require "nvim-tree.notify"
local utils = require "nvim-tree.utils"
local M = {}
---@param node Node
function M.fn(node)
if #M.config.system_open.cmd == 0 then
require("nvim-tree.utils").notify.warn "Cannot open file with system application. Unrecognized platform."
return
end
local process = {
cmd = M.config.system_open.cmd,
args = M.config.system_open.args,
errors = "\n",
stderr = vim.loop.new_pipe(false),
}
table.insert(process.args, node.link_to or node.absolute_path)
local opts = {
args = process.args,
stdio = { nil, nil, process.stderr },
detached = true,
}
process.handle, process.pid = vim.loop.spawn(process.cmd, opts, function(code)
process.stderr:read_stop()
process.stderr:close()
process.handle:close()
if code ~= 0 then
notify.warn(string.format("system_open failed with return code %d: %s", code, process.errors))
end
end)
table.remove(process.args)
if not process.handle then
notify.warn(string.format("system_open failed to spawn command '%s': %s", process.cmd, process.pid))
return
end
vim.loop.read_start(process.stderr, function(err, data)
if err then
return
end
if data then
process.errors = process.errors .. data
end
end)
vim.loop.unref(process.handle)
end
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"
end
end
end
return M