~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Formatter/FormattingVisitor_Expressions.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// AstFormattingVisitor_Expressions.cs
 
3
//
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using System.Collections.Generic;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp
 
31
{
 
32
        partial class FormattingVisitor : DepthFirstAstVisitor
 
33
        {
 
34
                public override void VisitComposedType(ComposedType composedType)
 
35
                {
 
36
                        var spec = composedType.ArraySpecifiers.FirstOrDefault();
 
37
                        if (spec != null)
 
38
                                ForceSpacesBefore(spec.LBracketToken, policy.SpaceBeforeArrayDeclarationBrackets);
 
39
 
 
40
                        if (composedType.HasNullableSpecifier)
 
41
                                ForceSpacesBefore(composedType.NullableSpecifierToken, false);
 
42
 
 
43
                        if (composedType.PointerRank > 0)
 
44
                                foreach (var token in composedType.PointerTokens)
 
45
                                        ForceSpacesBefore(token, false);
 
46
 
 
47
                        base.VisitComposedType(composedType);
 
48
                }
 
49
 
 
50
                public override void VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression)
 
51
                {
 
52
                        if (!anonymousMethodExpression.Body.IsNull) {
 
53
                                if (anonymousMethodExpression.Body.LBraceToken.GetNextNode(NoWhitespacePredicate) != anonymousMethodExpression.Body.RBraceToken) {
 
54
                                        FixOpenBrace(policy.AnonymousMethodBraceStyle, anonymousMethodExpression.Body.LBraceToken);
 
55
                                        VisitBlockWithoutFixingBraces(anonymousMethodExpression.Body, policy.IndentBlocks);
 
56
                                        FixClosingBrace(policy.AnonymousMethodBraceStyle, anonymousMethodExpression.Body.RBraceToken);
 
57
                                } else {
 
58
                                        VisitBlockWithoutFixingBraces(anonymousMethodExpression.Body, policy.IndentBlocks);
 
59
                                }
 
60
                                return;
 
61
                        }
 
62
                        base.VisitAnonymousMethodExpression(anonymousMethodExpression);
 
63
                }
 
64
 
 
65
                public override void VisitAssignmentExpression(AssignmentExpression assignmentExpression)
 
66
                {
 
67
                        ForceSpacesAround(assignmentExpression.OperatorToken, policy.SpaceAroundAssignment);
 
68
                        base.VisitAssignmentExpression(assignmentExpression);
 
69
                }
 
70
 
 
71
                public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
 
72
                {
 
73
                        bool forceSpaces = false;
 
74
                        switch (binaryOperatorExpression.Operator) {
 
75
                                case BinaryOperatorType.Equality:
 
76
                                        case BinaryOperatorType.InEquality:
 
77
                                        forceSpaces = policy.SpaceAroundEqualityOperator;
 
78
                                        break;
 
79
                                        case BinaryOperatorType.GreaterThan:
 
80
                                        case BinaryOperatorType.GreaterThanOrEqual:
 
81
                                        case BinaryOperatorType.LessThan:
 
82
                                        case BinaryOperatorType.LessThanOrEqual:
 
83
                                        forceSpaces = policy.SpaceAroundRelationalOperator;
 
84
                                        break;
 
85
                                        case BinaryOperatorType.ConditionalAnd:
 
86
                                        case BinaryOperatorType.ConditionalOr:
 
87
                                        forceSpaces = policy.SpaceAroundLogicalOperator;
 
88
                                        break;
 
89
                                        case BinaryOperatorType.BitwiseAnd:
 
90
                                        case BinaryOperatorType.BitwiseOr:
 
91
                                        case BinaryOperatorType.ExclusiveOr:
 
92
                                        forceSpaces = policy.SpaceAroundBitwiseOperator;
 
93
                                        break;
 
94
                                        case BinaryOperatorType.Add:
 
95
                                        case BinaryOperatorType.Subtract:
 
96
                                        forceSpaces = policy.SpaceAroundAdditiveOperator;
 
97
                                        break;
 
98
                                        case BinaryOperatorType.Multiply:
 
99
                                        case BinaryOperatorType.Divide:
 
100
                                        case BinaryOperatorType.Modulus:
 
101
                                        forceSpaces = policy.SpaceAroundMultiplicativeOperator;
 
102
                                        break;
 
103
                                        case BinaryOperatorType.ShiftLeft:
 
104
                                        case BinaryOperatorType.ShiftRight:
 
105
                                        forceSpaces = policy.SpaceAroundShiftOperator;
 
106
                                        break;
 
107
                                        case BinaryOperatorType.NullCoalescing:
 
108
                                        forceSpaces = policy.SpaceAroundNullCoalescingOperator;
 
109
                                        break;
 
110
                        }
 
111
                        ForceSpacesAround(binaryOperatorExpression.OperatorToken, forceSpaces);
 
112
 
 
113
                        base.VisitBinaryOperatorExpression(binaryOperatorExpression);
 
114
                        // Handle line breaks in binary opeartor expression.
 
115
                        if (binaryOperatorExpression.Left.EndLocation.Line != binaryOperatorExpression.Right.StartLocation.Line) {
 
116
                                curIndent.Push(IndentType.Block);
 
117
                                if (binaryOperatorExpression.OperatorToken.StartLocation.Line == binaryOperatorExpression.Right.StartLocation.Line) {
 
118
                                        FixStatementIndentation(binaryOperatorExpression.OperatorToken.StartLocation);
 
119
                                } else {
 
120
                                        FixStatementIndentation(binaryOperatorExpression.Right.StartLocation);
 
121
                                }
 
122
                                curIndent.Pop ();
 
123
                        }
 
124
                }
 
125
 
 
126
                public override void VisitConditionalExpression(ConditionalExpression conditionalExpression)
 
127
                {
 
128
                        ForceSpacesBefore(conditionalExpression.QuestionMarkToken, policy.SpaceBeforeConditionalOperatorCondition);
 
129
                        ForceSpacesAfter(conditionalExpression.QuestionMarkToken, policy.SpaceAfterConditionalOperatorCondition);
 
130
                        ForceSpacesBefore(conditionalExpression.ColonToken, policy.SpaceBeforeConditionalOperatorSeparator);
 
131
                        ForceSpacesAfter(conditionalExpression.ColonToken, policy.SpaceAfterConditionalOperatorSeparator);
 
132
                        base.VisitConditionalExpression(conditionalExpression);
 
133
                }
 
134
 
 
135
                public override void VisitCastExpression(CastExpression castExpression)
 
136
                {
 
137
                        if (castExpression.RParToken != null) {
 
138
                                ForceSpacesAfter(castExpression.LParToken, policy.SpacesWithinCastParentheses);
 
139
                                ForceSpacesBefore(castExpression.RParToken, policy.SpacesWithinCastParentheses);
 
140
 
 
141
                                ForceSpacesAfter(castExpression.RParToken, policy.SpaceAfterTypecast);
 
142
                        }
 
143
                        base.VisitCastExpression(castExpression);
 
144
                }
 
145
 
 
146
                void ForceSpacesAround(AstNode node, bool forceSpaces)
 
147
                {
 
148
                        if (node.IsNull) {
 
149
                                return;
 
150
                        }
 
151
                        ForceSpacesBefore(node, forceSpaces);
 
152
                        ForceSpacesAfter(node, forceSpaces);
 
153
                }
 
154
 
 
155
                void FormatCommas(AstNode parent, bool before, bool after)
 
156
                {
 
157
                        if (parent.IsNull) {
 
158
                                return;
 
159
                        }
 
160
                        foreach (CSharpTokenNode comma in parent.Children.Where (node => node.Role == Roles.Comma)) {
 
161
                                ForceSpacesAfter(comma, after);
 
162
                                ForceSpacesBefore(comma, before);
 
163
                        }
 
164
                }
 
165
 
 
166
                bool DoWrap (Wrapping wrapping, AstNode wrapNode, int argumentCount)
 
167
                {
 
168
                        return wrapping == Wrapping.WrapAlways || 
 
169
                                options.WrapLineLength > 0 && argumentCount > 1 && wrapping == Wrapping.WrapIfTooLong && wrapNode.StartLocation.Column >= options.WrapLineLength;
 
170
                }
 
171
 
 
172
                void FormatArguments(AstNode node)
 
173
                {
 
174
                        Wrapping methodCallArgumentWrapping;
 
175
                        NewLinePlacement newLineAferMethodCallOpenParentheses;
 
176
                        bool doAlignToFirstArgument;
 
177
                        NewLinePlacement methodClosingParenthesesOnNewLine;
 
178
                        bool spaceWithinMethodCallParentheses;
 
179
                        bool spaceWithinEmptyParentheses;
 
180
                        bool spaceAfterMethodCallParameterComma;
 
181
                        bool spaceBeforeMethodCallParameterComma;
 
182
 
 
183
                        CSharpTokenNode rParToken, lParToken;
 
184
                        List<AstNode> arguments;
 
185
 
 
186
                        var constructorDeclaration = node as ConstructorDeclaration;
 
187
                        if (constructorDeclaration != null) {
 
188
                                methodCallArgumentWrapping = policy.MethodDeclarationParameterWrapping;
 
189
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferMethodDeclarationOpenParentheses;
 
190
                                methodClosingParenthesesOnNewLine = policy.MethodDeclarationClosingParenthesesOnNewLine;
 
191
                                doAlignToFirstArgument = policy.AlignToFirstMethodDeclarationParameter;
 
192
                                spaceWithinMethodCallParentheses = policy.SpaceWithinConstructorDeclarationParentheses;
 
193
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterConstructorDeclarationParameterComma;
 
194
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeConstructorDeclarationParameterComma;
 
195
                                spaceWithinEmptyParentheses = policy.SpaceBetweenEmptyMethodDeclarationParentheses;
 
196
                                lParToken = constructorDeclaration.LParToken;
 
197
                                rParToken = constructorDeclaration.RParToken;
 
198
                                arguments = constructorDeclaration.Parameters.Cast<AstNode> ().ToList ();
 
199
                        } else if (node is IndexerDeclaration) {
 
200
                                var indexer = (IndexerDeclaration)node;
 
201
                                methodCallArgumentWrapping = policy.IndexerDeclarationParameterWrapping;
 
202
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferIndexerDeclarationOpenBracket;
 
203
                                methodClosingParenthesesOnNewLine = policy.IndexerDeclarationClosingBracketOnNewLine;
 
204
                                doAlignToFirstArgument = policy.AlignToFirstIndexerDeclarationParameter;
 
205
                                spaceWithinMethodCallParentheses = policy.SpaceWithinIndexerDeclarationBracket;
 
206
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterIndexerDeclarationParameterComma;
 
207
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeIndexerDeclarationParameterComma;
 
208
                                spaceWithinEmptyParentheses = policy.SpaceBetweenEmptyMethodDeclarationParentheses;
 
209
                                lParToken = indexer.LBracketToken;
 
210
                                rParToken = indexer.RBracketToken;
 
211
                                arguments = indexer.Parameters.Cast<AstNode> ().ToList ();
 
212
                        } else if (node is OperatorDeclaration) {
 
213
                                var op = (OperatorDeclaration)node;
 
214
                                methodCallArgumentWrapping = policy.MethodDeclarationParameterWrapping;
 
215
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferMethodDeclarationOpenParentheses;
 
216
                                methodClosingParenthesesOnNewLine = policy.MethodDeclarationClosingParenthesesOnNewLine;
 
217
                                doAlignToFirstArgument = policy.AlignToFirstMethodDeclarationParameter;
 
218
                                spaceWithinMethodCallParentheses = policy.SpaceWithinMethodDeclarationParentheses;
 
219
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterMethodDeclarationParameterComma;
 
220
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeMethodDeclarationParameterComma;
 
221
                                spaceWithinEmptyParentheses = policy.SpaceBetweenEmptyMethodDeclarationParentheses;
 
222
                                lParToken = op.LParToken;
 
223
                                rParToken = op.RParToken;
 
224
                                arguments = op.Parameters.Cast<AstNode> ().ToList ();
 
225
                        } else if (node is MethodDeclaration)  {
 
226
                                var methodDeclaration = node as MethodDeclaration;
 
227
                                methodCallArgumentWrapping = policy.MethodDeclarationParameterWrapping;
 
228
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferMethodDeclarationOpenParentheses;
 
229
                                methodClosingParenthesesOnNewLine = policy.MethodDeclarationClosingParenthesesOnNewLine;
 
230
                                doAlignToFirstArgument = policy.AlignToFirstMethodDeclarationParameter;
 
231
                                spaceWithinMethodCallParentheses = policy.SpaceWithinMethodDeclarationParentheses;
 
232
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterMethodDeclarationParameterComma;
 
233
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeMethodDeclarationParameterComma;
 
234
                                spaceWithinEmptyParentheses = policy.SpaceBetweenEmptyMethodDeclarationParentheses;
 
235
                                lParToken = methodDeclaration.LParToken;
 
236
                                rParToken = methodDeclaration.RParToken;
 
237
                                arguments = methodDeclaration.Parameters.Cast<AstNode> ().ToList ();
 
238
                        } else if (node is IndexerExpression) {
 
239
                                var indexer = (IndexerExpression)node;
 
240
                                methodCallArgumentWrapping = policy.IndexerArgumentWrapping;
 
241
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferIndexerOpenBracket;
 
242
                                doAlignToFirstArgument = policy.AlignToFirstIndexerArgument;
 
243
                                methodClosingParenthesesOnNewLine = policy.IndexerClosingBracketOnNewLine;
 
244
                                spaceWithinMethodCallParentheses = policy.SpacesWithinBrackets;
 
245
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterBracketComma;
 
246
                                spaceWithinEmptyParentheses = spaceWithinMethodCallParentheses;
 
247
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeBracketComma;
 
248
                                rParToken = indexer.RBracketToken;
 
249
                                lParToken = indexer.LBracketToken;
 
250
                                arguments = indexer.Arguments.Cast<AstNode> ().ToList ();
 
251
                        } else if (node is ObjectCreateExpression) {
 
252
                                var oce = node as ObjectCreateExpression;
 
253
                                methodCallArgumentWrapping = policy.MethodCallArgumentWrapping;
 
254
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferMethodCallOpenParentheses;
 
255
                                doAlignToFirstArgument = policy.AlignToFirstMethodCallArgument;
 
256
                                methodClosingParenthesesOnNewLine = policy.MethodCallClosingParenthesesOnNewLine;
 
257
                                spaceWithinMethodCallParentheses = policy.SpacesWithinNewParentheses;
 
258
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterNewParameterComma;
 
259
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeNewParameterComma;
 
260
                                spaceWithinEmptyParentheses = policy.SpacesBetweenEmptyNewParentheses;
 
261
 
 
262
                                rParToken = oce.RParToken;
 
263
                                lParToken = oce.LParToken;
 
264
                                arguments = oce.Arguments.Cast<AstNode> ().ToList ();
 
265
                        } else {
 
266
                                InvocationExpression invocationExpression = node as InvocationExpression;
 
267
                                methodCallArgumentWrapping = policy.MethodCallArgumentWrapping;
 
268
                                newLineAferMethodCallOpenParentheses = policy.NewLineAferMethodCallOpenParentheses;
 
269
                                methodClosingParenthesesOnNewLine = policy.MethodCallClosingParenthesesOnNewLine;
 
270
                                doAlignToFirstArgument = policy.AlignToFirstMethodCallArgument;
 
271
                                spaceWithinMethodCallParentheses = policy.SpaceWithinMethodCallParentheses;
 
272
                                spaceAfterMethodCallParameterComma = policy.SpaceAfterMethodCallParameterComma;
 
273
                                spaceBeforeMethodCallParameterComma = policy.SpaceBeforeMethodCallParameterComma;
 
274
                                spaceWithinEmptyParentheses = policy.SpaceBetweenEmptyMethodCallParentheses;
 
275
 
 
276
                                rParToken = invocationExpression.RParToken;
 
277
                                lParToken = invocationExpression.LParToken;
 
278
                                arguments = invocationExpression.Arguments.Cast<AstNode> ().ToList ();
 
279
                        }
 
280
 
 
281
                        if (formatter.FormattingMode == ICSharpCode.NRefactory.CSharp.FormattingMode.OnTheFly)
 
282
                                methodCallArgumentWrapping = Wrapping.DoNotChange;
 
283
                        int argumentStart = 1;
 
284
                        var firstarg = arguments.FirstOrDefault();
 
285
                        if (firstarg != null && firstarg.GetPrevNode().Role == Roles.NewLine) {
 
286
                                doAlignToFirstArgument = false;
 
287
                                argumentStart = 0;
 
288
                        }
 
289
 
 
290
                        bool wrapMethodCall = DoWrap(methodCallArgumentWrapping, rParToken, arguments.Count);
 
291
                        if (wrapMethodCall && arguments.Any()) {
 
292
                                if (ShouldBreakLine (newLineAferMethodCallOpenParentheses, lParToken)) {
 
293
                                        curIndent.Push(IndentType.Continuation);
 
294
                                        foreach (var arg in arguments) {
 
295
                                                FixStatementIndentation(arg.StartLocation);
 
296
                                        }
 
297
                                        curIndent.Pop();
 
298
                                } else {
 
299
                                        if (!doAlignToFirstArgument) {
 
300
                                                curIndent.Push(IndentType.Continuation);
 
301
                                                foreach (var arg in arguments.Skip (argumentStart)) {
 
302
                                                        FixStatementIndentation(arg.StartLocation);
 
303
                                                }
 
304
                                                curIndent.Pop();
 
305
                                        } else {
 
306
                                                int extraSpaces = arguments.First().StartLocation.Column - 1 - curIndent.IndentString.Length;
 
307
                                                curIndent.ExtraSpaces += extraSpaces;
 
308
                                                foreach (var arg in arguments.Skip(argumentStart)) {
 
309
                                                        FixStatementIndentation(arg.StartLocation);
 
310
                                                }
 
311
                                                curIndent.ExtraSpaces -= extraSpaces;
 
312
                                        }
 
313
                                }
 
314
 
 
315
                                if (!rParToken.IsNull) {
 
316
                                        if (ShouldBreakLine (methodClosingParenthesesOnNewLine, rParToken)) {
 
317
                                                FixStatementIndentation(rParToken.StartLocation);
 
318
                                        } else if (methodClosingParenthesesOnNewLine == NewLinePlacement.SameLine) {
 
319
                                                ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses);
 
320
                                        }
 
321
                                }
 
322
                        } else {
 
323
                                foreach (var arg in arguments.Skip(argumentStart)) {
 
324
                                        if (arg.GetPrevSibling(NoWhitespacePredicate) != null) {
 
325
                                                if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
 
326
                                                        ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.GetPrevSibling(NoWhitespacePredicate).Role == Roles.Comma);
 
327
                                                } else {
 
328
                                                        if (!doAlignToFirstArgument && arg.PrevSibling.Role == Roles.NewLine) {
 
329
                                                                curIndent.Push(IndentType.Continuation);
 
330
                                                                FixStatementIndentation(arg.StartLocation);
 
331
                                                                curIndent.Pop();
 
332
                                                        } else {
 
333
                                                                if (arg.PrevSibling.StartLocation.Line == arg.StartLocation.Line) {
 
334
                                                                        ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.GetPrevSibling(NoWhitespacePredicate).Role == Roles.Comma);
 
335
                                                                } else {
 
336
                                                                        int extraSpaces = arguments.First().StartLocation.Column - 1 - curIndent.IndentString.Length;
 
337
                                                                        curIndent.ExtraSpaces += extraSpaces;
 
338
                                                                        FixStatementIndentation(arg.StartLocation);
 
339
                                                                        curIndent.ExtraSpaces -= extraSpaces;
 
340
                                                                }
 
341
                                                        }
 
342
                                                }
 
343
                                        }
 
