示例#1
1
        private static bool IsExtensionMethodParameterContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            // TODO(cyrusn): lambda/anon methods can have out/ref parameters
            if (!context.SyntaxTree.IsParameterModifierContext(context.Position, context.LeftToken, cancellationToken, allowableIndex: 0))
            {
                return false;
            }

            var token = context.LeftToken;
            var method = token.GetAncestor<MethodDeclarationSyntax>();
            var typeDecl = method.GetAncestorOrThis<TypeDeclarationSyntax>();

            if (method == null || typeDecl == null)
            {
                return false;
            }

            if (typeDecl.Kind() != SyntaxKind.ClassDeclaration)
            {
                return false;
            }

            if (!method.Modifiers.Any(t => t.Kind() == SyntaxKind.StaticKeyword))
            {
                return false;
            }

            if (!typeDecl.Modifiers.Any(t => t.Kind() == SyntaxKind.StaticKeyword))
            {
                return false;
            }

            return true;
        }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     var syntaxTree = context.SyntaxTree;
     return
         context.IsNonAttributeExpressionContext ||
         context.IsDefiniteCastTypeContext ||
         context.IsStatementContext ||
         context.IsGlobalStatementContext ||
         context.IsObjectCreationTypeContext ||
         context.IsGenericTypeArgumentContext ||
         context.IsEnumBaseListContext ||
         context.IsIsOrAsTypeContext ||
         context.IsLocalVariableDeclarationContext ||
         context.IsFixedVariableDeclarationContext ||
         context.IsParameterTypeContext ||
         context.IsPossibleLambdaOrAnonymousMethodParameterTypeContext ||
         context.IsImplicitOrExplicitOperatorTypeContext ||
         context.IsPrimaryFunctionExpressionContext ||
         context.IsCrefContext ||
         syntaxTree.IsAfterKeyword(position, SyntaxKind.ConstKeyword, cancellationToken) ||
         syntaxTree.IsAfterKeyword(position, SyntaxKind.StackAllocKeyword, cancellationToken) ||
         context.IsDelegateReturnTypeContext ||
         syntaxTree.IsGlobalMemberDeclarationContext(position, SyntaxKindSet.AllGlobalMemberModifiers, cancellationToken) ||
         context.IsMemberDeclarationContext(
             validModifiers: SyntaxKindSet.AllMemberModifiers,
             validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations,
             canBePartial: false,
             cancellationToken: cancellationToken);
 }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         context.IsStatementContext ||
         context.IsGlobalStatementContext ||
         context.IsNonAttributeExpressionContext;
 }
示例#4
1
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         IsInstanceExpressionOrStatement(context) ||
         IsExtensionMethodParameterContext(context, cancellationToken) ||
         IsConstructorInitializerContext(context);
 }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         context.IsGlobalStatementContext ||
         IsMemberDeclarationContext(context, cancellationToken) ||
         IsTypeDeclarationContext(context, cancellationToken);
 }
        private static IEnumerable<ISymbol> GetSymbolsForCurrentContext(
            CSharpSyntaxContext context,
            bool filterOutOfScopeLocals,
            CancellationToken cancellationToken)
        {
            if (context.IsGlobalStatementContext)
            {
                // Script and interactive
                return GetSymbolsForGlobalStatementContext(context, cancellationToken);
            }
            else if (context.IsAnyExpressionContext || context.IsStatementContext)
            {
                return GetSymbolsForExpressionOrStatementContext(context, filterOutOfScopeLocals, cancellationToken);
            }
            else if (context.IsTypeContext || context.IsNamespaceContext)
            {
                return GetSymbolsForTypeOrNamespaceContext(context, cancellationToken);
            }
            else if (context.IsLabelContext)
            {
                return GetSymbolsForLabelContext(context, cancellationToken);
            }
            else if (context.IsTypeArgumentOfConstraintContext)
            {
                return GetSymbolsForTypeArgumentOfConstraintClause(context, cancellationToken);
            }
            else if (context.IsDestructorTypeContext)
            {
                return SpecializedCollections.SingletonEnumerable(context.SemanticModel.GetDeclaredSymbol(context.ContainingTypeOrEnumDeclaration, cancellationToken));
            }

            return SpecializedCollections.EmptyEnumerable<ISymbol>();
        }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            if (context.IsAnyExpressionContext || context.IsStatementContext)
            {
                foreach (var node in context.LeftToken.GetAncestors<SyntaxNode>())
                {
					if (node.IsAnyLambdaOrAnonymousMethod())
                    {
                        return true;
                    }

                    if (node.IsKind(SyntaxKind.QueryExpression))
                    {
                        return false;
                    }

                    if (node.IsKind(SyntaxKind.LockStatement))
                    {
                        var lockStatement = (LockStatementSyntax)node;
                        if (lockStatement.Statement != null &&
                            !lockStatement.Statement.IsMissing &&
                            lockStatement.Statement.Span.Contains(position))
                        {
                            return false;
                        }
                    }
                }

                return true;
            }

            return false;
        }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            if (context.IsStatementContext ||
                context.IsGlobalStatementContext)
            {
                return true;
            }

            // do {
            // } |

            // do {
            // } w|

            // Note: the case of
            //   do
            //     Foo();
            //   |
            // is taken care of in the IsStatementContext case.

            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.CloseBraceToken &&
                token.Parent.IsKind(SyntaxKind.Block) &&
                token.Parent.IsParentKind(SyntaxKind.DoStatement))
            {
                return true;
            }

            return false;
        }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            // cases:
            //   join a in expr o1 |
            //   join a in expr o1 e|

            var token = context.TargetToken;

            var join = token.GetAncestor<JoinClauseSyntax>();
            if (join == null)
            {
                return false;
            }

            var lastToken = join.LeftExpression.GetLastToken(includeSkipped: true);

            // join a in expr |
            if (join.LeftExpression.Width() > 0 &&
                token == lastToken)
            {
                return true;
            }

            return false;
        }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         IsValidContextForAccessor(context) ||
         IsValidContextForType(context, cancellationToken) ||
         IsValidContextForMember(context, cancellationToken);
 }
