plugins projectsearch: expose its functionality (#1235)

This commit is contained in:
Jefferson González 2022-12-14 11:53:20 -04:00 committed by GitHub
parent 9d48441685
commit e152db2cce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 12 deletions

View File

@ -6,7 +6,7 @@ local command = require "core.command"
local style = require "core.style" local style = require "core.style"
local View = require "core.view" local View = require "core.view"
---@class plugins.projectsearch.resultsview : core.view
local ResultsView = View:extend() local ResultsView = View:extend()
ResultsView.context = "session" ResultsView.context = "session"
@ -219,6 +219,10 @@ function ResultsView:draw()
end end
---@param path string
---@param text string
---@param fn fun(line_text:string):...
---@return plugins.projectsearch.resultsview?
local function begin_search(path, 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")
@ -226,6 +230,7 @@ local function begin_search(path, text, fn)
end end
local rv = ResultsView(path, 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)
return rv
end end
@ -249,6 +254,59 @@ local function normalize_path(path)
return path return path
end end
---@class plugins.projectsearch
local projectsearch = {}
---@type plugins.projectsearch.resultsview
projectsearch.ResultsView = ResultsView
---@param text string
---@param path string
---@param insensitive? boolean
---@return plugins.projectsearch.resultsview?
function projectsearch.search_plain(text, path, insensitive)
if insensitive then text = text:lower() end
return begin_search(path, text, function(line_text)
if insensitive then
return line_text:lower():find(text, nil, true)
else
return line_text:find(text, nil, true)
end
end)
end
---@param text string
---@param path string
---@param insensitive? boolean
---@return plugins.projectsearch.resultsview?
function projectsearch.search_regex(text, path, insensitive)
local re, errmsg
if insensitive then
re, errmsg = regex.compile(text, "i")
else
re, errmsg = regex.compile(text)
end
if not re then core.log("%s", errmsg) return end
return begin_search(path, text, function(line_text)
return regex.cmatch(re, line_text)
end)
end
---@param text string
---@param path string
---@param insensitive? boolean
---@return plugins.projectsearch.resultsview?
function projectsearch.search_fuzzy(text, path, insensitive)
if insensitive then text = text:lower() end
return begin_search(path, text, function(line_text)
if insensitive then
return common.fuzzy_match(line_text:lower(), text) and 1
else
return common.fuzzy_match(line_text, text) and 1
end
end)
end
command.add(nil, { command.add(nil, {
["project-search:find"] = function(path) ["project-search:find"] = function(path)
@ -256,10 +314,7 @@ command.add(nil, {
text = get_selected_text(), text = get_selected_text(),
select_text = true, select_text = true,
submit = function(text) submit = function(text)
text = text:lower() projectsearch.search_plain(text, path, true)
begin_search(path, text, function(line_text)
return line_text:lower():find(text, nil, true)
end)
end end
}) })
end, end,
@ -267,10 +322,7 @@ command.add(nil, {
["project-search:find-regex"] = function(path) ["project-search:find-regex"] = function(path)
core.command_view:enter("Find Regex In " .. (normalize_path(path) or "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") projectsearch.search_regex(text, path, true)
begin_search(path, text, function(line_text)
return regex.cmatch(re, line_text)
end)
end end
}) })
end, end,
@ -280,9 +332,7 @@ command.add(nil, {
text = get_selected_text(), text = get_selected_text(),
select_text = true, select_text = true,
submit = function(text) submit = function(text)
begin_search(path, text, function(line_text) projectsearch.search_fuzzy(text, path, true)
return common.fuzzy_match(line_text, text) and 1
end)
end end
}) })
end, end,
@ -344,3 +394,6 @@ keymap.add {
["home"] = "project-search:move-to-start-of-doc", ["home"] = "project-search:move-to-start-of-doc",
["end"] = "project-search:move-to-end-of-doc" ["end"] = "project-search:move-to-end-of-doc"
} }
return projectsearch