344
                                        arg.AcceptVisitor(this);
 
345
                                }
 
346
                                if (!rParToken.IsNull) {
 
347
                                        if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
 
348
                                                ForceSpacesBeforeRemoveNewLines(rParToken, arguments.Any() ? spaceWithinMethodCallParentheses : spaceWithinEmptyParentheses);
 
349
                                        } else {
 
350
                                                bool sameLine = rParToken.GetPrevNode(n => n.Role == Roles.Argument || n.Role == Roles.Parameter || n.Role == Roles.LPar || n.Role == Roles.Comma).EndLocation.Line == rParToken.StartLocation.Line;
 
351
                                                if (sameLine) {
 
352
                                                        ForceSpacesBeforeRemoveNewLines(rParToken, arguments.Any() ? spaceWithinMethodCallParentheses : spaceWithinEmptyParentheses);
 
353
                                                } else {
 
354
                                                        FixStatementIndentation(rParToken.StartLocation);
 
355
                                                }
 
356
                                        }
 
357
                                }
 
358
                        }
 
359
                        if (!rParToken.IsNull) {
 
360
                                foreach (CSharpTokenNode comma in rParToken.Parent.Children.Where(n => n.Role == Roles.Comma)) {
 
361
                                        ForceSpacesBefore(comma, spaceBeforeMethodCallParameterComma);
 
362
                                }
 
363
                        }
 
