From 4d31b1bc404a56a1160f75ed82adec24042d676e Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 25 Dec 2021 12:57:00 +0800 Subject: [PATCH] add toggle-block-comment --- data/core/commands/doc.lua | 48 ++++++++++++++++++++++++++++++++++ data/core/keymap.lua | 1 + data/plugins/language_c.lua | 1 + data/plugins/language_cpp.lua | 1 + data/plugins/language_css.lua | 1 + data/plugins/language_html.lua | 31 +++++++++++----------- data/plugins/language_js.lua | 1 + data/plugins/language_lua.lua | 1 + data/plugins/language_xml.lua | 1 + 9 files changed, 71 insertions(+), 15 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 8e2078f3..1e65ad11 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -292,6 +292,54 @@ local commands = { end end, + ["doc:toggle-block-comments"] = function() + local comment = doc().syntax.multiline_comment + if not comment then return end + + for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do + local text = doc():get_text(line1, col1, line2, col2) + + -- might need to deal with unicode later... + local cs, cse = text:find(comment[1], 1, true) + local ce = text:find(comment[2], #text - #comment[2] + 1, true) + + local new_col1, new_col2 = col1, col2 + -- uncomment + if cs and ce then + if line1 == line2 then + new_col2 = new_col2 - #table.concat(comment) + else + new_col2 = new_col2 - #comment[2] + end + + -- remove 1 whitespace if possible + if text:sub(cse + 1, cse + 1) == " " then + cse = cse + 1 + if line1 == line2 then + new_col2 = new_col2 - 1 + end + end + if text:sub(ce - 1, ce - 1) == " " then + ce = ce - 1 + new_col2 = new_col2 - 1 + end + + text = text:sub(cse + 1, ce - 1) + -- comment + else + text = comment[1] .. " " .. text .. " " .. comment[2] + if line1 == line2 then + new_col2 = new_col2 + #table.concat(comment) + 2 + else + new_col2 = new_col2 + #comment[2] + 1 + end + end + doc():remove(line1, col1, line2, col2) + doc():insert(line1, col1, text) + doc():set_selections(idx, line1, new_col1, line2, new_col2) + end + end, + ["doc:toggle-line-comments"] = function() local comment = doc().syntax.comment if not comment then return end diff --git a/data/core/keymap.lua b/data/core/keymap.lua index 404cff39..6716fca9 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -215,6 +215,7 @@ keymap.add_direct { ["ctrl+l"] = "doc:select-lines", ["ctrl+shift+l"] = { "find-replace:select-add-all", "doc:select-word" }, ["ctrl+/"] = "doc:toggle-line-comments", + ["ctrl+shift+/"] = "doc:toggle-block-comments", ["ctrl+up"] = "doc:move-lines-up", ["ctrl+down"] = "doc:move-lines-down", ["ctrl+shift+d"] = "doc:duplicate-lines", diff --git a/data/plugins/language_c.lua b/data/plugins/language_c.lua index 88cc7c5a..ecbeed7a 100644 --- a/data/plugins/language_c.lua +++ b/data/plugins/language_c.lua @@ -5,6 +5,7 @@ syntax.add { name = "C", files = { "%.c$", "%.h$", "%.inl$" }, comment = "//", + multiline_comment = { "/*", "*/" }, patterns = { { pattern = "//.-\n", type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" }, diff --git a/data/plugins/language_cpp.lua b/data/plugins/language_cpp.lua index a945fd1f..03554a48 100644 --- a/data/plugins/language_cpp.lua +++ b/data/plugins/language_cpp.lua @@ -10,6 +10,7 @@ syntax.add { "%.c++$", "%.hh$", "%.H$", "%.hxx$", "%.hpp$", "%.h++$" }, comment = "//", + multiline_comment = { "/*", "*/" }, patterns = { { pattern = "//.-\n", type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" }, diff --git a/data/plugins/language_css.lua b/data/plugins/language_css.lua index 395e375c..871598a2 100644 --- a/data/plugins/language_css.lua +++ b/data/plugins/language_css.lua @@ -4,6 +4,7 @@ local syntax = require "core.syntax" syntax.add { name = "CSS", files = { "%.css$" }, + multiline_comment = { "/*", "*/" }, patterns = { { pattern = "\\.", type = "normal" }, { pattern = "//.-\n", type = "comment" }, diff --git a/data/plugins/language_html.lua b/data/plugins/language_html.lua index 1f4515bc..e22e8897 100644 --- a/data/plugins/language_html.lua +++ b/data/plugins/language_html.lua @@ -4,28 +4,29 @@ local syntax = require "core.syntax" syntax.add { name = "HTML", files = { "%.html?$" }, + multiline_comment = { "" }, patterns = { - { - pattern = { + { + pattern = { "<%s*[sS][cC][rR][iI][pP][tT]%s+[tT][yY][pP][eE]%s*=%s*" .. "['\"]%a+/[jJ][aA][vV][aA][sS][cC][rR][iI][pP][tT]['\"]%s*>", - "<%s*/[sS][cC][rR][iI][pP][tT]>" - }, - syntax = ".js", - type = "function" - }, - { - pattern = { - "<%s*[sS][cC][rR][iI][pP][tT]%s*>", - "<%s*/%s*[sS][cC][rR][iI][pP][tT]>" + "<%s*/[sS][cC][rR][iI][pP][tT]>" }, syntax = ".js", type = "function" }, - { - pattern = { - "<%s*[sS][tT][yY][lL][eE][^>]*>", - "<%s*/%s*[sS][tT][yY][lL][eE]%s*>" + { + pattern = { + "<%s*[sS][cC][rR][iI][pP][tT]%s*>", + "<%s*/%s*[sS][cC][rR][iI][pP][tT]>" + }, + syntax = ".js", + type = "function" + }, + { + pattern = { + "<%s*[sS][tT][yY][lL][eE][^>]*>", + "<%s*/%s*[sS][tT][yY][lL][eE]%s*>" }, syntax = ".css", type = "function" diff --git a/data/plugins/language_js.lua b/data/plugins/language_js.lua index 94ab8499..d23e412b 100644 --- a/data/plugins/language_js.lua +++ b/data/plugins/language_js.lua @@ -5,6 +5,7 @@ syntax.add { name = "JavaScript", files = { "%.js$", "%.json$", "%.cson$" }, comment = "//", + multiline_comment = { "/*", "*/" }, patterns = { { pattern = "//.-\n", type = "comment" }, { pattern = { "/%*", "%*/" }, type = "comment" }, diff --git a/data/plugins/language_lua.lua b/data/plugins/language_lua.lua index 5c770d43..79a8289b 100644 --- a/data/plugins/language_lua.lua +++ b/data/plugins/language_lua.lua @@ -6,6 +6,7 @@ syntax.add { files = "%.lua$", headers = "^#!.*[ /]lua", comment = "--", + multiline_comment = { "--[[", "]]" }, patterns = { { pattern = { '"', '"', '\\' }, type = "string" }, { pattern = { "'", "'", '\\' }, type = "string" }, diff --git a/data/plugins/language_xml.lua b/data/plugins/language_xml.lua index c858d3cf..971a53de 100644 --- a/data/plugins/language_xml.lua +++ b/data/plugins/language_xml.lua @@ -5,6 +5,7 @@ syntax.add { name = "XML", files = { "%.xml$" }, headers = "<%?xml", + multiline_comment = { "" }, patterns = { { pattern = { "" }, type = "comment" }, { pattern = { '%f[^>][^<]', '%f[<]' }, type = "normal" },