Skip to content

Commit

Permalink
Initial POC for ranged ignore
Browse files Browse the repository at this point in the history
closes #678
  • Loading branch information
belav committed Jun 6, 2022
1 parent cb2baa2 commit 60a309c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 7 deletions.
27 changes: 27 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/CSharpierIgnore.cst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,31 @@ public class ClassName
var unformatted = true;
}
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
// csharpier-ignore-end
var formatMe = true;
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
// csharpier-ignore-end
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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;
}
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
// csharpier-ignore-end
var formatMe = true;
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
// csharpier-ignore-end
}

void MethodName()
{
var formatMe = true;
// csharpier-ignore-start
var unformatted = true;
var unformatted = true;
}
}
43 changes: 40 additions & 3 deletions Src/CSharpier/SyntaxPrinter/CSharpierIgnore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,49 @@ or NamespaceDeclarationSyntax
return false;
}

return syntaxNode
.GetLeadingTrivia()
return (HasLeadingComment(syntaxNode, "// csharpier-ignore"));
}

// TODO better name
// TODO members of types
// TODO these last two could get tricky because of the using vs members thing
// unless we don't support using
// TODO top level statements
// TODO compilation unit
public static List<Doc> GetPrintedNodes<T>(SyntaxList<T> list, FormattingContext context)
where T : SyntaxNode
{
var statements = new List<Doc>();
var printUnformatted = false;
// TODO what about if the end is the final line of the block?
// TODO what about any kind of error detection? if they have multiple starts? or other cases
// TODO what about getting this into other areas? make it generic
foreach (var node in list)
{
if (HasLeadingComment(node, "// csharpier-ignore-end"))
{
printUnformatted = false;
}
else if (HasLeadingComment(node, "// csharpier-ignore-start"))
{
printUnformatted = true;
}

statements.Add(
printUnformatted ? PrintWithoutFormatting(node) : Node.Print(node, context)
);
}

return statements;
}

private static bool HasLeadingComment(SyntaxNode node, string comment)
{
return node.GetLeadingTrivia()
.Any(
o =>
o.RawSyntaxKind() is SyntaxKind.SingleLineCommentTrivia
&& o.ToString().Equals("// csharpier-ignore")
&& o.ToString().Equals(comment)
);
}

Expand Down
7 changes: 3 additions & 4 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public static Doc Print(BlockSyntax node, FormattingContext context)

if (node.Statements.Count > 0)
{
innerDoc = Doc.Indent(
statementSeparator,
Doc.Join(statementSeparator, node.Statements.Select(o => Node.Print(o, context)))
);
var statements = CSharpierIgnore.GetPrintedNodes(node.Statements, context);

innerDoc = Doc.Indent(statementSeparator, Doc.Join(statementSeparator, statements));

DocUtilities.RemoveInitialDoubleHardLine(innerDoc);
}
Expand Down

0 comments on commit 60a309c

Please sign in to comment.