aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2024-07-23 09:47:18 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2024-07-23 09:48:15 -0700
commit2270fa19e03a3d6f59f3b3f157058336a985b996 (patch)
tree047129c8ed8396155d3e5a170dcd697ca273af1c
parentacc7337cfd24ddfa3109bfc8c258c09c88c5c450 (diff)
refactor: trim_whitespace and trim_newlines don't require awk
-rw-r--r--lua/conform/formatters/trim_newlines.lua15
-rw-r--r--lua/conform/formatters/trim_whitespace.lua16
2 files changed, 21 insertions, 10 deletions
diff --git a/lua/conform/formatters/trim_newlines.lua b/lua/conform/formatters/trim_newlines.lua
index 434f35b..de7987a 100644
--- a/lua/conform/formatters/trim_newlines.lua
+++ b/lua/conform/formatters/trim_newlines.lua
@@ -1,9 +1,14 @@
----@type conform.FileFormatterConfig
+---@type conform.FileLuaFormatterConfig
return {
meta = {
- url = "https://www.gnu.org/software/gawk/manual/gawk.html",
- description = "Trim new lines with awk.",
+ url = "https://github.com/stevearc/conform.nvim/blob/master/lua/conform/formatters/trim_whitespace.lua",
+ description = "Trim empty lines at the end of the file.",
},
- command = "awk",
- args = { 'NF{print s $0; s=""; next} {s=s ORS}' },
+ format = function(self, ctx, lines, callback)
+ local out_lines = vim.deepcopy(lines)
+ while #out_lines > 0 and out_lines[#out_lines] == "" do
+ table.remove(out_lines)
+ end
+ callback(nil, out_lines)
+ end,
}
diff --git a/lua/conform/formatters/trim_whitespace.lua b/lua/conform/formatters/trim_whitespace.lua
index 3bbd89e..4d6d664 100644
--- a/lua/conform/formatters/trim_whitespace.lua
+++ b/lua/conform/formatters/trim_whitespace.lua
@@ -1,9 +1,15 @@
----@type conform.FileFormatterConfig
+---@type conform.FileLuaFormatterConfig
return {
meta = {
- url = "https://www.gnu.org/software/gawk/manual/gawk.html",
- description = "Trim whitespaces with awk.",
+ url = "https://github.com/stevearc/conform.nvim/blob/master/lua/conform/formatters/trim_whitespace.lua",
+ description = "Trim trailing whitespace.",
},
- command = "awk",
- args = { '{ sub(/[ \t]+$/, ""); print }' },
+ format = function(self, ctx, lines, callback)
+ local out_lines = {}
+ for _, line in ipairs(lines) do
+ local trimmed = line:gsub("%s+$", "")
+ table.insert(out_lines, trimmed)
+ end
+ callback(nil, out_lines)
+ end,
}