364
                }
 
365
 
 
366
                public override void VisitInvocationExpression(InvocationExpression invocationExpression)
 
367
                {
 
368
                        ForceSpacesBefore(invocationExpression.LParToken, policy.SpaceBeforeMethodCallParentheses);
 
369
                        if (invocationExpression.Arguments.Any()) {
 
370
                                ForceSpacesAfter(invocationExpression.LParToken, policy.SpaceWithinMethodCallParentheses);
 
371
                        } else {
 
372
                                ForceSpacesAfter(invocationExpression.LParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
 
373
                                ForceSpacesBefore(invocationExpression.RParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
 
374
                        }
 
375
 
 
376
                        if (!invocationExpression.Target.IsNull)
 
377
                                invocationExpression.Target.AcceptVisitor(this);
 
378
 
 
379
                        if (invocationExpression.Target is MemberReferenceExpression) {
 
380
                                var mt = (MemberReferenceExpression)invocationExpression.Target;
 
381
                                if (mt.Target is InvocationExpression) {
 
382
                                        if (DoWrap(policy.ChainedMethodCallWrapping, mt.DotToken, 2)) {
 
383
                                                curIndent.Push(IndentType.Continuation);
 
384
                                                FixStatementIndentation(mt.DotToken.StartLocation);
 
385
                                                curIndent.Pop();
 
386
                                        } else {
 
387
                                                if (policy.ChainedMethodCallWrapping == Wrapping.DoNotWrap)
 
388
                                                        ForceSpacesBeforeRemoveNewLines(mt.DotToken, false);
 
389
                                        }
 
390
                                }
 
391
                        }
 
392
                        FormatArguments(invocationExpression);
 
393
                }
 
394
 
 
395
                public override void VisitIndexerExpression(IndexerExpression indexerExpression)
 
396
                {
 
397
                        ForceSpacesBefore(indexerExpression.LBracketToken, policy.SpacesBeforeBrackets);
 
398
                        ForceSpacesAfter(indexerExpression.LBracketToken, policy.SpacesWithinBrackets);
 
399
 
 
400
                        if (!indexerExpression.Target.IsNull)
 
401
                                indexerExpression.Target.AcceptVisitor(this);
 
402
 
 
403
                        FormatArguments(indexerExpression);
 
404
 
 
405
 
 
406
 
 
407
                }
 
408
 
 
409
                public override void VisitParenthesizedExpression(ParenthesizedExpression parenthesizedExpression)
 
410
                {
 
411
                        ForceSpacesAfter(parenthesizedExpression.LParToken, policy.SpacesWithinParentheses);
 
412
                        ForceSpacesBefore(parenthesizedExpression.RParToken, policy.SpacesWithinParentheses);
 
413
                        base.VisitParenthesizedExpression(parenthesizedExpression);
 
414
                }
 
415
 
 
416
                public override void VisitSizeOfExpression(SizeOfExpression sizeOfExpression)
 
417
                {
 
418
                        ForceSpacesBefore(sizeOfExpression.LParToken, policy.SpaceBeforeSizeOfParentheses);
 
419
                        ForceSpacesAfter(sizeOfExpression.LParToken, policy.SpacesWithinSizeOfParentheses);
 
420
                        ForceSpacesBefore(sizeOfExpression.RParToken, policy.SpacesWithinSizeOfParentheses);
 
421
                        base.VisitSizeOfExpression(sizeOfExpression);
 
422
                }
 
423
 
 
424
                public override void VisitTypeOfExpression(TypeOfExpression typeOfExpression)
 
425
                {
 
426
                        ForceSpacesBefore(typeOfExpression.LParToken, policy.SpaceBeforeTypeOfParentheses);
 
427
                        ForceSpacesAfter(typeOfExpression.LParToken, policy.SpacesWithinTypeOfParentheses);
 
428
                        ForceSpacesBefore(typeOfExpression.RParToken, policy.SpacesWithinTypeOfParentheses);
 
429
                        base.VisitTypeOfExpression(typeOfExpression);
 
430
                }
 
431
 
 
432
                public override void VisitCheckedExpression(CheckedExpression checkedExpression)
 
433
                {
 
434
                        ForceSpacesAfter(checkedExpression.LParToken, policy.SpacesWithinCheckedExpressionParantheses);
 
435
                        ForceSpacesBefore(checkedExpression.RParToken, policy.SpacesWithinCheckedExpressionParantheses);
 
436
                        base.VisitCheckedExpression(checkedExpression);
 
437
                }
 
438
 
 
439
                public override void VisitUncheckedExpression(UncheckedExpression uncheckedExpression)
 
440
                {
 
441
                        ForceSpacesAfter(uncheckedExpression.LParToken, policy.SpacesWithinCheckedExpressionParantheses);
 
442
                        ForceSpacesBefore(uncheckedExpression.RParToken, policy.SpacesWithinCheckedExpressionParantheses);
 
443
                        base.VisitUncheckedExpression(uncheckedExpression);
 
444
                }
 
445
 
 
446
                public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression)
 
447
                {
 
448
                        ForceSpacesBefore(objectCreateExpression.LParToken, policy.SpaceBeforeNewParentheses);
 
449
 
 
450
                        if (objectCreateExpression.Arguments.Any()) {
 
451
                                if (!objectCreateExpression.LParToken.IsNull)
 
452
                                        ForceSpacesAfter(objectCreateExpression.LParToken, policy.SpacesWithinNewParentheses);
 
453
                        } else {
 
454
                                if (!objectCreateExpression.LParToken.IsNull)
 
455
                                        ForceSpacesAfter(objectCreateExpression.LParToken, policy.SpacesBetweenEmptyNewParentheses);
 
456
                        }
 
457
 
 
458
                        if (!objectCreateExpression.Type.IsNull)
 
459
                                objectCreateExpression.Type.AcceptVisitor(this);
 
460
                        objectCreateExpression.Initializer.AcceptVisitor(this);
 
461
                        FormatArguments(objectCreateExpression);
 
462
 
 
463
                }
 
464
 
 
465
                public override void VisitArrayCreateExpression(ArrayCreateExpression arrayObjectCreateExpression)
 
466
                {
 
467
                        FormatCommas(arrayObjectCreateExpression, policy.SpaceBeforeMethodCallParameterComma, policy.SpaceAfterMethodCallParameterComma);
 
468
                        base.VisitArrayCreateExpression(arrayObjectCreateExpression);
 
469
                }
 
470
 
 
471
                public override void VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression)
 
