Merge pull request #455 from adamharrison/fix-multicursor-commands

Fixed replace to make it multicursor-aware.
This commit is contained in:
Adam 2021-08-31 16:09:08 -04:00 committed by GitHub
commit 49bee555fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 7 deletions

View File

@ -464,12 +464,7 @@ function Doc:text_input(text, idx)
end end
end end
function Doc:replace_cursor(idx, line1, col1, line2, col2, fn)
function Doc:replace(fn)
local line1, col1, line2, col2 = self:get_selection(true)
if line1 == line2 and col1 == col2 then
line1, col1, line2, col2 = 1, 1, #self.lines, #self.lines[#self.lines]
end
local old_text = self:get_text(line1, col1, line2, col2) local old_text = self:get_text(line1, col1, line2, col2)
local new_text, n = fn(old_text) local new_text, n = fn(old_text)
if old_text ~= new_text then if old_text ~= new_text then
@ -477,12 +472,26 @@ function Doc:replace(fn)
self:remove(line1, col1, line2, col2) self:remove(line1, col1, line2, col2)
if line1 == line2 and col1 == col2 then if line1 == line2 and col1 == col2 then
line2, col2 = self:position_offset(line1, col1, #new_text) line2, col2 = self:position_offset(line1, col1, #new_text)
self:set_selection(line1, col1, line2, col2) self:set_selections(idx, line1, col1, line2, col2)
end end
end end
return n return n
end end
function Doc:replace(fn)
local has_selection = false
for idx, line1, col1, line2, col2 in self:get_selections(true) do
if line1 ~= line2 or col1 ~= col2 then
self:replace_cursor(idx, line1, col1, line2, col2, fn)
has_selection = true
end
end
if not has_selection then
self:set_selection(table.unpack(self.selections))
self:replace_cursor(1, 1, 1, #self.lines, #self.lines[#self.lines], fn)
end
end
function Doc:delete_to_cursor(idx, ...) function Doc:delete_to_cursor(idx, ...)
for sidx, line1, col1, line2, col2 in self:get_selections(true, idx) do for sidx, line1, col1, line2, col2 in self:get_selections(true, idx) do