VoidKeywordRecommender.cs 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.CSharp.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
    internal class VoidKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
    {
13
        private static readonly ISet<SyntaxKind> s_validModifiers = new HashSet<SyntaxKind>(SyntaxFacts.EqualityComparer)
14 15 16 17 18 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
        {
            SyntaxKind.NewKeyword,
            SyntaxKind.PublicKeyword,
            SyntaxKind.ProtectedKeyword,
            SyntaxKind.InternalKeyword,
            SyntaxKind.PrivateKeyword,
            SyntaxKind.StaticKeyword,
            SyntaxKind.VirtualKeyword,
            SyntaxKind.SealedKeyword,
            SyntaxKind.OverrideKeyword,
            SyntaxKind.AbstractKeyword,
            SyntaxKind.ExternKeyword,
            SyntaxKind.UnsafeKeyword,
            SyntaxKind.AsyncKeyword
        };

        public VoidKeywordRecommender()
            : base(SyntaxKind.VoidKeyword)
        {
        }

        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var syntaxTree = context.SyntaxTree;
            return
                IsMemberReturnTypeContext(position, context, cancellationToken) ||
                context.IsGlobalStatementContext ||
                context.IsTypeOfExpressionContext ||
                syntaxTree.IsSizeOfExpressionContext(position, context.LeftToken, cancellationToken) ||
                context.IsDelegateReturnTypeContext ||
                IsUnsafeLocalVariableDeclarationContext(context) ||
                IsUnsafeParameterTypeContext(context) ||
                IsUnsafeCastTypeContext(context) ||
                IsUnsafeDefaultExpressionContext(context, cancellationToken) ||
48 49
                context.IsFixedVariableDeclarationContext ||
                context.SyntaxTree.IsLocalFunctionDeclarationContext(position, cancellationToken);
50 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 96 97 98 99 100 101 102 103 104
        }

        private bool IsUnsafeDefaultExpressionContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            return
                context.TargetToken.IsUnsafeContext() &&
                context.SyntaxTree.IsDefaultExpressionContext(context.Position, context.LeftToken, cancellationToken);
        }

        private bool IsUnsafeCastTypeContext(CSharpSyntaxContext context)
        {
            if (context.TargetToken.IsUnsafeContext())
            {
                if (context.IsDefiniteCastTypeContext)
                {
                    return true;
                }

                var token = context.TargetToken;

                if (token.Kind() == SyntaxKind.OpenParenToken &&
                    token.Parent.IsKind(SyntaxKind.ParenthesizedExpression))
                {
                    return true;
                }
            }

            return false;
        }

        private bool IsUnsafeParameterTypeContext(CSharpSyntaxContext context)
        {
            return
                context.TargetToken.IsUnsafeContext() &&
                context.IsParameterTypeContext;
        }

        private bool IsUnsafeLocalVariableDeclarationContext(CSharpSyntaxContext context)
        {
            if (context.TargetToken.IsUnsafeContext())
            {
                return
                    context.IsLocalVariableDeclarationContext ||
                    context.IsStatementContext;
            }

            return false;
        }

        private bool IsMemberReturnTypeContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var syntaxTree = context.SyntaxTree;
            return
                syntaxTree.IsGlobalMemberDeclarationContext(position, SyntaxKindSet.AllGlobalMemberModifiers, cancellationToken) ||
                context.IsMemberDeclarationContext(
105
                    validModifiers: s_validModifiers,
106 107 108 109 110 111
                    validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations,
                    canBePartial: true,
                    cancellationToken: cancellationToken);
        }
    }
}