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