CSharpSyntaxContext.cs 17.7 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

using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Utilities;
R
Ravi Chande 已提交
7
using Microsoft.CodeAnalysis.LanguageServices;
P
Pilchie 已提交
8 9 10 11 12
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Extensions.ContextQuery;

namespace Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery
{
C
CyrusNajmabadi 已提交
13
    internal sealed class CSharpSyntaxContext : SyntaxContext
P
Pilchie 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    {
        public readonly TypeDeclarationSyntax ContainingTypeDeclaration;
        public readonly BaseTypeDeclarationSyntax ContainingTypeOrEnumDeclaration;

        public readonly bool IsInNonUserCode;

        public readonly bool IsPreProcessorKeywordContext;
        public readonly bool IsPreProcessorExpressionContext;

        public readonly bool IsGlobalStatementContext;

        public readonly bool IsNonAttributeExpressionContext;
        public readonly bool IsConstantExpressionContext;

        public readonly bool IsLabelContext;
        public readonly bool IsTypeArgumentOfConstraintContext;

        public readonly bool IsIsOrAsContext;
        public readonly bool IsObjectCreationTypeContext;
        public readonly bool IsDefiniteCastTypeContext;
        public readonly bool IsGenericTypeArgumentContext;
        public readonly bool IsEnumBaseListContext;
        public readonly bool IsIsOrAsTypeContext;
        public readonly bool IsLocalVariableDeclarationContext;
38
        public readonly bool IsDeclarationExpressionContext;
P
Pilchie 已提交
39 40 41 42 43 44 45 46 47 48 49
        public readonly bool IsFixedVariableDeclarationContext;
        public readonly bool IsParameterTypeContext;
        public readonly bool IsPossibleLambdaOrAnonymousMethodParameterTypeContext;
        public readonly bool IsImplicitOrExplicitOperatorTypeContext;
        public readonly bool IsPrimaryFunctionExpressionContext;
        public readonly bool IsDelegateReturnTypeContext;
        public readonly bool IsTypeOfExpressionContext;
        public readonly ISet<SyntaxKind> PrecedingModifiers;
        public readonly bool IsInstanceContext;
        public readonly bool IsCrefContext;
        public readonly bool IsCatchFilterContext;
50
        public readonly bool IsDestructorTypeContext;
P
Pilchie 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

        private CSharpSyntaxContext(
            Workspace workspace,
            SemanticModel semanticModel,
            int position,
            SyntaxToken leftToken,
            SyntaxToken targetToken,
            TypeDeclarationSyntax containingTypeDeclaration,
            BaseTypeDeclarationSyntax containingTypeOrEnumDeclaration,
            bool isInNonUserCode,
            bool isPreProcessorDirectiveContext,
            bool isPreProcessorKeywordContext,
            bool isPreProcessorExpressionContext,
            bool isTypeContext,
            bool isNamespaceContext,
66
            bool isNamespaceDeclarationNameContext,
P
Pilchie 已提交
67 68 69 70 71 72 73
            bool isStatementContext,
            bool isGlobalStatementContext,
            bool isAnyExpressionContext,
            bool isNonAttributeExpressionContext,
            bool isConstantExpressionContext,
            bool isAttributeNameContext,
            bool isEnumTypeMemberAccessContext,
74
            bool isNameOfContext,
P
Pilchie 已提交
75 76 77 78 79 80 81 82 83 84 85 86
            bool isInQuery,
            bool isInImportsDirective,
            bool isLabelContext,
            bool isTypeArgumentOfConstraintContext,
            bool isRightOfDotOrArrowOrColonColon,
            bool isIsOrAsContext,
            bool isObjectCreationTypeContext,
            bool isDefiniteCastTypeContext,
            bool isGenericTypeArgumentContext,
            bool isEnumBaseListContext,
            bool isIsOrAsTypeContext,
            bool isLocalVariableDeclarationContext,
87
            bool isDeclarationExpressionContext,
P
Pilchie 已提交
88 89 90 91 92 93 94 95 96 97
            bool isFixedVariableDeclarationContext,
            bool isParameterTypeContext,
            bool isPossibleLambdaOrAnonymousMethodParameterTypeContext,
            bool isImplicitOrExplicitOperatorTypeContext,
            bool isPrimaryFunctionExpressionContext,
            bool isDelegateReturnTypeContext,
            bool isTypeOfExpressionContext,
            ISet<SyntaxKind> precedingModifiers,
            bool isInstanceContext,
            bool isCrefContext,
98
            bool isCatchFilterContext,
99
            bool isDestructorTypeContext,
100
            bool isPossibleTupleContext,
101
            CancellationToken cancellationToken)
P
Pilchie 已提交
102
            : base(workspace, semanticModel, position, leftToken, targetToken,
103
                   isTypeContext, isNamespaceContext, isNamespaceDeclarationNameContext,
P
Pilchie 已提交
104 105
                   isPreProcessorDirectiveContext,
                   isRightOfDotOrArrowOrColonColon, isStatementContext, isAnyExpressionContext,
106
                   isAttributeNameContext, isEnumTypeMemberAccessContext, isNameOfContext,
107
                   isInQuery, isInImportsDirective, IsWithinAsyncMethod(), isPossibleTupleContext, cancellationToken)
P
Pilchie 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        {
            this.ContainingTypeDeclaration = containingTypeDeclaration;
            this.ContainingTypeOrEnumDeclaration = containingTypeOrEnumDeclaration;
            this.IsInNonUserCode = isInNonUserCode;
            this.IsPreProcessorKeywordContext = isPreProcessorKeywordContext;
            this.IsPreProcessorExpressionContext = isPreProcessorExpressionContext;
            this.IsGlobalStatementContext = isGlobalStatementContext;
            this.IsNonAttributeExpressionContext = isNonAttributeExpressionContext;
            this.IsConstantExpressionContext = isConstantExpressionContext;
            this.IsLabelContext = isLabelContext;
            this.IsTypeArgumentOfConstraintContext = isTypeArgumentOfConstraintContext;
            this.IsIsOrAsContext = isIsOrAsContext;
            this.IsObjectCreationTypeContext = isObjectCreationTypeContext;
            this.IsDefiniteCastTypeContext = isDefiniteCastTypeContext;
            this.IsGenericTypeArgumentContext = isGenericTypeArgumentContext;
            this.IsEnumBaseListContext = isEnumBaseListContext;
            this.IsIsOrAsTypeContext = isIsOrAsTypeContext;
            this.IsLocalVariableDeclarationContext = isLocalVariableDeclarationContext;
126
            this.IsDeclarationExpressionContext = isDeclarationExpressionContext;
P
Pilchie 已提交
127 128 129 130 131 132 133 134 135 136 137
            this.IsFixedVariableDeclarationContext = isFixedVariableDeclarationContext;
            this.IsParameterTypeContext = isParameterTypeContext;
            this.IsPossibleLambdaOrAnonymousMethodParameterTypeContext = isPossibleLambdaOrAnonymousMethodParameterTypeContext;
            this.IsImplicitOrExplicitOperatorTypeContext = isImplicitOrExplicitOperatorTypeContext;
            this.IsPrimaryFunctionExpressionContext = isPrimaryFunctionExpressionContext;
            this.IsDelegateReturnTypeContext = isDelegateReturnTypeContext;
            this.IsTypeOfExpressionContext = isTypeOfExpressionContext;
            this.PrecedingModifiers = precedingModifiers;
            this.IsInstanceContext = isInstanceContext;
            this.IsCrefContext = isCrefContext;
            this.IsCatchFilterContext = isCatchFilterContext;
138
            this.IsDestructorTypeContext = isDestructorTypeContext;
P
Pilchie 已提交
139 140 141
        }

        public static CSharpSyntaxContext CreateContext(Workspace workspace, SemanticModel semanticModel, int position, CancellationToken cancellationToken)
142 143 144 145 146
        {
            return CreateContextWorker(workspace, semanticModel, position, cancellationToken);
        }

        private static CSharpSyntaxContext CreateContextWorker(Workspace workspace, SemanticModel semanticModel, int position, CancellationToken cancellationToken)
P
Pilchie 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        {
            var syntaxTree = semanticModel.SyntaxTree;

            var isInNonUserCode = syntaxTree.IsInNonUserCode(position, cancellationToken);

            var preProcessorTokenOnLeftOfPosition = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken, includeDirectives: true);
            var isPreProcessorDirectiveContext = syntaxTree.IsPreProcessorDirectiveContext(position, preProcessorTokenOnLeftOfPosition, cancellationToken);

            var leftToken = isPreProcessorDirectiveContext
                ? preProcessorTokenOnLeftOfPosition
                : syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken);

