Skip to content

Commit f9aa7c6

Browse files
committed
feat(files): add find_by_full_path_words option to filter action
1 parent 498aae6 commit f9aa7c6

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

lua/neo-tree/defaults.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ local config = {
123123
["q"] = "close_window",
124124
},
125125
},
126+
find_by_full_path_words = false, -- `false` means it only searches the tail of a path.
127+
-- `true` will change the filter into a full path
128+
-- search with space as an implicit ".*", so
129+
-- `fi init`
130+
-- will match: `./sources/filesystem/init.lua
126131
--find_command = "fd",
127132
--find_args = { -- you can specify extra args to pass to the find command.
128133
-- "--exclude", ".git",

lua/neo-tree/sources/filesystem/lib/filter_external.lua

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local vim = vim
22
local log = require("neo-tree.log")
33
local Job = require("plenary.job")
4+
local utils = require("neo-tree.utils")
45

56
local M = {}
67
local fd_supports_max_results = nil
@@ -47,10 +48,21 @@ M.find_files = function(opts)
4748
local limit = opts.limit or 200
4849
local cmd = get_find_command(opts)
4950
local path = opts.path
50-
local term = opts.term
51+
local full_path_words = opts.find_by_full_path_words
52+
local regex, glob = opts.term, opts.term
5153

52-
if term ~= "*" and not term:find("*") then
53-
term = "*" .. term .. "*"
54+
if full_path_words then
55+
local words = utils.split(glob, " ")
56+
regex = ".*" .. table.concat(words, ".*") .. ".*"
57+
else
58+
if glob ~= "*" then
59+
if glob:sub(1) ~= "*" then
60+
glob = "*" .. glob
61+
end
62+
if glob:sub(-1) ~= "*" then
63+
glob = glob .. "*"
64+
end
65+
end
5466
end
5567

5668
local args = {}
@@ -67,7 +79,12 @@ M.find_files = function(opts)
6779
if not filters.respect_gitignore then
6880
append("--no-ignore")
6981
end
70-
append("--glob", term, path)
82+
if full_path_words then
83+
append("--full-path", regex)
84+
else
85+
append("--glob", glob)
86+
end
87+
append(path)
7188
append("--color", "never")
7289
if fd_supports_max_results then
7390
append("--max-results", limit)
@@ -78,9 +95,17 @@ M.find_files = function(opts)
7895
if not filters.show_hidden then
7996
append("-not", "-path", "*/.*")
8097
end
81-
append("-iname", term)
98+
if full_path_words then
99+
append("-regextype", "sed", "-regex", regex)
100+
else
101+
append("-iname", glob)
102+
end
103+
elseif cmd == "fzf" then
104+
-- This does not work yet, there's some kind of issue with how fzf uses stdout
105+
error("fzf is not a supported find_command")
106+
append("--no-sort", "--no-expect", "--filter", opts.term) -- using the raw term without glob patterns
82107
elseif cmd == "where" then
83-
append("/r", path, term)
108+
append("/r", path, glob)
84109
else
85110
return { "No search command found!" }
86111
end
@@ -91,13 +116,15 @@ M.find_files = function(opts)
91116
elseif type(opts.find_args) == "table" then
92117
append(unpack(opts.find_args))
93118
elseif type(opts.find_args) == "function" then
94-
args = opts.find_args(cmd, path, term, args)
119+
args = opts.find_args(cmd, path, glob, args)
95120
end
96121
end
97122

123+
log.info(cmd, args)
98124
Job
99125
:new({
100126
command = cmd,
127+
cwd = path,
101128
args = args,
102129
enable_recording = false,
103130
maximum_results = limit or 100,
@@ -114,7 +141,7 @@ M.find_files = function(opts)
114141
opts.on_insert(err, line)
115142
end
116143
end,
117-
on_exit = function(j, return_val)
144+
on_exit = function(_, return_val)
118145
if opts.on_exit then
119146
opts.on_exit(return_val)
120147
end

lua/neo-tree/sources/filesystem/lib/fs_scan.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ M.get_items_async = function(state, parent_id, path_to_reveal, callback)
112112
path = root.path,
113113
term = state.search_pattern,
114114
find_args = state.find_args,
115+
--find_by_full_path_words = state.find_by_full_path_words,
115116
on_insert = function(err, path)
116117
if err and #err > 0 then
117118
log.error(err, path)

0 commit comments

Comments
 (0)