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 @@ -445,6 +445,24 @@ namespace ICSharpCode.NRefactory.CSharp
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)
{
FormatAttributedNode(propertyDeclaration);
@ -1412,18 +1430,11 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1412,18 +1430,11 @@ namespace ICSharpCode.NRefactory.CSharp
}
var lastLoc = variableDeclarationStatement.StartLocation;
foreach (var initializer in variableDeclarationStatement.Variables) {
var indent = !(initializer.Initializer is AnonymousMethodExpression);
if (indent) {
curIndent.Push(IndentType.Block);
}
if (lastLoc.Line != initializer.StartLocation.Line) {
FixStatementIndentation(initializer.StartLocation);
lastLoc = initializer.StartLocation;
}
initializer.AcceptVisitor(this);
if (indent) {
curIndent.Pop ();
}
}
FormatCommas(variableDeclarationStatement, policy.SpaceBeforeLocalVariableDeclarationComma, policy.SpaceAfterLocalVariableDeclarationComma);
@ -1681,6 +1692,28 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1681,6 +1692,28 @@ namespace ICSharpCode.NRefactory.CSharp
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)
{
ForceSpacesAfter(lambdaExpression.ArrowToken, true);

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

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

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

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

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

@ -57,7 +57,7 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -57,7 +57,7 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
}
[Test()]
public void TestKeepCommentsAtFirstColumnFalse ()
public void TestKeepCommentsAtFirstColumnFalse()
{
var policy = new CSharpFormattingOptions() {
KeepCommentsAtFirstColumn = false
@ -79,6 +79,29 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -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 @@ @@ -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 @@ @@ -264,6 +264,7 @@
<Compile Include="CSharp\CodeActions\ExtractMethodTests.cs" />
<Compile Include="CSharp\Parser\Bugs\ParserBugTests.cs" />
<Compile Include="FormattingTests\TestKeepReformattingRules.cs" />
<Compile Include="FormattingTests\TestWrapping.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save