            var targetToken = leftToken.GetPreviousTokenIfTouchingWord(position);

            var isPreProcessorKeywordContext = isPreProcessorDirectiveContext
                ? syntaxTree.IsPreProcessorKeywordContext(position, leftToken, cancellationToken)
                : false;

            var isPreProcessorExpressionContext = isPreProcessorDirectiveContext
                ? targetToken.IsPreProcessorExpressionContext()
                : false;

            var isStatementContext = !isPreProcessorDirectiveContext
                ? targetToken.IsBeginningOfStatementContext()
                : false;

            var isGlobalStatementContext = !isPreProcessorDirectiveContext
                ? syntaxTree.IsGlobalStatementContext(position, cancellationToken)
                : false;

            var isAnyExpressionContext = !isPreProcessorDirectiveContext
                ? syntaxTree.IsExpressionContext(position, leftToken, attributes: true, cancellationToken: cancellationToken, semanticModelOpt: semanticModel)
                : false;

            var isNonAttributeExpressionContext = !isPreProcessorDirectiveContext
                ? syntaxTree.IsExpressionContext(position, leftToken, attributes: false, cancellationToken: cancellationToken, semanticModelOpt: semanticModel)
                : false;

            var isConstantExpressionContext = !isPreProcessorDirectiveContext
                ? syntaxTree.IsConstantExpressionContext(position, leftToken, cancellationToken)
                : false;

