diff --git a/.gitignore b/.gitignore index f099b84a7..a23198e2c 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,5 @@ publish *.actual.cst *.vsix /.husky/pre-commit + +debug.txt diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index 90409ac6f..3064afd2f 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -38,7 +38,7 @@ public async Task Should_Format_Basic_File(string lineEnding) var formattedContent = "public class ClassName { }" + lineEnding; var unformattedContent = $"public class ClassName {{{lineEnding}{lineEnding}}}"; - await WriteFileAsync("BasicFile.cs", unformattedContent); + await this.WriteFileAsync("BasicFile.cs", unformattedContent); var result = await new CsharpierProcess().WithArguments("BasicFile.cs").ExecuteAsync(); @@ -49,7 +49,7 @@ public async Task Should_Format_Basic_File(string lineEnding) "Total files: 1" ); result.ExitCode.Should().Be(0); - (await ReadAllTextAsync("BasicFile.cs")).Should().Be(formattedContent); + (await this.ReadAllTextAsync("BasicFile.cs")).Should().Be(formattedContent); } [Test] @@ -57,11 +57,11 @@ public async Task Should_Respect_Ignore_File_With_Subdirectory_When_DirectorOrFi { var unformattedContent = "public class Unformatted { }"; var filePath = "Subdirectory/IgnoredFile.cs"; - await WriteFileAsync(filePath, unformattedContent); - await WriteFileAsync(".csharpierignore", filePath); + await this.WriteFileAsync(filePath, unformattedContent); + await this.WriteFileAsync(".csharpierignore", filePath); await new CsharpierProcess().WithArguments(".").ExecuteAsync(); - var result = await ReadAllTextAsync(filePath); + var result = await this.ReadAllTextAsync(filePath); result.Should().Be(unformattedContent, $"The file at {filePath} should have been ignored"); } @@ -129,7 +129,7 @@ public async Task With_Check_Should_Write_Unformatted_File() { var unformattedContent = "public class ClassName1 {\n\n}"; - await WriteFileAsync("CheckUnformatted.cs", unformattedContent); + await this.WriteFileAsync("CheckUnformatted.cs", unformattedContent); var result = await new CsharpierProcess() .WithArguments("CheckUnformatted.cs --check") @@ -190,7 +190,7 @@ public async Task Should_Ignore_Piped_File_With_Multiple_Piped_Files() { const string ignoredFile = "public class ClassName { }"; var fileName = Path.Combine(testFileDirectory, "Ignored.cs"); - await WriteFileAsync(".csharpierignore", "Ignored.cs"); + await this.WriteFileAsync(".csharpierignore", "Ignored.cs"); var result = await new CsharpierProcess() .WithArguments("--pipe-multiple-files") @@ -206,7 +206,7 @@ public async Task Should_Support_Config_With_Multiple_Piped_Files() { const string fileContent = "var myVariable = someLongValue;"; var fileName = Path.Combine(testFileDirectory, "TooWide.cs"); - await WriteFileAsync(".csharpierrc", "printWidth: 10"); + await this.WriteFileAsync(".csharpierrc", "printWidth: 10"); var result = await new CsharpierProcess() .WithArguments("--pipe-multiple-files") @@ -220,7 +220,7 @@ public async Task Should_Support_Config_With_Multiple_Piped_Files() [Test] public async Task Should_Not_Fail_On_Empty_File() { - await WriteFileAsync("BasicFile.cs", ""); + await this.WriteFileAsync("BasicFile.cs", ""); var result = await new CsharpierProcess().WithArguments(".").ExecuteAsync(); @@ -232,7 +232,7 @@ public async Task Should_Not_Fail_On_Empty_File() private async Task WriteFileAsync(string path, string content) { var fileInfo = new FileInfo(Path.Combine(testFileDirectory, path)); - EnsureExists(fileInfo.Directory!); + this.EnsureExists(fileInfo.Directory!); await File.WriteAllTextAsync(fileInfo.FullName, content); } @@ -246,7 +246,7 @@ private void EnsureExists(DirectoryInfo directoryInfo) { if (directoryInfo.Parent != null) { - EnsureExists(directoryInfo.Parent); + this.EnsureExists(directoryInfo.Parent); } if (!directoryInfo.Exists) diff --git a/Src/CSharpier.Cli/Program.cs b/Src/CSharpier.Cli/Program.cs index 11d5d87ca..1263a0472 100644 --- a/Src/CSharpier.Cli/Program.cs +++ b/Src/CSharpier.Cli/Program.cs @@ -28,7 +28,7 @@ public static async Task Run( CancellationToken cancellationToken ) { - Log("Starting"); + DebugLogger.Log("Starting"); var console = new SystemConsole(); var logger = new ConsoleLogger(console); @@ -82,23 +82,6 @@ CancellationToken cancellationToken ); } - // this is used for troubleshooting new IDE plugins and can eventually go away. - [Conditional("DEBUG")] - private static void Log(string message) - { - try - { - File.AppendAllText( - @"C:\projects\csharpier\Src\CSharpier.Cli\bin\Debug\net6.0\log.txt", - message + "\n" - ); - } - catch (Exception) - { - // we don't care if this fails - } - } - private static async Task PipeMultipleFiles( SystemConsole console, ILogger logger, @@ -125,10 +108,10 @@ CancellationToken cancellationToken return exitCode; } var character = Convert.ToChar(value); - Log("Got " + character); + DebugLogger.Log("Got " + character); if (character == '\u0003') { - Log("Got EOF"); + DebugLogger.Log("Got EOF"); break; } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.cst new file mode 100644 index 000000000..2287b0602 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.cst @@ -0,0 +1,7 @@ +public class ClassName { + +} + +public class ClassName +{ +} \ No newline at end of file diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.expected.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.expected.cst new file mode 100644 index 000000000..eeb9fe9e2 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ClassDeclarations_EdgeCase.expected.cst @@ -0,0 +1,3 @@ +public class ClassName { } + +public class ClassName { } diff --git a/Src/CSharpier/CSharpier.csproj b/Src/CSharpier/CSharpier.csproj index 650cc6ace..d819c84a0 100644 --- a/Src/CSharpier/CSharpier.csproj +++ b/Src/CSharpier/CSharpier.csproj @@ -29,7 +29,7 @@ - + diff --git a/Src/CSharpier/DebugLogger.cs b/Src/CSharpier/DebugLogger.cs new file mode 100644 index 000000000..45027403d --- /dev/null +++ b/Src/CSharpier/DebugLogger.cs @@ -0,0 +1,19 @@ +using System.Diagnostics; + +namespace CSharpier; + +public class DebugLogger +{ + [Conditional("DEBUG")] + public static void Log(string message) + { + try + { + File.AppendAllText(@"C:\projects\csharpier\debug.txt", message + "\n"); + } + catch (Exception) + { + // we don't care if this fails + } + } +} diff --git a/Src/CSharpier/SyntaxPrinter/PreprocessorSymbols.cs b/Src/CSharpier/SyntaxPrinter/PreprocessorSymbols.cs index f892cfe3d..5d7204c5e 100644 --- a/Src/CSharpier/SyntaxPrinter/PreprocessorSymbols.cs +++ b/Src/CSharpier/SyntaxPrinter/PreprocessorSymbols.cs @@ -98,7 +98,6 @@ public static IEnumerable GetSymbolSets() { if (SymbolSets == null) { - yield return Array.Empty(); yield break; } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseTypeDeclaration.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseTypeDeclaration.cs index c3c3f75e8..896a50d67 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseTypeDeclaration.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseTypeDeclaration.cs @@ -123,7 +123,11 @@ public static Doc Print(BaseTypeDeclarationSyntax node) } else if (node.OpenBraceToken.Kind() != SyntaxKind.None) { - Doc separator = node.CloseBraceToken.LeadingTrivia.Any() ? Doc.Line : " "; + Doc separator = node.CloseBraceToken.LeadingTrivia.Any( + o => o.Kind() is not (SyntaxKind.WhitespaceTrivia or SyntaxKind.EndOfLineTrivia) + ) + ? Doc.Line + : " "; docs.Add( separator,