2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2022-12-20 23:13:56 +01:00
|
|
|
local common = require "core.common"
|
|
|
|
local config = require "core.config"
|
2019-12-28 12:16:32 +01:00
|
|
|
local command = require "core.command"
|
|
|
|
local Doc = require "core.doc"
|
|
|
|
|
2022-12-20 23:13:56 +01:00
|
|
|
---@class config.plugins.trimwhitespace
|
|
|
|
---@field enabled boolean
|
|
|
|
---@field trim_empty_end_lines boolean
|
|
|
|
config.plugins.trimwhitespace = common.merge({
|
2023-03-29 01:57:24 +02:00
|
|
|
enabled = false,
|
2022-12-20 23:13:56 +01:00
|
|
|
trim_empty_end_lines = false,
|
|
|
|
config_spec = {
|
|
|
|
name = "Trim Whitespace",
|
|
|
|
{
|
|
|
|
label = "Enabled",
|
|
|
|
description = "Disable or enable the trimming of white spaces by default.",
|
|
|
|
path = "enabled",
|
|
|
|
type = "toggle",
|
2023-03-29 01:57:24 +02:00
|
|
|
default = false
|
2022-12-20 23:13:56 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label = "Trim Empty End Lines",
|
|
|
|
description = "Remove any empty new lines at the end of documents.",
|
|
|
|
path = "trim_empty_end_lines",
|
|
|
|
type = "toggle",
|
|
|
|
default = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, config.plugins.trimwhitespace)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-12-20 23:13:56 +01:00
|
|
|
---@class plugins.trimwhitespace
|
|
|
|
local trimwhitespace = {}
|
|
|
|
|
|
|
|
---Disable whitespace trimming for a specific document.
|
|
|
|
---@param doc core.doc
|
|
|
|
function trimwhitespace.disable(doc)
|
|
|
|
doc.disable_trim_whitespace = true
|
|
|
|
end
|
|
|
|
|
|
|
|
---Re-enable whitespace trimming if previously disabled.
|
|
|
|
---@param doc core.doc
|
|
|
|
function trimwhitespace.enable(doc)
|
|
|
|
doc.disable_trim_whitespace = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
---Perform whitespace trimming in all lines of a document except the
|
|
|
|
---line where the caret is currently positioned.
|
|
|
|
---@param doc core.doc
|
|
|
|
function trimwhitespace.trim(doc)
|
2020-06-19 13:09:00 +02:00
|
|
|
local cline, ccol = doc:get_selection()
|
2019-12-28 12:16:32 +01:00
|
|
|
for i = 1, #doc.lines do
|
|
|
|
local old_text = doc:get_text(i, 1, i, math.huge)
|
|
|
|
local new_text = old_text:gsub("%s*$", "")
|
2020-06-19 13:09:00 +02:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
if old_text ~= new_text then
|
|
|
|
doc:insert(i, 1, new_text)
|
|
|
|
doc:remove(i, #new_text + 1, i, math.huge)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-20 23:13:56 +01:00
|
|
|
---Removes all empty new lines at the end of the document.
|
|
|
|
---@param doc core.doc
|
|
|
|
---@param raw_remove? boolean Perform the removal not registering to undo stack
|
|
|
|
function trimwhitespace.trim_empty_end_lines(doc, raw_remove)
|
|
|
|
for _=#doc.lines, 1, -1 do
|
|
|
|
local l = #doc.lines
|
|
|
|
if l > 1 and doc.lines[l] == "\n" then
|
|
|
|
local current_line = doc:get_selection()
|
|
|
|
if current_line == l then
|
|
|
|
doc:set_selection(l-1, math.huge, l-1, math.huge)
|
|
|
|
end
|
|
|
|
if not raw_remove then
|
|
|
|
doc:remove(l-1, math.huge, l, math.huge)
|
|
|
|
else
|
|
|
|
table.remove(doc.lines, l)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
command.add("core.docview", {
|
2022-08-14 01:56:58 +02:00
|
|
|
["trim-whitespace:trim-trailing-whitespace"] = function(dv)
|
2022-12-20 23:13:56 +01:00
|
|
|
trimwhitespace.trim(dv.doc)
|
|
|
|
end,
|
|
|
|
|
|
|
|
["trim-whitespace:trim-empty-end-lines"] = function(dv)
|
|
|
|
trimwhitespace.trim_empty_end_lines(dv.doc)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2022-12-20 23:13:56 +01:00
|
|
|
local doc_save = Doc.save
|
2019-12-28 12:16:32 +01:00
|
|
|
Doc.save = function(self, ...)
|
2022-12-20 23:13:56 +01:00
|
|
|
if
|
|
|
|
config.plugins.trimwhitespace.enabled
|
|
|
|
and
|
|
|
|
not self.disable_trim_whitespace
|
|
|
|
then
|
|
|
|
trimwhitespace.trim(self)
|
|
|
|
if config.plugins.trimwhitespace.trim_empty_end_lines then
|
|
|
|
trimwhitespace.trim_empty_end_lines(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
doc_save(self, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-12-20 23:13:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
return trimwhitespace
|