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

using System.Linq;
4
using System.Threading;
P
Pilchie 已提交
5 6 7 8 9 10 11
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery
{
12
    internal static partial class SyntaxTokenExtensions
P
Pilchie 已提交
13 14 15 16
    {
        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
                {
                    var parenExpr = token.Parent as ParenthesizedExpressionSyntax;
                    var expr = parenExpr.Expression;

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

            return false;
        }

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

278 279
            if (token.Kind() == SyntaxKind.IdentifierToken &&
                token.GetPreviousToken(includeSkipped: true).Kind() == SyntaxKind.IntoKeyword)
P
Pilchie 已提交
280 281 282 283 284 285 286 287 288 289 290
            {
                return true;
            }

            return false;
        }

        public static bool IsPreProcessorExpressionContext(this SyntaxToken targetToken)
        {
            // cases:
            //   #if |
291 292
            //   #if goo || |
            //   #if goo && |
P
Pilchie 已提交
293 294 295 296 297 298 299 300 301 302 303
            //   #if ( |
            //   #if ! |
            // Same for elif

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

            // #if
            // #elif
304 305
            if (targetToken.Kind() == SyntaxKind.IfKeyword ||
                targetToken.Kind() == SyntaxKind.ElifKeyword)
P
Pilchie 已提交
306 307 308 309 310
            {
                return true;
            }

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

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

            // orderby a |
            // orderby a, b |
356 357 358
            var lastToken = ordering.Expression.GetLastToken(includeSkipped: true);

            if (targetToken == lastToken)
P
Pilchie 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            {
                return true;
            }

            return false;
        }

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

375
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
376
                targetToken.Parent.IsKind(SyntaxKind.SwitchStatement))
P
Pilchie 已提交
377 378 379 380
            {
                return true;
            }

381
            if (targetToken.Kind() == SyntaxKind.ColonToken)
