From 95b0c252f11a0444533166adf7bf7f2aa7205434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Tue, 17 Apr 2012 07:38:55 +0200 Subject: [PATCH] [Formatting] New line placement is now a 3 state. --- .../Formatter/AstFormattingVisitor.cs | 24 ++++++----- .../Formatter/CSharpFormattingOptions.cs | 16 ++++--- .../Formatter/FormattingOptionsFactory.cs | 16 +++---- .../FormattingTests/TestFormattingBugs.cs | 6 +-- .../TestStatementIndentation.cs | 42 +++++++++---------- 5 files changed, 56 insertions(+), 48 deletions(-) diff --git a/ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs b/ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs index b63558b97a..b56962fd87 100644 --- a/ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs +++ b/ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs @@ -1283,11 +1283,14 @@ namespace ICSharpCode.NRefactory.CSharp } if (!ifElseStatement.FalseStatement.IsNull) { - PlaceOnNewLine(policy.PlaceElseOnNewLine || !(ifElseStatement.TrueStatement is BlockStatement) && policy.IfElseBraceForcement != BraceForcement.AddBraces, ifElseStatement.ElseToken); + var placeElseOnNewLine = policy.ElseNewLinePlacement; + if (!(ifElseStatement.TrueStatement is BlockStatement) && policy.IfElseBraceForcement != BraceForcement.AddBraces) + placeElseOnNewLine = NewLinePlacement.NewLine; + PlaceOnNewLine(placeElseOnNewLine, ifElseStatement.ElseToken); var forcement = policy.IfElseBraceForcement; if (ifElseStatement.FalseStatement is IfElseStatement) { forcement = BraceForcement.DoNotChange; - PlaceOnNewLine(policy.PlaceElseIfOnNewLine, ((IfElseStatement)ifElseStatement.FalseStatement).IfToken); + PlaceOnNewLine(policy.ElseIfNewLinePlacement, ((IfElseStatement)ifElseStatement.FalseStatement).IfToken); } FixEmbeddedStatment(policy.StatementBraceStyle, forcement, ifElseStatement.ElseToken, policy.AllowIfBlockInline, ifElseStatement.FalseStatement, ifElseStatement.FalseStatement is IfElseStatement); } @@ -1378,7 +1381,7 @@ namespace ICSharpCode.NRefactory.CSharp } foreach (CatchClause clause in tryCatchStatement.CatchClauses) { - PlaceOnNewLine(policy.PlaceCatchOnNewLine, clause.CatchToken); + PlaceOnNewLine(policy.CatchNewLinePlacement, clause.CatchToken); if (!clause.LParToken.IsNull) { ForceSpacesBefore(clause.LParToken, policy.SpaceBeforeCatchParentheses); @@ -1389,7 +1392,7 @@ namespace ICSharpCode.NRefactory.CSharp } if (!tryCatchStatement.FinallyBlock.IsNull) { - PlaceOnNewLine(policy.PlaceFinallyOnNewLine, tryCatchStatement.FinallyToken); + PlaceOnNewLine(policy.FinallyNewLinePlacement, tryCatchStatement.FinallyToken); FixEmbeddedStatment(policy.StatementBraceStyle, BraceForcement.DoNotChange, tryCatchStatement.FinallyBlock); } @@ -1443,7 +1446,7 @@ namespace ICSharpCode.NRefactory.CSharp public override void VisitDoWhileStatement(DoWhileStatement doWhileStatement) { - PlaceOnNewLine(policy.PlaceWhileOnNewLine, doWhileStatement.WhileToken); + PlaceOnNewLine(policy.WhileNewLinePlacement, doWhileStatement.WhileToken); FixEmbeddedStatment(policy.StatementBraceStyle, policy.WhileBraceForcement, doWhileStatement.EmbeddedStatement); } @@ -1653,7 +1656,6 @@ namespace ICSharpCode.NRefactory.CSharp } bool wrapMethodCall = DoWrap(methodCallArgumentWrapping, rParToken); - if (wrapMethodCall && arguments.Any()) { if (newLineAferMethodCallOpenParentheses) { curIndent.Push(IndentType.Continuation); @@ -1673,8 +1675,7 @@ namespace ICSharpCode.NRefactory.CSharp FixStatementIndentation(rParToken.StartLocation); } else { foreach (var arg in arguments) { - if (methodCallArgumentWrapping == Wrapping.DoNotWrap) - ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma); + ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma); arg.AcceptVisitor(this); } ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses); @@ -1907,15 +1908,16 @@ namespace ICSharpCode.NRefactory.CSharp } } - void PlaceOnNewLine(bool newLine, AstNode keywordNode) + void PlaceOnNewLine(NewLinePlacement newLine, AstNode keywordNode) { - if (keywordNode == null) { + if (keywordNode == null || newLine == NewLinePlacement.DoNotCare) { return; } + int offset = document.GetOffset(keywordNode.StartLocation); int whitespaceStart = SearchWhitespaceStart(offset); - string indentString = newLine ? this.options.EolMarker + this.curIndent.IndentString : " "; + string indentString = newLine == NewLinePlacement.NewLine ? this.options.EolMarker + this.curIndent.IndentString : " "; AddChange(whitespaceStart, offset - whitespaceStart, indentString); } diff --git a/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs b/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs index 7daad6cc08..23152e5bde 100644 --- a/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs +++ b/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs @@ -62,6 +62,12 @@ namespace ICSharpCode.NRefactory.CSharp WrapIfTooLong } + public enum NewLinePlacement { + DoNotCare, + NewLine, + SameLine + } + public class CSharpFormattingOptions { public string Name { @@ -299,27 +305,27 @@ namespace ICSharpCode.NRefactory.CSharp #endregion #region NewLines - public bool PlaceElseOnNewLine { // tested + public NewLinePlacement ElseNewLinePlacement { // tested get; set; } - public bool PlaceElseIfOnNewLine { // tested + public NewLinePlacement ElseIfNewLinePlacement { // tested get; set; } - public bool PlaceCatchOnNewLine { // tested + public NewLinePlacement CatchNewLinePlacement { // tested get; set; } - public bool PlaceFinallyOnNewLine { // tested + public NewLinePlacement FinallyNewLinePlacement { // tested get; set; } - public bool PlaceWhileOnNewLine { // tested + public NewLinePlacement WhileNewLinePlacement { // tested get; set; } diff --git a/ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs b/ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs index 40a8a82d83..40b3371d04 100644 --- a/ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs +++ b/ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs @@ -81,10 +81,10 @@ namespace ICSharpCode.NRefactory.CSharp AllowEventRemoveBlockInline = true, StatementBraceStyle = BraceStyle.EndOfLine, - PlaceElseOnNewLine = false, - PlaceCatchOnNewLine = false, - PlaceFinallyOnNewLine = false, - PlaceWhileOnNewLine = false, + ElseNewLinePlacement = NewLinePlacement.SameLine, + CatchNewLinePlacement = NewLinePlacement.SameLine, + FinallyNewLinePlacement = NewLinePlacement.SameLine, + WhileNewLinePlacement = NewLinePlacement.SameLine, ArrayInitializerWrapping = Wrapping.WrapIfTooLong, ArrayInitializerBraceStyle = BraceStyle.EndOfLine, @@ -227,10 +227,10 @@ namespace ICSharpCode.NRefactory.CSharp AllowEventRemoveBlockInline = true, StatementBraceStyle = BraceStyle.EndOfLine, - PlaceElseOnNewLine = false, - PlaceCatchOnNewLine = false, - PlaceFinallyOnNewLine = false, - PlaceWhileOnNewLine = false, + ElseNewLinePlacement = NewLinePlacement.SameLine, + CatchNewLinePlacement = NewLinePlacement.SameLine, + FinallyNewLinePlacement = NewLinePlacement.SameLine, + WhileNewLinePlacement = NewLinePlacement.SameLine, ArrayInitializerWrapping = Wrapping.WrapIfTooLong, ArrayInitializerBraceStyle = BraceStyle.EndOfLine, diff --git a/ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs b/ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs index 178ad1a3c5..ab6356af6c 100644 --- a/ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs +++ b/ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs @@ -38,10 +38,10 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests /// Bug 325187 - Bug in smart indent /// [Test()] - public void TestBug325187 () + public void TestBug325187() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); - policy.PlaceElseOnNewLine = true; + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); + policy.ElseNewLinePlacement = NewLinePlacement.NewLine; TestStatementFormatting (policy, @"foreach (int i in myints) diff --git a/ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs b/ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs index b69d3e6043..71edbd340e 100644 --- a/ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs +++ b/ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs @@ -1294,10 +1294,10 @@ if (b) { } [Test()] - public void TestElseOnNewLine () + public void TestElseOnNewLine() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); - policy.PlaceElseOnNewLine = true; + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); + policy.ElseNewLinePlacement = NewLinePlacement.NewLine; Test (policy, @"class Test { @@ -1325,10 +1325,10 @@ if (b) { } [Test()] - public void TestElseIfOnNewLine () + public void TestElseIfOnNewLine() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); - policy.PlaceElseIfOnNewLine = true; + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); + policy.ElseIfNewLinePlacement = NewLinePlacement.NewLine; Test (policy, @"class Test { @@ -1356,10 +1356,10 @@ if (b) { } [Test()] - public void TestElseOnNewLineOff () + public void TestElseOnNewLineOff() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); - policy.PlaceElseOnNewLine = false; + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); + policy.ElseNewLinePlacement = NewLinePlacement.SameLine; Test (policy, @"class Test { @@ -1387,12 +1387,12 @@ if (b) { } [Test()] - public void TestSimpleIfElseComment () + public void TestSimpleIfElseComment() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); policy.StatementBraceStyle = BraceStyle.EndOfLine; - policy.PlaceElseIfOnNewLine = false; // for simple statements it must be new line. + policy.ElseIfNewLinePlacement = NewLinePlacement.SameLine; // for simple statements it must be new line. Test (policy, @"class Test { @@ -1865,11 +1865,11 @@ if (b) { } [Test()] - public void TestPlaceCatchOnNewLine () + public void TestPlaceCatchOnNewLine() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); - policy.PlaceCatchOnNewLine = true; + policy.CatchNewLinePlacement = NewLinePlacement.NewLine; Test (policy, @"class Test { @@ -1897,10 +1897,10 @@ if (b) { } [Test()] - public void TestPlaceFinallyOnNewLine () + public void TestPlaceFinallyOnNewLine() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); - policy.PlaceFinallyOnNewLine = true; + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); + policy.FinallyNewLinePlacement = NewLinePlacement.NewLine; Test (policy, @"class Test { @@ -1928,11 +1928,11 @@ if (b) { } [Test()] - public void TestPlaceWhileOnNewLine () + public void TestPlaceWhileOnNewLine() { - CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono (); + CSharpFormattingOptions policy = FormattingOptionsFactory.CreateMono(); - policy.PlaceWhileOnNewLine = true; + policy.WhileNewLinePlacement = NewLinePlacement.NewLine; Test (policy, @"class Test {