-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Unexpected newline at the end of file when formatting it #241
Comments
#181 may or may not be related to this. |
Oh wait, it seems the problem only occurs when the input file does not end with a newline character.
When the input file has a newline character at the EOF when opening the file on neovim, it works without problem. |
do you set |
It seems it is on by default. I set nothing related to EOL. |
Hmm, formatting does not modify outputs.
|
I have the same issue. Did anyone find a suitable solution yet? |
I dug a little deeper today and hope I can provide additional information: The problem only occurs for me when Prettier needs to change the last line, e.g. with semicolons required in JS: export const test = () => {} results in export const test = () => {};
<-- newline here This is the corresponding log:
On the other hand const x = 0
export const test = () => {}; correctly results in const x = 0;
export const test = () => {}; log:
Please let me know if I can help any further in investigating. This is the relevant part of my nvim config btw in case someone wants to try to reproduce this: -- init.lua
local formatting_lsps = {
'efm',
}
local filter = function(client)
for _, lsp in pairs(formatting_lsps) do
if client.name == lsp then
return true
end
end
return false
end
local common_on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({
bufnr = bufnr,
filter = filter,
})
end,
})
end
end
-- efm
local eslint = {
lintCommand = "eslint_d --format unix --stdin --stdin-filename ${INPUT}",
lintIgnoreExitCode = true,
lintStdin = true,
lintFormats = {"%f:%l:%c: %m"},
formatCommand = "eslint_d --fix-to-stdout --stdin --stdin-filename ${INPUT}",
formatStdin = true,
rootMarkers = {
'.eslintrc',
'.eslintrc.cjs',
'.eslintrc.js',
'.eslintrc.json',
'.eslintrc.yaml',
'.eslintrc.yml',
'package.json',
},
}
local prettier = {
formatCanRange = true,
formatCommand = "prettierd ${INPUT} ${--range-start=charStart} ${--range-end=charEnd}",
formatStdin = true,
rootMarkers = {
'.prettierrc',
'.prettierrc.json',
'.prettierrc.js',
'.prettierrc.yml',
'.prettierrc.yaml',
'.prettierrc.json5',
'.prettierrc.mjs',
'.prettierrc.cjs',
'.prettierrc.toml',
},
}
nvim_lsp.efm.setup{
cmd = { "efm-langserver", "-logfile", "efm.log", "-loglevel", "5" },
on_attach = common_on_attach,
init_options = {documentFormatting = true},
settings = {
rootMarkers = {".git/"},
languages = {
javascript = {
prettier,
eslint,
},
},
filetypes = {
'javascript',
}
} I was running efm version |
FWIW: This issue is also happening to me when using efm with helix. This is my config: # languages.toml
[language-server.efm]
command = "efm-langserver"
[language-server.efm.config]
documentFormatting = true
[language-server.efm.config.languages]
css = [
{ formatCommand ="prettierd --stdin-filepath ${INPUT}", formatStdin = true },
{ lintCommand = "stylelint_d --stdin --stdin-filename ${INPUT} --formatter compact", lintIgnoreExitCode = true, lintStdin = true, lintFormats = [ "%f: line %l, col %c, %tarning - %m", "%f: line %l, col %c, %trror - %m" ], formatCommand = "stylelint_d --fix --stdin-filename ${INPUT}", formatStdin = true, rootMarkers = [ ".stylelintrc", ".stylelintrc.json", "stylelint.config.js", "stylelint.config.cjs", "stylelint.config.mjs", "stylelint.config.ts" ] },
]
[[language]]
name = "css"
scope = "source.css"
language-servers = [ "efm" ]
auto-format = true |
When formatting a file, if you don't have a blank line at the end, efm-langserver appends it.
For example, if you have this file (
main.ts
) in your neovim buffer:And executes the formatting command, which is
{ formatCommand = "/path/to/prettier --stdin-filepath ${INPUT}", formatStdin = true } }
, the file will be the following:The log shows that efm-langserver is going to write out a newline (see
\n\n
at the end of the line):However, the manual invocation of the same command does not produce a newline.
This issue occurs on macOS, NVIM v0.9.0.
The text was updated successfully, but these errors were encountered: