diff --git a/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt b/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt index d2584733a..cc72b081b 100644 --- a/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt +++ b/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt @@ -23,6 +23,11 @@ namespace CSharpier.SyntaxPrinter throw new InTooDeepException(); } + if (CSharpierIgnore.IsNodeIgnored(syntaxNode)) + { + return CSharpierIgnore.PrintWithoutFormatting(syntaxNode); + } + depth++; try { diff --git a/Src/CSharpier.Playground/ClientApp/src/FormatCode.ts b/Src/CSharpier.Playground/ClientApp/src/FormatCode.ts index 7ad93b89d..f38347493 100644 --- a/Src/CSharpier.Playground/ClientApp/src/FormatCode.ts +++ b/Src/CSharpier.Playground/ClientApp/src/FormatCode.ts @@ -35,7 +35,7 @@ export const formatCode = async (code: string) => { } }; - for (let x = 0; x < 5; x++) { + for (let x = 0; x < 20; x++) { try { return makeRequest(); } catch { diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore.cst new file mode 100644 index 000000000..a4db609b0 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore.cst @@ -0,0 +1,20 @@ +// csharpier-ignore +public class Unformatted { } + +public class ClassName +{ + // csharpier-ignore + private string unformatted; + + public void MethodName() + { + // csharpier-ignore + var unformatted = true; + + if (true) + { + // csharpier-ignore + var unformatted = true; + } + } +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.cst new file mode 100644 index 000000000..7fa9fa333 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.cst @@ -0,0 +1,15 @@ +public +// csharpier-ignore +class ClassName { + private void MethodName( + // csharpier-ignore + string one) { + var x + // csharpier-ignore + = someRandomValue; + + this.CallMethod( + // csharpier-ignore + true, false); + } +} \ No newline at end of file diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.expected.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.expected.cst new file mode 100644 index 000000000..0f4ce0536 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore_Dont.expected.cst @@ -0,0 +1,20 @@ +public +// csharpier-ignore +class ClassName +{ + private void MethodName( + // csharpier-ignore + string one + ) + { + var x + // csharpier-ignore + = someRandomValue; + + this.CallMethod( + // csharpier-ignore + true, + false + ); + } +} diff --git a/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs b/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs new file mode 100644 index 000000000..88c606fb1 --- /dev/null +++ b/Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs @@ -0,0 +1,33 @@ +namespace CSharpier.SyntaxPrinter; + +internal static class CSharpierIgnore +{ + public static bool IsNodeIgnored(SyntaxNode syntaxNode) + { + if ( + syntaxNode.Parent + is not ( + BaseTypeDeclarationSyntax + or BlockSyntax + or CompilationUnitSyntax + or NamespaceDeclarationSyntax + ) + ) + { + return false; + } + + return syntaxNode + .GetLeadingTrivia() + .Any( + o => + o.Kind() is SyntaxKind.SingleLineCommentTrivia + && o.ToString().Equals("// csharpier-ignore") + ); + } + + public static Doc PrintWithoutFormatting(SyntaxNode syntaxNode) + { + return syntaxNode.GetText().ToString().Trim(); + } +}