SyntaxTokenExtensions.cs 22.8 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery
{
    internal static class SyntaxTokenExtensions
    {
        public static bool IsUsingOrExternKeyword(this SyntaxToken token)
        {
            return
17 18
                token.Kind() == SyntaxKind.UsingKeyword ||
                token.Kind() == SyntaxKind.ExternKeyword;
P
Pilchie 已提交
19 20
        }

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        public static bool IsUsingKeywordInUsingDirective(this SyntaxToken token)
        {
            if (token.IsKind(SyntaxKind.UsingKeyword))
            {
                var usingDirective = token.GetAncestor<UsingDirectiveSyntax>();
                if (usingDirective != null &&
                    usingDirective.UsingKeyword == token)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool IsStaticKeywordInUsingDirective(this SyntaxToken token)
        {
            if (token.IsKind(SyntaxKind.StaticKeyword))
            {
                var usingDirective = token.GetAncestor<UsingDirectiveSyntax>();
                if (usingDirective != null &&
                    usingDirective.StaticKeyword == token)
                {
                    return true;
                }
            }

            return false;
        }

P
Pilchie 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        public static bool IsBeginningOfStatementContext(this SyntaxToken token)
        {
            // cases:
            //    {
            //      |

            // }
            // |

            // Note, the following is *not* a legal statement context: 
            //    do { } |

            // ...;
            // |

            // case 0:
            //   |

            // default:
            //   |

            // label:
            //   |

            // if (foo)
            //   |

            // while (true)
            //   |

            // do
            //   |

            // for (;;)
            //   |

            // foreach (var v in c)
            //   |

            // else
            //   |

            // using (expr)
            //   |

96 97 98
            // fixed (void* v = &expr)
            //   |

P
Pilchie 已提交
99 100 101 102 103
            // lock (expr)
            //   |

            // for ( ; ; Foo(), |

104
            if (token.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
105
                token.Parent.IsKind(SyntaxKind.Block))
P
Pilchie 已提交
106 107 108 109
            {
                return true;
            }

110
            if (token.Kind() == SyntaxKind.SemicolonToken)
P
Pilchie 已提交
111 112 113 114 115 116 117 118 119
            {
                var statement = token.GetAncestor<StatementSyntax>();
                if (statement != null && !statement.IsParentKind(SyntaxKind.GlobalStatement) &&
                    statement.GetLastToken(includeZeroWidth: true) == token)
                {
                    return true;
                }
            }

120
            if (token.Kind() == SyntaxKind.CloseBraceToken &&
J
jasonmalinowski 已提交
121
                token.Parent.IsKind(SyntaxKind.Block))
P
Pilchie 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            {
                if (token.Parent.Parent is StatementSyntax)
                {
                    // Most blocks that are the child of statement are places
                    // that we can follow with another statement.  i.e.:
                    // if { }
                    // while () { }
                    // There are two exceptions.
                    // try {}
                    // do {}
                    if (!token.Parent.IsParentKind(SyntaxKind.TryStatement) &&
                        !token.Parent.IsParentKind(SyntaxKind.DoStatement))
                    {
                        return true;
                    }
                }
                else if (
                    token.Parent.IsParentKind(SyntaxKind.ElseClause) ||
                    token.Parent.IsParentKind(SyntaxKind.FinallyClause) ||
                    token.Parent.IsParentKind(SyntaxKind.CatchClause) ||
                    token.Parent.IsParentKind(SyntaxKind.SwitchSection))
                {
                    return true;
                }
            }

148
            if (token.Kind() == SyntaxKind.CloseBraceToken &&
J
jasonmalinowski 已提交
149
                token.Parent.IsKind(SyntaxKind.SwitchStatement))
P
Pilchie 已提交
150 151 152 153
            {
                return true;
            }

154
            if (token.Kind() == SyntaxKind.ColonToken)
P
Pilchie 已提交
155
            {
J
jasonmalinowski 已提交
156
                if (token.Parent.IsKind(SyntaxKind.CaseSwitchLabel, SyntaxKind.DefaultSwitchLabel, SyntaxKind.LabeledStatement))
P
Pilchie 已提交
157 158 159 160 161
                {
                    return true;
                }
            }

162
            if (token.Kind() == SyntaxKind.DoKeyword &&
J
jasonmalinowski 已提交
163
                token.Parent.IsKind(SyntaxKind.DoStatement))
P
Pilchie 已提交
164 165 166 167
            {
                return true;
            }

168
            if (token.Kind() == SyntaxKind.CloseParenToken)
P
Pilchie 已提交
169 170 171 172
            {
                var parent = token.Parent;
                if (parent.IsKind(SyntaxKind.ForStatement) ||
                    parent.IsKind(SyntaxKind.ForEachStatement) ||
173
                    parent.IsKind(SyntaxKind.ForEachVariableStatement) ||
P
Pilchie 已提交
174 175 176
                    parent.IsKind(SyntaxKind.WhileStatement) ||
                    parent.IsKind(SyntaxKind.IfStatement) ||
                    parent.IsKind(SyntaxKind.LockStatement) ||
177 178
                    parent.IsKind(SyntaxKind.UsingStatement) ||
                    parent.IsKind(SyntaxKind.FixedStatement))
P
Pilchie 已提交
179 180 181 182 183
                {
                    return true;
                }
            }

184
            if (token.Kind() == SyntaxKind.ElseKeyword)
P
Pilchie 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
            {
                return true;
            }

            return false;
        }

        public static bool IsBeginningOfGlobalStatementContext(this SyntaxToken token)
        {
            // cases:
            // }
            // |

            // ...;
            // |

            // extern alias Foo;
            // using System;
            // |

205
            // [assembly: Foo]
P
Pilchie 已提交
206 207
            // |

208
            if (token.Kind() == SyntaxKind.CloseBraceToken)
P
Pilchie 已提交
209 210 211 212 213 214 215 216 217
            {
                var memberDeclaration = token.GetAncestor<MemberDeclarationSyntax>();
                if (memberDeclaration != null && memberDeclaration.GetLastToken(includeZeroWidth: true) == token &&
                    memberDeclaration.IsParentKind(SyntaxKind.CompilationUnit))
                {
                    return true;
                }
            }

218
            if (token.Kind() == SyntaxKind.SemicolonToken)
P
Pilchie 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            {
                var globalStatement = token.GetAncestor<GlobalStatementSyntax>();
                if (globalStatement != null && globalStatement.GetLastToken(includeZeroWidth: true) == token)
                {
                    return true;
                }

                var memberDeclaration = token.GetAncestor<MemberDeclarationSyntax>();
                if (memberDeclaration != null && memberDeclaration.GetLastToken(includeZeroWidth: true) == token &&
                    memberDeclaration.IsParentKind(SyntaxKind.CompilationUnit))
                {
                    return true;
                }

                var compUnit = token.GetAncestor<CompilationUnitSyntax>();
                if (compUnit != null)
                {
                    if (compUnit.Usings.Count > 0 && compUnit.Usings.Last().GetLastToken(includeZeroWidth: true) == token)
                    {
                        return true;
                    }

                    if (compUnit.Externs.Count > 0 && compUnit.Externs.Last().GetLastToken(includeZeroWidth: true) == token)
                    {
                        return true;
                    }
                }
            }

248
            if (token.Kind() == SyntaxKind.CloseBracketToken)
P
Pilchie 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            {
                var compUnit = token.GetAncestor<CompilationUnitSyntax>();
                if (compUnit != null)
                {
                    if (compUnit.AttributeLists.Count > 0 && compUnit.AttributeLists.Last().GetLastToken(includeZeroWidth: true) == token)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool IsAfterPossibleCast(this SyntaxToken token)
        {
265
            if (token.Kind() == SyntaxKind.CloseParenToken)
P
Pilchie 已提交
266
            {
J
jasonmalinowski 已提交
267
                if (token.Parent.IsKind(SyntaxKind.CastExpression))
P
Pilchie 已提交
268 269 270 271
                {
                    return true;
                }

J
jasonmalinowski 已提交
272
                if (token.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
                {
                    var parenExpr = token.Parent as ParenthesizedExpressionSyntax;
                    var expr = parenExpr.Expression;

                    if (expr is TypeSyntax)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool IsLastTokenOfNode<T>(this SyntaxToken token)
            where T : SyntaxNode
        {
            var node = token.GetAncestor<T>();
            return node != null && token == node.GetLastToken(includeZeroWidth: true);
        }

        public static bool IsLastTokenOfQueryClause(this SyntaxToken token)
        {
            if (token.IsLastTokenOfNode<QueryClauseSyntax>())
            {
                return true;
            }

301 302
            if (token.Kind() == SyntaxKind.IdentifierToken &&
                token.GetPreviousToken(includeSkipped: true).Kind() == SyntaxKind.IntoKeyword)
P
Pilchie 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            {
                return true;
            }

            return false;
        }

        public static bool IsPreProcessorExpressionContext(this SyntaxToken targetToken)
        {
            // cases:
            //   #if |
            //   #if foo || |
            //   #if foo && |
            //   #if ( |
            //   #if ! |
            // Same for elif

            if (targetToken.GetAncestor<ConditionalDirectiveTriviaSyntax>() == null)
            {
                return false;
            }

            // #if
            // #elif
327 328
            if (targetToken.Kind() == SyntaxKind.IfKeyword ||
                targetToken.Kind() == SyntaxKind.ElifKeyword)
P
Pilchie 已提交
329 330 331 332 333
            {
                return true;
            }

            // ( |
334
            if (targetToken.Kind() == SyntaxKind.OpenParenToken &&
J
jasonmalinowski 已提交
335
                targetToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
            {
                return true;
            }

            // ! |
            if (targetToken.Parent is PrefixUnaryExpressionSyntax)
            {
                var prefix = targetToken.Parent as PrefixUnaryExpressionSyntax;
                return prefix.OperatorToken == targetToken;
            }

            // a &&
            // a ||
            if (targetToken.Parent is BinaryExpressionSyntax)
            {
                var binary = targetToken.Parent as BinaryExpressionSyntax;
                return binary.OperatorToken == targetToken;
            }

            return false;
        }

        public static bool IsOrderByDirectionContext(this SyntaxToken targetToken)
        {
            // cases:
            //   orderby a |
            //   orderby a a|
            //   orderby a, b |
            //   orderby a, b a|

J
jasonmalinowski 已提交
366
            if (!targetToken.IsKind(SyntaxKind.IdentifierToken, SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken))
P
Pilchie 已提交
367 368 369 370 371 372 373 374 375 376 377 378
            {
                return false;
            }

            var ordering = targetToken.GetAncestor<OrderingSyntax>();
            if (ordering == null)
            {
                return false;
            }

            // orderby a |
            // orderby a, b |
379 380 381
            var lastToken = ordering.Expression.GetLastToken(includeSkipped: true);

            if (targetToken == lastToken)
P
Pilchie 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
            {
                return true;
            }

            return false;
        }

        public static bool IsSwitchLabelContext(this SyntaxToken targetToken)
        {
            // cases:
            //   case X: |
            //   default: |
            //   switch (e) { |
            //
            //   case X: Statement(); |

398
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
399
                targetToken.Parent.IsKind(SyntaxKind.SwitchStatement))
P
Pilchie 已提交
400 401 402 403
            {
                return true;
            }

404
            if (targetToken.Kind() == SyntaxKind.ColonToken)
P
Pilchie 已提交
405
            {
J
jasonmalinowski 已提交
406
                if (targetToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel, SyntaxKind.DefaultSwitchLabel))
P
Pilchie 已提交
407 408 409 410 411
                {
                    return true;
                }
            }

412 413
            if (targetToken.Kind() == SyntaxKind.SemicolonToken ||
                targetToken.Kind() == SyntaxKind.CloseBraceToken)
P
Pilchie 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            {
                var section = targetToken.GetAncestor<SwitchSectionSyntax>();
                if (section != null)
                {
                    foreach (var statement in section.Statements)
                    {
                        if (targetToken == statement.GetLastToken(includeSkipped: true))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public static bool IsXmlCrefParameterModifierContext(this SyntaxToken targetToken)
        {
J
jasonmalinowski 已提交
433 434
            return targetToken.IsKind(SyntaxKind.CommaToken, SyntaxKind.OpenParenToken)
                && targetToken.Parent.IsKind(SyntaxKind.CrefBracketedParameterList, SyntaxKind.CrefParameterList);
P
Pilchie 已提交
435 436 437 438 439 440 441 442 443 444 445
        }

        public static bool IsConstructorOrMethodParameterArgumentContext(this SyntaxToken targetToken)
        {
            // cases:
            //   Foo( |
            //   Foo(expr, |
            //   Foo(bar: |
            //   new Foo( |
            //   new Foo(expr, |
            //   new Foo(bar: |
446 447 448
            //   Foo : base( |
            //   Foo : base(bar: |
            //   Foo : this( |
C
Charles Stoner 已提交
449
            //   Foo : this(bar: |
P
Pilchie 已提交
450 451

            // Foo(bar: |
452
            if (targetToken.Kind() == SyntaxKind.ColonToken &&
J
jasonmalinowski 已提交
453
                targetToken.Parent.IsKind(SyntaxKind.NameColon) &&
P
Pilchie 已提交
454 455 456 457 458
                targetToken.Parent.IsParentKind(SyntaxKind.Argument) &&
                targetToken.Parent.GetParent().IsParentKind(SyntaxKind.ArgumentList))
            {
                var owner = targetToken.Parent.GetParent().GetParent().GetParent();
                if (owner.IsKind(SyntaxKind.InvocationExpression) ||
459 460 461
                    owner.IsKind(SyntaxKind.ObjectCreationExpression) ||
                    owner.IsKind(SyntaxKind.BaseConstructorInitializer) ||
                    owner.IsKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
462 463 464 465 466
                {
                    return true;
                }
            }

467

468 469
            if (targetToken.Kind() == SyntaxKind.OpenParenToken ||
                targetToken.Kind() == SyntaxKind.CommaToken)
P
Pilchie 已提交
470
            {
J
jasonmalinowski 已提交
471
                if (targetToken.Parent.IsKind(SyntaxKind.ArgumentList))
P
Pilchie 已提交
472
                {
473
                    if (targetToken.Parent.IsParentKind(SyntaxKind.ObjectCreationExpression) ||
474 475
                        targetToken.Parent.IsParentKind(SyntaxKind.BaseConstructorInitializer) ||
                        targetToken.Parent.IsParentKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
476 477 478
                    {
                        return true;
                    }
479 480 481 482 483 484 485 486

                    // var( |
                    // var(expr, |
                    // Those are more likely to be deconstruction-declarations being typed than invocations a method "var"
                    if (targetToken.Parent.IsParentKind(SyntaxKind.InvocationExpression) && !targetToken.IsInvocationOfVarExpression())
                    {
                        return true;
                    }
P
Pilchie 已提交
487 488 489 490 491 492 493 494
                }
            }

            return false;
        }

        public static bool IsUnaryOperatorContext(this SyntaxToken targetToken)
        {
495
            if (targetToken.Kind() == SyntaxKind.OperatorKeyword &&
P
Pilchie 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
                targetToken.GetPreviousToken(includeSkipped: true).IsLastTokenOfNode<TypeSyntax>())
            {
                return true;
            }

            return false;
        }

        public static bool IsUnsafeContext(this SyntaxToken targetToken)
        {
            return
                targetToken.GetAncestors<StatementSyntax>().Any(s => s.IsKind(SyntaxKind.UnsafeStatement)) ||
                targetToken.GetAncestors<MemberDeclarationSyntax>().Any(m => m.GetModifiers().Any(SyntaxKind.UnsafeKeyword));
        }

        public static bool IsAfterYieldKeyword(this SyntaxToken targetToken)
        {
            // yield |
            // yield r|

516
            return targetToken.IsKindOrHasMatchingText(SyntaxKind.YieldKeyword);
P
Pilchie 已提交
517 518
        }

519 520 521 522 523 524 525
        public static bool IsAnyAccessorDeclarationContext(this SyntaxToken targetToken, int position, SyntaxKind kind = SyntaxKind.None)
        {
            return targetToken.IsAccessorDeclarationContext<EventDeclarationSyntax>(position, kind) ||
                targetToken.IsAccessorDeclarationContext<PropertyDeclarationSyntax>(position, kind) ||
                targetToken.IsAccessorDeclarationContext<IndexerDeclarationSyntax>(position, kind);
        }

P
Pilchie 已提交
526 527 528
        public static bool IsAccessorDeclarationContext<TMemberNode>(this SyntaxToken targetToken, int position, SyntaxKind kind = SyntaxKind.None)
            where TMemberNode : SyntaxNode
        {
529
            if (!IsAccessorDeclarationContextWorker(ref targetToken))
P
Pilchie 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
            {
                return false;
            }

            var list = targetToken.GetAncestor<AccessorListSyntax>();
            if (list == null)
            {
                return false;
            }

            // Check if we already have this accessor.  (however, don't count it
            // if the user is *on* that accessor.
            var existingAccessor = list.Accessors
                .Select(a => a.Keyword)
                .FirstOrDefault(a => !a.IsMissing && a.IsKindOrHasMatchingText(kind));

546
            if (existingAccessor.Kind() != SyntaxKind.None)
P
Pilchie 已提交
547 548 549 550 551 552 553 554 555 556 557 558
            {
                var existingAccessorSpan = existingAccessor.Span;
                if (!existingAccessorSpan.IntersectsWith(position))
                {
                    return false;
                }
            }

            var decl = targetToken.GetAncestor<TMemberNode>();
            return decl != null;
        }

559
        private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetToken)
P
Pilchie 已提交
560 561 562 563 564 565 566 567 568
        {
            // cases:
            //   int Foo { |
            //   int Foo { private |
            //   int Foo { set { } |
            //   int Foo { set; |
            //   int Foo { [Bar]|

            // Consume all preceding access modifiers
569 570 571 572
            while (targetToken.Kind() == SyntaxKind.InternalKeyword ||
                targetToken.Kind() == SyntaxKind.PublicKeyword ||
                targetToken.Kind() == SyntaxKind.ProtectedKeyword ||
                targetToken.Kind() == SyntaxKind.PrivateKeyword)
P
Pilchie 已提交
573 574 575 576 577 578
            {
                targetToken = targetToken.GetPreviousToken(includeSkipped: true);
            }

            // int Foo { |
            // int Foo { private |
579
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
580
                targetToken.Parent.IsKind(SyntaxKind.AccessorList))
P
Pilchie 已提交
581 582 583 584 585 586
            {
                return true;
            }

            // int Foo { set { } |
            // int Foo { set { } private |
587
            if (targetToken.Kind() == SyntaxKind.CloseBraceToken &&
J
jasonmalinowski 已提交
588
                targetToken.Parent.IsKind(SyntaxKind.Block) &&
P
Pilchie 已提交
589 590 591 592 593 594
                targetToken.Parent.GetParent() is AccessorDeclarationSyntax)
            {
                return true;
            }

            // int Foo { set; |
595
            if (targetToken.Kind() == SyntaxKind.SemicolonToken &&
P
Pilchie 已提交
596 597 598 599 600 601
                targetToken.Parent is AccessorDeclarationSyntax)
            {
                return true;
            }

            // int Foo { [Bar]|
602
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
603
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                targetToken.Parent.GetParent() is AccessorDeclarationSyntax)
            {
                return true;
            }

            return false;
        }

        private static bool IsGenericInterfaceOrDelegateTypeParameterList(SyntaxNode node)
        {
            if (node.IsKind(SyntaxKind.TypeParameterList))
            {
                if (node.IsParentKind(SyntaxKind.InterfaceDeclaration))
                {
                    var decl = node.Parent as TypeDeclarationSyntax;
                    return decl.TypeParameterList == node;
                }
                else if (node.IsParentKind(SyntaxKind.DelegateDeclaration))
                {
                    var decl = node.Parent as DelegateDeclarationSyntax;
                    return decl.TypeParameterList == node;
                }
            }

            return false;
        }

        public static bool IsTypeParameterVarianceContext(this SyntaxToken targetToken)
        {
            // cases:
            // interface IFoo<|
            // interface IFoo<A,|
            // interface IFoo<[Bar]|

C
Charles Stoner 已提交
638 639 640
            // delegate X D<|
            // delegate X D<A,|
            // delegate X D<[Bar]|
641
            if (targetToken.Kind() == SyntaxKind.LessThanToken &&
P
Pilchie 已提交
642 643 644 645 646
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

647
            if (targetToken.Kind() == SyntaxKind.CommaToken &&
P
Pilchie 已提交
648 649 650 651 652
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

653
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
654
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
655 656 657 658 659 660 661 662
                targetToken.Parent.IsParentKind(SyntaxKind.TypeParameter) &&
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent.GetParent().GetParent()))
            {
                return true;
            }

            return false;
        }
663 664 665

        public static bool IsMandatoryNamedParameterPosition(this SyntaxToken token)
        {
666
            if (token.Kind() == SyntaxKind.CommaToken && token.Parent is BaseArgumentListSyntax)
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
            {
                var argumentList = (BaseArgumentListSyntax)token.Parent;

                foreach (var item in argumentList.Arguments.GetWithSeparators())
                {
                    if (item.IsToken && item.AsToken() == token)
                    {
                        return false;
                    }

                    if (item.IsNode)
                    {
                        var node = item.AsNode() as ArgumentSyntax;
                        if (node != null && node.NameColon != null)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
P
Pilchie 已提交
690
    }
691
}