Skip to content

Commit

Permalink
feat: improve default formatters
Browse files Browse the repository at this point in the history
Closes #304
  • Loading branch information
ferplnat authored and mhartington committed Sep 24, 2024
1 parent 26efb55 commit 01d9761
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ require("formatter").setup {
["*"] = {
-- "formatter.filetypes.any" defines default configurations for any
-- filetype
require("formatter.filetypes.any").remove_trailing_whitespace
require("formatter.filetypes.any").remove_trailing_whitespace,
-- Remove trailing whitespace without 'sed'
-- require("formatter.filetypes.any").substitute_trailing_whitespace,
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion doc/formatter.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ Setup:
["*"] = {
-- "formatter.filetypes.any" defines default configurations for any
-- filetype
require("formatter.filetypes.any").remove_trailing_whitespace
require("formatter.filetypes.any").remove_trailing_whitespace,

-- Remove trailing whitespace without 'sed'
-- require("formatter.filetypes.any").substitute_trailing_whitespace,
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion lua/formatter/filetypes/any.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ local util = require "formatter.util"
M.remove_trailing_whitespace = util.withl(defaults.sed, "[ \t]*$")

M.substitute_trailing_whitespace = function()
vim.cmd([[silent! :keeppatterns %s/\[ \t]+$//ge]])
local line, col = unpack(vim.api.nvim_win_get_cursor(0))

vim.cmd [[silent! :keeppatterns %s/[ \t]\+$//ge]]

-- Restore cursor position without going out of bounds
local lastline = vim.fn.line "$"
if line > lastline then
line = lastline
end
vim.api.nvim_win_set_cursor(0, { line, col })
end

return M

0 comments on commit 01d9761

Please sign in to comment.