Browse Source

Added chained method call wrapping option.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
fa4f2e033e
  1. 12
      ICSharpCode.NRefactory.CSharp/Ast/Expressions/MemberReferenceExpression.cs
  2. 33
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  3. 6
      ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs
  5. 10
      ICSharpCode.NRefactory.CSharp/Formatter/TextEditorOptions.cs
  6. 48
      ICSharpCode.NRefactory.Tests/FormattingTests/TestWrapping.cs
  7. 9
      ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

12
ICSharpCode.NRefactory.CSharp/Ast/Expressions/MemberReferenceExpression.cs

@ -34,8 +34,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -34,8 +34,16 @@ namespace ICSharpCode.NRefactory.CSharp
public class MemberReferenceExpression : Expression
{
public Expression Target {
get { return GetChildByRole (Roles.TargetExpression); }
set { SetChildByRole(Roles.TargetExpression, value); }
get {
return GetChildByRole(Roles.TargetExpression);
}
set {
SetChildByRole(Roles.TargetExpression, value);
}
}
public CSharpTokenNode DotToken {
get { return GetChildByRole (Roles.Dot); }
}
public string MemberName {

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

@ -445,7 +445,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -445,7 +445,7 @@ namespace ICSharpCode.NRefactory.CSharp
return i;
}
int ForceSpacesBeforeRemoveNewLines(AstNode n)
int ForceSpacesBeforeRemoveNewLines(AstNode n, bool forceSpace = true)
{
if (n == null || n.IsNull) {
return 0;
@ -459,7 +459,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -459,7 +459,7 @@ namespace ICSharpCode.NRefactory.CSharp
i--;
}
var length = System.Math.Max(0, (offset - 1) - i);
AddChange(i + 1, length, " ");
AddChange(i + 1, length, forceSpace ? " " : "");
return i;
}
@ -1600,6 +1600,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1600,6 +1600,12 @@ namespace ICSharpCode.NRefactory.CSharp
}
}
bool DoWrap(Wrapping wrapping, AstNode wrapNode)
{
return wrapping == Wrapping.WrapAlways ||
options.WrapLineLength > 0 && wrapping == Wrapping.WrapIfTooLong && wrapNode.StartLocation.Column >= options.WrapLineLength;
}
public override void VisitInvocationExpression(InvocationExpression invocationExpression)
{
ForceSpacesBefore(invocationExpression.LParToken, policy.SpaceBeforeMethodCallParentheses);
@ -1612,9 +1618,24 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1612,9 +1618,24 @@ namespace ICSharpCode.NRefactory.CSharp
}
FormatCommas(invocationExpression, policy.SpaceBeforeMethodCallParameterComma, policy.SpaceAfterMethodCallParameterComma);
if (invocationExpression.Target is MemberReferenceExpression) {
var mt = (MemberReferenceExpression)invocationExpression.Target;
if (mt.Target is InvocationExpression) {
if (policy.ChainedMethodCallWrapping == Wrapping.DoNotWrap) {
ForceSpacesBeforeRemoveNewLines(mt.DotToken, false);
} else if (DoWrap(policy.ChainedMethodCallWrapping, mt.DotToken)) {
curIndent.Push(IndentType.Continuation);
FixStatementIndentation(mt.DotToken.StartLocation);
curIndent.Pop();
}
}
}
base.VisitInvocationExpression(invocationExpression);
}
public override void VisitIndexerExpression(IndexerExpression indexerExpression)
{
ForceSpacesBefore(indexerExpression.LBracketToken, policy.SpacesBeforeBrackets);
@ -1694,7 +1715,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1694,7 +1715,7 @@ namespace ICSharpCode.NRefactory.CSharp
public override void VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression)
{
if (policy.ArrayInitializerWrapping == Wrapping.WrapAlways) {
if (DoWrap(policy.ArrayInitializerWrapping, arrayInitializerExpression.RBraceToken)) {
EnforceBraceStyle(policy.ArrayInitializerBraceStyle, arrayInitializerExpression.LBraceToken, arrayInitializerExpression.RBraceToken);
curIndent.Push(IndentType.Block);
foreach (var init in arrayInitializerExpression.Elements) {
@ -1728,6 +1749,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1728,6 +1749,12 @@ namespace ICSharpCode.NRefactory.CSharp
base.VisitNamedArgumentExpression(namedArgumentExpression);
}
public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression)
{
ForceSpacesAfter(memberReferenceExpression.DotToken, false);
base.VisitMemberReferenceExpression(memberReferenceExpression);
}
#endregion
void ForceSpaceBefore(int offset, bool forceSpace)

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

@ -786,6 +786,12 @@ namespace ICSharpCode.NRefactory.CSharp @@ -786,6 +786,12 @@ namespace ICSharpCode.NRefactory.CSharp
set;
}
public Wrapping ChainedMethodCallWrapping {
get;
set;
}
#endregion
internal CSharpFormattingOptions()

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

@ -175,6 +175,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -175,6 +175,7 @@ namespace ICSharpCode.NRefactory.CSharp
BlankLinesBetweenMembers = 1,
KeepCommentsAtFirstColumn = true,
ChainedMethodCallWrapping = Wrapping.WrapAlways
};
}
@ -313,6 +314,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -313,6 +314,7 @@ namespace ICSharpCode.NRefactory.CSharp
BlankLinesBetweenMembers = 1,
KeepCommentsAtFirstColumn = true,
ChainedMethodCallWrapping = Wrapping.WrapAlways
};
}

