Browse Source

[Formatting] New line placement is now a 3 state.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
95b0c252f1
  1. 24
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 16
      ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs
  3. 16
      ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs
  4. 6
      ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs
  5. 42
      ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs

24
ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs

@ -1283,11 +1283,14 @@ namespace ICSharpCode.NRefactory.CSharp @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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);
}

16
ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs

@ -62,6 +62,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -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 @@ -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;
}

16
ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs

@ -81,10 +81,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -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 @@ -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,

6
ICSharpCode.NRefactory.Tests/FormattingTests/TestFormattingBugs.cs

@ -38,10 +38,10 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -38,10 +38,10 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
/// Bug 325187 - Bug in smart indent
/// </summary>
[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)

42
ICSharpCode.NRefactory.Tests/FormattingTests/TestStatementIndentation.cs

@ -1294,10 +1294,10 @@ if (b) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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
{

Loading…
Cancel
Save