P
Pilchie 已提交
382
            {
383 384
                if (targetToken.Parent.IsKind(
                        SyntaxKind.CaseSwitchLabel, SyntaxKind.DefaultSwitchLabel, SyntaxKind.CasePatternSwitchLabel))
P
Pilchie 已提交
385 386 387 388 389
                {
                    return true;
                }
            }

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

        public static bool IsConstructorOrMethodParameterArgumentContext(this SyntaxToken targetToken)
        {
            // cases:
418 419 420 421 422 423 424 425 426 427 428 429
            //   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: |
430
            if (targetToken.Kind() == SyntaxKind.ColonToken &&
J
jasonmalinowski 已提交
431
                targetToken.Parent.IsKind(SyntaxKind.NameColon) &&
P
Pilchie 已提交
432
                targetToken.Parent.IsParentKind(SyntaxKind.Argument) &&
C
Cyrus Najmabadi 已提交
433
                targetToken.Parent.Parent.IsParentKind(SyntaxKind.ArgumentList))
P
Pilchie 已提交
434
            {
C
Cyrus Najmabadi 已提交
435
                var owner = targetToken.Parent.Parent.Parent.Parent;
P
Pilchie 已提交
436
                if (owner.IsKind(SyntaxKind.InvocationExpression) ||
437 438 439
                    owner.IsKind(SyntaxKind.ObjectCreationExpression) ||
                    owner.IsKind(SyntaxKind.BaseConstructorInitializer) ||
                    owner.IsKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
440 441 442 443 444
                {
                    return true;
                }
            }

445

446 447
            if (targetToken.Kind() == SyntaxKind.OpenParenToken ||
                targetToken.Kind() == SyntaxKind.CommaToken)
P
Pilchie 已提交
448
            {
J
jasonmalinowski 已提交
449
                if (targetToken.Parent.IsKind(SyntaxKind.ArgumentList))
P
Pilchie 已提交
450
                {
451
                    if (targetToken.Parent.IsParentKind(SyntaxKind.ObjectCreationExpression) ||
452 453
                        targetToken.Parent.IsParentKind(SyntaxKind.BaseConstructorInitializer) ||
                        targetToken.Parent.IsParentKind(SyntaxKind.ThisConstructorInitializer))
P
Pilchie 已提交
454 455 456
                    {
                        return true;
                    }
457 458 459 460 461 462 463 464

                    // 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 已提交
465 466 467 468 469 470 471 472
                }
            }

            return false;
        }

        public static bool IsUnaryOperatorContext(this SyntaxToken targetToken)
        {
473
            if (targetToken.Kind() == SyntaxKind.OperatorKeyword &&
P
Pilchie 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
                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|

494
            return targetToken.IsKindOrHasMatchingText(SyntaxKind.YieldKeyword);
P
Pilchie 已提交
495 496
        }

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

524
            if (existingAccessor.Kind() != SyntaxKind.None)
P
Pilchie 已提交
525 526 527 528 529 530 531 532 533 534 535 536
            {
                var existingAccessorSpan = existingAccessor.Span;
                if (!existingAccessorSpan.IntersectsWith(position))
                {
                    return false;
                }
            }

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

537
        private static bool IsAccessorDeclarationContextWorker(ref SyntaxToken targetToken)
P
Pilchie 已提交
538 539
        {
            // cases:
540 541 542 543 544
            //   int Goo { |
            //   int Goo { private |
            //   int Goo { set { } |
            //   int Goo { set; |
            //   int Goo { [Bar]|
P
Pilchie 已提交
545 546

            // Consume all preceding access modifiers
547 548 549 550
            while (targetToken.Kind() == SyntaxKind.InternalKeyword ||
                targetToken.Kind() == SyntaxKind.PublicKeyword ||
                targetToken.Kind() == SyntaxKind.ProtectedKeyword ||
                targetToken.Kind() == SyntaxKind.PrivateKeyword)
P
Pilchie 已提交
551 552 553 554
            {
                targetToken = targetToken.GetPreviousToken(includeSkipped: true);
            }

555 556
            // int Goo { |
            // int Goo { private |
557
            if (targetToken.Kind() == SyntaxKind.OpenBraceToken &&
J
jasonmalinowski 已提交
558
                targetToken.Parent.IsKind(SyntaxKind.AccessorList))
P
Pilchie 已提交
559 560 561 562
            {
                return true;
            }

563 564
            // int Goo { set { } |
            // int Goo { set { } private |
565
            if (targetToken.Kind() == SyntaxKind.CloseBraceToken &&
J
jasonmalinowski 已提交
566
                targetToken.Parent.IsKind(SyntaxKind.Block) &&
C
Cyrus Najmabadi 已提交
567
                targetToken.Parent.Parent is AccessorDeclarationSyntax)
P
Pilchie 已提交
568 569 570 571
            {
                return true;
            }

572
            // int Goo { set; |
573
            if (targetToken.Kind() == SyntaxKind.SemicolonToken &&
P
Pilchie 已提交
574 575 576 577 578
                targetToken.Parent is AccessorDeclarationSyntax)
            {
                return true;
            }

579
            // int Goo { [Bar]|
580
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
581
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
C
Cyrus Najmabadi 已提交
582
                targetToken.Parent.Parent is AccessorDeclarationSyntax)
P
Pilchie 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
            {
                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:
612 613 614
            // interface IGoo<|
            // interface IGoo<A,|
            // interface IGoo<[Bar]|
P
Pilchie 已提交
615

C
Charles Stoner 已提交
616 617 618
            // delegate X D<|
            // delegate X D<A,|
            // delegate X D<[Bar]|
619
            if (targetToken.Kind() == SyntaxKind.LessThanToken &&
P
Pilchie 已提交
620 621 622 623 624
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

625
            if (targetToken.Kind() == SyntaxKind.CommaToken &&
P
Pilchie 已提交
626 627 628 629 630
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent))
            {
                return true;
            }

631
            if (targetToken.Kind() == SyntaxKind.CloseBracketToken &&
J
jasonmalinowski 已提交
632
                targetToken.Parent.IsKind(SyntaxKind.AttributeList) &&
P
Pilchie 已提交
633
                targetToken.Parent.IsParentKind(SyntaxKind.TypeParameter) &&
C
Cyrus Najmabadi 已提交
634
                IsGenericInterfaceOrDelegateTypeParameterList(targetToken.Parent.Parent.Parent))
P
Pilchie 已提交
635 636 637 638 639 640
            {
                return true;
            }

            return false;
        }
641 642 643

        public static bool IsMandatoryNamedParameterPosition(this SyntaxToken token)
        {
644
            if (token.Kind() == SyntaxKind.CommaToken && token.Parent is BaseArgumentListSyntax)
645 646 647 648 649 650 651 652 653 654 655 656
            {
                var argumentList = (BaseArgumentListSyntax)token.Parent;

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

                    if (item.IsNode)
                    {
C
CyrusNajmabadi 已提交
657
                        if (item.AsNode() is ArgumentSyntax node && node.NameColon != null)
658 659 660 661 662 663 664 665 666
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
667 668 669 670 671 672 673 674 675 676 677

        public static bool IsNumericTypeContext(this SyntaxToken token, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (!(token.Parent is MemberAccessExpressionSyntax memberAccessExpression))
            {
                return false;
            }

            var typeInfo = semanticModel.GetTypeInfo(memberAccessExpression.Expression, cancellationToken);
            return typeInfo.Type.IsNumericType();
        }
P
Pilchie 已提交
678
    }
679
}