Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve default formatter #304

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ 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
Loading