From 8332e60ca99187b1c08be50173357b2f1cc68dc4 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Mon, 9 Jun 2025 06:00:17 -0700 Subject: [PATCH] language: Don't add final newline on format for an empty buffer (#32320) Closes #32313 Release Notes: - Fixed newline getting added on format to empty files --- crates/language/src/buffer.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 0f43ff5c98..4e88f351c8 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -1874,9 +1874,12 @@ impl Buffer { } /// Ensures that the buffer ends with a single newline character, and - /// no other whitespace. + /// no other whitespace. Skips if the buffer is empty. pub fn ensure_final_newline(&mut self, cx: &mut Context) { let len = self.len(); + if len == 0 { + return; + } let mut offset = len; for chunk in self.as_rope().reversed_chunks_in_range(0..len) { let non_whitespace_len = chunk