SyntaxTokenExtensions.cs 22.9 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
        public static bool IsBeginningOfStatementContext(this SyntaxToken token)
        {
            // cases:
            //    {
            //      |

            // }
            // |

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

            // ...;
            // |

            // case 0:
            //   |

            // default:
            //   |

            // label:
            //   |

75
            // if (goo)
P
Pilchie 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            //   |

            // while (true)
            //   |

            // do
            //   |

            // for (;;)
            //   |

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

            // else
            //   |

            // using (expr)
            //   |

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

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

102
            // for ( ; ; Goo(), |
P
Pilchie 已提交
103

C
CyrusNajmabadi 已提交
104
            switch (token.Kind())
P
Pilchie 已提交
105
            {
C
CyrusNajmabadi 已提交
106
                case SyntaxKind.OpenBraceToken when token.Parent.IsKind(SyntaxKind.Block):
P
Pilchie 已提交
107 108
                    return true;

C
CyrusNajmabadi 已提交
109 110 111 112 113 114 115
                case SyntaxKind.SemicolonToken:
                    var statement = token.GetAncestor<StatementSyntax>();
                    return statement != null && !statement.IsParentKind(SyntaxKind.GlobalStatement) &&
                           statement.GetLastToken(includeZeroWidth: true) == token;

                case SyntaxKind.CloseBraceToken:
                    if (token.Parent.IsKind(SyntaxKind.Block))
P
Pilchie 已提交
116
                    {
C
CyrusNajmabadi 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                        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;
                        }
P
Pilchie 已提交
140 141
                    }

C
CyrusNajmabadi 已提交
142 143 144 145
                    if (token.Parent.IsKind(SyntaxKind.SwitchStatement))
                    {
                        return true;
                    }
P
Pilchie 已提交
146

C
CyrusNajmabadi 已提交
147
                    return false;
P
Pilchie 已提交
148

C
CyrusNajmabadi 已提交
149 150 151 152 153
                case SyntaxKind.ColonToken:
                    return token.Parent.IsKind(SyntaxKind.CaseSwitchLabel,
                                               SyntaxKind.DefaultSwitchLabel,
                                               SyntaxKind.CasePatternSwitchLabel,
                                               SyntaxKind.LabeledStatement);
P
Pilchie 已提交
154

C
CyrusNajmabadi 已提交
155
                case SyntaxKind.DoKeyword when token.Parent.IsKind(SyntaxKind.DoStatement):
P
Pilchie 已提交
156 157
                    return true;

C
CyrusNajmabadi 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
                case SyntaxKind.CloseParenToken:
                    var parent = token.Parent;
                    return parent.IsKind(SyntaxKind.ForStatement) ||
                           parent.IsKind(SyntaxKind.ForEachStatement) ||
                           parent.IsKind(SyntaxKind.ForEachVariableStatement) ||
                           parent.IsKind(SyntaxKind.WhileStatement) ||
                           parent.IsKind(SyntaxKind.IfStatement) ||
                           parent.IsKind(SyntaxKind.LockStatement) ||
                           parent.IsKind(SyntaxKind.UsingStatement) ||
                           parent.IsKind(SyntaxKind.FixedStatement);

                case SyntaxKind.ElseKeyword:
                    return true;
P
Pilchie 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
            }

            return false;
        }

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

            // ...;
            // |

185
            // extern alias Goo;
P
Pilchie 已提交
186 187 188
            // using System;
            // |

189
            // [assembly: Goo]
P
Pilchie 已提交
190 191
            // |

192
            if (token.Kind() == SyntaxKind.CloseBraceToken)
P
Pilchie 已提交
193 194 195 196 197 198 199 200 201
            {
                var memberDeclaration = token.GetAncestor<MemberDeclarationSyntax>();
                if (memberDeclaration != null && memberDeclaration.GetLastToken(includeZeroWidth: true) == token &&
                    memberDeclaration.IsParentKind(SyntaxKind.CompilationUnit))
                {
                    return true;
                }
            }

202
            if (token.Kind() == SyntaxKind.SemicolonToken)
P
Pilchie 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
            {
                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;
                    }
                }
            }

232
            if (token.Kind() == SyntaxKind.CloseBracketToken)
