@@ -153,20 +153,25 @@ function comment(bp, args)
153153 for i = 1 ,# bp .Buf :getCursors () do
154154 local cursor = bp .Buf :getCursor (i - 1 )
155155 local hasSelection = cursor :HasSelection ()
156- local staticEnd = false
157- local startSel = 1
158- local endSel = 2
156+ local excludedEnd = nil
159157 if hasSelection then
160- if cursor .CurSelection [1 ]:GreaterThan (- cursor .CurSelection [2 ]) then
158+ local startSel = 1
159+ local endSel = 2
160+ if cursor .CurSelection [startSel ]:GreaterThan (- cursor .CurSelection [endSel ]) then
161161 startSel = 2
162162 endSel = 1
163163 end
164- for lineN = cursor .CurSelection [startSel ].Y , cursor .CurSelection [endSel ].Y do
165- lines [lineN ] = true
166- end
164+ local fromLineNo = cursor .CurSelection [startSel ].Y
165+ local toLineNo = cursor .CurSelection [endSel ].Y
166+
167+ -- don't indent the line after when selection ends in a newline
167168 if cursor .CurSelection [endSel ].X == 0 then
168- lines [cursor .CurSelection [endSel ].Y ] = nil
169- staticEnd = true
169+ excludedEnd = endSel
170+ toLineNo = toLineNo - 1
171+ end
172+
173+ for lineN = fromLineNo ,toLineNo do
174+ lines [lineN ] = true
170175 end
171176 else
172177 lines [cursor .Y ] = true
@@ -176,9 +181,7 @@ function comment(bp, args)
176181 curpos = - cursor .Loc ,
177182 cursor = cursor ,
178183 hasSelection = hasSelection ,
179- staticEnd = staticEnd ,
180- startSel = startSel ,
181- endSel = endSel
184+ excludedEnd = excludedEnd ,
182185 })
183186 end
184187 -- (un)comment selected lines
@@ -188,11 +191,10 @@ function comment(bp, args)
188191 for i = 1 ,# curData do
189192 local cursor = curData [i ].cursor
190193 if curData [i ].hasSelection then
191- local sel , startSel , endSel = curData [i ].sel , curData [i ].startSel , curData [i ].endSel
192- cursor .CurSelection [startSel ].X = sel [startSel ].X + displacement
193- cursor .CurSelection [startSel ].Y = sel [startSel ].Y
194- cursor .CurSelection [endSel ].X = sel [endSel ].X + (curData [i ].staticEnd and 0 or displacement )
195- cursor .CurSelection [endSel ].Y = sel [endSel ].Y
194+ for j = 1 ,2 do
195+ cursor .CurSelection [j ].Y = curData [i ].sel [j ].Y
196+ cursor .CurSelection [j ].X = curData [i ].sel [j ].X + (j == curData [i ].excludedEnd and 0 or displacement )
197+ end
196198 else
197199 cursor .Y = curData [i ].curpos .Y
198200 cursor .X = curData [i ].curpos .X + displacement
@@ -206,7 +208,7 @@ function string.starts(String,Start)
206208 return string.sub (String ,1 ,string.len (Start ))== Start
207209end
208210
209- functioninit ()
211+ function init ()
210212 config .MakeCommand (" comment" , comment , config .NoComplete )
211213 config .TryBindKey (" Alt-/" , " lua:comment.comment" , false )
212214 config .TryBindKey (" CtrlUnderscore" , " lua:comment.comment" , false )
0 commit comments