Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement csharpier-ignore for some node types #584

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}