示例#11
0
        private bool IsValidContextInForEachClause(CSharpSyntaxContext context)
        {
            // cases:
            //   foreach (var v |
            //   foreach (var v i|
            //   foreach (var (x, y) |

            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.IdentifierToken)
            {
                var statement = token.Parent as ForEachStatementSyntax;
                if (statement != null && token == statement.Identifier)
                {
                    return true;
                }
            }
            else if (token.Kind() == SyntaxKind.CloseParenToken)
            {
                var statement = token.GetAncestor<ForEachComponentStatementSyntax>();
                if (statement != null && token.Span.End == statement.VariableComponent.Span.End)
                {
                    return true;
                }
            }

            return false;
        }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         context.IsStatementContext ||
         context.TargetToken.IsAfterYieldKeyword() ||
         IsAttributeContext(context, cancellationToken);
 }
 private static bool IsMemberDeclarationContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         context.TargetToken.IsUnsafeContext() &&
        (context.SyntaxTree.IsGlobalMemberDeclarationContext(context.Position, SyntaxKindSet.AllGlobalMemberModifiers, cancellationToken) ||
        context.IsMemberDeclarationContext(validModifiers: s_validModifiers, validTypeDeclarations: SyntaxKindSet.StructOnlyTypeDeclarations, canBePartial: false, cancellationToken: cancellationToken));
 }
        private static bool IsInBreakableConstructContext(CSharpSyntaxContext context)
        {
            if (!context.IsStatementContext)
            {
                return false;
            }

            // allowed if we're inside a loop/switch construct.

            var token = context.LeftToken;
            foreach (var v in token.GetAncestors<SyntaxNode>())
            {
                if (v.IsAnyLambdaOrAnonymousMethod())
                {
                    // if we hit a lambda while walking up, then we can't
                    // 'continue' any outer loops.
                    return false;
                }

                if (v.IsBreakableConstruct())
                {
                    return true;
                }
            }

            return false;
        }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            // cases:
            //   group e |
            //   group e b|

            var token = context.LeftToken;
            var group = token.GetAncestor<GroupClauseSyntax>();

            if (group == null)
            {
                return false;
            }

            var lastToken = group.GroupExpression.GetLastToken(includeSkipped: true);

            // group e |
            if (!token.IntersectsWith(position) &&
                token == lastToken)
            {
                return true;
            }

            // group e b|
            if (token.IntersectsWith(position) &&
                token.Kind() == SyntaxKind.IdentifierToken &&
                token.GetPreviousToken(includeSkipped: true) == lastToken)
            {
                return true;
            }

            return false;
        }
