1- VERSION = " 1.0.5 "
1+ VERSION = " 1.0.6 "
22
33ft = {}
44
55ft [" c" ] = " // %s"
6- ft [" c++" ] = " // %s"
7- ft [" d" ] = " // %s"
86ft [" go" ] = " // %s"
7+ ft [" python" ] = " # %s"
8+ ft [" python3" ] = " # %s"
99ft [" html" ] = " <!-- %s -->"
1010ft [" java" ] = " // %s"
11- ft [" javascript" ] = " // %s"
12- ft [" julia" ] = " # %s"
13- ft [" lua" ] = " -- %s"
1411ft [" perl" ] = " # %s"
15- ft [" php" ] = " // %s"
16- ft [" python" ] = " # %s"
17- ft [" python3" ] = " # %s"
18- ft [" ruby" ] = " # %s"
1912ft [" rust" ] = " // %s"
2013ft [" shell" ] = " # %s"
14+ ft [" lua" ] = " -- %s"
15+ ft [" javascript" ] = " // %s"
16+ ft [" ruby" ] = " # %s"
17+ ft [" d" ] = " // %s"
2118ft [" swift" ] = " // %s"
2219
2320function onViewOpen (v )
@@ -34,32 +31,33 @@ function commentLine(lineN)
3431 local v = CurView ()
3532 local line = v .Buf :Line (lineN )
3633 local commentType = v .Buf .Settings [" commenttype" ]
37- if commentType == " " or not string.find (commentType , " %%s" ) then
38- return
39- end
40- local commentRegex = " ^%s*" .. commentType :gsub (" %*" , " %*" ):gsub (" %-" , " %-" ):gsub (" %." , " %." ):gsub (" %+" , " %+" ):gsub (" %[" , " %[" ):gsub (" %%s" , " (.*)" )
34+ local commentRegex = " ^%s*" .. commentType :gsub (" %*" , " %*" ):gsub (" %-" , " %-" ):gsub (" %." , " %." ):gsub (" %+" , " %+" ):gsub (" %]" , " %]" ):gsub (" %[" , " %[" ):gsub (" %%s" , " (.*)" )
35+ local sel = - v .Buf .Cursor .CurSelection
36+ local curpos = - v .Buf .Cursor .Loc
4137 local index = string.find (commentType , " %%s" ) - 1
4238 if string.match (line , commentRegex ) then
4339 uncommentedLine = string.match (line , commentRegex )
4440 v .Buf :Replace (Loc (0 , lineN ), Loc (# line , lineN ), GetLeadingWhitespace (line ) .. uncommentedLine )
4541 if v .Buf .Cursor :HasSelection () then
46- if v .Buf .Cursor .CurSelection [1 ].Y == v . Buf . Cursor . CurSelection [ 2 ].Y then
47- v .Buf .Cursor .CurSelection [1 ]. X = v . Buf . Cursor . CurSelection [ 1 ]. X - index
48- v .Buf .Cursor .CurSelection [2 ].X = v . Buf . Cursor . CurSelection [ 2 ].X - index
49- end
42+ v .Buf .Cursor .CurSelection [1 ].Y = sel [ 1 ].Y
43+ v .Buf .Cursor .CurSelection [2 ]. Y = sel [ 2 ]. Y
44+ v .Buf .Cursor .CurSelection [1 ].X = sel [ 1 ].X
45+ v . Buf . Cursor . CurSelection [ 2 ]. X = sel [ 2 ]. X
5046 else
51- v .Buf .Cursor .X = v .Buf .Cursor .X - index
47+ v .Buf .Cursor .X = curpos .X - index
48+ v .Buf .Cursor .Y = curpos .Y
5249 end
5350 else
5451 local commentedLine = commentType :gsub (" %%s" , trim (line ))
5552 v .Buf :Replace (Loc (0 , lineN ), Loc (# line , lineN ), GetLeadingWhitespace (line ) .. commentedLine )
5653 if v .Buf .Cursor :HasSelection () then
57- if v .Buf .Cursor .CurSelection [1 ].Y == v . Buf . Cursor . CurSelection [ 2 ].Y then
58- v .Buf .Cursor .CurSelection [1 ]. X = v . Buf . Cursor . CurSelection [ 1 ]. X + index
59- v .Buf .Cursor .CurSelection [2 ].X = v . Buf . Cursor . CurSelection [ 2 ].X + index
60- end
54+ v .Buf .Cursor .CurSelection [1 ].Y = sel [ 1 ].Y
55+ v .Buf .Cursor .CurSelection [2 ]. Y = sel [ 2 ]. Y
56+ v .Buf .Cursor .CurSelection [1 ].X = sel [ 1 ].X
57+ v . Buf . Cursor . CurSelection [ 2 ]. X = sel [ 2 ]. X
6158 else
62- v .Buf .Cursor .X = v .Buf .Cursor .X + index
59+ v .Buf .Cursor .X = curpos .X + index
60+ v .Buf .Cursor .Y = curpos .Y
6361 end
6462 end
6563 v .Cursor :Relocate ()
@@ -76,9 +74,17 @@ function comment()
7674 local v = CurView ()
7775 if v .Cursor :HasSelection () then
7876 if v .Cursor .CurSelection [1 ]:GreaterThan (- v .Cursor .CurSelection [2 ]) then
79- commentSelection (v .Cursor .CurSelection [2 ].Y , v .Cursor .CurSelection [1 ].Y )
77+ local endLine = v .Cursor .CurSelection [1 ].Y
78+ if v .Cursor .CurSelection [1 ].X == 0 then
79+ endLine = endLine - 1
80+ end
81+ commentSelection (v .Cursor .CurSelection [2 ].Y , endLine )
8082 else
81- commentSelection (v .Cursor .CurSelection [1 ].Y , v .Cursor .CurSelection [2 ].Y )
83+ local endLine = v .Cursor .CurSelection [2 ].Y
84+ if v .Cursor .CurSelection [2 ].X == 0 then
85+ endLine = endLine - 1
86+ end
87+ commentSelection (v .Cursor .CurSelection [1 ].Y , endLine )
8288 end
8389 else
8490 commentLine (v .Cursor .Y )
@@ -97,4 +103,3 @@ MakeCommand("comment", "comment.comment")
97103BindKey (" Alt-/" , " comment.comment" )
98104
99105AddRuntimeFile (" comment" , " help" , " help/comment-plugin.md" )
100-
0 commit comments