Replaced pattern-based commands with regexes.

This commit is contained in:
Adam Harrison 2021-05-04 18:31:46 -04:00
parent b3f9faff4a
commit c0c6bc5be9
5 changed files with 46 additions and 30 deletions

View File

@ -90,6 +90,7 @@ local function has_selection()
and core.active_view.doc:has_selection() and core.active_view.doc:has_selection()
end end
command.add(has_selection, { command.add(has_selection, {
["find-replace:select-next"] = function() ["find-replace:select-next"] = function()
local l1, c1, l2, c2 = doc():get_selection(true) local l1, c1, l2, c2 = doc():get_selection(true)
@ -107,9 +108,9 @@ command.add("core.docview", {
end) end)
end, end,
["find-replace:find-pattern"] = function() ["find-replace:find-regex"] = function()
find("Find Text Pattern", function(doc, line, col, text) find("Find Text Regex", function(doc, line, col, text)
local opt = { wrap = true, no_case = true, pattern = true } local opt = { wrap = true, no_case = true, regex = true }
return search.find(doc, line, col, text, opt) return search.find(doc, line, col, text, opt)
end) end)
end, end,
@ -144,9 +145,10 @@ command.add("core.docview", {
end) end)
end, end,
["find-replace:replace-pattern"] = function() ["find-replace:replace-regex"] = function()
replace("Pattern", "", function(text, old, new) replace("Regex", "", function(text, old, new)
return text:gsub(old, new) local re = regex.compile(old)
return regex.gsub(re, text, new)
end) end)
end, end,

View File

@ -15,13 +15,9 @@ local function init_args(doc, line, col, text, opt)
opt = opt or default_opt opt = opt or default_opt
line, col = doc:sanitize_position(line, col) line, col = doc:sanitize_position(line, col)
if opt.no_case then if opt.no_case and not opt.regex then
if opt.pattern then
text = text:gsub("%%?.", pattern_lower)
else
text = text:lower() text = text:lower()
end end
end
return doc, line, col, text, opt return doc, line, col, text, opt
end end
@ -30,20 +26,32 @@ end
function search.find(doc, line, col, text, opt) function search.find(doc, line, col, text, opt)
doc, line, col, text, opt = init_args(doc, line, col, text, opt) doc, line, col, text, opt = init_args(doc, line, col, text, opt)
local re
if opt.regex then
re = regex.compile(text, opt.no_case and "i" or "")
end
for line = line, #doc.lines do for line = line, #doc.lines do
local line_text = doc.lines[line] local line_text = doc.lines[line]
if opt.regex then
local s, e = re:match(line_text, col, true)
if s then
return line, s, line, e + 1
end
col = 1
else
if opt.no_case then if opt.no_case then
line_text = line_text:lower() line_text = line_text:lower()
end end
local s, e = line_text:find(text, col, not opt.pattern) local s, e = line_text:find(text, col, true)
if s then if s then
return line, s, line, e + 1 return line, s, line, e + 1
end end
col = 1 col = 1
end end
end
if opt.wrap then if opt.wrap then
opt = { no_case = opt.no_case, pattern = opt.pattern } opt = { no_case = opt.no_case, regex = opt.regex }
return search.find(doc, 1, 1, text, opt) return search.find(doc, 1, 1, text, opt)
end end
end end

View File

@ -38,10 +38,11 @@ end
regex.gsub = function(pattern_string, string, replacement) regex.gsub = function(pattern_string, string, replacement)
local pattern = type(pattern_string) == "table" and local pattern = type(pattern_string) == "table" and
pattern_string or regex.compile(pattern_string) pattern_string or regex.compile(pattern_string)
local result, str, indices = "", string local result, str, indices, n = "", string
repeat repeat
indices = { regex.cmatch(pattern, str) } indices = { regex.cmatch(pattern, str) }
if #indices > 0 then if #indices > 0 then
n = n + 1
local currentReplacement = replacement local currentReplacement = replacement
if #indices > 2 then if #indices > 2 then
for i = 1, (#indices/2 - 1) do for i = 1, (#indices/2 - 1) do
@ -62,6 +63,6 @@ regex.gsub = function(pattern_string, string, replacement)
str = str:sub(indices[2]) str = str:sub(indices[2])
end end
until #indices == 0 or indices[1] == indices[2] until #indices == 0 or indices[1] == indices[2]
return result .. str return result .. str, n
end end

View File

@ -229,9 +229,12 @@ command.add(nil, {
end) end)
end, end,
["project-search:find-pattern"] = function() ["project-search:find-regex"] = function()
core.command_view:enter("Find Pattern In Project", function(text) core.command_view:enter("Find Regex In Project", function(text)
begin_search(text, function(line_text) return line_text:find(text) end) local re = regex.compile(text, "i")
begin_search(text, function(line_text)
return regex.cmatch(re, line_text)
end)
end) end)
end, end,

View File

@ -53,13 +53,15 @@ static int f_pcre_compile(lua_State *L) {
// Takes string, compiled regex, returns list of indices of matched groups // Takes string, compiled regex, returns list of indices of matched groups
// (including the whole match), if a match was found. // (including the whole match), if a match was found.
static int f_pcre_match(lua_State *L) { static int f_pcre_match(lua_State *L) {
size_t len; size_t len, offset = 0;
const char* str = luaL_checklstring(L, -1, &len); luaL_checktype(L, 1, LUA_TTABLE);
luaL_checktype(L, -2, LUA_TTABLE); const char* str = luaL_checklstring(L, 2, &len);
lua_rawgeti(L, -2, 1); if (lua_gettop(L) > 2)
offset = luaL_checknumber(L, 3);
lua_rawgeti(L, 1, 1);
pcre2_code* re = (pcre2_code*)lua_touserdata(L, -1); pcre2_code* re = (pcre2_code*)lua_touserdata(L, -1);
pcre2_match_data* md = pcre2_match_data_create_from_pattern(re, NULL); pcre2_match_data* md = pcre2_match_data_create_from_pattern(re, NULL);
int rc = pcre2_match(re, (PCRE2_SPTR)str, len, 0, 0, md, NULL); int rc = pcre2_match(re, (PCRE2_SPTR)str, len, offset, 0, md, NULL);
if (rc < 0) { if (rc < 0) {
pcre2_match_data_free(md); pcre2_match_data_free(md);
if (rc != PCRE2_ERROR_NOMATCH) if (rc != PCRE2_ERROR_NOMATCH)