示例#16
0
 private async Task CheckResultAsync(bool absent, int position, CSharpSyntaxContext context, SemanticModel semanticModel, int? matchPriority)
 {
     if (absent)
     {
         if (RecommendKeywordsAsync != null)
         {
             var keywords = await RecommendKeywordsAsync(position, context);
             Assert.True(keywords == null || !keywords.Any(), "Keywords must be null or empty.");
         }
     }
     else
     {
         if (RecommendKeywordsAsync == null)
         {
             Assert.False(true, "No recommender for: " + keywordText);
         }
         else
         {
             var result = (await RecommendKeywordsAsync(position, context)).Single();
             Assert.NotNull(result);
             Assert.Equal(keywordText, result.Keyword);
             if (matchPriority != null)
             {
                 Assert.Equal(matchPriority.Value, result.MatchPriority);
             }
         }
     }
 }
示例#17
0
        private bool IsValidContextInJoinClause(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.IdentifierToken)
            {
                var joinClause = token.Parent.FirstAncestorOrSelf<JoinClauseSyntax>();
                if (joinClause != null)
                {
                    // case:
                    //   join int x |
                    if (token == joinClause.Identifier && joinClause.Type != null)
                    {
                        return true;
                    }

                    // case:
                    //   join x |
                    if (joinClause.Type != null &&
                        joinClause.Type.IsKind(SyntaxKind.IdentifierName) &&
                        token == ((IdentifierNameSyntax)joinClause.Type).Identifier &&
                        !joinClause.Type.IsPotentialTypeName(context.SemanticModel, cancellationToken))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         IsValidContextForJoin(context) ||
         IsValidContextForSelect(context) ||
         IsValidContextForGroup(context);
 }
        private bool IsValidContextForGroup(CSharpSyntaxContext context)
        {
            var token = context.TargetToken;

            var group = token.GetAncestor<GroupClauseSyntax>();
            if (group == null)
            {
                return false;
            }

            if (group.ByExpression.Width() == 0 ||
                group.GroupExpression.Width() == 0)
            {
                return false;
            }

            var lastToken = group.ByExpression.GetLastToken(includeSkipped: true);

            if (lastToken == token)
            {
                return true;
            }

            return false;
        }
示例#20
0
        private bool IsValidContextInFromClause(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.IdentifierToken)
            {
                // case:
                //   from x |
                if (token.GetPreviousToken(includeSkipped: true).IsKindOrHasMatchingText(SyntaxKind.FromKeyword))
                {
                    var typeSyntax = token.Parent as TypeSyntax;
                    if (!typeSyntax.IsPotentialTypeName(context.SemanticModel, cancellationToken))
                    {
                        return true;
                    }
                }

                var fromClause = token.Parent as FromClauseSyntax;
                if (fromClause != null)
                {
                    // case:
                    //   from int x |
                    if (token == fromClause.Identifier && fromClause.Type != null)
                    {
                        return true;
                    }
                }
            }

            return false;
        }
示例#21
0
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     var syntaxTree = context.SyntaxTree;
     return
         context.IsNonAttributeExpressionContext ||
         context.IsDefiniteCastTypeContext ||
         context.IsStatementContext ||
         context.IsGlobalStatementContext ||
         context.IsObjectCreationTypeContext ||
         (context.IsGenericTypeArgumentContext && !context.TargetToken.Parent.HasAncestor<XmlCrefAttributeSyntax>()) ||
         context.IsIsOrAsTypeContext ||
         context.IsLocalVariableDeclarationContext ||
         context.IsParameterTypeContext ||
         context.IsPossibleLambdaOrAnonymousMethodParameterTypeContext ||
         context.IsImplicitOrExplicitOperatorTypeContext ||
         context.IsTypeOfExpressionContext ||
         context.IsCrefContext ||
         syntaxTree.IsDefaultExpressionContext(position, context.LeftToken, cancellationToken) ||
         syntaxTree.IsAfterKeyword(position, SyntaxKind.ConstKeyword, cancellationToken) ||
         context.IsDelegateReturnTypeContext ||
         syntaxTree.IsGlobalMemberDeclarationContext(position, SyntaxKindSet.AllGlobalMemberModifiers, cancellationToken) ||
         context.IsMemberDeclarationContext(
             validModifiers: SyntaxKindSet.AllMemberModifiers,
             validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations,
             canBePartial: false,
             cancellationToken: cancellationToken);
 }
