nvim-tree.lua/lua/nvim-tree/actions/tree/find-file.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.5 KiB
Lua

local core = require "nvim-tree.core"
local lib = require "nvim-tree.lib"
local view = require "nvim-tree.view"
local finders_find_file = require "nvim-tree.actions.finders.find-file"
local M = {}
--- Find file or buffer
---@param opts ApiTreeFindFileOpts|nil|boolean legacy -> opts.buf
function M.fn(opts)
-- legacy arguments
if type(opts) == "string" then
opts = {
buf = opts,
}
end
opts = opts or {}
-- do nothing if closed and open not requested
if not opts.open and not core.get_explorer() then
return
end
local bufnr, path
-- (optional) buffer number and path
if type(opts.buf) == "nil" then
bufnr = vim.api.nvim_get_current_buf()
path = vim.api.nvim_buf_get_name(bufnr)
elseif type(opts.buf) == "number" then
if not vim.api.nvim_buf_is_valid(opts.buf) then
return
end
bufnr = tonumber(opts.buf)
path = vim.api.nvim_buf_get_name(bufnr)
elseif type(opts.buf) == "string" then
bufnr = nil
path = tostring(opts.buf)
else
return
end
if view.is_visible() then
-- focus
if opts.focus then
lib.set_target_win()
view.focus()
end
elseif opts.open then
-- open
lib.open { current_window = opts.current_window, winid = opts.winid }
if not opts.focus then
vim.cmd "noautocmd wincmd p"
end
end
-- update root
if opts.update_root or M.config.update_focused_file.update_root then
require("nvim-tree").change_root(path, bufnr)
end
-- find
finders_find_file.fn(path)
end
function M.setup(opts)
M.config = opts or {}
end
return M