From 30cc205cd4cbb9e25936b8829f0bd1edf1751909 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 18 Jan 2022 21:19:16 -0500 Subject: [PATCH 1/3] Fixed issue first mentioned in #771. --- data/core/commands/doc.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 54b84921..eb4c9e38 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -383,11 +383,13 @@ local commands = { block = true end - for _, line1, _, line2 in doc_multiline_selections(true) do + for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do if block then + local nline1, ncol1, nline2, ncol2 for line = line1, line2 do - block_comment(comment, line, 1, line, #doc().lines[line]) + nline1, ncol1, nline2, ncol2 = block_comment(comment, line, 1, line, #doc().lines[line]) end + doc():set_selections(idx, line1, col1, nline2, ncol2) else line_comment(comment, line1, line2) end From cdbfecc5ceaaaaee5cc710f78a535b73e10d6b3b Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Tue, 18 Jan 2022 21:37:26 -0500 Subject: [PATCH 2/3] Streamlined, and fixed guldo's problem. --- data/core/commands/doc.lua | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index eb4c9e38..a23e17ed 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -97,8 +97,8 @@ local function set_cursor(x, y, snap_type) core.blink_reset() end -local function line_comment(comment, line1, line2) - local comment_text = comment .. " " +local function line_comment(comment, line1, col1, line2, col2) + local comment_text = (type(comment) == 'table' and comment[1] or comment) .. " " local uncomment = true local start_offset = math.huge for line = line1, line2 do @@ -120,8 +120,17 @@ local function line_comment(comment, line1, line2) end elseif s then doc():insert(line, start_offset, comment_text) + if type(comment) == 'table' and #comment > 1 then + doc():insert(line, #doc().lines[line], " " .. comment[2]) + if line == line2 then + col2 = col2 + #comment[1] + #comment[2] + 2 + end + elseif line == line2 then + col2 = col2 + #comment_text + end end end + return line1, col1, line2, col2 end local function block_comment(comment, line1, col1, line2, col2) @@ -375,23 +384,11 @@ local commands = { end, ["doc:toggle-line-comments"] = function() - local comment = doc().syntax.comment - local block = false - if not comment then - comment = doc().syntax.block_comment - if not comment then return end - block = true - end - - for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do - if block then - local nline1, ncol1, nline2, ncol2 - for line = line1, line2 do - nline1, ncol1, nline2, ncol2 = block_comment(comment, line, 1, line, #doc().lines[line]) - end - doc():set_selections(idx, line1, col1, nline2, ncol2) - else - line_comment(comment, line1, line2) + local comment = doc().syntax.comment or doc().syntax.block_comment + if comment then + for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do + line1, col1, line2, col2 = line_comment(comment, line1, col1, line2, col2) + doc():set_selections(idx, line1, col1, line2, col2) end end end, From b523bd5cee0831ea046f38682e8062538b83e99d Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Thu, 20 Jan 2022 22:17:21 -0500 Subject: [PATCH 3/3] Fixed end of block-style line comments. --- data/core/commands/doc.lua | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index a23e17ed..b57c7adc 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -98,38 +98,44 @@ local function set_cursor(x, y, snap_type) end local function line_comment(comment, line1, col1, line2, col2) - local comment_text = (type(comment) == 'table' and comment[1] or comment) .. " " + local start_comment = (type(comment) == 'table' and comment[1] or comment) .. " " + local end_comment = (type(comment) == 'table' and " " .. comment[2]) local uncomment = true local start_offset = math.huge for line = line1, line2 do local text = doc().lines[line] local s = text:find("%S") - local cs, ce = text:find(comment_text, s, true) + local cs, ce = text:find(start_comment, s, true) if s and cs ~= s then uncomment = false - start_offset = math.min(start_offset, s) end + start_offset = math.min(start_offset, s) end + + local end_line = col2 == #doc().lines[line2] 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 end_comment and text:sub(#text - #end_comment, #text - 1) == end_comment then + doc():remove(line, #text - #end_comment, line, #text) + end + local cs, ce = text:find(start_comment, s, true) if ce then doc():remove(line, cs, line, ce + 1) end elseif s then - doc():insert(line, start_offset, comment_text) - if type(comment) == 'table' and #comment > 1 then + doc():insert(line, start_offset, start_comment) + if end_comment then doc():insert(line, #doc().lines[line], " " .. comment[2]) - if line == line2 then - col2 = col2 + #comment[1] + #comment[2] + 2 - end - elseif line == line2 then - col2 = col2 + #comment_text end end end + col1 = col1 + (col1 > start_offset and #start_comment or 0) * (uncomment and -1 or 1) + col2 = col2 + (col2 > start_offset and #start_comment or 0) * (uncomment and -1 or 1) + if end_comment and end_line then + col2 = col2 + #end_comment * (uncomment and -1 or 1) + end return line1, col1, line2, col2 end @@ -377,9 +383,7 @@ local commands = { col1 = 1 col2 = #doc().lines[line2] end - - line1, col1, line2, col2 = block_comment(comment, line1, col1, line2, col2) - doc():set_selections(idx, line1, col1, line2, col2) + doc():set_selections(idx, block_comment(comment, line1, col1, line2, col2)) end end, @@ -387,8 +391,7 @@ local commands = { local comment = doc().syntax.comment or doc().syntax.block_comment if comment then for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do - line1, col1, line2, col2 = line_comment(comment, line1, col1, line2, col2) - doc():set_selections(idx, line1, col1, line2, col2) + doc():set_selections(idx, line_comment(comment, line1, col1, line2, col2)) end end end,