Skip to content

fix: emit a real edit when inserting past the last line - #898

Open
nfvelten wants to merge 1 commit into
stevearc:masterfrom
nfvelten:fix/append-at-eof-edit
Open

nfvelten wants to merge 1 commit into
stevearc:masterfrom
nfvelten:fix/append-at-eof-edit

Conversation

@nfvelten

Copy link
Copy Markdown

Fixes #891. Root cause writeup is in this comment, short version below.

When the diff wants to insert at the end of the buffer, create_text_edit fell into the branch that skips appending the newline, on the grounds that it is implicit at EOF. That leaves a zero-width range with empty newText, which is a no-op. It happened to work because the old vim.lsp.util.apply_text_edits added the line anyway. Neovim 0.12 no longer does, and I think 0.12 is right there.

The visible effect is not limited to the tests: a formatter whose output ends with an extra blank line has that line silently dropped on 0.12.

This anchors the edit to the end of the last line and leads with the newline, so the same edit is produced on every supported version:

-- before, on a buffer holding {"hello"}
{ newText = "",   range = { start = { line = 1, character = 0 }, ["end"] = { line = 1, character = 0 } } }
-- after
{ newText = "\n", range = { start = { line = 0, character = 5 }, ["end"] = { line = 0, character = 5 } } }

On master ./run_tests.sh fails two tests for me on 0.12.5, runner e2e sets the correct output and fuzzer formats correctly. With this patch the whole suite passes, 65 tests, no failures. I did not add a test since the existing cases already cover it, including the multi-line variants like run_formatter_test("hello", "hello\n\n").

Checked the resulting buffer on both 0.11.7 and 0.12.5 with the same formatter output, and both now end up with {"hello", ""}. stylua --check is clean.

If you would rather shape the fix differently, say so and I will rework it.

An insert at the end of the buffer produced a zero-width edit with empty
newText, relying on vim.lsp.util.apply_text_edits adding the line anyway.
Neovim 0.12 no longer does, which is correct: that edit is a no-op. The
result is that a trailing blank line from a formatter is silently dropped.

Anchor the edit to the end of the last line and lead with the newline
instead. Same edit on every supported Neovim version.
@github-actions
github-actions Bot requested a review from stevearc August 31, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: tests fail with neovim v0.12.4

1 participant