From c43d2080e7cbc8ba011d2082a4c63bb3742d436e Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sun, 8 Sep 2024 11:29:23 -0500 Subject: [PATCH] fixing issue when formatting with tabs that would convert tabs in comments into spaces. closes #1343 --- .../TestFiles/cs/Comments_Tabs.expected.test | 29 +++++++++++++++++++ .../TestFiles/cs/Comments_Tabs.test | 16 ++++++++++ Src/CSharpier/DocPrinter/DocPrinter.cs | 6 ++++ 3 files changed, 51 insertions(+) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.expected.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.expected.test new file mode 100644 index 000000000..e7d9db26a --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.expected.test @@ -0,0 +1,29 @@ +public class Foo +{ + /** + * comment + */ + public class Bar + { + /** + * comment + */ + var x = 0; + } + + public void SomeFunction() + { + /* + The following line is an example with an indent: + This line is indented by one tab. + */ + /* + The following line is an example with an indent: + This line is indented by 4 spaces but will be converted to 1 tab + */ + /* + The following line is an example with an indent: + This line is indented by 3 spaces but will be left as 3 spaces + */ + } +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.test index 34eeb769c..4da2abccc 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Comments_Tabs.test @@ -10,4 +10,20 @@ public class Foo */ var x = 0; } + + public void SomeFunction() + { + /* + The following line is an example with an indent: + This line is indented by one tab. + */ + /* + The following line is an example with an indent: + This line is indented by 4 spaces but will be converted to 1 tab + */ + /* + The following line is an example with an indent: + This line is indented by 3 spaces but will be left as 3 spaces + */ + } } diff --git a/Src/CSharpier/DocPrinter/DocPrinter.cs b/Src/CSharpier/DocPrinter/DocPrinter.cs index 0bdc3895c..8354fd47d 100644 --- a/Src/CSharpier/DocPrinter/DocPrinter.cs +++ b/Src/CSharpier/DocPrinter/DocPrinter.cs @@ -221,6 +221,12 @@ int CalculateIndentLength(string line) => this.Output.Append(indent.Value); spacesToAppend -= indentLength; } + + while (spacesToAppend > 0 && spacesToAppend >= this.PrinterOptions.IndentSize) + { + this.Output.Append('\t'); + spacesToAppend -= this.PrinterOptions.IndentSize; + } } if (spacesToAppend > 0) {