From 902539b97a55d630d41b7aaff2d0d829a39363e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jefferson=20Gonz=C3=A1lez?=
Date: Tue, 20 Dec 2022 18:13:56 -0400
Subject: [PATCH] trimwhitespace: expose functionality and extra features
(#1238)
---
data/plugins/trimwhitespace.lua | 93 ++++++++++++++++++++++++++++++---
1 file changed, 87 insertions(+), 6 deletions(-)
diff --git a/data/plugins/trimwhitespace.lua b/data/plugins/trimwhitespace.lua
index d6057da84..6fb67230c 100644
--- a/data/plugins/trimwhitespace.lua
+++ b/data/plugins/trimwhitespace.lua
@@ -1,10 +1,53 @@
-- mod-version:3
-local core = require "core"
+local common = require "core.common"
+local config = require "core.config"
local command = require "core.command"
local Doc = require "core.doc"
+---@class config.plugins.trimwhitespace
+---@field enabled boolean
+---@field trim_empty_end_lines boolean
+config.plugins.trimwhitespace = common.merge({
+ enabled = true,
+ 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",
+ default = true
+ },
+ {
+ 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)
-local function trim_trailing_whitespace(doc)
+---@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)
local cline, ccol = doc:get_selection()
for i = 1, #doc.lines do
local old_text = doc:get_text(i, 1, i, math.huge)
@@ -22,16 +65,54 @@ local function trim_trailing_whitespace(doc)
end
end
+---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
+
command.add("core.docview", {
["trim-whitespace:trim-trailing-whitespace"] = function(dv)
- trim_trailing_whitespace(dv.doc)
+ trimwhitespace.trim(dv.doc)
+ end,
+
+ ["trim-whitespace:trim-empty-end-lines"] = function(dv)
+ trimwhitespace.trim_empty_end_lines(dv.doc)
end,
})
-local save = Doc.save
+local doc_save = Doc.save
Doc.save = function(self, ...)
- trim_trailing_whitespace(self)
- save(self, ...)
+ 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, ...)
end
+
+
+return trimwhitespace