            var containingTypeDeclaration = syntaxTree.GetContainingTypeDeclaration(position, cancellationToken);
            var containingTypeOrEnumDeclaration = syntaxTree.GetContainingTypeOrEnumDeclaration(position, cancellationToken);

J
jasonmalinowski 已提交
192 193 194
            var isDestructorTypeContext = targetToken.IsKind(SyntaxKind.TildeToken) &&
                                            targetToken.Parent.IsKind(SyntaxKind.DestructorDeclaration) &&
                                            targetToken.Parent.Parent.IsKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration);
195

P
Pilchie 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209
            return new CSharpSyntaxContext(
                workspace,
                semanticModel,
                position,
                leftToken,
                targetToken,
                containingTypeDeclaration,
                containingTypeOrEnumDeclaration,
                isInNonUserCode,
                isPreProcessorDirectiveContext,
                isPreProcessorKeywordContext,
                isPreProcessorExpressionContext,
                syntaxTree.IsTypeContext(position, cancellationToken, semanticModelOpt: semanticModel),
                syntaxTree.IsNamespaceContext(position, cancellationToken, semanticModelOpt: semanticModel),
210
                syntaxTree.IsNamespaceDeclarationNameContext(position, cancellationToken),
P
Pilchie 已提交
211 212 213 214 215 216 217
                isStatementContext,
                isGlobalStatementContext,
                isAnyExpressionContext,
                isNonAttributeExpressionContext,
                isConstantExpressionContext,
                syntaxTree.IsAttributeNameContext(position, cancellationToken),
                syntaxTree.IsEnumTypeMemberAccessContext(semanticModel, position, cancellationToken),
218
                syntaxTree.IsNameOfContext(position, semanticModel, cancellationToken),
P
Pilchie 已提交
219 220 221 222 223 224 225 226 227 228 229 230
                leftToken.GetAncestor<QueryExpressionSyntax>() != null,
                IsLeftSideOfUsingAliasDirective(leftToken, cancellationToken),
                syntaxTree.IsLabelContext(position, cancellationToken),
                syntaxTree.IsTypeArgumentOfConstraintClause(position, cancellationToken),
                syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken),
                syntaxTree.IsIsOrAsContext(position, leftToken, cancellationToken),
                syntaxTree.IsObjectCreationTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsDefiniteCastTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsGenericTypeArgumentContext(position, leftToken, cancellationToken),
                syntaxTree.IsEnumBaseListContext(position, leftToken, cancellationToken),
                syntaxTree.IsIsOrAsTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsLocalVariableDeclarationContext(position, leftToken, cancellationToken),
231
                syntaxTree.IsDeclarationExpressionContext(position, leftToken, cancellationToken),
P
Pilchie 已提交
232 233 234 235 236 237 238 239 240
                syntaxTree.IsFixedVariableDeclarationContext(position, leftToken, cancellationToken),
                syntaxTree.IsParameterTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsPossibleLambdaOrAnonymousMethodParameterTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsImplicitOrExplicitOperatorTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsPrimaryFunctionExpressionContext(position, leftToken, cancellationToken),
                syntaxTree.IsDelegateReturnTypeContext(position, leftToken, cancellationToken),
                syntaxTree.IsTypeOfExpressionContext(position, leftToken, cancellationToken),
                syntaxTree.GetPrecedingModifiers(position, leftToken, cancellationToken),
                syntaxTree.IsInstanceContext(position, leftToken, cancellationToken),
