-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcomment.lua
110 lines (100 loc) · 3.41 KB
/
comment.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
VERSION = "1.0.6"
ft = {}
ft["c"] = "// %s"
ft["c++"] = "// %s"
ft["go"] = "// %s"
ft["python"] = "# %s"
ft["python3"] = "# %s"
ft["html"] = "<!-- %s -->"
ft["java"] = "// %s"
ft["julia"] = "# %s"
ft["perl"] = "# %s"
ft["php"] = "// %s"
ft["rust"] = "// %s"
ft["shell"] = "# %s"
ft["lua"] = "-- %s"
ft["javascript"] = "// %s"
ft["ruby"] = "# %s"
ft["d"] = "// %s"
ft["swift"] = "// %s"
ft["julia"] = "# %s"
ft["latex"] = "\% %s"
function onViewOpen(v)
if v.Buf.Settings["commenttype"] == nil then
if ft[v.Buf.Settings["filetype"]] ~= nil then
v.Buf.Settings["commenttype"] = ft[v.Buf.Settings["filetype"]]
else
v.Buf.Settings["commenttype"] = "# %s"
end
end
end
function commentLine(lineN)
local v = CurView()
local line = v.Buf:Line(lineN)
local commentType = v.Buf.Settings["commenttype"]
local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%s", "(.*)")
local sel = -v.Buf.Cursor.CurSelection
local curpos = -v.Buf.Cursor.Loc
local index = string.find(commentType, "%%s") - 1
if string.match(line, commentRegex) then
uncommentedLine = string.match(line, commentRegex)
v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. uncommentedLine)
if v.Buf.Cursor:HasSelection() then
v.Buf.Cursor.CurSelection[1].Y = sel[1].Y
v.Buf.Cursor.CurSelection[2].Y = sel[2].Y
v.Buf.Cursor.CurSelection[1].X = sel[1].X
v.Buf.Cursor.CurSelection[2].X = sel[2].X
else
v.Buf.Cursor.X = curpos.X - index
v.Buf.Cursor.Y = curpos.Y
end
else
local commentedLine = commentType:gsub("%%s", trim(line))
v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. commentedLine)
if v.Buf.Cursor:HasSelection() then
v.Buf.Cursor.CurSelection[1].Y = sel[1].Y
v.Buf.Cursor.CurSelection[2].Y = sel[2].Y
v.Buf.Cursor.CurSelection[1].X = sel[1].X
v.Buf.Cursor.CurSelection[2].X = sel[2].X
else
v.Buf.Cursor.X = curpos.X + index
v.Buf.Cursor.Y = curpos.Y
end
end
v.Cursor:Relocate()
v.Cursor.LastVisualX = v.Cursor:GetVisualX()
end
function commentSelection(startLine, endLine)
for line = startLine, endLine do
commentLine(line)
end
end
function comment()
local v = CurView()
if v.Cursor:HasSelection() then
if v.Cursor.CurSelection[1]:GreaterThan(-v.Cursor.CurSelection[2]) then
local endLine = v.Cursor.CurSelection[1].Y
if v.Cursor.CurSelection[1].X == 0 then
endLine = endLine - 1
end
commentSelection(v.Cursor.CurSelection[2].Y, endLine)
else
local endLine = v.Cursor.CurSelection[2].Y
if v.Cursor.CurSelection[2].X == 0 then
endLine = endLine - 1
end
commentSelection(v.Cursor.CurSelection[1].Y, endLine)
end
else
commentLine(v.Cursor.Y)
end
end
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
MakeCommand("comment", "comment.comment")
BindKey("Alt-/", "comment.comment")
AddRuntimeFile("comment", "help", "help/comment-plugin.md")