X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=runtime%2Fplugins%2Fcomment%2Fcomment.lua;h=ce49dcaabb7becbadda52f0e5727dfa38f6319f6;hb=54c23cae72d7237bc898a59f79aad0acffdf0ffe;hp=ab25f9d74b522fe72faf1d0b096de2dfb8f7b99a;hpb=05a0598f164efecb0e88624a7a9a201842b97fff;p=micro.git diff --git a/runtime/plugins/comment/comment.lua b/runtime/plugins/comment/comment.lua index ab25f9d7..ce49dcaa 100644 --- a/runtime/plugins/comment/comment.lua +++ b/runtime/plugins/comment/comment.lua @@ -6,23 +6,59 @@ local buffer = import("micro/buffer") local ft = {} +ft["apacheconf"] = "# %s" +ft["bat"] = ":: %s" ft["c"] = "// %s" ft["c++"] = "// %s" +ft["cmake"] = "# %s" +ft["conf"] = "# %s" +ft["crystal"] = "# %s" +ft["css"] = "/* %s */" +ft["d"] = "// %s" +ft["dart"] = "// %s" +ft["dockerfile"] = "# %s" +ft["elm"] = "-- %s" +ft["fish"] = "# %s" +ft["gdscript"] = "# %s" +ft["glsl"] = "// %s" ft["go"] = "// %s" -ft["python"] = "# %s" -ft["python3"] = "# %s" +ft["haskell"] = "-- %s" ft["html"] = "" +ft["ini"] = "; %s" ft["java"] = "// %s" +ft["javascript"] = "// %s" +ft["jinja2"] = "{# %s #}" ft["julia"] = "# %s" +ft["kotlin"] = "// %s" +ft["lua"] = "-- %s" +ft["markdown"] = "" +ft["nginx"] = "# %s" +ft["nim"] = "# %s" +ft["objc"] = "// %s" +ft["ocaml"] = "(* %s *)" +ft["pascal"] = "{ %s }" ft["perl"] = "# %s" ft["php"] = "// %s" +ft["pony"] = "// %s" +ft["powershell"] = "# %s" +ft["proto"] = "// %s" +ft["python"] = "# %s" +ft["python3"] = "# %s" +ft["ruby"] = "# %s" ft["rust"] = "// %s" +ft["scala"] = "// %s" ft["shell"] = "# %s" -ft["lua"] = "-- %s" -ft["javascript"] = "// %s" -ft["ruby"] = "# %s" -ft["d"] = "// %s" +ft["sql"] = "-- %s" ft["swift"] = "// %s" +ft["tex"] = "% %s" +ft["toml"] = "# %s" +ft["twig"] = "{# %s #}" +ft["v"] = "// %s" +ft["xml"] = "" +ft["yaml"] = "# %s" +ft["zig"] = "// %s" +ft["zscript"] = "// %s" +ft["zsh"] = "# %s" function onBufferOpen(buf) if buf.Settings["commenttype"] == nil then @@ -34,10 +70,38 @@ function onBufferOpen(buf) end end +function isCommented(bp, lineN, commentRegex) + local line = bp.Buf:Line(lineN) + if string.match(line, commentRegex) then + return true + end + return false +end + function commentLine(bp, lineN) local line = bp.Buf:Line(lineN) local commentType = bp.Buf.Settings["commenttype"] - local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%s", "(.*)") + local sel = -bp.Cursor.CurSelection + local curpos = -bp.Cursor.Loc + local index = string.find(commentType, "%%s") - 1 + local commentedLine = commentType:gsub("%%s", trim(line)) + bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine) + if bp.Cursor:HasSelection() then + bp.Cursor.CurSelection[1].Y = sel[1].Y + bp.Cursor.CurSelection[2].Y = sel[2].Y + bp.Cursor.CurSelection[1].X = sel[1].X + bp.Cursor.CurSelection[2].X = sel[2].X + else + bp.Cursor.X = curpos.X + index + bp.Cursor.Y = curpos.Y + end + bp.Cursor:Relocate() + bp.Cursor.LastVisualX = bp.Cursor:GetVisualX() +end + +function uncommentLine(bp, lineN, commentRegex) + local line = bp.Buf:Line(lineN) + local commentType = bp.Buf.Settings["commenttype"] local sel = -bp.Cursor.CurSelection local curpos = -bp.Cursor.Loc local index = string.find(commentType, "%%s") - 1 @@ -53,51 +117,63 @@ function commentLine(bp, lineN) bp.Cursor.X = curpos.X - index bp.Cursor.Y = curpos.Y end - else - local commentedLine = commentType:gsub("%%s", trim(line)) - bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine) - if bp.Cursor:HasSelection() then - bp.Cursor.CurSelection[1].Y = sel[1].Y - bp.Cursor.CurSelection[2].Y = sel[2].Y - bp.Cursor.CurSelection[1].X = sel[1].X - bp.Cursor.CurSelection[2].X = sel[2].X - else - bp.Cursor.X = curpos.X + index - bp.Cursor.Y = curpos.Y - end end bp.Cursor:Relocate() bp.Cursor.LastVisualX = bp.Cursor:GetVisualX() end -function commentSelection(bp, startLine, endLine) +function toggleCommentLine(bp, lineN, commentRegex) + if isCommented(bp, lineN, commentRegex) then + uncommentLine(bp, lineN, commentRegex) + else + commentLine(bp, lineN) + end +end + +function toggleCommentSelection(bp, startLine, endLine, commentRegex) + local allComments = true + for line = startLine, endLine do + if not isCommented(bp, line, commentRegex) then + allComments = false + break + end + end + for line = startLine, endLine do - commentLine(bp, line) + if allComments then + uncommentLine(bp, line, commentRegex) + else + commentLine(bp, line) + end end end function comment(bp, args) + local commentType = bp.Buf.Settings["commenttype"] + local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)"):gsub("%s+", "%s*") + if bp.Cursor:HasSelection() then if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then local endLine = bp.Cursor.CurSelection[1].Y if bp.Cursor.CurSelection[1].X == 0 then endLine = endLine - 1 end - commentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine) + toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex) else local endLine = bp.Cursor.CurSelection[2].Y if bp.Cursor.CurSelection[2].X == 0 then endLine = endLine - 1 end - commentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine) + toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex) end else - commentLine(bp, bp.Cursor.Y) + toggleCommentLine(bp, bp.Cursor.Y, commentRegex) end end function trim(s) - return (s:gsub("^%s*(.-)%s*$", "%1")) + local trimmed = s:gsub("^%s*(.-)%s*$", "%1"):gsub("%%","%%%%") + return trimmed end function string.starts(String,Start) @@ -107,5 +183,6 @@ end function init() config.MakeCommand("comment", comment, config.NoComplete) config.TryBindKey("Alt-/", "lua:comment.comment", false) + config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false) config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md") end