StructKeywordRecommender.cs 1.7 KB
Newer Older
S
Sam Harwell 已提交
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.
2 3 4 5 6 7 8 9 10 11

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

namespace Microsoft.CodeAnalysis.CSharp.Completion.KeywordRecommenders
{
    internal class StructKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
    {
12
        private static readonly ISet<SyntaxKind> s_validModifiers = new HashSet<SyntaxKind>(SyntaxFacts.EqualityComparer)
13 14 15 16 17
            {
                SyntaxKind.InternalKeyword,
                SyntaxKind.PublicKeyword,
                SyntaxKind.PrivateKeyword,
                SyntaxKind.ProtectedKeyword,
18 19 20
                SyntaxKind.UnsafeKeyword,
                SyntaxKind.RefKeyword,
                SyntaxKind.ReadOnlyKeyword
21 22 23 24 25 26 27 28 29 30 31 32 33
            };

        public StructKeywordRecommender()
            : base(SyntaxKind.StructKeyword)
        {
        }

        protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            var syntaxTree = context.SyntaxTree;
            return
                context.IsGlobalStatementContext ||
                context.IsTypeDeclarationContext(
34
                    validModifiers: s_validModifiers,
35
                    validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations,
36 37 38 39 40 41
                    canBePartial: true,
                    cancellationToken: cancellationToken) ||
                syntaxTree.IsTypeParameterConstraintStartContext(position, context.LeftToken, cancellationToken);
        }
    }
}