diff options
| author | Adam Harrison <adamdharrison@gmail.com> | 2021-08-30 22:56:33 -0400 |
|---|---|---|
| committer | Adam Harrison <adamdharrison@gmail.com> | 2021-08-30 22:56:33 -0400 |
| commit | 1c4a4e763e34a8ffdb6d042ec96adef390bc749f (patch) | |
| tree | e24a88f78c44e89e34c5517265e2cc7d52cfdd9b | |
| parent | 8d6ac47cd0a33fe62d25d5b4ccfd810507dc0076 (diff) | |
| download | lite-xl-1c4a4e763e34a8ffdb6d042ec96adef390bc749f.tar.gz lite-xl-1c4a4e763e34a8ffdb6d042ec96adef390bc749f.zip | |
Fixed replace to make it multicursor-aware.
| -rw-r--r-- | data/core/doc/init.lua | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 65d2665c..2bb63ac4 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -464,12 +464,7 @@ function Doc:text_input(text, idx) end end - -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 +function Doc:replace_cursor(idx, line1, col1, line2, col2, fn) local old_text = self:get_text(line1, col1, line2, col2) local new_text, n = fn(old_text) if old_text ~= new_text then @@ -477,12 +472,26 @@ function Doc:replace(fn) self:remove(line1, col1, line2, col2) if line1 == line2 and col1 == col2 then 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 return n 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, ...) for sidx, line1, col1, line2, col2 in self:get_selections(true, idx) do |
