Skip to content

Commit

Permalink
Implement csharpier-ignore for some node types (#584)
Browse files Browse the repository at this point in the history
closes #581
  • Loading branch information
belav authored Feb 4, 2022
1 parent 1b0c12b commit 106fb30
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Src/CSharpier.Generators/NodePrinterGenerator.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ namespace CSharpier.SyntaxPrinter
throw new InTooDeepException();
}

if (CSharpierIgnore.IsNodeIgnored(syntaxNode))
{
return CSharpierIgnore.PrintWithoutFormatting(syntaxNode);
}

depth++;
try
{
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Playground/ClientApp/src/FormatCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore.cst
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
);
}
}
33 changes: 33 additions & 0 deletions Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 106fb30

Please sign in to comment.