Skip to content

#837 search subdirs #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 50 additions & 70 deletions lua/nvim-tree/actions/search-node.lua
Original file line number Diff line number Diff line change
@@ -1,95 +1,75 @@
local api = vim.api
local uv = vim.loop
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local find_file = require("nvim-tree.actions.find-file").fn

local M = {}

function M.fn()
if not core.get_explorer() then
local function search(dir, input_path)
local path, name, stat, handle, _

if not dir then
return
end

local input_path = vim.fn.input("Search node: ", "", "file")
utils.clear_prompt()

local absolute_input_path = utils.path_join {
core.get_cwd(),
input_path,
}

local function count_visible_nodes(nodes)
local visible_nodes = 0
for _, node in ipairs(nodes) do
visible_nodes = visible_nodes + 1

if node.open and node.nodes then
visible_nodes = visible_nodes + count_visible_nodes(node.nodes)
end
end

return visible_nodes
handle, _ = uv.fs_scandir(dir)
if not handle then
return
end

local tree_altered = false
local found_something = false

local function search_node(nodes)
local index = 0
name, _ = uv.fs_scandir_next(handle)
while name do
path = dir .. "/" .. name

for _, node in ipairs(nodes) do
index = index + 1

if absolute_input_path == node.absolute_path then
found_something = true

if node.nodes and not node.open then
node.open = true
core.get_explorer():expand(node)
tree_altered = true
end

return index
end

if node.nodes then
-- e.g. user searches for "/foo/bar.txt", than directory "/foo/bar" should not match with filename
local matches = utils.str_find(absolute_input_path, node.absolute_path .. "/")

if matches then
found_something = true

-- if node is not open -> open it
if not node.open then
node.open = true
core.get_explorer():expand(node)
tree_altered = true
end
stat, _ = uv.fs_stat(path)
if not stat then
break
end

return index + search_node(node.nodes)
end
end
if string.find(path, "/" .. input_path .. "$") then
return path
end

if node.open then
index = index + count_visible_nodes(node.nodes)
if stat.type == "directory" then
path = search(path, input_path)
if path then
return path
end
end

return index
name, _ = uv.fs_scandir_next(handle)
end
end

local index = search_node(core.get_explorer().nodes)
function M.fn()
if not core.get_explorer() then
return
end

-- temporarily set &path
local bufnr = api.nvim_get_current_buf()
local path_existed, path_opt = pcall(api.nvim_buf_get_option, bufnr, "path")
api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")

if tree_altered then
renderer.draw()
-- completes files/dirs under cwd
local input_path = vim.fn.input("Search: ", "", "file_in_path")
utils.clear_prompt()

-- reset &path
if path_existed then
api.nvim_buf_set_option(bufnr, "path", path_opt)
else
api.nvim_buf_set_option(bufnr, "path", nil)
end

if found_something and view.is_visible() then
if view.is_root_folder_visible() then
index = index + 1
end
-- strip trailing slash
input_path = string.gsub(input_path, "/$", "")

view.set_cursor { index, 0 }
-- search under cwd
local found = search(core.get_cwd(), input_path)
if found then
find_file(found)
end
end

Expand Down