Browse Source

Improved array initializer wrapping options.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
ca289f5fc5
  1. 47
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 34
      ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs
  4. 25
      ICSharpCode.NRefactory.Tests/FormattingTests/TestKeepReformattingRules.cs
  5. 125
      ICSharpCode.NRefactory.Tests/FormattingTests/TestWrapping.cs
  6. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

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

@ -445,6 +445,24 @@ namespace ICSharpCode.NRefactory.CSharp
return i; return i;
} }
int ForceSpacesBeforeRemoveNewLines(AstNode n)
{
if (n == null || n.IsNull) {
return 0;
}
int offset = document.GetOffset(n.StartLocation);
int i = offset - 1;
while (i >= 0) {
char ch = document.GetCharAt(i);
if (!IsSpacing(ch) && ch != '\r' && ch != '\n')
break;
i--;
}
var length = System.Math.Max(0, (offset - 1) - i);
AddChange(i + 1, length, " ");
return i;
}
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
{ {
FormatAttributedNode(propertyDeclaration); FormatAttributedNode(propertyDeclaration);
@ -1412,18 +1430,11 @@ namespace ICSharpCode.NRefactory.CSharp
} }
var lastLoc = variableDeclarationStatement.StartLocation; var lastLoc = variableDeclarationStatement.StartLocation;
foreach (var initializer in variableDeclarationStatement.Variables) { foreach (var initializer in variableDeclarationStatement.Variables) {
var indent = !(initializer.Initializer is AnonymousMethodExpression);
if (indent) {
curIndent.Push(IndentType.Block);
}
if (lastLoc.Line != initializer.StartLocation.Line) { if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation); FixStatementIndentation(initializer.StartLocation);
lastLoc = initializer.StartLocation; lastLoc = initializer.StartLocation;
} }
initializer.AcceptVisitor(this); initializer.AcceptVisitor(this);
if (indent) {
curIndent.Pop ();
}
} }
FormatCommas(variableDeclarationStatement, policy.SpaceBeforeLocalVariableDeclarationComma, policy.SpaceAfterLocalVariableDeclarationComma); FormatCommas(variableDeclarationStatement, policy.SpaceBeforeLocalVariableDeclarationComma, policy.SpaceAfterLocalVariableDeclarationComma);
@ -1681,6 +1692,28 @@ namespace ICSharpCode.NRefactory.CSharp
base.VisitArrayCreateExpression(arrayObjectCreateExpression); base.VisitArrayCreateExpression(arrayObjectCreateExpression);
} }
public override void VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression)
{
if (policy.ArrayInitializerWrapping == Wrapping.WrapAlways) {
EnforceBraceStyle(policy.ArrayInitializerBraceStyle, arrayInitializerExpression.LBraceToken, arrayInitializerExpression.RBraceToken);
curIndent.Push(IndentType.Block);
foreach (var init in arrayInitializerExpression.Elements) {
FixStatementIndentation(init.StartLocation);
init.AcceptVisitor(this);
}
curIndent.Pop();
} else if (policy.ArrayInitializerWrapping == Wrapping.DoNotWrap) {
ForceSpacesBeforeRemoveNewLines(arrayInitializerExpression.LBraceToken);
ForceSpacesBeforeRemoveNewLines(arrayInitializerExpression.RBraceToken);
foreach (var init in arrayInitializerExpression.Elements) {
ForceSpacesBeforeRemoveNewLines(init);
init.AcceptVisitor(this);
}
} else {
base.VisitArrayInitializerExpression(arrayInitializerExpression);
}
}
public override void VisitLambdaExpression(LambdaExpression lambdaExpression) public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
{ {
ForceSpacesAfter(lambdaExpression.ArrowToken, true); ForceSpacesAfter(lambdaExpression.ArrowToken, true);

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

@ -48,12 +48,6 @@ namespace ICSharpCode.NRefactory.CSharp
AddBraces AddBraces
} }
public enum ArrayInitializerPlacement
{
AlwaysNewLine,
AlwaysSameLine
}
public enum PropertyFormatting public enum PropertyFormatting
{ {
AllowOneLine, AllowOneLine,
@ -61,6 +55,12 @@ namespace ICSharpCode.NRefactory.CSharp
ForceNewLine ForceNewLine
} }
public enum Wrapping {
DoNotWrap,
WrapAlways,
WrapIfTooLong
}
public class CSharpFormattingOptions public class CSharpFormattingOptions
{ {
public string Name { public string Name {
@ -322,11 +322,6 @@ namespace ICSharpCode.NRefactory.CSharp
get; get;
set; set;
} }
public ArrayInitializerPlacement PlaceArrayInitializersOnNewLine {
get;
set;
}
#endregion #endregion
#region Spaces #region Spaces
@ -779,6 +774,20 @@ namespace ICSharpCode.NRefactory.CSharp
} }
#endregion #endregion
#region Wrapping
public Wrapping ArrayInitializerWrapping {
get;
set;
}
public BraceStyle ArrayInitializerBraceStyle {
get;
set;
}
#endregion
public CSharpFormattingOptions() public CSharpFormattingOptions()
{ {
IndentNamespaceBody = true; IndentNamespaceBody = true;
@ -804,7 +813,8 @@ namespace ICSharpCode.NRefactory.CSharp
PlaceCatchOnNewLine = false; PlaceCatchOnNewLine = false;
PlaceFinallyOnNewLine = false; PlaceFinallyOnNewLine = false;
PlaceWhileOnNewLine = false; PlaceWhileOnNewLine = false;
PlaceArrayInitializersOnNewLine = ArrayInitializerPlacement.AlwaysSameLine; ArrayInitializerWrapping = Wrapping.WrapIfTooLong;
ArrayInitializerBraceStyle = BraceStyle.EndOfLine;
SpaceBeforeMethodCallParentheses = true; SpaceBeforeMethodCallParentheses = true;
SpaceBeforeMethodDeclarationParentheses = true; SpaceBeforeMethodDeclarationParentheses = true;

2
ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -660,7 +660,7 @@ namespace ICSharpCode.NRefactory.CSharp
void PrintInitializerElements(AstNodeCollection<Expression> elements) void PrintInitializerElements(AstNodeCollection<Expression> elements)
{ {
BraceStyle style; BraceStyle style;
if (policy.PlaceArrayInitializersOnNewLine == ArrayInitializerPlacement.AlwaysNewLine) { if (policy.ArrayInitializerWrapping == Wrapping.WrapAlways) {
style = BraceStyle.NextLine; style = BraceStyle.NextLine;
} else { } else {
style = BraceStyle.EndOfLine; style = BraceStyle.EndOfLine;

25
ICSharpCode.NRefactory.Tests/FormattingTests/TestKeepReformattingRules.cs

@ -57,7 +57,7 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
} }
[Test()] [Test()]
public void TestKeepCommentsAtFirstColumnFalse () public void TestKeepCommentsAtFirstColumnFalse()
{ {
var policy = new CSharpFormattingOptions() { var policy = new CSharpFormattingOptions() {
KeepCommentsAtFirstColumn = false KeepCommentsAtFirstColumn = false
@ -79,6 +79,29 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
} }
[Test()]
public void TestKeepCommentsAfterStatement()
{
var policy = new CSharpFormattingOptions() {
KeepCommentsAtFirstColumn = true
};
Test(policy, @"class Test
{
void TestMe ()
{
FooBar (); // comment
}
}",
@"class Test
{
void TestMe ()
{
FooBar (); // comment
}
}");
}
} }
} }

125
ICSharpCode.NRefactory.Tests/FormattingTests/TestWrapping.cs

@ -0,0 +1,125 @@
//
// TestWrapping.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp;
namespace ICSharpCode.NRefactory.CSharp.FormattingTests
{
[TestFixture()]
public class TestWrapping : TestBase
{
[Test()]
public void TestInitializerWrapAlways()
{
var policy = new CSharpFormattingOptions() {
ArrayInitializerWrapping = Wrapping.WrapAlways
};
Test(policy, @"class Test
{
void TestMe ()
{
var foo = new [] { 1, 2, 3 };
}
}",
@"class Test
{
void TestMe ()
{
var foo = new [] {
1,
2,
3
};
}
}");
}
[Test()]
public void TestInitializerDoNotWrap()
{
var policy = new CSharpFormattingOptions() {
ArrayInitializerWrapping = Wrapping.DoNotWrap
};
Test(policy,
@"class Test
{
void TestMe ()
{
var foo = new [] {
1,
2,
3
};
}
}", @"class Test
{
void TestMe ()
{
var foo = new [] { 1, 2, 3 };
}
}");
}
[Test()]
public void TestInitializerBraceStyle()
{
var policy = new CSharpFormattingOptions() {
ArrayInitializerWrapping = Wrapping.WrapAlways,
ArrayInitializerBraceStyle = BraceStyle.NextLine
};
Test(policy, @"class Test
{
void TestMe ()
{
var foo = new [] {
1,
2,
3
};
}
}",
@"class Test
{
void TestMe ()
{
var foo = new []
{
1,
2,
3
};
}
}");
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -264,6 +264,7 @@
<Compile Include="CSharp\CodeActions\ExtractMethodTests.cs" /> <Compile Include="CSharp\CodeActions\ExtractMethodTests.cs" />
<Compile Include="CSharp\Parser\Bugs\ParserBugTests.cs" /> <Compile Include="CSharp\Parser\Bugs\ParserBugTests.cs" />
<Compile Include="FormattingTests\TestKeepReformattingRules.cs" /> <Compile Include="FormattingTests\TestKeepReformattingRules.cs" />
<Compile Include="FormattingTests\TestWrapping.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj"> <ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save