J
jasonmalinowski 已提交
241
                syntaxTree.IsCrefContext(position, cancellationToken) && !leftToken.IsKind(SyntaxKind.DotToken),
242
                syntaxTree.IsCatchFilterContext(position, leftToken),
243
                isDestructorTypeContext,
244
                leftToken.IsPossibleTupleElementNameContext(position),
245
                cancellationToken);
P
Pilchie 已提交
246 247 248 249
        }

        public static CSharpSyntaxContext CreateContext_Test(SemanticModel semanticModel, int position, CancellationToken cancellationToken)
        {
250 251 252
            var inferenceService = new CSharpTypeInferenceService();
            var types = inferenceService.InferTypes(semanticModel, position, cancellationToken);
            return CreateContextWorker(workspace: null, semanticModel: semanticModel, position: position, cancellationToken: cancellationToken);
P
Pilchie 已提交
253 254
        }

C
CyrusNajmabadi 已提交
255 256 257 258 259 260 261
        private new static bool IsWithinAsyncMethod()
        {
            // TODO: Implement this if any C# completion code needs to know if it is in an async 
            // method or not.
            return false;
        }

P
Pilchie 已提交
262 263 264 265 266 267 268 269 270 271
        public bool IsTypeAttributeContext(CancellationToken cancellationToken)
        {
            // cases:
            //    [ |
            //    class C { [ |
            var token = this.TargetToken;

            // Note that we pass the token.SpanStart to IsTypeDeclarationContext below. This is a bit subtle,
            // but we want to be sure that the attribute itself (i.e. the open square bracket, '[') is in a
            // type declaration context.
272 273
            if (token.Kind() == SyntaxKind.OpenBracketToken &&
                token.Parent.Kind() == SyntaxKind.AttributeList &&
P
Pilchie 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                this.SyntaxTree.IsTypeDeclarationContext(
                    token.SpanStart, contextOpt: null, validModifiers: null, validTypeDeclarations: SyntaxKindSet.ClassStructTypeDeclarations, canBePartial: false, cancellationToken: cancellationToken))
            {
                return true;
            }

            return false;
        }

        public bool IsTypeDeclarationContext(
            ISet<SyntaxKind> validModifiers = null,
            ISet<SyntaxKind> validTypeDeclarations = null,
            bool canBePartial = false,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            return this.SyntaxTree.IsTypeDeclarationContext(this.Position, this, validModifiers, validTypeDeclarations, canBePartial, cancellationToken);
        }

        public bool IsMemberAttributeContext(ISet<SyntaxKind> validTypeDeclarations, CancellationToken cancellationToken)
        {
            // cases:
            //   class C { [ |
            var token = this.TargetToken;

298 299
            if (token.Kind() == SyntaxKind.OpenBracketToken &&
                token.Parent.Kind() == SyntaxKind.AttributeList &&
P
Pilchie 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                this.SyntaxTree.IsMemberDeclarationContext(
                    token.SpanStart, contextOpt: null, validModifiers: null, validTypeDeclarations: validTypeDeclarations, canBePartial: false, cancellationToken: cancellationToken))
            {
                return true;
            }

            return false;
        }

        public bool IsMemberDeclarationContext(
            ISet<SyntaxKind> validModifiers = null,
            ISet<SyntaxKind> validTypeDeclarations = null,
            bool canBePartial = false,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            return this.SyntaxTree.IsMemberDeclarationContext(this.Position, this, validModifiers, validTypeDeclarations, canBePartial, cancellationToken);
        }

        private static bool IsLeftSideOfUsingAliasDirective(SyntaxToken leftToken, CancellationToken cancellationToken)
        {
            var usingDirective = leftToken.GetAncestor<UsingDirectiveSyntax>();

            if (usingDirective != null)
            {
                // No = token: 
                if (usingDirective.Alias == null || usingDirective.Alias.EqualsToken.IsMissing)
                {
                    return true;
                }

                return leftToken.SpanStart < usingDirective.Alias.EqualsToken.SpanStart;
            }

            return false;
        }
335 336 337 338 339

        internal override ITypeInferenceService GetTypeInferenceServiceWithoutWorkspace()
        {
            return new CSharpTypeInferenceService();
        }
P
Pilchie 已提交
340
    }
341
}