示例#22
0
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     var syntaxTree = context.SyntaxTree;
     return
         context.IsPreProcessorKeywordContext &&
         syntaxTree.IsBeforeFirstToken(position, cancellationToken);
 }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            if (!context.IsStatementContext)
            {
                return false;
            }

            // allowed if we're inside a loop construct.

            var leaf = context.LeftToken;
            foreach (var v in leaf.GetAncestors<SyntaxNode>())
            {
                if (v.IsAnyLambdaOrAnonymousMethod())
                {
                    // if we hit a lambda while walking up, then we can't
                    // 'continue' any outer loops.
                    return false;
                }

                if (v.IsContinuableConstruct())
                {
                    return true;
                }
            }

            return false;
        }
示例#24
0
 protected override async Task<bool> IsValidContextAsync(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     var syntaxTree = context.SyntaxTree;
     return
         context.IsPreProcessorKeywordContext &&
         await syntaxTree.IsBeforeFirstTokenAsync(position, cancellationToken).ConfigureAwait(false);
 }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     var syntaxTree = context.SyntaxTree;
     return
         context.IsGlobalStatementContext ||
         syntaxTree.IsValidContextForFromClause(position, context.LeftToken, cancellationToken, semanticModelOpt: context.SemanticModel);
 }
 protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
 {
     return
         context.IsPreProcessorKeywordContext ||
         context.IsStatementContext ||
         context.IsGlobalStatementContext;
 }
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.OpenBracketToken &&
                token.Parent.IsKind(SyntaxKind.AttributeList))
            {
                var typeParameters = token.GetAncestor<TypeParameterListSyntax>();
                var type = typeParameters.GetAncestorOrThis<TypeDeclarationSyntax>();

                if (type != null && type.TypeParameterList == typeParameters)
                {
                    return true;
                }

                var @delegate = typeParameters.GetAncestorOrThis<DelegateDeclarationSyntax>();
                if (@delegate != null && @delegate.TypeParameterList == typeParameters)
                {
                    return true;
                }

                var method = typeParameters.GetAncestorOrThis<MethodDeclarationSyntax>();
                if (method != null && method.TypeParameterList == typeParameters)
                {
                    return true;
                }
            }

            return false;
        }
示例#28
0
        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            if (context.IsStatementContext || context.IsGlobalStatementContext)
            {
                return true;
            }

            // void M() => throw
            if (context.TargetToken.Kind() == SyntaxKind.EqualsGreaterThanToken)
            {
                return true;
            }

            // val ?? throw
            if (context.TargetToken.Kind() == SyntaxKind.QuestionQuestionToken)
            {
                return true;
            }

            //  expr ? throw : ...
            //  expr ? ... : throw
            if (context.TargetToken.Kind() == SyntaxKind.QuestionToken ||
                context.TargetToken.Kind() == SyntaxKind.ColonToken)
            {
                return context.TargetToken.Parent.Kind() == SyntaxKind.ConditionalExpression;
            }

            return false;
        }
        private static IEnumerable<ISymbol> GetSymbolsWorker(
            CSharpSyntaxContext context,
            bool filterOutOfScopeLocals,
            CancellationToken cancellationToken)
        {
            if (context.IsInNonUserCode ||
                context.IsPreProcessorDirectiveContext)
            {
                return SpecializedCollections.EmptyEnumerable<ISymbol>();
            }

            // TODO: don't show completion set at namespace name part to match Dev10 behavior
            // if we want to provide new feature that shows all existing namespaces later, remove this
            if (context.IsNamespaceDeclarationNameContext)
            {
                return SpecializedCollections.EmptyEnumerable<ISymbol>();
            }

            if (context.IsRightOfNameSeparator)
            {
                return GetSymbolsOffOfContainer(context, cancellationToken);
            }
            else
            {
                return GetSymbolsForCurrentContext(context, filterOutOfScopeLocals, cancellationToken);
            }
        }
示例#30
0
        private bool IsValidContextForSelect(CSharpSyntaxContext context)
        {
            var token = context.TargetToken;

            var select = token.GetAncestor<SelectClauseSyntax>();
            if (select == null)
            {
                return false;
            }

            if (select.Expression.Width() == 0)
            {
                return false;
            }


            // cases:
            //   select x.|
            //   select x.i|
            var lastCompleteToken = token.GetPreviousTokenIfTouchingWord(context.Position);
            if (lastCompleteToken.Kind() == SyntaxKind.DotToken)
            {
                return false;
            }

            var lastToken = select.Expression.GetLastToken(includeSkipped: true);
            if (lastToken == token)
            {
                return true;
            }

            return false;
        }