Rust-analyzer trims trailing newline just before EOF?

Does rust-analyzer trims newlines just before EOF? I am trying out Neovim with rustaceanvim, using LSP fallback as my formatter for Rust. Every time I save a file, my newline just before EOF gets discarded. Is RA doing this? This comment from r/neovim does say so. If so, how do I turn it off?

I have not seen RA remove the ending newline in VS Code. When I test it, it doesn't touch it even if there are multiple trailing newlines.

Is it possible youve configured these options?

Trim/insert is off by default in VS Code, and the same property exists for every file type it appears. E.g. I have apparently set trim to true for shell scripts, but left it off otherwise.

Either way, this shows it is not an issue with RA. It could still be an issue with the interaction between RA and neovim, or an issue in neovim itself.

1 Like

Could it be that your editor calls rustfmt?

Negative. There are no mentions of rustfmt in my whole ~/.config/nvim. As per the conform.nvim docs, I;m using LSP fallback formatting which is RA.

I don't think RA has it's own formatting? It probably just calls out to rustfmt. Do you have "format on save" or such set? That could remove all but one trailing newline.

1 Like

I don't think RA has it's own formatting? It probably just calls out to rustfmt.

Yes.

Do you have "format on save" or such set? That could remove all but one trailing newline.

return {
    "stevearc/conform.nvim",
    event = { "BufWritePre" },
    cmd = { "ConformInfo" },
    keys = {
        {
            "<leader>f",
            function()
                require("conform").format { async = true, lsp_format = "fallback" }
            end,
            mode = "",
            desc = "[F]ormat buffer",
        },
    },
    --- @type conform.setupOpts
    opts = {
        notify_on_error = true,
        format_on_save = function(bufnr)
            local disable_filetypes = { c = true, cpp = true }
            if disable_filetypes[vim.bo[bufnr].filetype] then
                return nil
            else
                return {
                    timeout_ms = 500,
                    lsp_format = "fallback",
                }
            end
        end,
        formatters_by_ft = {
            bash = { "shfmt" },
        },
        formatters = {
            shfmt = {
                prepend_args = { "--case-indent", "--space-redirects" },
            },
        },
    },
}

As you can see, for rust, formatting is done with "lsp_format = fallback" which just means RA in this case.
Conform does have trim_newlines but I;m not using that.

I don't know vim config syntax, sorry. But if it is rustfmt doing this, you could look into rustfmt configuration to see if it can be changed there:

(I don't know if it can.)

Also note that not having lots of trailing newlines at the end of the file is considered the standard style. In general on Unix/Linux systems having a single trailing newline is considered the normal style.

But as stated above: none of this happens with vscode + RA, so you might have better luck getting an answer in a neovim-specific forum.

1 Like