Skip to content

Commit

Permalink
CSharpOutputVisitor: respect policy.ElseNewLinePlacement
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Nov 22, 2016
1 parent 89ef970 commit 85cf91f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 51 deletions.
100 changes: 57 additions & 43 deletions ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,31 @@ protected virtual void WriteQualifiedIdentifier(IEnumerable<Identifier> identifi
writer.WriteIdentifier(ident);
}
}

protected virtual void WriteEmbeddedStatement(Statement embeddedStatement)

/// <summary>
/// Writes an embedded statement.
/// </summary>
/// <param name="embeddedStatement">The statement to write.</param>
/// <param name="nlp">Determines whether a trailing newline should be written following a block.
/// Non-blocks always write a trailing newline.</param>
/// <remarks>
/// Blocks may or may not write a leading newline depending on StatementBraceStyle.
/// Non-blocks always write a leading newline.
/// </remarks>
protected virtual void WriteEmbeddedStatement(Statement embeddedStatement, NewLinePlacement nlp = NewLinePlacement.NewLine)
{
if (embeddedStatement.IsNull) {
NewLine();
return;
}
BlockStatement block = embeddedStatement as BlockStatement;
if (block != null) {
VisitBlockStatement(block);
WriteBlock(block, policy.StatementBraceStyle);
if (nlp == NewLinePlacement.SameLine) {
Space(); // if not a trailing newline, then at least a trailing space
} else {
NewLine();
}
} else {
NewLine();
writer.Indent();
Expand All @@ -441,12 +456,13 @@ protected virtual void WriteEmbeddedStatement(Statement embeddedStatement)
}
}

protected virtual void WriteMethodBody(BlockStatement body)
protected virtual void WriteMethodBody(BlockStatement body, BraceStyle style)
{
if (body.IsNull) {
Semicolon();
} else {
VisitBlockStatement(body);
WriteBlock(body, style);
NewLine();
}
}

Expand Down Expand Up @@ -480,7 +496,7 @@ public virtual void VisitAnonymousMethodExpression(AnonymousMethodExpression ano
Space(policy.SpaceBeforeMethodDeclarationParentheses);
WriteCommaSeparatedListInParenthesis(anonymousMethodExpression.Parameters, policy.SpaceWithinMethodDeclarationParentheses);
}
anonymousMethodExpression.Body.AcceptVisitor(this);
WriteBlock(anonymousMethodExpression.Body, policy.AnonymousMethodBraceStyle);
EndNode(anonymousMethodExpression);
}

Expand Down Expand Up @@ -801,8 +817,12 @@ public virtual void VisitLambdaExpression(LambdaExpression lambdaExpression)
}
Space();
WriteToken(LambdaExpression.ArrowRole);
Space();
lambdaExpression.Body.AcceptVisitor(this);
if (lambdaExpression.Body is BlockStatement) {
WriteBlock((BlockStatement)lambdaExpression.Body, policy.AnonymousMethodBraceStyle);
} else {
Space();
lambdaExpression.Body.AcceptVisitor(this);
}
EndNode(lambdaExpression);
}

Expand Down Expand Up @@ -1311,43 +1331,29 @@ public virtual void VisitExternAliasDeclaration(ExternAliasDeclaration externAli
}

#endregion

#region Statements
public virtual void VisitBlockStatement(BlockStatement blockStatement)
{
WriteBlock(blockStatement, policy.StatementBraceStyle);
NewLine();
}

/// <summary>
/// Writes a block statement.
/// Similar to VisitBlockStatement() except that:
/// 1) it allows customizing the BraceStyle
/// 2) it does not write a trailing newline after the '}' (this job is left to the caller)
/// </summary>
protected virtual void WriteBlock(BlockStatement blockStatement, BraceStyle style)
{
StartNode(blockStatement);
BraceStyle style;
if (blockStatement.Parent is AnonymousMethodExpression || blockStatement.Parent is LambdaExpression) {
style = policy.AnonymousMethodBraceStyle;
} else if (blockStatement.Parent is ConstructorDeclaration) {
style = policy.ConstructorBraceStyle;
} else if (blockStatement.Parent is DestructorDeclaration) {
style = policy.DestructorBraceStyle;
} else if (blockStatement.Parent is MethodDeclaration) {
style = policy.MethodBraceStyle;
} else if (blockStatement.Parent is Accessor) {
if (blockStatement.Parent.Role == PropertyDeclaration.GetterRole) {
style = policy.PropertyGetBraceStyle;
} else if (blockStatement.Parent.Role == PropertyDeclaration.SetterRole) {
style = policy.PropertySetBraceStyle;
} else if (blockStatement.Parent.Role == CustomEventDeclaration.AddAccessorRole) {
style = policy.EventAddBraceStyle;
} else if (blockStatement.Parent.Role == CustomEventDeclaration.RemoveAccessorRole) {
style = policy.EventRemoveBraceStyle;
} else {
style = policy.StatementBraceStyle;
}
} else {
style = policy.StatementBraceStyle;
}
OpenBrace(style);
foreach (var node in blockStatement.Statements) {
node.AcceptVisitor(this);
}
EndNode(blockStatement);
CloseBrace(style);
if (!(blockStatement.Parent is Expression))
NewLine();
}

