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