Skip to content

Commit d23fdbc

Browse files
committed
added exception for selections that end in a new line (for backwards compatibility)
1 parent 609794b commit d23fdbc

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

runtime/plugins/comment/comment.lua

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,36 @@ function comment(bp, args)
150150
local lines = {}
151151
local curData = {}
152152
-- gather cursor data and lines to (un)comment
153-
for i = 0,#bp.Buf:getCursors()-1 do
154-
local cursor = bp.Buf:getCursor(i)
153+
for i = 1,#bp.Buf:getCursors() do
154+
local cursor = bp.Buf:getCursor(i-1)
155155
local hasSelection = cursor:HasSelection()
156-
table.insert(curData, {
157-
sel = -cursor.CurSelection,
158-
curpos = -cursor.Loc,
159-
cursor = cursor,
160-
hasSelection = hasSelection
161-
})
156+
local staticEnd = false
157+
local startSel = 1
158+
local endSel = 2
162159
if hasSelection then
163-
for lineN = cursor.CurSelection[1].Y, cursor.CurSelection[2].Y do
160+
if cursor.CurSelection[1]:GreaterThan(-cursor.CurSelection[2]) then
161+
startSel = 2
162+
endSel = 1
163+
end
164+
for lineN = cursor.CurSelection[startSel].Y, cursor.CurSelection[endSel].Y do
164165
lines[lineN] = true
165166
end
167+
if cursor.CurSelection[endSel].X == 0 then
168+
lines[cursor.CurSelection[endSel].Y] = nil
169+
staticEnd = true
170+
end
166171
else
167172
lines[cursor.Y] = true
168173
end
174+
table.insert(curData, {
175+
sel = -cursor.CurSelection,
176+
curpos = -cursor.Loc,
177+
cursor = cursor,
178+
hasSelection = hasSelection,
179+
staticEnd = staticEnd,
180+
startSel = startSel,
181+
endSel = endSel
182+
})
169183
end
170184
-- (un)comment selected lines
171185
local commented = toggleCommentSelection(bp, lines, commentRegex)
@@ -174,10 +188,11 @@ function comment(bp, args)
174188
for i=1,#curData do
175189
local cursor = curData[i].cursor
176190
if curData[i].hasSelection then
177-
cursor.CurSelection[1].Y = curData[i].sel[1].Y
178-
cursor.CurSelection[2].Y = curData[i].sel[2].Y
179-
cursor.CurSelection[1].X = curData[i].sel[1].X + displacement
180-
cursor.CurSelection[2].X = curData[i].sel[2].X + displacement
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
181196
else
182197
cursor.Y = curData[i].curpos.Y
183198
cursor.X = curData[i].curpos.X + displacement
@@ -191,7 +206,7 @@ function string.starts(String,Start)
191206
return string.sub(String,1,string.len(Start))==Start
192207
end
193208

194-
function init()
209+
functioninit()
195210
config.MakeCommand("comment", comment, config.NoComplete)
196211
config.TryBindKey("Alt-/", "lua:comment.comment", false)
197212
config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false)

0 commit comments

Comments
 (0)