]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/comment/comment.lua
better top
[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["c"] = "// %s"
10 ft["c++"] = "// %s"
11 ft["go"] = "// %s"
12 ft["python"] = "# %s"
13 ft["python3"] = "# %s"
14 ft["html"] = "<!-- %s -->"
15 ft["java"] = "// %s"
16 ft["julia"] = "# %s"
17 ft["perl"] = "# %s"
18 ft["php"] = "// %s"
19 ft["rust"] = "// %s"
20 ft["shell"] = "# %s"
21 ft["lua"] = "-- %s"
22 ft["javascript"] = "// %s"
23 ft["ruby"] = "# %s"
24 ft["d"] = "// %s"
25 ft["swift"] = "// %s"
26
27 function onBufferOpen(buf)
28     if buf.Settings["commenttype"] == nil then
29         if ft[buf.Settings["filetype"]] ~= nil then
30             buf.Settings["commenttype"] = ft[buf.Settings["filetype"]]
31         else
32             buf.Settings["commenttype"] = "# %s"
33         end
34     end
35 end
36
37 function commentLine(bp, lineN)
38     local line = bp.Buf:Line(lineN)
39     local commentType = bp.Buf.Settings["commenttype"]
40     local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%s", "(.*)")
41     local sel = -bp.Cursor.CurSelection
42     local curpos = -bp.Cursor.Loc
43     local index = string.find(commentType, "%%s") - 1
44     if string.match(line, commentRegex) then
45         uncommentedLine = string.match(line, commentRegex)
46         bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine)
47         if bp.Cursor:HasSelection() then
48             bp.Cursor.CurSelection[1].Y = sel[1].Y
49             bp.Cursor.CurSelection[2].Y = sel[2].Y
50             bp.Cursor.CurSelection[1].X = sel[1].X
51             bp.Cursor.CurSelection[2].X = sel[2].X
52         else
53             bp.Cursor.X = curpos.X - index
54             bp.Cursor.Y = curpos.Y
55         end
56     else
57         local commentedLine = commentType:gsub("%%s", trim(line))
58         bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. commentedLine)
59         if bp.Cursor:HasSelection() then
60             bp.Cursor.CurSelection[1].Y = sel[1].Y
61             bp.Cursor.CurSelection[2].Y = sel[2].Y
62             bp.Cursor.CurSelection[1].X = sel[1].X
63             bp.Cursor.CurSelection[2].X = sel[2].X
64         else
65             bp.Cursor.X = curpos.X + index
66             bp.Cursor.Y = curpos.Y
67         end
68     end
69     bp.Cursor:Relocate()
70     bp.Cursor.LastVisualX = bp.Cursor:GetVisualX()
71 end
72
73 function commentSelection(bp, startLine, endLine)
74     for line = startLine, endLine do
75         commentLine(bp, line)
76     end
77 end
78
79 function comment(bp, args)
80     if bp.Cursor:HasSelection() then
81         if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
82             local endLine = bp.Cursor.CurSelection[1].Y
83             if bp.Cursor.CurSelection[1].X == 0 then
84                 endLine = endLine - 1
85             end
86             commentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine)
87         else
88             local endLine = bp.Cursor.CurSelection[2].Y
89             if bp.Cursor.CurSelection[2].X == 0 then
90                 endLine = endLine - 1
91             end
92             commentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine)
93         end
94     else
95         commentLine(bp, bp.Cursor.Y)
96     end
97 end
98
99 function trim(s)
100     return (s:gsub("^%s*(.-)%s*$", "%1"))
101 end
102
103 function string.starts(String,Start)
104     return string.sub(String,1,string.len(Start))==Start
105 end
106
107 function init()
108     config.MakeCommand("comment", comment, config.NoComplete)
109     config.TryBindKey("Alt-/", "lua:comment.comment", false)
110     config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md")
111 end