+7
data/plugins/trimwhitespace.lua
+7
data/plugins/trimwhitespace.lua
···
4
5
6
local function trim_trailing_whitespace(doc)
7
+
local cline, ccol = doc:get_selection()
8
for i = 1, #doc.lines do
9
local old_text = doc:get_text(i, 1, i, math.huge)
10
local new_text = old_text:gsub("%s*$", "")
11
+
12
+
-- don't remove whitespace which would cause the caret to reposition
13
+
if cline == i and ccol > #new_text then
14
+
new_text = old_text:sub(1, ccol - 1)
15
+
end
16
+
17
if old_text ~= new_text then
18
doc:insert(i, 1, new_text)
19
doc:remove(i, #new_text + 1, i, math.huge)