Indent Enhancements (#202)

* Indent enhancements.

* Fixed to match style guidelines.

* Added in useful explanatory comment.

* Changed which selection we're using, as we don't want this kind of wrapping to happen.

* Fixed bug involving lines full of whitespace.

* Removed unecessary commit.

* Actually reverted function, so that we don't screw up commenting.

* Fixed hard tab issue.
This commit is contained in:
Adam 2021-05-22 09:01:19 -04:00 committed by GitHub
parent 04c7a49d00
commit a254d393db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 20 deletions

View File

@ -45,22 +45,15 @@ local function insert_at_start_of_selected_lines(text, skip_empty)
doc():set_selection(line1, col1 + #text, line2, col2 + #text, swap)
end
local function remove_from_start_of_selected_lines(text, skip_empty, remove_partial)
local function remove_from_start_of_selected_lines(text, skip_empty)
local line1, col1, line2, col2, swap = doc_multiline_selection(true)
for line = line1, line2 do
local line_text = doc().lines[line]
for i = #text, 1, -1 do
if line_text:sub(1, i) == text:sub(1, i)
and (not skip_empty or line_text:find("%S"))
then
doc():remove(line, 1, line, i + 1)
break
end
if not remove_partial then
break
end
if line_text:sub(1, #text) == text
and (not skip_empty or line_text:find("%S"))
then
doc():remove(line, 1, line, #text + 1)
end
end
doc():set_selection(line1, col1 - #text, line2, col2 - #text, swap)
@ -81,6 +74,56 @@ local function save(filename)
core.log("Saved \"%s\"", saved_filename)
end
-- returns the size of the original indent, and the indent
-- in your config format, rounded either up or down
local function get_line_indent(line, rnd_up)
local _, e = line:find("^[ \t]+")
local soft_tab = string.rep(" ", config.indent_size)
if config.tab_type == "hard" then
local indent = e and line:sub(1, e):gsub(soft_tab, "\t") or ""
return e, indent:gsub(" +", rnd_up and "\t" or "")
else
local indent = e and line:sub(1, e):gsub("\t", soft_tab) or ""
local number = #indent / #soft_tab
return e, indent:sub(1,
(rnd_up and math.ceil(number) or math.floor(number))*#soft_tab)
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():get_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()
@ -190,17 +233,11 @@ local commands = {
end,
["doc:indent"] = function()
local text = get_indent_string()
if doc():has_selection() then
insert_at_start_of_selected_lines(text)
else
doc():text_input(text)
end
indent_text()
end,
["doc:unindent"] = function()
local text = get_indent_string()
remove_from_start_of_selected_lines(text, false, true)
indent_text(true)
end,
["doc:duplicate-lines"] = function()