Skip to content

Commit

Permalink
If file has no preprocessor symbols, only format it once (#560)
Browse files Browse the repository at this point in the history
* Don't format a document with no preprocessor symbols twice.

closes #555

* Fixing edge case that showed up after we no longer format a file twice in a row
  • Loading branch information
belav committed Jan 22, 2022
1 parent ce369b9 commit 934b85a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ publish
*.actual.cst
*.vsix
/.husky/pre-commit

debug.txt
22 changes: 11 additions & 11 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -49,19 +49,19 @@ 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]
public async Task Should_Respect_Ignore_File_With_Subdirectory_When_DirectorOrFile_Is_Dot()
{
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");
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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();

Expand All @@ -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);
}
Expand All @@ -246,7 +246,7 @@ private void EnsureExists(DirectoryInfo directoryInfo)
{
if (directoryInfo.Parent != null)
{
EnsureExists(directoryInfo.Parent);
this.EnsureExists(directoryInfo.Parent);
}

if (!directoryInfo.Exists)
Expand Down
23 changes: 3 additions & 20 deletions Src/CSharpier.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task<int> Run(
CancellationToken cancellationToken
)
{
Log("Starting");
DebugLogger.Log("Starting");
var console = new SystemConsole();
var logger = new ConsoleLogger(console);

Expand Down Expand Up @@ -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<int> PipeMultipleFiles(
SystemConsole console,
ILogger logger,
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class ClassName {

}

public class ClassName
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class ClassName { }

public class ClassName { }
2 changes: 1 addition & 1 deletion Src/CSharpier/CSharpier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</ItemGroup>

<Target Name="husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0">
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High"/>
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High" />
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High" WorkingDirectory="../../" />
</Target>
</Project>
19 changes: 19 additions & 0 deletions Src/CSharpier/DebugLogger.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
1 change: 0 additions & 1 deletion Src/CSharpier/SyntaxPrinter/PreprocessorSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public static IEnumerable<string[]> GetSymbolSets()
{
if (SymbolSets == null)
{
yield return Array.Empty<string>();
yield break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 934b85a

Please sign in to comment.