From 58f4963ade167ddb09da05b7bde73160ad18ab13 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 28 Aug 2021 13:42:06 -0400 Subject: [PATCH 1/3] Added in two new VSC-style multicursor shortcuts. --- data/core/commands/findreplace.lua | 32 ++++++++++++++++++++++++------ data/core/keymap-macos.lua | 1 + data/core/keymap.lua | 1 + 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/data/core/commands/findreplace.lua b/data/core/commands/findreplace.lua index 6dd7ddae..b9a424fa 100644 --- a/data/core/commands/findreplace.lua +++ b/data/core/commands/findreplace.lua @@ -96,13 +96,33 @@ local function has_selection() return core.active_view:is(DocView) and core.active_view.doc:has_selection() end -command.add(has_selection, { - ["find-replace:select-next"] = function() - local l1, c1, l2, c2 = doc():get_selection(true) - local text = doc():get_text(l1, c1, l2, c2) - l1, c1, l2, c2 = search.find(doc(), l2, c2, text, { wrap = true }) - if l2 then doc():set_selection(l2, c2, l1, c1) end +local function has_unique_selection() + local text = nil + for idx, line1, col1, line2, col2 in doc():get_selections(true, true) do + if line1 == line2 and col1 == col2 then return false end + local selection = doc():get_text(line1, col1, line2, col2) + if text ~= nil and text ~= selection then return false end + text = selection end + return text ~= nil +end + +local function select_next(all) + local il1, ic1 = doc():get_selection(true) + for idx, l1, c1, l2, c2 in doc():get_selections(true, true) do + local text = doc():get_text(l1, c1, l2, c2) + repeat + l1, c1, l2, c2 = search.find(doc(), l2, c2, text, { wrap = true }) + if l1 == il1 and c1 == ic1 then break end + if l2 then doc():add_selection(l2, c2, l1, c1) end + until not all or not l2 + break + end +end + +command.add(has_unique_selection, { + ["find-replace:select-next"] = function() select_next(false) end, + ["find-replace:select-all"] = function() select_next(true) end }) command.add("core.docview", { diff --git a/data/core/keymap-macos.lua b/data/core/keymap-macos.lua index 647cb132..89f68949 100644 --- a/data/core/keymap-macos.lua +++ b/data/core/keymap-macos.lua @@ -66,6 +66,7 @@ local function keymap_macos(keymap) ["cmd+a"] = "doc:select-all", ["cmd+d"] = { "find-replace:select-next", "doc:select-word" }, ["cmd+l"] = "doc:select-lines", + ["cmd+shift+l"] = { "find-replace:select-all", "doc:select-word" }, ["cmd+/"] = "doc:toggle-line-comments", ["cmd+up"] = "doc:move-lines-up", ["cmd+down"] = "doc:move-lines-down", diff --git a/data/core/keymap.lua b/data/core/keymap.lua index 0b08259d..2be0dfc7 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -170,6 +170,7 @@ keymap.add_direct { ["ctrl+a"] = "doc:select-all", ["ctrl+d"] = { "find-replace:select-next", "doc:select-word" }, ["ctrl+l"] = "doc:select-lines", + ["ctrl+shift+l"] = { "find-replace:select-all", "doc:select-word" }, ["ctrl+/"] = "doc:toggle-line-comments", ["ctrl+up"] = "doc:move-lines-up", ["ctrl+down"] = "doc:move-lines-down", From bbe4e21f52ea47057ee11991c4ad9fedb48c5520 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 29 Aug 2021 17:54:57 -0400 Subject: [PATCH 2/3] Fixed cursors moving around with removal and inserts with cursors. Also fixed drawing line highlights with multicursors. --- data/core/doc/init.lua | 19 +++++++++++++++++++ data/core/docview.lua | 10 +++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index d05d9d45..2165ed83 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -309,6 +309,7 @@ end function Doc:raw_insert(line, col, text, undo_stack, time) -- split text into lines and merge with line at insertion point local lines = split_lines(text) + local len = #lines[#lines] local before = self.lines[line]:sub(1, col - 1) local after = self.lines[line]:sub(col) for i = 1, #lines - 1 do @@ -325,6 +326,15 @@ function Doc:raw_insert(line, col, text, undo_stack, time) 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() @@ -343,6 +353,15 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time) -- splice line into line array 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 + end -- update highlighter and assure selection is in bounds self.highlighter:invalidate(line1) diff --git a/data/core/docview.lua b/data/core/docview.lua index 89da8190..54eec575 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -366,16 +366,20 @@ function DocView:draw_line_body(idx, x, y) local x1 = x + self:get_col_x_offset(idx, col1) local x2 = x + self:get_col_x_offset(idx, col2) local lh = self:get_line_height() - renderer.draw_rect(x1, y, x2 - x1, lh, style.selection) + if x1 ~= x2 then + renderer.draw_rect(x1, y, x2 - x1, lh, style.selection) + end end end + local draw_highlight = nil for lidx, line1, col1, line2, col2 in self.doc:get_selections(true) do -- draw line highlight if caret is on this line - if config.highlight_current_line and (line1 == line2 and col1 == col2) + if draw_highlight ~= false and config.highlight_current_line and line1 == idx and core.active_view == self then - self:draw_line_highlight(x + self.scroll.x, y) + draw_highlight = (line1 == line2 and col1 == col2) end end + if draw_highlight then self:draw_line_highlight(x + self.scroll.x, y) end -- draw line's text self:draw_line_text(idx, x, y) From 4ae16615e88251ff294533624692c2bec49e3a68 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sun, 29 Aug 2021 20:05:58 -0400 Subject: [PATCH 3/3] Fixed cursor movement. --- data/core/doc/init.lua | 30 ++++++++++++++---------------- 1 file 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