Added in simple directory search to treeview. (#1110)

This commit is contained in:
Adam 2022-09-14 00:14:13 -04:00 committed by GitHub
parent a19cfb4f80
commit 10d810b7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 19 deletions

View File

@ -11,11 +11,11 @@ local ResultsView = View:extend()
ResultsView.context = "session" ResultsView.context = "session"
function ResultsView:new(text, fn) function ResultsView:new(path, text, fn)
ResultsView.super.new(self) ResultsView.super.new(self)
self.scrollable = true self.scrollable = true
self.brightness = 0 self.brightness = 0
self:begin_search(text, fn) self:begin_search(path, text, fn)
end end
@ -45,8 +45,8 @@ local function find_all_matches_in_file(t, filename, fn)
end end
function ResultsView:begin_search(text, fn) function ResultsView:begin_search(path, text, fn)
self.search_args = { text, fn } self.search_args = { path, text, fn }
self.results = {} self.results = {}
self.last_file_idx = 1 self.last_file_idx = 1
self.query = text self.query = text
@ -56,9 +56,9 @@ function ResultsView:begin_search(text, fn)
core.add_thread(function() core.add_thread(function()
local i = 1 local i = 1
for dir_name, file in core.get_project_files() do for dir_name, file in core.get_project_files() do
if file.type == "file" then if file.type == "file" and (not path or (dir_name .. "/" .. file.filename):find(path, 1, true) == 1) then
local path = (dir_name == core.project_dir and "" or (dir_name .. PATHSEP)) local truncated_path = (dir_name == core.project_dir and "" or (dir_name .. PATHSEP))
find_all_matches_in_file(self.results, path .. file.filename, fn) find_all_matches_in_file(self.results, truncated_path .. file.filename, fn)
end end
self.last_file_idx = i self.last_file_idx = i
i = i + 1 i = i + 1
@ -219,12 +219,12 @@ function ResultsView:draw()
end end
local function begin_search(text, fn) local function begin_search(path, text, fn)
if text == "" then if text == "" then
core.error("Expected non-empty string") core.error("Expected non-empty string")
return return
end end
local rv = ResultsView(text, fn) local rv = ResultsView(path, text, fn)
core.root_view:get_active_node_default():add_view(rv) core.root_view:get_active_node_default():add_view(rv)
end end
@ -238,37 +238,49 @@ local function get_selected_text()
end end
local function normalize_path(path)
if not path then return nil end
path = common.normalize_path(path)
for i, project_dir in ipairs(core.project_directories) do
if common.path_belongs_to(path, project_dir.name) then
return project_dir.item.filename .. PATHSEP .. common.relative_path(project_dir.name, path)
end
end
return path
end
command.add(nil, { command.add(nil, {
["project-search:find"] = function() ["project-search:find"] = function(path)
core.command_view:enter("Find Text In Project", { core.command_view:enter("Find Text In " .. (normalize_path(path) or "Project"), {
text = get_selected_text(), text = get_selected_text(),
select_text = true, select_text = true,
submit = function(text) submit = function(text)
text = text:lower() text = text:lower()
begin_search(text, function(line_text) begin_search(path, text, function(line_text)
return line_text:lower():find(text, nil, true) return line_text:lower():find(text, nil, true)
end) end)
end end
}) })
end, end,
["project-search:find-regex"] = function() ["project-search:find-regex"] = function(path)
core.command_view:enter("Find Regex In Project", { core.command_view:enter("Find Regex In " .. (normalize_path(path) or "Project"), {
submit = function(text) submit = function(text)
local re = regex.compile(text, "i") local re = regex.compile(text, "i")
begin_search(text, function(line_text) begin_search(path, text, function(line_text)
return regex.cmatch(re, line_text) return regex.cmatch(re, line_text)
end) end)
end end
}) })
end, end,
["project-search:fuzzy-find"] = function() ["project-search:fuzzy-find"] = function(path)
core.command_view:enter("Fuzzy Find Text In Project", { core.command_view:enter("Fuzzy Find Text In " .. (normalize_path(path) or "Project"), {
text = get_selected_text(), text = get_selected_text(),
select_text = true, select_text = true,
submit = function(text) submit = function(text)
begin_search(text, function(line_text) begin_search(path, text, function(line_text)
return common.fuzzy_match(line_text, text) and 1 return common.fuzzy_match(line_text, text) and 1
end) end)
end end

View File

@ -467,7 +467,7 @@ view.node = node:split("left", view, {x = true}, true)
-- plugin module that plug itself in the active node but it is plugged here -- plugin module that plug itself in the active node but it is plugged here
-- in the treeview node. -- in the treeview node.
local toolbar_view = nil local toolbar_view = nil
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview") local toolbar_plugin, ToolbarView = pcall(require, "plugins.toolbarview")
if config.plugins.toolbarview ~= false and toolbar_plugin then if config.plugins.toolbarview ~= false and toolbar_plugin then
toolbar_view = ToolbarView() toolbar_view = ToolbarView()
view.node:split("down", toolbar_view, {y = true}) view.node:split("down", toolbar_view, {y = true})
@ -819,6 +819,22 @@ command.add(function()
end end
}) })
local projectsearch = pcall(require, "plugins.projectsearch")
if projectsearch then
menu:register(function()
return view.hovered_item and view.hovered_item.type == "dir"
end, {
{ text = "Find in directory", command = "treeview:search-in-directory" }
})
command.add(function()
return view.hovered_item and view.hovered_item.type == "dir"
end, {
["treeview:search-in-directory"] = function(item)
command.perform("project-search:find", view.hovered_item.abs_filename)
end
})
end
command.add(function() command.add(function()
local item = treeitem() local item = treeitem()
return item return item