nvim-tree.lua/lua/nvim-tree/actions/finders/search-node.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

97 lines
2.2 KiB
Lua

local core = require "nvim-tree.core"
local filters = require "nvim-tree.explorer.filters"
local find_file = require("nvim-tree.actions.finders.find-file").fn
local M = {}
---@param search_dir string|nil
---@param input_path string
---@return string|nil
local function search(search_dir, input_path)
local realpaths_searched = {}
if not search_dir then
return
end
---@param dir string
---@return string|nil
local function iter(dir)
local realpath, path, name, stat, handle, _
local filter_status = filters.prepare()
handle, _ = vim.loop.fs_scandir(dir)
if not handle then
return
end
realpath, _ = vim.loop.fs_realpath(dir)
if not realpath or vim.tbl_contains(realpaths_searched, realpath) then
return
end
table.insert(realpaths_searched, realpath)
name, _ = vim.loop.fs_scandir_next(handle)
while name do
path = dir .. "/" .. name
stat, _ = vim.loop.fs_stat(path)
if not stat then
break
end
if not filters.should_filter(path, filter_status) then
if string.find(path, "/" .. input_path .. "$") then
return path
end
if stat.type == "directory" then
path = iter(path)
if path then
return path
end
end
end
name, _ = vim.loop.fs_scandir_next(handle)
end
end
return iter(search_dir)
end
function M.fn()
if not core.get_explorer() then
return
end
-- temporarily set &path
local bufnr = vim.api.nvim_get_current_buf()
local path_existed, path_opt = pcall(vim.api.nvim_buf_get_option, bufnr, "path")
vim.api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path)
if not input_path or input_path == "" then
return
end
-- reset &path
if path_existed then
vim.api.nvim_buf_set_option(bufnr, "path", path_opt)
else
vim.api.nvim_buf_set_option(bufnr, "path", nil)
end
-- strip trailing slash
input_path = string.gsub(input_path, "/$", "")
-- search under cwd
local found = search(core.get_cwd(), input_path)
if found then
find_file(found)
end
end)
end
return M