Restore in-selection replace as discussed in #1331 (#1368)

This commit is contained in:
Jefferson González 2023-02-06 13:34:59 -04:00 committed by George Sokianos
parent 1fcc69d7aa
commit 7f91514d6a
1 changed files with 22 additions and 10 deletions

View File

@ -193,6 +193,23 @@ local function select_next(reverse)
if l2 then doc():set_selection(l2, c2, l1, c1) end if l2 then doc():set_selection(l2, c2, l1, c1) end
end end
---@param in_selection? boolean whether to replace in the selections only, or in the whole file.
local function find_replace(in_selection)
local l1, c1, l2, c2 = doc():get_selection()
local selected_text = ""
if not in_selection then
selected_text = doc():get_text(l1, c1, l2, c2)
doc():set_selection(l2, c2, l2, c2)
end
replace("Text", l1 == l2 and selected_text or "", function(text, old, new)
if not find_regex then
return text:gsub(old:gsub("%W", "%%%1"), new:gsub("%%", "%%%%"), nil)
end
local result, matches = regex.gsub(regex.compile(old, "m"), text, new)
return result, matches
end)
end
command.add(has_unique_selection, { command.add(has_unique_selection, {
["find-replace:select-next"] = select_next, ["find-replace:select-next"] = select_next,
["find-replace:select-previous"] = function() select_next(true) end, ["find-replace:select-previous"] = function() select_next(true) end,
@ -209,16 +226,11 @@ command.add("core.docview!", {
end, end,
["find-replace:replace"] = function() ["find-replace:replace"] = function()
local l1, c1, l2, c2 = doc():get_selection() find_replace()
local selected_text = doc():get_text(l1, c1, l2, c2) end,
doc():set_selection(l2, c2, l2, c2)
replace("Text", l1 == l2 and selected_text or "", function(text, old, new) ["find-replace:replace-in-selection"] = function()
if not find_regex then find_replace(true)
return text:gsub(old:gsub("%W", "%%%1"), new:gsub("%%", "%%%%"), nil)
end
local result, matches = regex.gsub(regex.compile(old, "m"), text, new)
return result, matches
end)
end, end,
["find-replace:replace-symbol"] = function() ["find-replace:replace-symbol"] = function()