SyntaxTokenExtensions.cs 23.0 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
            {
156 157 158 159
                if (token.Parent.IsKind(SyntaxKind.CaseSwitchLabel,
                                        SyntaxKind.DefaultSwitchLabel,
                                        SyntaxKind.CasePatternSwitchLabel,
                                        SyntaxKind.LabeledStatement))
P
Pilchie 已提交
160 161 162 163 164
                {
                    return true;
                }
            }

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

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

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

            return false;
        }

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

            // ...;
            // |

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

208
            // [assembly: Foo]
P
Pilchie 已提交
209 210
            // |

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

221
            if (token.Kind() == SyntaxKind.SemicolonToken)
P
Pilchie 已提交
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 248 249 250
            {
                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;
                    }
                }
            }

251
            if (token.Kind() == SyntaxKind.CloseBracketToken)
P
Pilchie 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
            {
                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)
        {
268
            if (token.Kind() == SyntaxKind.CloseParenToken)
P
Pilchie 已提交
269
            {
J
jasonmalinowski 已提交
270
                if (token.Parent.IsKind(SyntaxKind.CastExpression))
P
Pilchie 已提交
271 272 273 274
                {
                    return true;
                }

J
jasonmalinowski 已提交
275
                if (token.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
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 301 302 303
                {
                    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;
            }

304 305
            if (token.Kind() == SyntaxKind.IdentifierToken &&
                token.GetPreviousToken(includeSkipped: true).Kind() == SyntaxKind.IntoKeyword)
P
Pilchie 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
            {
                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
330 331
            if (targetToken.Kind() == SyntaxKind.IfKeyword ||
                targetToken.Kind() == SyntaxKind.ElifKeyword)
P
Pilchie 已提交
332 333 334 335 336
            {
                return true;
            }

            // ( |
337
            if (targetToken.Kind() == SyntaxKind.OpenParenToken &&
J
jasonmalinowski 已提交
338
                targetToken.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
P
Pilchie 已提交
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 366 367 368
            {
                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 已提交
369
            if (!targetToken.IsKind(SyntaxKind.IdentifierToken, SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken))
P
Pilchie 已提交
370 371 372 373 374 375 376 377 378 379 380 381
            {
                return false;
            }

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

            // orderby a |
            // orderby a, b |
382 383 384
            var lastToken = ordering.Expression.GetLastToken(includeSkipped: true);

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

            return false;
        }

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

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

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

415 416
            if (targetToken.Kind() == SyntaxKind.SemicolonToken ||
                targetToken.Kind() == SyntaxKind.CloseBraceToken)
P
Pilchie 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
            {
                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 已提交
436 437
            return targetToken.IsKind(SyntaxKind.CommaToken, SyntaxKind.OpenParenToken)
                && targetToken.Parent.IsKind(SyntaxKind.CrefBracketedParameterList, SyntaxKind.CrefParameterList);
P
Pilchie 已提交
438 439 440 441 442 443 444 445 446 447 448
        }

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

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

470

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

                    // 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 已提交
490 491 492 493 494 495 496 497
                }
            }

            return false;
        }

        public static bool IsUnaryOperatorContext(this SyntaxToken targetToken)
        {
498
            if (targetToken.Kind() == SyntaxKind.OperatorKeyword &&
P
Pilchie 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
                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|

519
            return targetToken.IsKindOrHasMatchingText(SyntaxKind.YieldKeyword);
P
Pilchie 已提交
520 521
        }

522 523 524 525 526 527 528
        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 已提交
529 530 531
        public static bool IsAccessorDeclarationContext<TMemberNode>(this SyntaxToken targetToken, int position, SyntaxKind kind = SyntaxKind.None)
            where TMemberNode : SyntaxNode
        {
532
            if (!IsAccessorDeclarationContextWorker(ref targetToken))
P
Pilchie 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
            {
                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));

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

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

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

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

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

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

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

            // int Foo { [Bar]|
605
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
606
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
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 638 639 640
                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 已提交
641 642 643
            // delegate X D<|
            // delegate X D<A,|
            // delegate X D<[Bar]|
644
            if (targetToken.Kind() == SyntaxKind.LessThanToken &&
P
Pilchie 已提交
645 646 647 648 649
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

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

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

            return false;
        }
666 667 668

        public static bool IsMandatoryNamedParameterPosition(this SyntaxToken token)
        {
669
            if (token.Kind() == SyntaxKind.CommaToken && token.Parent is BaseArgumentListSyntax)
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
            {
                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 已提交
693
    }
694
}