P
Pilchie 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            {
                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)
        {
249
            if (token.Kind() == SyntaxKind.CloseParenToken)
P
Pilchie 已提交
250
            {
J
jasonmalinowski 已提交
251
                if (token.Parent.IsKind(SyntaxKind.CastExpression))
P
Pilchie 已提交
252 253 254 255
                {
                    return true;
                }

J
jasonmalinowski 已提交
256
                if (token.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
                {
                    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;
            }

285 286
            if (token.Kind() == SyntaxKind.IdentifierToken &&
                token.GetPreviousToken(includeSkipped: true).Kind() == SyntaxKind.IntoKeyword)
P
Pilchie 已提交
287 288 289 290 291 292 293 294 295 296 297
            {
                return true;
            }

            return false;
        }

        public static bool IsPreProcessorExpressionContext(this SyntaxToken targetToken)
        {
            // cases:
            //   #if |
298 299
            //   #if goo || |
            //   #if goo && |
P
Pilchie 已提交
300 301 302 303 304 305 306 307 308 309 310
            //   #if ( |
            //   #if ! |
            // Same for elif

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

            // #if
            // #elif
311 312
            if (targetToken.Kind() == SyntaxKind.IfKeyword ||
                targetToken.Kind() == SyntaxKind.ElifKeyword)
P
Pilchie 已提交
313 314 315 316 317
            {
                return true;
            }

            // ( |
318
            if (targetToken.Kind() == SyntaxKind.OpenParenToken &&
J
jasonmalinowski 已提交
319
                targetToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            {
                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 已提交
350
            if (!targetToken.IsKind(SyntaxKind.IdentifierToken, SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken))
P
Pilchie 已提交
351 352 353 354 355 356 357 358 359 360 361 362
            {
                return false;
            }

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

            // orderby a |
            // orderby a, b |
363 364 365
            var lastToken = ordering.Expression.GetLastToken(includeSkipped: true);

            if (targetToken == lastToken)
P
Pilchie 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            {
                return true;
            }

            return false;
        }

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

382
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
383
                targetToken.Parent.IsKind(SyntaxKind.SwitchStatement))
P
Pilchie 已提交
384 385 386 387
            {
                return true;
            }

388
            if (targetToken.Kind() == SyntaxKind.ColonToken)
P
Pilchie 已提交
389
            {
390 391
                if (targetToken.Parent.IsKind(
                        SyntaxKind.CaseSwitchLabel, SyntaxKind.DefaultSwitchLabel, SyntaxKind.CasePatternSwitchLabel))
P
Pilchie 已提交
392 393 394 395 396
                {
                    return true;
                }
            }

397 398
            if (targetToken.Kind() == SyntaxKind.SemicolonToken ||
                targetToken.Kind() == SyntaxKind.CloseBraceToken)
P
Pilchie 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
            {
                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 已提交
418 419
            return targetToken.IsKind(SyntaxKind.CommaToken, SyntaxKind.OpenParenToken)
                && targetToken.Parent.IsKind(SyntaxKind.CrefBracketedParameterList, SyntaxKind.CrefParameterList);
P
Pilchie 已提交
420 421 422 423 424
        }

        public static bool IsConstructorOrMethodParameterArgumentContext(this SyntaxToken targetToken)
        {
            // cases:
425 426 427 428 429 430 431 432 433 434 435 436
            //   Goo( |
            //   Goo(expr, |
            //   Goo(bar: |
            //   new Goo( |
            //   new Goo(expr, |
            //   new Goo(bar: |
            //   Goo : base( |
            //   Goo : base(bar: |
            //   Goo : this( |
            //   Goo : this(bar: |

            // Goo(bar: |
437
            if (targetToken.Kind() == SyntaxKind.ColonToken &&
J
jasonmalinowski 已提交
438
                targetToken.Parent.IsKind(SyntaxKind.NameColon) &&
P
Pilchie 已提交
439 440 441 442 443
                targetToken.Parent.IsParentKind(SyntaxKind.Argument) &&
                targetToken.Parent.GetParent().IsParentKind(SyntaxKind.ArgumentList))
            {
                var owner = targetToken.Parent.GetParent().GetParent().GetParent();
                if (owner.IsKind(SyntaxKind.InvocationExpression) ||
444 445 446
                    owner.IsKind(SyntaxKind.ObjectCreationExpression) ||
                    owner.IsKind(SyntaxKind.BaseConstructorInitializer) ||
                    owner.IsKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
447 448 449 450 451
                {
                    return true;
                }
            }

452

453 454
            if (targetToken.Kind() == SyntaxKind.OpenParenToken ||
                targetToken.Kind() == SyntaxKind.CommaToken)
P
Pilchie 已提交
455
            {
J
jasonmalinowski 已提交
456
                if (targetToken.Parent.IsKind(SyntaxKind.ArgumentList))
P
Pilchie 已提交
457
                {
458
                    if (targetToken.Parent.IsParentKind(SyntaxKind.ObjectCreationExpression) ||
459 460
                        targetToken.Parent.IsParentKind(SyntaxKind.BaseConstructorInitializer) ||
                        targetToken.Parent.IsParentKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
461 462 463
                    {
                        return true;
                    }
464 465 466 467 468 469 470 471

                    // 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 已提交
472 473 474 475 476 477 478 479
                }
            }

            return false;
        }

        public static bool IsUnaryOperatorContext(this SyntaxToken targetToken)
        {
480
            if (targetToken.Kind() == SyntaxKind.OperatorKeyword &&
P
Pilchie 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
                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|

501
            return targetToken.IsKindOrHasMatchingText(SyntaxKind.YieldKeyword);
P
Pilchie 已提交
502 503
        }

504 505 506 507 508 509 510
        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 已提交
511 512 513
        public static bool IsAccessorDeclarationContext<TMemberNode>(this SyntaxToken targetToken, int position, SyntaxKind kind = SyntaxKind.None)
            where TMemberNode : SyntaxNode
        {
514
            if (!IsAccessorDeclarationContextWorker(ref targetToken))
P
Pilchie 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
            {
                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));

531
            if (existingAccessor.Kind() != SyntaxKind.None)
P
Pilchie 已提交
532 533 534 535 536 537 538 539 540 541 542 543
            {
                var existingAccessorSpan = existingAccessor.Span;
                if (!existingAccessorSpan.IntersectsWith(position))
                {
                    return false;
                }
            }

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

544
        private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetToken)
P
Pilchie 已提交
545 546
        {
            // cases:
547 548 549 550 551
            //   int Goo { |
            //   int Goo { private |
            //   int Goo { set { } |
            //   int Goo { set; |
            //   int Goo { [Bar]|
P
Pilchie 已提交
552 553

            // Consume all preceding access modifiers
554 555 556 557
            while (targetToken.Kind() == SyntaxKind.InternalKeyword ||
                targetToken.Kind() == SyntaxKind.PublicKeyword ||
                targetToken.Kind() == SyntaxKind.ProtectedKeyword ||
                targetToken.Kind() == SyntaxKind.PrivateKeyword)
P
Pilchie 已提交
558 559 560 561
            {
                targetToken = targetToken.GetPreviousToken(includeSkipped: true);
            }

562 563
            // int Goo { |
            // int Goo { private |
564
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
565
                targetToken.Parent.IsKind(SyntaxKind.AccessorList))
P
Pilchie 已提交
566 567 568 569
            {
                return true;
            }

570 571
            // int Goo { set { } |
            // int Goo { set { } private |
572
            if (targetToken.Kind() == SyntaxKind.CloseBraceToken &&
J
jasonmalinowski 已提交
573
                targetToken.Parent.IsKind(SyntaxKind.Block) &&
P
Pilchie 已提交
574 575 576 577 578
                targetToken.Parent.GetParent() is AccessorDeclarationSyntax)
            {
                return true;
            }

579
            // int Goo { set; |
580
            if (targetToken.Kind() == SyntaxKind.SemicolonToken &&
P
Pilchie 已提交
581 582 583 584 585
                targetToken.Parent is AccessorDeclarationSyntax)
            {
                return true;
            }

586
            // int Goo { [Bar]|
587
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
588
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
                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:
619 620 621
            // interface IGoo<|
            // interface IGoo<A,|
            // interface IGoo<[Bar]|
P
Pilchie 已提交
622

C
Charles Stoner 已提交
623 624 625
            // delegate X D<|
            // delegate X D<A,|
            // delegate X D<[Bar]|
626
            if (targetToken.Kind() == SyntaxKind.LessThanToken &&
P
Pilchie 已提交
627 628 629 630 631
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

632
            if (targetToken.Kind() == SyntaxKind.CommaToken &&
P
Pilchie 已提交
633 634 635 636 637
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

638
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
639
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
640 641 642 643 644 645 646 647
                targetToken.Parent.IsParentKind(SyntaxKind.TypeParameter) &&
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent.GetParent().GetParent()))
            {
                return true;
            }

            return false;
        }
648 649 650

        public static bool IsMandatoryNamedParameterPosition(this SyntaxToken token)
        {
651
            if (token.Kind() == SyntaxKind.CommaToken && token.Parent is BaseArgumentListSyntax)
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
            {
                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 已提交
675
    }
676
}