@@ -156,22 +156,36 @@ function comment(bp, args)
156156 local lines = {}
157157 local curData = {}
158158 -- gather cursor data and lines to (un)comment
159- for i = 0 ,# bp .Buf :getCursors ()- 1 do
160- local cursor = bp .Buf :getCursor (i )
159+ for i = 1 ,# bp .Buf :getCursors () do
160+ local cursor = bp .Buf :getCursor (i - 1 )
161161 local hasSelection = cursor :HasSelection ()
162- table.insert (curData , {
163- sel = - cursor .CurSelection ,
164- curpos = - cursor .Loc ,
165- cursor = cursor ,
166- hasSelection = hasSelection
167- })
162+ local staticEnd = false
163+ local startSel = 1
164+ local endSel = 2
168165 if hasSelection then
169- for lineN = cursor .CurSelection [1 ].Y , cursor .CurSelection [2 ].Y do
166+ if cursor .CurSelection [1 ]:GreaterThan (- cursor .CurSelection [2 ]) then
167+ startSel = 2
168+ endSel = 1
169+ end
170+ for lineN = cursor .CurSelection [startSel ].Y , cursor .CurSelection [endSel ].Y do
170171 lines [lineN ] = true
171172 end
173+ if cursor .CurSelection [endSel ].X == 0 then
174+ lines [cursor .CurSelection [endSel ].Y ] = nil
175+ staticEnd = true
176+ end
172177 else
173178 lines [cursor .Y ] = true
174179 end
180+ table.insert (curData , {
181+ sel = - cursor .CurSelection ,
182+ curpos = - cursor .Loc ,
183+ cursor = cursor ,
184+ hasSelection = hasSelection ,
185+ staticEnd = staticEnd ,
186+ startSel = startSel ,
187+ endSel = endSel
188+ })
175189 end
176190 -- (un)comment selected lines
177191 local commented = toggleCommentSelection (bp , lines , commentRegex )
@@ -180,10 +194,11 @@ function comment(bp, args)
180194 for i = 1 ,# curData do
181195 local cursor = curData [i ].cursor
182196 if curData [i ].hasSelection then
183- cursor .CurSelection [1 ].Y = curData [i ].sel [1 ].Y
184- cursor .CurSelection [2 ].Y = curData [i ].sel [2 ].Y
185- cursor .CurSelection [1 ].X = curData [i ].sel [1 ].X + displacement
186- cursor .CurSelection [2 ].X = curData [i ].sel [2 ].X + displacement
197+ local sel , startSel , endSel = curData [i ].sel , curData [i ].startSel , curData [i ].endSel
198+ cursor .CurSelection [startSel ].X = sel [startSel ].X + displacement
199+ cursor .CurSelection [startSel ].Y = sel [startSel ].Y
200+ cursor .CurSelection [endSel ].X = sel [endSel ].X + (curData [i ].staticEnd and 0 or displacement )
201+ cursor .CurSelection [endSel ].Y = sel [endSel ].Y
187202 else
188203 cursor .Y = curData [i ].curpos .Y
189204 cursor .X = curData [i ].curpos .X + displacement
0 commit comments