Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/xamarin/NRefactory.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Grunwald <daniel@danielgrunwald.de>2016-11-22 19:55:31 +0300
committerDaniel Grunwald <daniel@danielgrunwald.de>2016-11-22 19:55:31 +0300
commit85cf91fe5138252a44b8e1d14a10366f90a5a10d (patch)
tree1c35c0f4d2bc43da1b122e31793ae4f7541814d3
parent89ef9706523059f0a39b04b15ebcd4dea87e0e08 (diff)
CSharpOutputVisitor: respect policy.ElseNewLinePlacement
-rw-r--r--ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs100
-rw-r--r--ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertWhileToDoWhileLoopTests.cs3
-rw-r--r--ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs4
-rw-r--r--ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AutoAsyncTests.cs5
-rw-r--r--ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs2
5 files changed, 63 insertions, 51 deletions
diff --git a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
index 90463057..bbed832d 100644
--- a/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
+++ b/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
@@ -423,8 +423,18 @@ namespace ICSharpCode.NRefactory.CSharp
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();
@@ -432,7 +442,12 @@ namespace ICSharpCode.NRefactory.CSharp
}
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();
@@ -441,12 +456,13 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
- 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();
}
}
@@ -480,7 +496,7 @@ namespace ICSharpCode.NRefactory.CSharp
Space(policy.SpaceBeforeMethodDeclarationParentheses);
WriteCommaSeparatedListInParenthesis(anonymousMethodExpression.Parameters, policy.SpaceWithinMethodDeclarationParentheses);
}
- anonymousMethodExpression.Body.AcceptVisitor(this);
+ WriteBlock(anonymousMethodExpression.Body, policy.AnonymousMethodBraceStyle);
EndNode(anonymousMethodExpression);
}
@@ -801,8 +817,12 @@ namespace ICSharpCode.NRefactory.CSharp
}
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);
}
@@ -1311,43 +1331,29 @@ namespace ICSharpCode.NRefactory.CSharp
}
#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)
@@ -1378,7 +1384,7 @@ namespace ICSharpCode.NRefactory.CSharp
{
StartNode(doWhileStatement);
WriteKeyword(DoWhileStatement.DoKeywordRole);
- WriteEmbeddedStatement(doWhileStatement.EmbeddedStatement);
+ WriteEmbeddedStatement(doWhileStatement.EmbeddedStatement, policy.WhileNewLinePlacement);
WriteKeyword(DoWhileStatement.WhileKeywordRole);
Space(policy.SpaceBeforeWhileParentheses);
LPar();
@@ -1506,8 +1512,11 @@ namespace ICSharpCode.NRefactory.CSharp
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'
@@ -1779,16 +1788,21 @@ namespace ICSharpCode.NRefactory.CSharp
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);
}
@@ -1808,7 +1822,7 @@ namespace ICSharpCode.NRefactory.CSharp
Space();
constructorDeclaration.Initializer.AcceptVisitor(this);
}
- WriteMethodBody(constructorDeclaration.Body);
+ WriteMethodBody(constructorDeclaration.Body, policy.ConstructorBraceStyle);
EndNode(constructorDeclaration);
}
@@ -1844,7 +1858,7 @@ namespace ICSharpCode.NRefactory.CSharp
Space(policy.SpaceBeforeConstructorDeclarationParentheses);
LPar();
RPar();
- WriteMethodBody(destructorDeclaration.Body);
+ WriteMethodBody(destructorDeclaration.Body, policy.DestructorBraceStyle);
EndNode(destructorDeclaration);
}
@@ -1976,7 +1990,7 @@ namespace ICSharpCode.NRefactory.CSharp
foreach (Constraint constraint in methodDeclaration.Constraints) {
constraint.AcceptVisitor(this);
}
- WriteMethodBody(methodDeclaration.Body);
+ WriteMethodBody(methodDeclaration.Body, policy.MethodBraceStyle);
EndNode(methodDeclaration);
}
@@ -2002,7 +2016,7 @@ namespace ICSharpCode.NRefactory.CSharp
}
Space(policy.SpaceBeforeMethodDeclarationParentheses);
WriteCommaSeparatedListInParenthesis(operatorDeclaration.Parameters, policy.SpaceWithinMethodDeclarationParentheses);
- WriteMethodBody(operatorDeclaration.Body);
+ WriteMethodBody(operatorDeclaration.Body, policy.MethodBraceStyle);
EndNode(operatorDeclaration);
}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertWhileToDoWhileLoopTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertWhileToDoWhileLoopTests.cs
index d606e0ee..e64c3bed 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertWhileToDoWhileLoopTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertWhileToDoWhileLoopTests.cs
@@ -62,8 +62,7 @@ class Foo {
void Bar(int x) {
do {
x++;
- }
- while (x > 0);
+ } while (x > 0);
}
}");
}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs
index 9021f4f7..f8c6aa40 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs
@@ -564,7 +564,7 @@ class TestClass
public static void TestMethod ()
{
<-int i = 0;
- Action<string> action = (str) => {
+ Action<string> action = (str) => {
Console.WriteLine (str);
};->
}
@@ -573,7 +573,7 @@ class TestClass
static void NewMethod ()
{
int i = 0;
- Action<string> action = str => {
+ Action<string> action = str => {
Console.WriteLine (str);
};
}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AutoAsyncTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AutoAsyncTests.cs
index b96d1825..1f73e42c 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AutoAsyncTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/AutoAsyncTests.cs
@@ -414,8 +414,7 @@ class TestClass
if (i == 0) {
int precedentResult = await task1;
return 1;
- }
- else {
+ } else {
int precedentResult1 = await task2;
return 2;
}
@@ -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);
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs
index 88442e72..8bb0cfad 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/InsertParenthesesVisitorTests.cs
@@ -126,7 +126,7 @@ namespace ICSharpCode.NRefactory.CSharp
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]