From 4ffb5e36724b97b43abcdc182cb10256bc485c71 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 28 May 2021 02:17:49 -0400 Subject: [PATCH] Fixed commenting and uncommenting. (#224) --- data/core/commands/doc.lua | 51 ++++++++++++++------------------------ 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index daf71bad..ffaf029b 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -33,33 +33,6 @@ local function doc_multiline_selection(sort) return line1, col1, line2, col2, swap end - -local function insert_at_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] - if (not skip_empty or line_text:find("%S")) then - doc():insert(line, 1, text) - end - end - doc():set_selection(line1, col1 + #text, line2, col2 + #text, swap) -end - - -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] - 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) -end - - local function append_line_if_last_line(line) if line >= #doc().lines then doc():insert(line, math.huge, "\n") @@ -281,19 +254,31 @@ local commands = { ["doc:toggle-line-comments"] = function() local comment = doc().syntax.comment if not comment then return end + local indentation = get_indent_string() local comment_text = comment .. " " - local line1, _, line2 = doc():get_selection(true) + local line1, _, line2 = doc_multiline_selection(true) local uncomment = true + local start_offset = math.huge for line = line1, line2 do local text = doc().lines[line] - if text:find("%S") and text:find(comment_text, 1, true) ~= 1 then + local s = text:find("%S") + local cs, ce = text:find(comment_text, s, true) + if s and cs ~= s then uncomment = false + start_offset = math.min(start_offset, s) end end - if uncomment then - remove_from_start_of_selected_lines(comment_text, true) - else - insert_at_start_of_selected_lines(comment_text, true) + for line = line1, line2 do + local text = doc().lines[line] + local s = text:find("%S") + if uncomment then + local cs, ce = text:find(comment_text, s, true) + if ce then + doc():remove(line, cs, line, ce + 1) + end + elseif s then + doc():insert(line, start_offset, comment_text) + end end end,