Skip to content

Commit

Permalink
Improved logic for where to break long catch clauses
Browse files Browse the repository at this point in the history
closes #86
  • Loading branch information
belav committed Jun 14, 2021
1 parent 7ed2638 commit cee9d98
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
22 changes: 22 additions & 0 deletions Src/CSharpier.Tests/TestFiles/TryStatement/TryStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,27 @@ class ClassName
{
throw;
}

try
{
throw new Exception();
}
catch (Exception exception)
when (exception.Message.Contains("someLongValueThatMakesThisLineTooLong"))
{
return;
}

try
{
throw new Exception();
}
catch (Exception exception)
when (exception.Message.Contains("someLongValueThatMakesThisLineTooLong")
&& someLongCondition == someOtherLongValue
)
{
return;
}
}
}
55 changes: 28 additions & 27 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/CatchClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@ public static class CatchClause
{
public static Doc Print(CatchClauseSyntax node)
{
var docs = new List<Doc>();
docs.Add(Token.Print(node.CatchKeyword));
if (node.Declaration != null)
{
docs.Add(
" ",
Token.Print(node.Declaration.OpenParenToken),
Node.Print(node.Declaration.Type),
node.Declaration.Identifier.RawKind != 0 ? " " : Doc.Null,
Token.Print(node.Declaration.Identifier),
Token.Print(node.Declaration.CloseParenToken)
);
}

if (node.Filter != null)
{
docs.Add(
" ",
Token.PrintWithSuffix(node.Filter.WhenKeyword, " "),
Token.Print(node.Filter.OpenParenToken),
Node.Print(node.Filter.FilterExpression),
Token.Print(node.Filter.CloseParenToken)
);
}

docs.Add(Block.Print(node.Block));
return Doc.Concat(docs);
return Doc.Concat(
Token.Print(node.CatchKeyword),
Doc.Group(
node.Declaration != null
? Doc.Concat(
" ",
Token.Print(node.Declaration.OpenParenToken),
Node.Print(node.Declaration.Type),
node.Declaration.Identifier.RawKind != 0 ? " " : Doc.Null,
Token.Print(node.Declaration.Identifier),
Token.Print(node.Declaration.CloseParenToken)
)
: Doc.Null,
node.Filter != null
? Doc.Indent(
Doc.Line,
Token.PrintWithSuffix(node.Filter.WhenKeyword, " "),
Token.Print(node.Filter.OpenParenToken),
Doc.Group(
Doc.Indent(Node.Print(node.Filter.FilterExpression)),
Doc.SoftLine
),
Token.Print(node.Filter.CloseParenToken)
)
: Doc.Null
),
Block.Print(node.Block)
);
}
}
}

0 comments on commit cee9d98

Please sign in to comment.