Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NewKeywordRecommender.cs « KeywordRecommender « Completion « MonoDevelop.CSharp.Features « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5c8c5742fd1323fe4997e91e6873abd6110286a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
48
49
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 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.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Extensions.ContextQuery;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Utilities;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;

namespace ICSharpCode.NRefactory6.CSharp.Completion.KeywordRecommenders
{
	internal class NewKeywordRecommender : AbstractSyntacticSingleKeywordRecommender
    {
        private static readonly ISet<SyntaxKind> s_validMemberModifiers = new HashSet<SyntaxKind>(SyntaxFacts.EqualityComparer)
            {
                SyntaxKind.AbstractKeyword,
                SyntaxKind.ExternKeyword,
                SyntaxKind.InternalKeyword,
                SyntaxKind.PublicKeyword,
                SyntaxKind.PrivateKeyword,
                SyntaxKind.ProtectedKeyword,
                SyntaxKind.ReadOnlyKeyword,
                SyntaxKind.SealedKeyword,
                SyntaxKind.StaticKeyword,
                SyntaxKind.UnsafeKeyword,
                SyntaxKind.VirtualKeyword,
                SyntaxKind.VolatileKeyword,
            };

        protected static readonly ISet<SyntaxKind> ValidTypeModifiers = new HashSet<SyntaxKind>(SyntaxFacts.EqualityComparer)
            {
                SyntaxKind.AbstractKeyword,
                SyntaxKind.InternalKeyword,
                SyntaxKind.PublicKeyword,
                SyntaxKind.PrivateKeyword,
                SyntaxKind.ProtectedKeyword,
                SyntaxKind.SealedKeyword,
                SyntaxKind.StaticKeyword,
                SyntaxKind.UnsafeKeyword
            };

        public NewKeywordRecommender()
            : base(SyntaxKind.NewKeyword)
        {
        }

		protected override bool IsValidContext(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
		{
			return IsValid (position, context, cancellationToken);
		}

		public bool IsValid(int position, CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            return
                IsNewConstraintContext(context, cancellationToken) ||
                context.IsAnyExpressionContext ||
                context.IsStatementContext ||
                context.IsGlobalStatementContext ||
                IsMemberDeclarationContext(context, cancellationToken) ||
                IsTypeDeclarationContext(context, cancellationToken);
        }

        private bool IsTypeDeclarationContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            if (context.IsTypeDeclarationContext(validModifiers: ValidTypeModifiers, validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations, canBePartial: false, cancellationToken: cancellationToken))
            {
                // we must be on a nested type.
                var token = context.LeftToken;
                return token.GetAncestors<TypeDeclarationSyntax>()
                    .Any(t => token.SpanStart > t.OpenBraceToken.Span.End &&
                              token.Span.End < t.CloseBraceToken.SpanStart);
            }

            return false;
        }

        private bool IsMemberDeclarationContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            return
                context.SyntaxTree.IsGlobalMemberDeclarationContext(context.Position, SyntaxKindSet.AllGlobalMemberModifiers, cancellationToken) ||
                context.IsMemberDeclarationContext(
                    validModifiers: s_validMemberModifiers,
                    validTypeDeclarations: SyntaxKindSet.ClassInterfaceStructTypeDeclarations,
                    canBePartial: false,
                    cancellationToken: cancellationToken);
        }

        private static bool IsNewConstraintContext(CSharpSyntaxContext context, CancellationToken cancellationToken)
        {
            // cases:
            //    where T : |
            //    where T : class, |
            //    where T : Foo, |
            // note: 'new()' can't come after a 'struct' constraint.

            if (context.SyntaxTree.IsTypeParameterConstraintStartContext(context.Position, context.LeftToken, cancellationToken))
            {
                return true;
            }

            var token = context.TargetToken;

            if (token.Kind() == SyntaxKind.CommaToken &&
                token.Parent.IsKind(SyntaxKind.TypeParameterConstraintClause))
            {
                var constraintClause = token.Parent as TypeParameterConstraintClauseSyntax;
                if (!constraintClause.Constraints
                        .OfType<ClassOrStructConstraintSyntax>()
                        .Any(c => c.ClassOrStructKeyword.Kind() == SyntaxKind.StructKeyword))
                {
                    return true;
                }
            }

            return false;
        }
    }
}