| 1 |
VERSION = "1.0.0" |
| 2 |
|
| 3 |
local util = import("micro/util") |
| 4 |
local config = import("micro/config") |
| 5 |
local buffer = import("micro/buffer") |
| 6 |
local micro = import("micro") |
| 7 |
|
| 8 |
local ft = {} |
| 9 |
|
| 10 |
ft["apacheconf"] = "# %s" |
| 11 |
ft["batch"] = ":: %s" |
| 12 |
ft["c"] = "// %s" |
| 13 |
ft["c++"] = "// %s" |
| 14 |
ft["cmake"] = "# %s" |
| 15 |
ft["conf"] = "# %s" |
| 16 |
ft["crystal"] = "# %s" |
| 17 |
ft["css"] = "/* %s */" |
| 18 |
ft["d"] = "// %s" |
| 19 |
ft["dart"] = "// %s" |
| 20 |
ft["dockerfile"] = "# %s" |
| 21 |
ft["elm"] = "-- %s" |
| 22 |
ft["fish"] = "# %s" |
| 23 |
ft["gdscript"] = "# %s" |
| 24 |
ft["glsl"] = "// %s" |
| 25 |
ft["go"] = "// %s" |
| 26 |
ft["haskell"] = "-- %s" |
| 27 |
ft["html"] = "<!-- %s -->" |
| 28 |
ft["ini"] = "; %s" |
| 29 |
ft["java"] = "// %s" |
| 30 |
ft["javascript"] = "// %s" |
| 31 |
ft["jinja2"] = "{# %s #}" |
| 32 |
ft["json"] = "// %s" |
| 33 |
ft["julia"] = "# %s" |
| 34 |
ft["kotlin"] = "// %s" |
| 35 |
ft["lua"] = "-- %s" |
| 36 |
ft["markdown"] = "<!-- %s -->" |
| 37 |
ft["nginx"] = "# %s" |
| 38 |
ft["nim"] = "# %s" |
| 39 |
ft["objc"] = "// %s" |
| 40 |
ft["ocaml"] = "(* %s *)" |
| 41 |
ft["pascal"] = "{ %s }" |
| 42 |
ft["perl"] = "# %s" |
| 43 |
ft["php"] = "// %s" |
| 44 |
ft["pony"] = "// %s" |
| 45 |
ft["powershell"] = "# %s" |
| 46 |
ft["proto"] = "// %s" |
| 47 |
ft["python"] = "# %s" |
| 48 |
ft["python3"] = "# %s" |
| 49 |
ft["ruby"] = "# %s" |
| 50 |
ft["rust"] = "// %s" |
| 51 |
ft["scala"] = "// %s" |
| 52 |
ft["shell"] = "# %s" |
| 53 |
ft["sql"] = "-- %s" |
| 54 |
ft["swift"] = "// %s" |
| 55 |
ft["tex"] = "% %s" |
| 56 |
ft["toml"] = "# %s" |
| 57 |
ft["twig"] = "{# %s #}" |
| 58 |
ft["typescript"] = "// %s" |
| 59 |
ft["v"] = "// %s" |
| 60 |
ft["xml"] = "<!-- %s -->" |
| 61 |
ft["yaml"] = "# %s" |
| 62 |
ft["zig"] = "// %s" |
| 63 |
ft["zscript"] = "// %s" |
| 64 |
ft["zsh"] = "# %s" |
| 65 |
|
| 66 |
function updateCommentType(buf) |
| 67 |
-- NOTE: Using DoSetOptionNative to avoid LocalSettings[option] = true |
| 68 |
-- so that "comment.type" can be reset by a "filetype" change to default. |
| 69 |
if (buf.Settings["comment.type"] == "") then |
| 70 |
-- NOTE: This won't get triggered if a filetype is change via `setlocal filetype` |
| 71 |
-- since it is not registered with `RegisterGlobalOption()`` |
| 72 |
if buf.Settings["commenttype"] ~= nil then |
| 73 |
buf:DoSetOptionNative("comment.type", buf.Settings["commenttype"]) |
| 74 |
else |
| 75 |
if (ft[buf.Settings["filetype"]] ~= nil) then |
| 76 |
buf:DoSetOptionNative("comment.type", ft[buf.Settings["filetype"]]) |
| 77 |
else |
| 78 |
buf:DoSetOptionNative("comment.type", "# %s") |
| 79 |
end |
| 80 |
end |
| 81 |
end |
| 82 |
end |
| 83 |
|
| 84 |
function isCommented(bp, lineN, commentRegex) |
| 85 |
local line = bp.Buf:Line(lineN) |
| 86 |
local regex = commentRegex:gsub("%s+", "%s*") |
| 87 |
if string.match(line, regex) then |
| 88 |
return true |
| 89 |
end |
| 90 |
return false |
| 91 |
end |
| 92 |
|
| 93 |
function commentLine(bp, lineN, indentLen) |
| 94 |
updateCommentType(bp.Buf) |
| 95 |
|
| 96 |
local line = bp.Buf:Line(lineN) |
| 97 |
local commentType = bp.Buf.Settings["comment.type"] |
| 98 |
local sel = -bp.Cursor.CurSelection |
| 99 |
local curpos = -bp.Cursor.Loc |
| 100 |
local index = string.find(commentType, "%%s") - 1 |
| 101 |
local indent = string.sub(line, 1, indentLen) |
| 102 |
local trimmedLine = string.sub(line, indentLen + 1) |
| 103 |
trimmedLine = trimmedLine:gsub("%%", "%%%%") |
| 104 |
local commentedLine = commentType:gsub("%%s", trimmedLine) |
| 105 |
bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), indent .. commentedLine) |
| 106 |
if bp.Cursor:HasSelection() then |
| 107 |
bp.Cursor.CurSelection[1].Y = sel[1].Y |
| 108 |
bp.Cursor.CurSelection[2].Y = sel[2].Y |
| 109 |
bp.Cursor.CurSelection[1].X = sel[1].X |
| 110 |
bp.Cursor.CurSelection[2].X = sel[2].X |
| 111 |
else |
| 112 |
bp.Cursor.X = curpos.X + index |
| 113 |
bp.Cursor.Y = curpos.Y |
| 114 |
end |
| 115 |
bp.Cursor:Relocate() |
| 116 |
bp.Cursor:StoreVisualX() |
| 117 |
end |
| 118 |
|
| 119 |
function uncommentLine(bp, lineN, commentRegex) |
| 120 |
updateCommentType(bp.Buf) |
| 121 |
|
| 122 |
local line = bp.Buf:Line(lineN) |
| 123 |
local commentType = bp.Buf.Settings["comment.type"] |
| 124 |
local sel = -bp.Cursor.CurSelection |
| 125 |
local curpos = -bp.Cursor.Loc |
| 126 |
local index = string.find(commentType, "%%s") - 1 |
| 127 |
if not string.match(line, commentRegex) then |
| 128 |
commentRegex = commentRegex:gsub("%s+", "%s*") |
| 129 |
end |
| 130 |
if string.match(line, commentRegex) then |
| 131 |
uncommentedLine = string.match(line, commentRegex) |
| 132 |
bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine) |
| 133 |
if bp.Cursor:HasSelection() then |
| 134 |
bp.Cursor.CurSelection[1].Y = sel[1].Y |
| 135 |
bp.Cursor.CurSelection[2].Y = sel[2].Y |
| 136 |
bp.Cursor.CurSelection[1].X = sel[1].X |
| 137 |
bp.Cursor.CurSelection[2].X = sel[2].X |
| 138 |
else |
| 139 |
bp.Cursor.X = curpos.X - index |
| 140 |
bp.Cursor.Y = curpos.Y |
| 141 |
end |
| 142 |
end |
| 143 |
bp.Cursor:Relocate() |
| 144 |
bp.Cursor:StoreVisualX() |
| 145 |
end |
| 146 |
|
| 147 |
function toggleCommentLine(bp, lineN, commentRegex) |
| 148 |
if isCommented(bp, lineN, commentRegex) then |
| 149 |
uncommentLine(bp, lineN, commentRegex) |
| 150 |
else |
| 151 |
commentLine(bp, lineN, #util.GetLeadingWhitespace(bp.Buf:Line(lineN))) |
| 152 |
end |
| 153 |
end |
| 154 |
|
| 155 |
function toggleCommentSelection(bp, startLine, endLine, commentRegex) |
| 156 |
local allComments = true |
| 157 |
for line = startLine, endLine do |
| 158 |
if not isCommented(bp, line, commentRegex) then |
| 159 |
allComments = false |
| 160 |
break |
| 161 |
end |
| 162 |
end |
| 163 |
|
| 164 |
-- NOTE: we assume that the indentation is either tabs only or spaces only |
| 165 |
local indentMin = -1 |
| 166 |
if not allComments then |
| 167 |
for line = startLine, endLine do |
| 168 |
local indentLen = #util.GetLeadingWhitespace(bp.Buf:Line(line)) |
| 169 |
if indentMin == -1 or indentLen < indentMin then |
| 170 |
indentMin = indentLen |
| 171 |
end |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
for line = startLine, endLine do |
| 176 |
if allComments then |
| 177 |
uncommentLine(bp, line, commentRegex) |
| 178 |
else |
| 179 |
commentLine(bp, line, indentMin) |
| 180 |
end |
| 181 |
end |
| 182 |
end |
| 183 |
|
| 184 |
function comment(bp, args) |
| 185 |
updateCommentType(bp.Buf) |
| 186 |
|
| 187 |
local commentType = bp.Buf.Settings["comment.type"] |
| 188 |
local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)") |
| 189 |
|
| 190 |
if bp.Cursor:HasSelection() then |
| 191 |
if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then |
| 192 |
local endLine = bp.Cursor.CurSelection[1].Y |
| 193 |
if bp.Cursor.CurSelection[1].X == 0 then |
| 194 |
endLine = endLine - 1 |
| 195 |
end |
| 196 |
toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex) |
| 197 |
else |
| 198 |
local endLine = bp.Cursor.CurSelection[2].Y |
| 199 |
if bp.Cursor.CurSelection[2].X == 0 then |
| 200 |
endLine = endLine - 1 |
| 201 |
end |
| 202 |
toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex) |
| 203 |
end |
| 204 |
else |
| 205 |
toggleCommentLine(bp, bp.Cursor.Y, commentRegex) |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
function string.starts(String,Start) |
| 210 |
return string.sub(String,1,string.len(Start))==Start |
| 211 |
end |
| 212 |
|
| 213 |
function preinit() |
| 214 |
config.RegisterCommonOption("comment", "type", "") |
| 215 |
end |
| 216 |
|
| 217 |
function init() |
| 218 |
config.MakeCommand("comment", comment, config.NoComplete) |
| 219 |
config.TryBindKey("Alt-/", "lua:comment.comment", false) |
| 220 |
config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false) |
| 221 |
config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md") |
| 222 |
end |