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 commentLine(bp, lineN)
73 local line = bp.Buf:Line(lineN)
74 local commentType = bp.Buf.Settings["commenttype"]
75 local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)")
76 local sel = -bp.Cursor.CurSelection
77 local curpos = -bp.Cursor.Loc
78 local index = string.find(commentType, "%%s") - 1
79 if string.match(line, commentRegex) then
80 uncommentedLine = string.match(line, commentRegex)
81 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine)
82 if bp.Cursor:HasSelection() then
83 bp.Cursor.CurSelection[1].Y = sel[1].Y
84 bp.Cursor.CurSelection[2].Y = sel[2].Y
85 bp.Cursor.CurSelection[1].X = sel[1].X
86 bp.Cursor.CurSelection[2].X = sel[2].X
88 bp.Cursor.X = curpos.X - index
89 bp.Cursor.Y = curpos.Y
92 local commentedLine = commentType:gsub("%%s", trim(line))
93 bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
94 if bp.Cursor:HasSelection() then
95 bp.Cursor.CurSelection[1].Y = sel[1].Y
96 bp.Cursor.CurSelection[2].Y = sel[2].Y
97 bp.Cursor.CurSelection[1].X = sel[1].X
98 bp.Cursor.CurSelection[2].X = sel[2].X
100 bp.Cursor.X = curpos.X + index
101 bp.Cursor.Y = curpos.Y
105 bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
108 function commentSelection(bp, startLine, endLine)
109 for line = startLine, endLine do
110 commentLine(bp, line)
114 function comment(bp, args)
115 if bp.Cursor:HasSelection() then
116 if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
117 local endLine = bp.Cursor.CurSelection[1].Y
118 if bp.Cursor.CurSelection[1].X == 0 then
119 endLine = endLine - 1
121 commentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine)
123 local endLine = bp.Cursor.CurSelection[2].Y
124 if bp.Cursor.CurSelection[2].X == 0 then
125 endLine = endLine - 1
127 commentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine)
130 commentLine(bp, bp.Cursor.Y)
135 local trimmed = s:gsub("^%s*(.-)%s*$", "%1"):gsub("%%","%%%%")
139 function string.starts(String,Start)
140 return string.sub(String,1,string.len(Start))==Start
144 config.MakeCommand("comment", comment, config.NoComplete)
145 config.TryBindKey("Alt-/", "lua:comment.comment", false)
146 config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md")