]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/comment/comment.lua
Add support for dozens more languages to the comment plugin (#1729)
[micro.git] / runtime / plugins / comment / comment.lua
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
7 local ft = {}
8
9 ft["apacheconf"] = "# %s"
10 ft["bat"] = ":: %s"
11 ft["c"] = "// %s"
12 ft["c++"] = "// %s"
13 ft["cmake"] = "# %s"
14 ft["conf"] = "# %s"
15 ft["crystal"] = "# %s"
16 ft["css"] = "/* %s */"
17 ft["d"] = "// %s"
18 ft["dart"] = "// %s"
19 ft["dockerfile"] = "# %s"
20 ft["elm"] = "-- %s"
21 ft["fish"] = "# %s"
22 ft["gdscript"] = "# %s"
23 ft["glsl"] = "// %s"
24 ft["go"] = "// %s"
25 ft["haskell"] = "-- %s"
26 ft["html"] = "<!-- %s -->"
27 ft["ini"] = "; %s"
28 ft["java"] = "// %s"
29 ft["javascript"] = "// %s"
30 ft["jinja2"] = "{# %s #}"
31 ft["julia"] = "# %s"
32 ft["kotlin"] = "// %s"
33 ft["lua"] = "-- %s"
34 ft["markdown"] = "<!-- %s -->"
35 ft["nginx"] = "# %s"
36 ft["nim"] = "# %s"
37 ft["objc"] = "// %s"
38 ft["pascal"] = "{ %s }"
39 ft["perl"] = "# %s"
40 ft["php"] = "// %s"
41 ft["pony"] = "// %s"
42 ft["powershell"] = "# %s"
43 ft["proto"] = "// %s"
44 ft["python"] = "# %s"
45 ft["python3"] = "# %s"
46 ft["ruby"] = "# %s"
47 ft["rust"] = "// %s"
48 ft["scala"] = "// %s"
49 ft["shell"] = "# %s"
50 ft["sql"] = "-- %s"
51 ft["swift"] = "// %s"
52 ft["tex"] = "% %s"
53 ft["toml"] = "# %s"
54 ft["twig"] = "{# %s #}"
55 ft["v"] = "// %s"
56 ft["xml"] = "<!-- %s -->"
57 ft["yaml"] = "# %s"
58 ft["zig"] = "// %s"
59 ft["zscript"] = "// %s"
60 ft["zsh"] = "# %s"
61
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"]]
66         else
67             buf.Settings["commenttype"] = "# %s"
68         end
69     end
70 end
71
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
87         else
88             bp.Cursor.X = curpos.X - index
89             bp.Cursor.Y = curpos.Y
90         end
91     else
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
99         else
100             bp.Cursor.X = curpos.X + index
101             bp.Cursor.Y = curpos.Y
102         end
103     end
104     bp.Cursor:Relocate()
105     bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
106 end
107
108 function commentSelection(bp, startLine, endLine)
109     for line = startLine, endLine do
110         commentLine(bp, line)
111     end
112 end
113
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
120             end
121             commentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine)
122         else
123             local endLine = bp.Cursor.CurSelection[2].Y
124             if bp.Cursor.CurSelection[2].X == 0 then
125                 endLine = endLine - 1
126             end
127             commentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine)
128         end
129     else
130         commentLine(bp, bp.Cursor.Y)
131     end
132 end
133
134 function trim(s)
135     local trimmed = s:gsub("^%s*(.-)%s*$", "%1"):gsub("%%","%%%%")
136     return trimmed
137 end
138
139 function string.starts(String,Start)
140     return string.sub(String,1,string.len(Start))==Start
141 end
142
143 function init()
144     config.MakeCommand("comment", comment, config.NoComplete)
145     config.TryBindKey("Alt-/", "lua:comment.comment", false)
146     config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md")
147 end