aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Harrison <adamdharrison@gmail.com>2021-08-29 20:05:58 -0400
committerAdam Harrison <adamdharrison@gmail.com>2021-08-29 20:05:58 -0400
commit4ae16615e88251ff294533624692c2bec49e3a68 (patch)
tree3e4d1a2fa6f3ad1352b0b5718c186d76429f1972
parentbbe4e21f52ea47057ee11991c4ad9fedb48c5520 (diff)
downloadlite-xl-4ae16615e88251ff294533624692c2bec49e3a68.tar.gz
lite-xl-4ae16615e88251ff294533624692c2bec49e3a68.zip
Fixed cursor movement.
-rw-r--r--data/core/doc/init.lua30
1 files changed, 14 insertions, 16 deletions
diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua
index 2165ed83..854f2039 100644
--- a/data/core/doc/init.lua
+++ b/data/core/doc/init.lua
@@ -320,21 +320,20 @@ function Doc:raw_insert(line, col, text, undo_stack, time)
-- splice lines into line array
common.splice(self.lines, line, 1, lines)
+
+ -- keep cursors where they should be
+ for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true, true) do
+ if cline1 < line then break end
+ local line_addition = (line < cline1 or col < ccol1) and #lines - 1 or 0
+ local column_addition = line == cline1 and ccol1 > col and len or 0
+ self:set_selections(idx, cline1 + line_addition, ccol1 + column_addition, cline2 + line_addition, ccol2 + column_addition)
+ end
-- push undo
local line2, col2 = self:position_offset(line, col, #text)
push_undo(undo_stack, time, "selection", unpack(self.selections))
push_undo(undo_stack, time, "remove", line, col, line2, col2)
- -- keep cursors where they should be
- for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true) do
- if cline1 >= line then
- local line_addition = line > cline1 or ccol1 > col and #lines - 1 or 0
- local column_addition = line == cline1 and ccol1 > col and len or 0
- self:set_selections(idx, cline1 + line_addition, ccol1 + column_addition, cline2 + line_addition, ccol2 + column_addition)
- end
- end
-
-- update highlighter and assure selection is in bounds
self.highlighter:invalidate(line)
self:sanitize_selection()
@@ -355,12 +354,11 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
common.splice(self.lines, line1, line2 - line1 + 1, { before .. after })
-- move all cursors back if they share a line with the removed text
- for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true) do
- if cline1 >= line2 then
- local line_removal = line2 - line1
- local column_removal = line2 == cline2 and col2 < ccol1 and (line2 == line1 and col2 - col1 or col2) or 0
- self:set_selections(idx, cline1 - line_removal, ccol1 - column_removal, cline2 - line_removal, ccol2 - column_removal)
- end
+ for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true, true) do
+ if cline1 < line2 then break end
+ local line_removal = line2 - line1
+ local column_removal = line2 == cline2 and col2 < ccol1 and (line2 == line1 and col2 - col1 or col2) or 0
+ self:set_selections(idx, cline1 - line_removal, ccol1 - column_removal, cline2 - line_removal, ccol2 - column_removal)
end
-- update highlighter and assure selection is in bounds
@@ -398,7 +396,7 @@ end
function Doc:text_input(text, 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 or true) do
if line1 ~= line2 or col1 ~= col2 then
self:delete_to_cursor(sidx)
end