3 local util = import("micro/util")
4 local config = import("micro/config")
5 local buffer = import("micro/buffer")
9 ft["apacheconf"] = "# %s"
15 ft["crystal"] = "# %s"
16 ft["css"] = "/* %s */"
19 ft["dockerfile"] = "# %s"
22 ft["gdscript"] = "# %s"
25 ft["haskell"] = "-- %s"
26 ft["html"] = "<!-- %s -->"
29 ft["javascript"] = "// %s"
30 ft["jinja2"] = "{# %s #}"
32 ft["kotlin"] = "// %s"
34 ft["markdown"] = "<!-- %s -->"
38 ft["pascal"] = "{ %s }"
42 ft["powershell"] = "# %s"
45 ft["python3"] = "# %s"
54 ft["twig"] = "{# %s #}"
56 ft["xml"] = "<!-- %s -->"
59 ft["zscript"] = "// %s"
62 function onBufferOpen(buf)
63 if buf.Settings["commenttype"] == nil then
64 if ft[buf.Settings["filetype"]] ~= nil then
65 buf.Settings["commenttype"] = ft[buf.Settings["filetype"]]
67 buf.Settings["commenttype"] = "# %s"
72 function isCommented(bp, lineN, commentRegex)
73 local line = bp.Buf:Line(lineN)
74 if string.match(line, commentRegex) then
80 function commentLine(bp, lineN)
81 local line = bp.Buf:Line(lineN)
82 local commentType = bp.Buf.Settings["commenttype"]
83 local sel = -bp.Cursor.CurSelection
84 local curpos = -bp.Cursor.Loc
85 local index = string.find(commentType, "%%s") - 1
86 local commentedLine = commentType:gsub("%%s", trim(line))
87 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
88 if bp.Cursor:HasSelection() then
89 bp.Cursor.CurSelection[1].Y = sel[1].Y
90 bp.Cursor.CurSelection[2].Y = sel[2].Y
91 bp.Cursor.CurSelection[1].X = sel[1].X
92 bp.Cursor.CurSelection[2].X = sel[2].X
94 bp.Cursor.X = curpos.X + index
95 bp.Cursor.Y = curpos.Y
98 bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
101 function uncommentLine(bp, lineN, commentRegex)
102 local line = bp.Buf:Line(lineN)
103 local commentType = bp.Buf.Settings["commenttype"]
104 local sel = -bp.Cursor.CurSelection
105 local curpos = -bp.Cursor.Loc
106 local index = string.find(commentType, "%%s") - 1
107 if string.match(line, commentRegex) then
108 uncommentedLine = string.match(line, commentRegex)
109 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine)
110 if bp.Cursor:HasSelection() then
111 bp.Cursor.CurSelection[1].Y = sel[1].Y
112 bp.Cursor.CurSelection[2].Y = sel[2].Y
113 bp.Cursor.CurSelection[1].X = sel[1].X
114 bp.Cursor.CurSelection[2].X = sel[2].X
116 bp.Cursor.X = curpos.X - index
117 bp.Cursor.Y = curpos.Y
121 bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
124 function toggleCommentLine(bp, lineN, commentRegex)
125 if isCommented(bp, lineN, commentRegex) then
126 uncommentLine(bp, lineN, commentRegex)
128 commentLine(bp, lineN)
132 function toggleCommentSelection(bp, startLine, endLine, commentRegex)
133 local allComments = true
134 for line = startLine, endLine do
135 if not isCommented(bp, line, commentRegex) then
140 for line = startLine, endLine do
142 uncommentLine(bp, line, commentRegex)
144 commentLine(bp, line)
149 function comment(bp, args)
150 local commentType = bp.Buf.Settings["commenttype"]
151 local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)"):gsub("%s+", "%s*")
153 if bp.Cursor:HasSelection() then
154 if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
155 local endLine = bp.Cursor.CurSelection[1].Y
156 if bp.Cursor.CurSelection[1].X == 0 then
157 endLine = endLine - 1
159 toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex)
161 local endLine = bp.Cursor.CurSelection[2].Y
162 if bp.Cursor.CurSelection[2].X == 0 then
163 endLine = endLine - 1
165 toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex)
168 toggleCommentLine(bp, bp.Cursor.Y, commentRegex)
173 local trimmed = s:gsub("^%s*(.-)%s*$", "%1"):gsub("%%","%%%%")
177 function string.starts(String,Start)
178 return string.sub(String,1,string.len(Start))==Start
182 config.MakeCommand("comment", comment, config.NoComplete)
183 config.TryBindKey("Alt-/", "lua:comment.comment", false)
184 config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false)
185 config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md")