refactor toggle-block-comments, make command spaces aware, set selections correctly
This commit is contained in:
parent
33f7fe4fda
commit
e079ddfa37
|
@ -305,48 +305,41 @@ local commands = {
|
||||||
-- if nothing is selected, toggle the whole line
|
-- if nothing is selected, toggle the whole line
|
||||||
if line1 == line2 and col1 == col2 then
|
if line1 == line2 and col1 == col2 then
|
||||||
col1 = 1
|
col1 = 1
|
||||||
col2 = #doc().lines[line1]
|
col2 = #doc().lines[line2]
|
||||||
end
|
end
|
||||||
local text = doc():get_text(line1, col1, line2, col2)
|
|
||||||
|
|
||||||
-- might need to deal with unicode later...
|
-- automatically skip spaces
|
||||||
local cs, cse = text:find(comment[1], 1, true)
|
local word_start = doc():get_text(line1, col1, line1, math.huge):find("%S")
|
||||||
local ce = text:find(comment[2], #text - #comment[2] + 1, true)
|
local word_end = doc():get_text(line2, 1, line2, col2):find("%s*$")
|
||||||
|
col1 = col1 + (word_start and (word_start - 1) or 0)
|
||||||
|
col2 = word_end and word_end or col2
|
||||||
|
|
||||||
local new_col1, new_col2 = col1, col2
|
local block_start = doc():get_text(line1, col1, line1, col1 + #comment[1])
|
||||||
-- uncomment
|
local block_end = doc():get_text(line2, col2 - #comment[2], line2, col2)
|
||||||
if cs and ce then
|
|
||||||
if line1 == line2 then
|
if block_start == comment[1] and block_end == comment[2] then
|
||||||
new_col2 = new_col2 - #table.concat(comment)
|
-- remove up to 1 whitespace after the comment
|
||||||
else
|
local start_len, stop_len = #comment[1], #comment[2]
|
||||||
new_col2 = new_col2 - #comment[2]
|
if doc():get_text(line1, col1 + #comment[1], line1, col1 + #comment[1] + 1):find("%s$") then
|
||||||
|
start_len = start_len + 1
|
||||||
|
end
|
||||||
|
if doc():get_text(line2, col2 - #comment[2] - 1, line2, col2):find("^%s") then
|
||||||
|
stop_len = stop_len + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove 1 whitespace if possible
|
doc():remove(line1, col1, line1, col1 + start_len)
|
||||||
if text:sub(cse + 1, cse + 1) == " " then
|
col2 = col2 - (line1 == line2 and start_len or 0)
|
||||||
cse = cse + 1
|
doc():remove(line2, col2 - stop_len, line2, col2)
|
||||||
if line1 == line2 then
|
|
||||||
new_col2 = new_col2 - 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if text:sub(ce - 1, ce - 1) == " " then
|
|
||||||
ce = ce - 1
|
|
||||||
new_col2 = new_col2 - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
text = text:sub(cse + 1, ce - 1)
|
doc():set_selections(1, line1, col1, line2, col2 - stop_len)
|
||||||
-- comment
|
|
||||||
else
|
else
|
||||||
text = comment[1] .. " " .. text .. " " .. comment[2]
|
doc():insert(line1, col1, comment[1] .. " ")
|
||||||
if line1 == line2 then
|
col2 = col2 + (line1 == line2 and (#comment[1] + 1) or 0)
|
||||||
new_col2 = new_col2 + #table.concat(comment) + 2
|
doc():insert(line2, col2, " " .. comment[2])
|
||||||
else
|
|
||||||
new_col2 = new_col2 + #comment[2] + 1
|
col2 = col2 + #comment[2] + 1
|
||||||
end
|
doc():set_selections(idx, line1, col1, line2, col2)
|
||||||
end
|
end
|
||||||
doc():remove(line1, col1, line2, col2)
|
|
||||||
doc():insert(line1, col1, text)
|
|
||||||
doc():set_selections(idx, line1, new_col1, line2, new_col2)
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue