From bbf022404ca4a7f94e7dbf8149f1f5f1aa97a69f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 20:29:16 +0000 Subject: [PATCH] Leave the pending indentation alone on an empty Write [patch] Write("") and Write(null) reached IndentedTextWriter.Write, which writes the pending tabs and clears its tabs-pending flag even for empty text. A following NewLine() then left the tabs on the blank line and the next line started at column 0. Write now returns early for empty input. Fixes #126 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW3gZxmT7yZRzkmnHAX --- CodeBlocker.Test/CodeBlockerTests.cs | 19 +++++++++++++++++++ CodeBlocker/CodeBlocker.cs | 13 ++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CodeBlocker.Test/CodeBlockerTests.cs b/CodeBlocker.Test/CodeBlockerTests.cs index 40f71af..55460d7 100644 --- a/CodeBlocker.Test/CodeBlockerTests.cs +++ b/CodeBlocker.Test/CodeBlockerTests.cs @@ -87,6 +87,25 @@ public void NewLineAfterAPartialWriteKeepsTheNextLineIndented() Assert.AreEqual("class C\n{\n\tint x = 1;\n\tint y = 2;\n}\n", codeBlocker.ToString()); } + [TestMethod] + [DataRow("")] + [DataRow(null)] + public void NewLineAfterAnEmptyWriteKeepsTheNextLineIndented(string? text) + { + using CodeBlocker codeBlocker = CodeBlocker.Create(); + + codeBlocker.WriteLine("class C"); + using (new Scope(codeBlocker)) + { + codeBlocker.WriteLine("int a;"); + codeBlocker.Write(text!); + codeBlocker.NewLine(); + codeBlocker.WriteLine("int b;"); + } + + Assert.AreEqual("class C\n{\n\tint a;\n\n\tint b;\n}\n", codeBlocker.ToString()); + } + [TestMethod] public void NewLineAtTheStartOfALineStillWritesNoIndentation() { diff --git a/CodeBlocker/CodeBlocker.cs b/CodeBlocker/CodeBlocker.cs index 7c0ed6a..64f1338 100644 --- a/CodeBlocker/CodeBlocker.cs +++ b/CodeBlocker/CodeBlocker.cs @@ -262,13 +262,20 @@ public void WriteLine() /// Write a line of code with indentation. /// /// The text to write. + /// + /// Writing an empty or null string does nothing. In particular it does not consume the + /// indentation pending at the start of a line, which would leave it on the blank line a + /// following writes and drop the line after that to column 0. + /// public void Write(string text) { - IndentedTextWriter.Write(text); - if (!string.IsNullOrEmpty(text)) + if (string.IsNullOrEmpty(text)) { - isMidLine = true; + return; } + + IndentedTextWriter.Write(text); + isMidLine = true; } ///