Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 51 additions & 46 deletions runtime/plugins/comment/comment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,69 +90,69 @@ function isCommented(bp, lineN, commentRegex)
return false
end

function commentLine(bp, lineN, indentLen)
function commentLine(bp, cursor, lineN, indentLen)
updateCommentType(bp.Buf)

local line = bp.Buf:Line(lineN)
local commentType = bp.Buf.Settings["comment.type"]
local sel = -bp.Cursor.CurSelection
local curpos = -bp.Cursor.Loc
local sel = -cursor.CurSelection
local curpos = -cursor.Loc
local index = string.find(commentType, "%%s") - 1
local indent = string.sub(line, 1, indentLen)
local trimmedLine = string.sub(line, indentLen + 1)
trimmedLine = trimmedLine:gsub("%%", "%%%%")
local commentedLine = commentType:gsub("%%s", trimmedLine)
bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), indent .. commentedLine)
if bp.Cursor:HasSelection() then
bp.Cursor.CurSelection[1].Y = sel[1].Y
bp.Cursor.CurSelection[2].Y = sel[2].Y
bp.Cursor.CurSelection[1].X = sel[1].X
bp.Cursor.CurSelection[2].X = sel[2].X
if cursor:HasSelection() then
cursor.CurSelection[1].Y = sel[1].Y
cursor.CurSelection[2].Y = sel[2].Y
cursor.CurSelection[1].X = sel[1].X
cursor.CurSelection[2].X = sel[2].X
else
bp.Cursor.X = curpos.X + index
bp.Cursor.Y = curpos.Y
cursor.X = curpos.X + index
cursor.Y = curpos.Y
end
bp.Cursor:Relocate()
bp.Cursor:StoreVisualX()
cursor:Relocate()
cursor:StoreVisualX()
end

function uncommentLine(bp, lineN, commentRegex)
function uncommentLine(bp, cursor, lineN, commentRegex)
updateCommentType(bp.Buf)

local line = bp.Buf:Line(lineN)
local commentType = bp.Buf.Settings["comment.type"]
local sel = -bp.Cursor.CurSelection
local curpos = -bp.Cursor.Loc
local sel = -cursor.CurSelection
local curpos = -cursor.Loc
local index = string.find(commentType, "%%s") - 1
if not string.match(line, commentRegex) then
commentRegex = commentRegex:gsub("%s+", "%s*")
end
if string.match(line, commentRegex) then
uncommentedLine = string.match(line, commentRegex)
bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine)
if bp.Cursor:HasSelection() then
bp.Cursor.CurSelection[1].Y = sel[1].Y
bp.Cursor.CurSelection[2].Y = sel[2].Y
bp.Cursor.CurSelection[1].X = sel[1].X
bp.Cursor.CurSelection[2].X = sel[2].X
if cursor:HasSelection() then
cursor.CurSelection[1].Y = sel[1].Y
cursor.CurSelection[2].Y = sel[2].Y
cursor.CurSelection[1].X = sel[1].X
cursor.CurSelection[2].X = sel[2].X
else
bp.Cursor.X = curpos.X - index
bp.Cursor.Y = curpos.Y
cursor.X = curpos.X - index
cursor.Y = curpos.Y
end
end
bp.Cursor:Relocate()
bp.Cursor:StoreVisualX()
cursor:Relocate()
cursor:StoreVisualX()
end

function toggleCommentLine(bp, lineN, commentRegex)
function toggleCommentLine(bp, cursor, lineN, commentRegex)
if isCommented(bp, lineN, commentRegex) then
uncommentLine(bp, lineN, commentRegex)
uncommentLine(bp, cursor, lineN, commentRegex)
else
commentLine(bp, lineN, #util.GetLeadingWhitespace(bp.Buf:Line(lineN)))
commentLine(bp, cursor, lineN, #util.GetLeadingWhitespace(bp.Buf:Line(lineN)))
end
end

function toggleCommentSelection(bp, startLine, endLine, commentRegex)
function toggleCommentSelection(bp, cursor, startLine, endLine, commentRegex)
local allComments = true
for line = startLine, endLine do
if not isCommented(bp, line, commentRegex) then
Expand All @@ -174,35 +174,40 @@ function toggleCommentSelection(bp, startLine, endLine, commentRegex)

for line = startLine, endLine do
if allComments then
uncommentLine(bp, line, commentRegex)
uncommentLine(bp, cursor, line, commentRegex)
else
commentLine(bp, line, indentMin)
commentLine(bp, cursor, line, indentMin)
end
end
end

function comment(bp, args)
updateCommentType(bp.Buf)

local cursors = bp.Buf:GetCursors()

local commentType = bp.Buf.Settings["comment.type"]
local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)")

if bp.Cursor:HasSelection() then
if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then
local endLine = bp.Cursor.CurSelection[1].Y
if bp.Cursor.CurSelection[1].X == 0 then
endLine = endLine - 1
end
toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex)
else
local endLine = bp.Cursor.CurSelection[2].Y
if bp.Cursor.CurSelection[2].X == 0 then
endLine = endLine - 1
end
toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex)
end
else
toggleCommentLine(bp, bp.Cursor.Y, commentRegex)
for i = 1, #cursors do
local cursor = cursors[i]
if cursor:HasSelection() then
if cursor.CurSelection[1]:GreaterThan(-cursor.CurSelection[2]) then
local endLine = cursor.CurSelection[1].Y
if cursor.CurSelection[1].X == 0 then
endLine = endLine - 1
end
toggleCommentSelection(bp, cursor, cursor.CurSelection[2].Y, endLine, commentRegex)
else
local endLine = cursor.CurSelection[2].Y
if cursor.CurSelection[2].X == 0 then
endLine = endLine - 1
end
toggleCommentSelection(bp, cursor, cursor.CurSelection[1].Y, endLine, commentRegex)
end
else
toggleCommentLine(bp, cursor, cursor.Y, commentRegex)
end
end
end

Expand Down