aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Tobler <lukas@tobler.dev>2020-12-27 17:36:32 +0100
committerLukas Tobler <lukas@tobler.dev>2020-12-27 17:36:32 +0100
commita9ef7e10f0abebbc66117c2617b1204b95a6e6ee (patch)
treefd5c1e4a9c4442cfe2e055a81e585d4a0ccaed8e
parent7029a2a637355e6cee7611172f77be4648d1b2b2 (diff)
downloadvis-commentary-a9ef7e10f0abebbc66117c2617b1204b95a6e6ee.tar.gz
Strip trailing white space when uncommenting
-rw-r--r--vis-commentary.lua10
1 files changed, 8 insertions, 2 deletions
diff --git a/vis-commentary.lua b/vis-commentary.lua
index 6ad5f52..6ebecde 100644
--- a/vis-commentary.lua
+++ b/vis-commentary.lua
@@ -50,6 +50,12 @@ local function pesc(str)
return str:gsub('%%', '%%%%')
end
+function rtrim(s)
+ local n = #s
+ while n > 0 and s:find("^%s", n) do n = n - 1 end
+ return s:sub(1, n)
+ end
+
local function comment_line(lines, lnum, prefix, suffix)
if suffix ~= "" then suffix = " " .. suffix end
lines[lnum] = string.gsub(lines[lnum],
@@ -59,7 +65,8 @@ end
local function uncomment_line(lines, lnum, prefix, suffix)
local match_str = "^(%s*)" .. esc(prefix) .. "%s?(.*)" .. esc(suffix)
- lines[lnum] = table.concat(table.pack(lines[lnum]:match(match_str)))
+ m = table.pack(lines[lnum]:match(match_str))
+ lines[lnum] = m[1] .. rtrim(m[2])
end
local function is_comment(line, prefix)
@@ -155,4 +162,3 @@ end
vis:map(vis.modes.VISUAL_LINE, "gc", visual_f(1), "Toggle comment on the selected lines")
vis:map(vis.modes.VISUAL, "gc", visual_f(0), "Toggle comment on the selected lines")
-