nvim-tree.lua/lua/nvim-tree/explorer/init.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.6 KiB
Lua

local git = require "nvim-tree.git"
local watch = require "nvim-tree.explorer.watch"
local explorer_node = require "nvim-tree.explorer.node"
local M = {}
M.explore = require("nvim-tree.explorer.explore").explore
M.reload = require("nvim-tree.explorer.reload").reload
---@class Explorer
---@field absolute_path string
---@field nodes Node[]
---@field open boolean
local Explorer = {}
Explorer.__index = Explorer
---@param cwd string|nil
---@return Explorer
function Explorer.new(cwd)
cwd = vim.loop.fs_realpath(cwd or vim.loop.cwd())
---@class Explorer
local explorer = setmetatable({
absolute_path = cwd,
nodes = {},
open = true,
}, Explorer)
explorer.watcher = watch.create_watcher(explorer)
explorer:_load(explorer)
return explorer
end
---@private
---@param node Node
function Explorer:_load(node)
local cwd = node.link_to or node.absolute_path
local git_status = git.load_project_status(cwd)
M.explore(node, git_status)
end
---@param node Node
function Explorer:expand(node)
self:_load(node)
end
function Explorer:destroy()
local function iterate(node)
explorer_node.node_destroy(node)
if node.nodes then
for _, child in pairs(node.nodes) do
iterate(child)
end
end
end
iterate(self)
end
function M.setup(opts)
require("nvim-tree.explorer.node").setup(opts)
require("nvim-tree.explorer.explore").setup(opts)
require("nvim-tree.explorer.filters").setup(opts)
require("nvim-tree.explorer.sorters").setup(opts)
require("nvim-tree.explorer.reload").setup(opts)
require("nvim-tree.explorer.watch").setup(opts)
end
M.Explorer = Explorer
return M