public virtual void VisitBreakStatement(BreakStatement breakStatement)
Expand Down Expand Up @@ -1378,7 +1384,7 @@ public virtual void VisitDoWhileStatement(DoWhileStatement doWhileStatement)
{
StartNode(doWhileStatement);
WriteKeyword(DoWhileStatement.DoKeywordRole);
WriteEmbeddedStatement(doWhileStatement.EmbeddedStatement);
WriteEmbeddedStatement(doWhileStatement.EmbeddedStatement, policy.WhileNewLinePlacement);
WriteKeyword(DoWhileStatement.WhileKeywordRole);
Space(policy.SpaceBeforeWhileParentheses);
LPar();
Expand Down Expand Up @@ -1506,8 +1512,11 @@ public virtual void VisitIfElseStatement(IfElseStatement ifElseStatement)
ifElseStatement.Condition.AcceptVisitor(this);
Space(policy.SpacesWithinIfParentheses);
RPar();
WriteEmbeddedStatement(ifElseStatement.TrueStatement);
if (!ifElseStatement.FalseStatement.IsNull) {

if (ifElseStatement.FalseStatement.IsNull) {
WriteEmbeddedStatement(ifElseStatement.TrueStatement);
} else {
WriteEmbeddedStatement(ifElseStatement.TrueStatement, policy.ElseNewLinePlacement);
WriteKeyword(IfElseStatement.ElseKeywordRole);
if (ifElseStatement.FalseStatement is IfElseStatement) {
// don't put newline between 'else' and 'if'
Expand Down Expand Up @@ -1779,16 +1788,21 @@ public virtual void VisitAccessor(Accessor accessor)
StartNode(accessor);
WriteAttributes(accessor.Attributes);
WriteModifiers(accessor.ModifierTokens);
BraceStyle style = policy.StatementBraceStyle;
if (accessor.Role == PropertyDeclaration.GetterRole) {
WriteKeyword("get", PropertyDeclaration.GetKeywordRole);
style = policy.PropertyGetBraceStyle;
} else if (accessor.Role == PropertyDeclaration.SetterRole) {
WriteKeyword("set", PropertyDeclaration.SetKeywordRole);
style = policy.PropertySetBraceStyle;
} else if (accessor.Role == CustomEventDeclaration.AddAccessorRole) {
WriteKeyword("add", CustomEventDeclaration.AddKeywordRole);
style = policy.EventAddBraceStyle;
} else if (accessor.Role == CustomEventDeclaration.RemoveAccessorRole) {
WriteKeyword("remove", CustomEventDeclaration.RemoveKeywordRole);
style = policy.EventRemoveBraceStyle;
}
WriteMethodBody(accessor.Body);
WriteMethodBody(accessor.Body, style);
EndNode(accessor);
}

Expand All @@ -1808,7 +1822,7 @@ public virtual void VisitConstructorDeclaration(ConstructorDeclaration construct
Space();
constructorDeclaration.Initializer.AcceptVisitor(this);
}
WriteMethodBody(constructorDeclaration.Body);
WriteMethodBody(constructorDeclaration.Body, policy.ConstructorBraceStyle);
EndNode(constructorDeclaration);
}

Expand Down Expand Up @@ -1844,7 +1858,7 @@ public virtual void VisitDestructorDeclaration(DestructorDeclaration destructorD
Space(policy.SpaceBeforeConstructorDeclarationParentheses);
LPar();
RPar();
WriteMethodBody(destructorDeclaration.Body);
WriteMethodBody(destructorDeclaration.Body, policy.DestructorBraceStyle);
EndNode(destructorDeclaration);
}

Expand Down Expand Up @@ -1976,7 +1990,7 @@ public virtual void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
foreach (Constraint constraint in methodDeclaration.Constraints) {
constraint.AcceptVisitor(this);
}
WriteMethodBody(methodDeclaration.Body);
WriteMethodBody(methodDeclaration.Body, policy.MethodBraceStyle);
EndNode(methodDeclaration);
}

Expand All @@ -2002,7 +2016,7 @@ public virtual void VisitOperatorDeclaration(OperatorDeclaration operatorDeclara
}
Space(policy.SpaceBeforeMethodDeclarationParentheses);
WriteCommaSeparatedListInParenthesis(operatorDeclaration.Parameters, policy.SpaceWithinMethodDeclarationParentheses);
WriteMethodBody(operatorDeclaration.Body);
WriteMethodBody(operatorDeclaration.Body, policy.MethodBraceStyle);
EndNode(operatorDeclaration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class Foo {
void Bar(int x) {
do {
x++;
}
while (x > 0);
} while (x > 0);
}
}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public void TestBug13054 ()
public static void TestMethod ()
{
<-int i = 0;
Action<string> action = (str) => {
Action<string> action = (str) => {
Console.WriteLine (str);
};->
}
Expand All @@ -573,7 +573,7 @@ public static void TestMethod ()
static void NewMethod ()
{
int i = 0;
Action<string> action = str => {
Action<string> action = str => {
Console.WriteLine (str);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ public async Task<int> TestMethod (int i)
if (i == 0) {
int precedentResult = await task1;
return 1;
}
else {
} else {
int precedentResult1 = await task2;
return 2;
}
Expand Down Expand Up @@ -687,7 +686,7 @@ class TestClass
public Task Foo() { return null; }
public async Task TestMethod ()
{
int precedentResult = await Foo ().ContinueWith (precedent => {
int precedentResult = await Foo ().ContinueWith (precedent => {
return 1;
});
Console.WriteLine (precedentResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void TrickyCast6()
Expression expr = new PrimitiveExpression(int.MinValue).CastTo(new PrimitiveType("double"));

Assert.AreEqual("(double)-2147483648", InsertRequired(expr));
Assert.AreEqual("(double)-2147483648", InsertReadable(expr));
Assert.AreEqual("(double)(-2147483648)", InsertReadable(expr));
}

[Test]
Expand Down

0 comments on commit 85cf91f

Please sign in to comment.