472
                {
 
473
                        if (DoWrap(policy.ArrayInitializerWrapping, arrayInitializerExpression.RBraceToken, arrayInitializerExpression.Elements.Count)) {
 
474
                                FixOpenBrace(policy.ArrayInitializerBraceStyle, arrayInitializerExpression.LBraceToken);
 
475
                                curIndent.Push(IndentType.Block);
 
476
                                foreach (var init in arrayInitializerExpression.Elements) {
 
477
                                        FixStatementIndentation(init.StartLocation);
 
478
                                        init.AcceptVisitor(this);
 
479
                                }
 
480
                                curIndent.Pop();
 
481
                                FixClosingBrace(policy.ArrayInitializerBraceStyle, arrayInitializerExpression.RBraceToken);
 
482
                        } else if (policy.ArrayInitializerWrapping == Wrapping.DoNotWrap) {
 
483
                                ForceSpacesBeforeRemoveNewLines(arrayInitializerExpression.LBraceToken);
 
484
                                ForceSpacesBeforeRemoveNewLines(arrayInitializerExpression.RBraceToken);
 
485
                                foreach (var init in arrayInitializerExpression.Elements) {
 
486
                                        ForceSpacesBeforeRemoveNewLines(init);
 
487
                                        init.AcceptVisitor(this);
 
488
                                }
 
489
                        } else {
 
490
                                var lBrace = arrayInitializerExpression.LBraceToken;
 
491
                                var rBrace = arrayInitializerExpression.RBraceToken;
 
492
 
 
493
                                foreach (var child in arrayInitializerExpression.Children) {
 
494
                                        if (child.Role == Roles.LBrace) {
 
495
                                                if (lBrace.StartLocation.Line == rBrace.StartLocation.Line && policy.AllowOneLinedArrayInitialziers) {
 
496
                                                        ForceSpaceBefore(child, true);
 
497
                                                        ForceSpacesAfter(child, true);
 
498
                                                } else {
 
499
                                                        FixOpenBrace(policy.ArrayInitializerBraceStyle, child);
 
500
                                                }
 
501
                                                curIndent.Push(IndentType.Block);
 
502
                                                continue;
 
503
                                        }
 
504
                                        if (child.Role == Roles.RBrace) {
 
505
                                                curIndent.Pop();
 
506
                                                if (lBrace.StartLocation.Line == rBrace.StartLocation.Line && policy.AllowOneLinedArrayInitialziers) {
 
507
                                                        ForceSpaceBefore(child, true);
 
508
 
 
509
                                                } else {
 
510
                                                        FixClosingBrace(policy.ArrayInitializerBraceStyle, child);
 
511
                                                }
 
512
                                                continue;
 
513
                                        }
 
514
                                        if (child.Role == Roles.Expression) {
 
515
                                                if (child.PrevSibling.Role == Roles.NewLine)
 
516
                                                        FixIndentation(child);
 
517
                                                if (child.PrevSibling.Role == Roles.Comma)
 
518
                                                        ForceSpaceBefore(child, true);
 
519
                                                if (child.NextSibling.Role == Roles.Comma)
 
520
                                                        ForceSpacesAfter(child, false);
 
521
                                                continue;
 
522
                                        }
 
523
 
 
524
                                        child.AcceptVisitor(this);
 
525
                                }
 
526
                        }
 
527
                }
 
528
 
 
529
                public override void VisitLambdaExpression(LambdaExpression lambdaExpression)
 
530
                {
 
531
                        ForceSpacesAfter(lambdaExpression.ArrowToken, true);
 
532
                        ForceSpacesBefore(lambdaExpression.ArrowToken, true);
 
533
 
 
534
                        base.VisitLambdaExpression(lambdaExpression);
 
535
                }
 
536
 
 
537
                public override void VisitNamedArgumentExpression(NamedArgumentExpression namedArgumentExpression)
 
538
                {
 
539
                        ForceSpacesAfter(namedArgumentExpression.ColonToken, policy.SpaceInNamedArgumentAfterDoubleColon);
 
540
                        base.VisitNamedArgumentExpression(namedArgumentExpression);
 
541
                }
 
542
 
 
543
                public override void VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression)
 
544
                {
 
545
                        ForceSpacesAfter(memberReferenceExpression.DotToken, false);
 
546
                        base.VisitMemberReferenceExpression(memberReferenceExpression);
 
547
                }
 
548
        }
 
549
}
 
550