Many formatters inserts ^M (\r, CR) at the end of each line on windows.
jmwielandt opened this issue · 4 comments
Before proceeding
If you modified the configuration yourself, check that you tried options such as no_append
or ignore_exitcode
, and also, if you can, try to run the configuration in your shell.
Shell command and output (optional):
Which configuration? (at least:)
Type (custom or builtin): builtin
Filetype: markdown
Formatter: prettier
Configuration(s) (post all the variants you tried): no variants.
Expected behavior
Formatter respect my LF file on windows.
Actual behaviour
Formatter adds a ^M
char at the end of each line.
Additional context
Add any other context about the problem here.
I don't remember which formatter was, but other than prettier on markdown did the same thing.
update: gofmt + goimports do the same, but with null-ls didn't happened
Seems like it may be splitting lines via \n and keeping \r's in the lines (supposedly then being passed to stdin as \r\r\n). A workaround I found was to transform the input and trim \r's off the end of the lines, like so:
local formatter_prettierd = function()
-- Call function to get defaults.
local defaults = require('formatter.defaults.prettierd')();
-- Add a transform function.
defaults.transform = function(text)
-- text is an array of lines, trim any carriage returns/new lines from the end of each.
for key,value in pairs(text) do
text[key] = string.gsub(value, '[\n\r]+$', '');
end;
return text;
end;
return defaults;
end;
require('formatter').setup({
filetype = {
javascript = { formatter_prettierd },
javascriptreact = { formatter_prettierd },
typescript = { formatter_prettierd },
typescriptreact = { formatter_prettierd },
markdown = { formatter_prettierd },
},
});
local group = vim.api.nvim_create_augroup('BufWritePreFormatter', {});
vim.api.nvim_create_autocmd('BufWritePre', {
command = 'FormatWrite',
group = group,
pattern = { '*.js', '*.jsx', '*.ts', '*.tsx', '*.md' },
});
@nmaillet Does it format for you using BufWritePre
? For me most of the time I have to save twice to actually format.
@igor-ribeiro No actually, I did have issues with that. I believe I had moved everything over to BufWritePost
. I've switched over to using conform.nvim though, so I can't tell you exactly how I had it working before switching.