Merge pull request #808 from adamharrison/fix-commenting
Fix commenting selections.
This commit is contained in:
commit
8c8bd4692c
|
@ -97,31 +97,46 @@ local function set_cursor(x, y, snap_type)
|
|||
core.blink_reset()
|
||||
end
|
||||
|
||||
local function line_comment(comment, line1, line2)
|
||||
local comment_text = comment .. " "
|
||||
local function line_comment(comment, line1, col1, line2, col2)
|
||||
local start_comment = (type(comment) == 'table' and comment[1] or comment) .. " "
|
||||
local end_comment = (type(comment) == 'table' and " " .. comment[2])
|
||||
local uncomment = true
|
||||
local start_offset = math.huge
|
||||
for line = line1, line2 do
|
||||
local text = doc().lines[line]
|
||||
local s = text:find("%S")
|
||||
local cs, ce = text:find(comment_text, s, true)
|
||||
local cs, ce = text:find(start_comment, s, true)
|
||||
if s and cs ~= s then
|
||||
uncomment = false
|
||||
start_offset = math.min(start_offset, s)
|
||||
end
|
||||
start_offset = math.min(start_offset, s)
|
||||
end
|
||||
|
||||
local end_line = col2 == #doc().lines[line2]
|
||||
for line = line1, line2 do
|
||||
local text = doc().lines[line]
|
||||
local s = text:find("%S")
|
||||
if uncomment then
|
||||
local cs, ce = text:find(comment_text, s, true)
|
||||
if end_comment and text:sub(#text - #end_comment, #text - 1) == end_comment then
|
||||
doc():remove(line, #text - #end_comment, line, #text)
|
||||
end
|
||||
local cs, ce = text:find(start_comment, s, true)
|
||||
if ce then
|
||||
doc():remove(line, cs, line, ce + 1)
|
||||
end
|
||||
elseif s then
|
||||
doc():insert(line, start_offset, comment_text)
|
||||
doc():insert(line, start_offset, start_comment)
|
||||
if end_comment then
|
||||
doc():insert(line, #doc().lines[line], " " .. comment[2])
|
||||
end
|
||||
end
|
||||
end
|
||||
col1 = col1 + (col1 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
|
||||
col2 = col2 + (col2 > start_offset and #start_comment or 0) * (uncomment and -1 or 1)
|
||||
if end_comment and end_line then
|
||||
col2 = col2 + #end_comment * (uncomment and -1 or 1)
|
||||
end
|
||||
return line1, col1, line2, col2
|
||||
end
|
||||
|
||||
local function block_comment(comment, line1, col1, line2, col2)
|
||||
|
@ -368,28 +383,15 @@ local commands = {
|
|||
col1 = 1
|
||||
col2 = #doc().lines[line2]
|
||||
end
|
||||
|
||||
line1, col1, line2, col2 = block_comment(comment, line1, col1, line2, col2)
|
||||
doc():set_selections(idx, line1, col1, line2, col2)
|
||||
doc():set_selections(idx, block_comment(comment, line1, col1, line2, col2))
|
||||
end
|
||||
end,
|
||||
|
||||
["doc:toggle-line-comments"] = function()
|
||||
local comment = doc().syntax.comment
|
||||
local block = false
|
||||
if not comment then
|
||||
comment = doc().syntax.block_comment
|
||||
if not comment then return end
|
||||
block = true
|
||||
end
|
||||
|
||||
for _, line1, _, line2 in doc_multiline_selections(true) do
|
||||
if block then
|
||||
for line = line1, line2 do
|
||||
block_comment(comment, line, 1, line, #doc().lines[line])
|
||||
end
|
||||
else
|
||||
line_comment(comment, line1, line2)
|
||||
local comment = doc().syntax.comment or doc().syntax.block_comment
|
||||
if comment then
|
||||
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
|
||||
doc():set_selections(idx, line_comment(comment, line1, col1, line2, col2))
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
|
Loading…
Reference in New Issue