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["ocaml"] = "(* %s *)"
39 ft["pascal"] = "{ %s }"
43 ft["powershell"] = "# %s"
46 ft["python3"] = "# %s"
55 ft["twig"] = "{# %s #}"
57 ft["xml"] = "<!-- %s -->"
60 ft["zscript"] = "// %s"
63 function onBufferOpen(buf)
64 if buf.Settings["commenttype"] == nil then
65 if ft[buf.Settings["filetype"]] ~= nil then
66 buf.Settings["commenttype"] = ft[buf.Settings["filetype"]]
68 buf.Settings["commenttype"] = "# %s"
73 function isCommented(bp, lineN, commentRegex)
74 local line = bp.Buf:Line(lineN)
75 if string.match(line, commentRegex) then
81 function commentLine(bp, lineN)
82 local line = bp.Buf:Line(lineN)
83 local commentType = bp.Buf.Settings["commenttype"]
84 local sel = -bp.Cursor.CurSelection
85 local curpos = -bp.Cursor.Loc
86 local index = string.find(commentType, "%%s") - 1
87 local commentedLine = commentType:gsub("%%s", trim(line))
88 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
89 if bp.Cursor:HasSelection() then
90 bp.Cursor.CurSelection[1].Y = sel[1].Y
91 bp.Cursor.CurSelection[2].Y = sel[2].Y
92 bp.Cursor.CurSelection[1].X = sel[1].X
93 bp.Cursor.CurSelection[2].X = sel[2].X
95 bp.Cursor.X = curpos.X + index
96 bp.Cursor.Y = curpos.Y
99 bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
102 function uncommentLine(bp, lineN, commentRegex)
103 local line = bp.Buf:Line(lineN)
104 local commentType = bp.Buf.Settings["commenttype"]
105 local sel = -bp.Cursor.CurSelection
106 local curpos = -bp.Cursor.Loc
107 local index = string.find(commentType, "%%s") - 1
108 if string.match(line, commentRegex) then
109 uncommentedLine = string.match(line, commentRegex)
110 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine)
111 if bp.Cursor:HasSelection() then
112 bp.Cursor.CurSelection[1].Y = sel[1].Y
113 bp.Cursor.CurSelection[2].Y = sel[2].Y
114 bp.Cursor.CurSelection[1].X = sel[1].X
115 bp.Cursor.CurSelection[2].X = sel[2].X
117 bp.Cursor.X = curpos.X - index
118 bp.Cursor.Y = curpos.Y
122 bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
125 function toggleCommentLine(bp, lineN, commentRegex)
126 if isCommented(bp, lineN, commentRegex) then
127 uncommentLine(bp, lineN, commentRegex)
129 commentLine(bp, lineN)
133 function toggleCommentSelection(bp, startLine, endLine, commentRegex)
134 local allComments = true
135 for line = startLine, endLine do
136 if not isCommented(bp, line, commentRegex) then
142 for line = startLine, endLine do
144 uncommentLine(bp, line, commentRegex)
146 commentLine(bp, line)
151 function comment(bp, args)
152 local commentType = bp.Buf.Settings["commenttype"]
153 local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)"):gsub("%s+", "%s*")
155 if bp.Cursor:HasSelection() then
156 if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
157 local endLine = bp.Cursor.CurSelection[1].Y
158 if bp.Cursor.CurSelection[1].X == 0 then
159 endLine = endLine - 1
161 toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex)
163 local endLine = bp.Cursor.CurSelection[2].Y
164 if bp.Cursor.CurSelection[2].X == 0 then
165 endLine = endLine - 1
167 toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex)
170 toggleCommentLine(bp, bp.Cursor.Y, commentRegex)
175 local trimmed = s:gsub("^%s*(.-)%s*$", "%1"):gsub("%%","%%%%")
179 function string.starts(String,Start)
180 return string.sub(String,1,string.len(Start))==Start
184 config.MakeCommand("comment", comment, config.NoComplete)
185 config.TryBindKey("Alt-/", "lua:comment.comment", false)
186 config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false)
187 config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md")