Merge pull request #771 from takase1121/multiline-comment-command

add toggle-block-comment
This commit is contained in:
Adam 2022-01-18 21:07:46 -05:00 committed by GitHub
commit d3e1636881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 118 additions and 40 deletions

View File

@ -97,6 +97,67 @@ local function set_cursor(x, y, snap_type)
core.blink_reset() core.blink_reset()
end end
local function line_comment(comment, line1, line2)
local comment_text = comment .. " "
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)
if s and cs ~= s then
uncomment = false
start_offset = math.min(start_offset, s)
end
end
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
local function block_comment(comment, line1, col1, line2, col2)
-- automatically skip spaces
local word_start = doc():get_text(line1, col1, line1, math.huge):find("%S")
local word_end = doc():get_text(line2, 1, line2, col2):find("%s*$")
col1 = col1 + (word_start and (word_start - 1) or 0)
col2 = word_end and word_end or col2
local block_start = doc():get_text(line1, col1, line1, col1 + #comment[1])
local block_end = doc():get_text(line2, col2 - #comment[2], line2, col2)
if block_start == comment[1] and block_end == comment[2] then
-- remove up to 1 whitespace after the comment
local start_len, stop_len = #comment[1], #comment[2]
if doc():get_text(line1, col1 + #comment[1], line1, col1 + #comment[1] + 1):find("%s$") then
start_len = start_len + 1
end
if doc():get_text(line2, col2 - #comment[2] - 1, line2, col2):find("^%s") then
stop_len = stop_len + 1
end
doc():remove(line1, col1, line1, col1 + start_len)
col2 = col2 - (line1 == line2 and start_len or 0)
doc():remove(line2, col2 - stop_len, line2, col2)
return line1, col1, line2, col2 - stop_len
else
doc():insert(line1, col1, comment[1] .. " ")
col2 = col2 + (line1 == line2 and (#comment[1] + 1) or 0)
doc():insert(line2, col2, " " .. comment[2])
return line1, col1, line2, col2 + #comment[2] + 1
end
end
local selection_commands = { local selection_commands = {
["doc:select-none"] = function() ["doc:select-none"] = function()
local line, col = doc():get_selection() local line, col = doc():get_selection()
@ -292,34 +353,43 @@ local commands = {
end end
end, end,
["doc:toggle-block-comments"] = function()
local comment = doc().syntax.block_comment
if not comment then
if doc().syntax.comment then
command.perform "doc:toggle-line-comments"
end
return
end
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
-- if nothing is selected, toggle the whole line
if line1 == line2 and col1 == col2 then
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)
end
end,
["doc:toggle-line-comments"] = function() ["doc:toggle-line-comments"] = function()
local comment = doc().syntax.comment local comment = doc().syntax.comment
if not comment then return end local block = false
local indentation = doc():get_indent_string() if not comment then
local comment_text = comment .. " " comment = doc().syntax.block_comment
for idx, line1, _, line2 in doc_multiline_selections(true) do if not comment then return end
local uncomment = true block = true
local start_offset = math.huge end
for line = line1, line2 do
local text = doc().lines[line] for _, line1, _, line2 in doc_multiline_selections(true) do
local s = text:find("%S") if block then
local cs, ce = text:find(comment_text, s, true) for line = line1, line2 do
if s and cs ~= s then block_comment(comment, line, 1, line, #doc().lines[line])
uncomment = false
start_offset = math.min(start_offset, s)
end
end
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
else
line_comment(comment, line1, line2)
end end
end end
end, end,

View File

@ -215,6 +215,7 @@ keymap.add_direct {
["ctrl+l"] = "doc:select-lines", ["ctrl+l"] = "doc:select-lines",
["ctrl+shift+l"] = { "find-replace:select-add-all", "doc:select-word" }, ["ctrl+shift+l"] = { "find-replace:select-add-all", "doc:select-word" },
["ctrl+/"] = "doc:toggle-line-comments", ["ctrl+/"] = "doc:toggle-line-comments",
["ctrl+shift+/"] = "doc:toggle-block-comments",
["ctrl+up"] = "doc:move-lines-up", ["ctrl+up"] = "doc:move-lines-up",
["ctrl+down"] = "doc:move-lines-down", ["ctrl+down"] = "doc:move-lines-down",
["ctrl+shift+d"] = "doc:duplicate-lines", ["ctrl+shift+d"] = "doc:duplicate-lines",

View File

@ -5,6 +5,7 @@ syntax.add {
name = "C", name = "C",
files = { "%.c$", "%.h$", "%.inl$" }, files = { "%.c$", "%.h$", "%.inl$" },
comment = "//", comment = "//",
block_comment = { "/*", "*/" },
patterns = { patterns = {
{ pattern = "//.-\n", type = "comment" }, { pattern = "//.-\n", type = "comment" },
{ pattern = { "/%*", "%*/" }, type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" },

View File

@ -10,6 +10,7 @@ syntax.add {
"%.c++$", "%.hh$", "%.H$", "%.hxx$", "%.hpp$", "%.h++$" "%.c++$", "%.hh$", "%.H$", "%.hxx$", "%.hpp$", "%.h++$"
}, },
comment = "//", comment = "//",
block_comment = { "/*", "*/" },
patterns = { patterns = {
{ pattern = "//.-\n", type = "comment" }, { pattern = "//.-\n", type = "comment" },
{ pattern = { "/%*", "%*/" }, type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" },

View File

@ -4,6 +4,7 @@ local syntax = require "core.syntax"
syntax.add { syntax.add {
name = "CSS", name = "CSS",
files = { "%.css$" }, files = { "%.css$" },
block_comment = { "/*", "*/" },
patterns = { patterns = {
{ pattern = "\\.", type = "normal" }, { pattern = "\\.", type = "normal" },
{ pattern = "//.-\n", type = "comment" }, { pattern = "//.-\n", type = "comment" },

View File

@ -4,6 +4,7 @@ local syntax = require "core.syntax"
syntax.add { syntax.add {
name = "HTML", name = "HTML",
files = { "%.html?$" }, files = { "%.html?$" },
block_comment = { "<!--", "-->" },
patterns = { patterns = {
{ {
pattern = { pattern = {

View File

@ -5,6 +5,7 @@ syntax.add {
name = "JavaScript", name = "JavaScript",
files = { "%.js$", "%.json$", "%.cson$" }, files = { "%.js$", "%.json$", "%.cson$" },
comment = "//", comment = "//",
block_comment = { "/*", "*/" },
patterns = { patterns = {
{ pattern = "//.-\n", type = "comment" }, { pattern = "//.-\n", type = "comment" },
{ pattern = { "/%*", "%*/" }, type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" },

View File

@ -6,6 +6,7 @@ syntax.add {
files = "%.lua$", files = "%.lua$",
headers = "^#!.*[ /]lua", headers = "^#!.*[ /]lua",
comment = "--", comment = "--",
block_comment = { "--[[", "]]" },
patterns = { patterns = {
{ pattern = { '"', '"', '\\' }, type = "string" }, { pattern = { '"', '"', '\\' }, type = "string" },
{ pattern = { "'", "'", '\\' }, type = "string" }, { pattern = { "'", "'", '\\' }, type = "string" },

View File

@ -5,6 +5,7 @@ syntax.add {
name = "XML", name = "XML",
files = { "%.xml$" }, files = { "%.xml$" },
headers = "<%?xml", headers = "<%?xml",
block_comment = { "<!--", "-->" },
patterns = { patterns = {
{ pattern = { "<!%-%-", "%-%->" }, type = "comment" }, { pattern = { "<!%-%-", "%-%->" }, type = "comment" },
{ pattern = { '%f[^>][^<]', '%f[<]' }, type = "normal" }, { pattern = { '%f[^>][^<]', '%f[<]' }, type = "normal" },