Update config/vis/visrc.lua
This commit is contained in:
@@ -78,3 +78,31 @@ if vis.ftdetect then
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- On save: strip trailing spaces/tabs and extra blank lines at the end of
|
||||
-- the file. Skips files where trailing whitespace means something.
|
||||
-- ---------------------------------------------------------------------------
|
||||
local keep_whitespace = { md = true, markdown = true, diff = true, patch = true }
|
||||
|
||||
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function(file, path)
|
||||
local ext = path and path:match('%.(%w+)$')
|
||||
if ext and keep_whitespace[ext:lower()] then return true end
|
||||
|
||||
-- trailing spaces/tabs (not \r, so CRLF files stay CRLF);
|
||||
-- read every line first, then rewrite only the ones that changed
|
||||
local trimmed, n = {}, 0
|
||||
for line in file:lines_iterator() do
|
||||
n = n + 1
|
||||
local s = line:find('[ \t]+$')
|
||||
if s then trimmed[n] = line:sub(1, s - 1) end
|
||||
end
|
||||
for i, line in pairs(trimmed) do file.lines[i] = line end
|
||||
|
||||
-- blank lines at the end: keep exactly one final newline
|
||||
local size = file.size
|
||||
local tail = file:content(math.max(0, size - 256), math.min(size, 256))
|
||||
local extra = tail:match('\n(\n+)$')
|
||||
if extra then file:delete(size - #extra, #extra) end
|
||||
|
||||
return true
|
||||
end)
|
||||
Reference in New Issue
Block a user