10
ICSharpCode.NRefactory.CSharp/Formatter/TextEditorOptions.cs

@ -94,12 +94,22 @@ namespace ICSharpCode.NRefactory.CSharp @@ -94,12 +94,22 @@ namespace ICSharpCode.NRefactory.CSharp
set;
}
/// <summary>
/// Gets or sets the length of the desired line length. The formatting engine will wrap at wrap points set to Wrapping.WrapIfTooLong if the line length is too long.
/// 0 means do not wrap.
/// </summary>
public int WrapLineLength {
get;
set;
}
public TextEditorOptions()
{
TabsToSpaces = false;
TabSize = 4;
IndentSize = 4;
ContinuationIndent = 4;
WrapLineLength = 0;
EolMarker = Environment.NewLine;
}
}

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

@ -118,5 +118,53 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -118,5 +118,53 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
}");
}
[Test()]
public void TestChainedMethodCallWrapping()
{
var policy = FormattingOptionsFactory.CreateMono();
policy.ChainedMethodCallWrapping = Wrapping.WrapAlways;
Test(policy, @"class Test
{
void TestMe ()
{
Foo ().Bar (). Zoom();
}
}",
@"class Test
{
void TestMe ()
{
Foo ()
.Bar ()
.Zoom ();
}
}");
}
[Test()]
public void TestChainedMethodCallDoNotWrapWrapping()
{
var policy = FormattingOptionsFactory.CreateMono();
policy.ChainedMethodCallWrapping = Wrapping.DoNotWrap;
Test(policy, @"class Test
{
void TestMe ()
{
Foo ()
.Bar ()
.Zoom ();
}
}",
@"class Test
{
void TestMe ()
{
Foo ().Bar ().Zoom ();
}
}");
}
}
}

9
ICSharpCode.NRefactory.Tests/FormattingTests/TextEditorTestAdapter.cs

@ -28,12 +28,13 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests @@ -28,12 +28,13 @@ namespace ICSharpCode.NRefactory.CSharp.FormattingTests
return b.ToString();
}*/
protected static IDocument GetResult (CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.OnTheFly)
{
input = NormalizeNewlines (input);
var document = new StringBuilderDocument (input);
var options = new TextEditorOptions ();
input = NormalizeNewlines(input);
var document = new StringBuilderDocument(input);
var options = new TextEditorOptions();
options.EolMarker = "\n";
options.WrapLineLength = 80;
var visitor = new AstFormattingVisitor (policy, document, options);
visitor.FormattingMode = mode;
var compilationUnit = new CSharpParser ().Parse (document, "test.cs");

Loading…
Cancel
Save