Skip to content

Commit

Permalink
Changed trimwhitespace to never cause caret to reposition
Browse files Browse the repository at this point in the history
  • Loading branch information
rxi committed Jun 19, 2020
1 parent 6ec8fc5 commit ae48049
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions data/plugins/trimwhitespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ local Doc = require "core.doc"


local function trim_trailing_whitespace(doc)
local cline, ccol = doc:get_selection()
for i = 1, #doc.lines do
local old_text = doc:get_text(i, 1, i, math.huge)
local new_text = old_text:gsub("%s*$", "")

-- don't remove whitespace which would cause the caret to reposition
if cline == i and ccol > #new_text then
new_text = old_text:sub(1, ccol - 1)
end

if old_text ~= new_text then
doc:insert(i, 1, new_text)
doc:remove(i, #new_text + 1, i, math.huge)
Expand Down

0 comments on commit ae48049

Please sign in to comment.