@@ -159,20 +159,25 @@ function comment(bp, args)
159159 for i = 1 ,# bp .Buf :getCursors () do
160160 local cursor = bp .Buf :getCursor (i - 1 )
161161 local hasSelection = cursor :HasSelection ()
162- local staticEnd = false
163- local startSel = 1
164- local endSel = 2
162+ local excludedEnd = nil
165163 if hasSelection then
166- if cursor .CurSelection [1 ]:GreaterThan (- cursor .CurSelection [2 ]) then
164+ local startSel = 1
165+ local endSel = 2
166+ if cursor .CurSelection [startSel ]:GreaterThan (- cursor .CurSelection [endSel ]) then
167167 startSel = 2
168168 endSel = 1
169169 end
170- for lineN = cursor .CurSelection [startSel ].Y , cursor .CurSelection [endSel ].Y do
171- lines [lineN ] = true
172- end
170+ local fromLineNo = cursor .CurSelection [startSel ].Y
171+ local toLineNo = cursor .CurSelection [endSel ].Y
172+
173+ -- don't indent the line after when selection ends in a newline
173174 if cursor .CurSelection [endSel ].X == 0 then
174- lines [cursor .CurSelection [endSel ].Y ] = nil
175- staticEnd = true
175+ excludedEnd = endSel
176+ toLineNo = toLineNo - 1
177+ end
178+
179+ for lineN = fromLineNo ,toLineNo do
180+ lines [lineN ] = true
176181 end
177182 else
178183 lines [cursor .Y ] = true
@@ -182,9 +187,7 @@ function comment(bp, args)
182187 curpos = - cursor .Loc ,
183188 cursor = cursor ,
184189 hasSelection = hasSelection ,
185- staticEnd = staticEnd ,
186- startSel = startSel ,
187- endSel = endSel
190+ excludedEnd = excludedEnd ,
188191 })
189192 end
190193 -- (un)comment selected lines
@@ -194,11 +197,10 @@ function comment(bp, args)
194197 for i = 1 ,# curData do
195198 local cursor = curData [i ].cursor
196199 if curData [i ].hasSelection then
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
200+ for j = 1 ,2 do
201+ cursor .CurSelection [j ].Y = curData [i ].sel [j ].Y
202+ cursor .CurSelection [j ].X = curData [i ].sel [j ].X + (j == curData [i ].excludedEnd and 0 or displacement )
203+ end
202204 else
203205 cursor .Y = curData [i ].curpos .Y
204206 cursor .X = curData [i ].curpos .X + displacement
0 commit comments