From 090d67f2526f35a8221a45ce543677c2a703c336 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 5 Jun 2021 00:44:35 -0400 Subject: [PATCH] Exposed indent function, and made it more flexible. --- data/core/commands/doc.lua | 39 ++------------------------------------ data/core/doc/init.lua | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 8165b426..b7b389cf 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -63,41 +63,6 @@ local function get_line_indent(line, rnd_up) end end --- un/indents text; behaviour varies based on selection and un/indent. --- * if there's a selection, it will stay static around the --- text for both indenting and unindenting. --- * if you are in the beginning whitespace of a line, and are indenting, the --- cursor will insert the exactly appropriate amount of spaces, and jump the --- cursor to the beginning of first non whitespace characters --- * if you are not in the beginning whitespace of a line, and you indent, it --- inserts the appropriate whitespace, as if you typed them normally. --- * if you are unindenting, the cursor will jump to the start of the line, --- and remove the appropriate amount of spaces (or a tab). -local function indent_text(unindent) - local text = get_indent_string() - local line1, col1, line2, col2, swap = doc_multiline_selection(true) - local _, se = doc().lines[line1]:find("^[ \t]+") - local in_beginning_whitespace = col1 == 1 or (se and col1 <= se + 1) - if unindent or doc():has_selection() or in_beginning_whitespace then - local l1d, l2d = #doc().lines[line1], #doc().lines[line2] - for line = line1, line2 do - local e, rnded = get_line_indent(doc().lines[line], unindent) - doc():remove(line, 1, line, (e or 0) + 1) - doc():insert(line, 1, - unindent and rnded:sub(1, #rnded - #text) or rnded .. text) - end - l1d, l2d = #doc().lines[line1] - l1d, #doc().lines[line2] - l2d - if (unindent or in_beginning_whitespace) and not doc():has_selection() then - local start_cursor = (se and se + 1 or 1) + l1d or #(doc().lines[line1]) - doc():set_selection(line1, start_cursor, line2, start_cursor, swap) - else - doc():set_selection(line1, col1 + l1d, line2, col2 + l2d, swap) - end - else - doc():text_input(text) - end -end - local commands = { ["doc:undo"] = function() doc():undo() @@ -206,11 +171,11 @@ local commands = { end, ["doc:indent"] = function() - indent_text() + doc():indent_text(false, doc_multiline_selection(true)) end, ["doc:unindent"] = function() - indent_text(true) + doc():indent_text(true, doc_multiline_selection(true)) end, ["doc:duplicate-lines"] = function() diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 39fae9ca..4967eed7 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -405,6 +405,41 @@ function Doc:select_to(...) self:set_selection(line, col, line2, col2) end + +-- un/indents text; behaviour varies based on selection and un/indent. +-- * if there's a selection, it will stay static around the +-- text for both indenting and unindenting. +-- * if you are in the beginning whitespace of a line, and are indenting, the +-- cursor will insert the exactly appropriate amount of spaces, and jump the +-- cursor to the beginning of first non whitespace characters +-- * if you are not in the beginning whitespace of a line, and you indent, it +-- inserts the appropriate whitespace, as if you typed them normally. +-- * if you are unindenting, the cursor will jump to the start of the line, +-- and remove the appropriate amount of spaces (or a tab). +function Doc:indent_text(unindent, line1, col1, line2, col2, swap) + local text = get_indent_string() + local _, se = self.lines[line1]:find("^[ \t]+") + local in_beginning_whitespace = col1 == 1 or (se and col1 <= se + 1) + if unindent or self:has_selection() or in_beginning_whitespace then + local l1d, l2d = #doc().lines[line1], #self.lines[line2] + for line = line1, line2 do + local e, rnded = get_line_indent(self.lines[line], unindent) + self:remove(line, 1, line, (e or 0) + 1) + self:insert(line, 1, + unindent and rnded:sub(1, #rnded - #text) or rnded .. text) + end + l1d, l2d = #self.lines[line1] - l1d, #self.lines[line2] - l2d + if (unindent or in_beginning_whitespace) and not self:has_selection() then + local start_cursor = (se and se + 1 or 1) + l1d or #(self.lines[line1]) + self:set_selection(line1, start_cursor, line2, start_cursor, swap) + else + self:set_selection(line1, col1 + l1d, line2, col2 + l2d, swap) + end + else + self:text_input(text) + end +end + -- For plugins to add custom actions of document change function Doc:on_text_change(type) end