Exemple #1
0
        public static bool TryReduceOrSimplifyQualifiedCref(
            this QualifiedCrefSyntax crefSyntax, SemanticModel semanticModel,
            CrefSyntax replacement, out CrefSyntax replacementNode, out TextSpan issueSpan,
            CancellationToken cancellationToken)
        {
            var oldSymbol = semanticModel.GetSymbolInfo(crefSyntax, cancellationToken).Symbol;

            if (oldSymbol != null)
            {
                var speculativeBindingOption = SpeculativeBindingOption.BindAsExpression;
                if (oldSymbol is INamespaceOrTypeSymbol)
                {
                    speculativeBindingOption = SpeculativeBindingOption.BindAsTypeOrNamespace;
                }

                var newSymbol = semanticModel.GetSpeculativeSymbolInfo(crefSyntax.SpanStart, replacement, speculativeBindingOption).Symbol;

                if (Equals(newSymbol, oldSymbol))
                {
                    // Copy Trivia and Annotations
                    replacement     = replacement.WithLeadingTrivia(crefSyntax.GetLeadingTrivia());
                    replacement     = crefSyntax.CopyAnnotationsTo(replacement);
                    issueSpan       = crefSyntax.Container.Span;
                    replacementNode = replacement;
                    return(true);
                }
            }

            replacementNode = default;
            issueSpan       = default;
            return(false);
        }
Exemple #2
0
        private static TypeCrefSyntax CreateReplacement(QualifiedCrefSyntax crefSyntax, SyntaxKind keywordKind)
        {
            var annotation = new SyntaxAnnotation(nameof(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess));
            var token      = Token(crefSyntax.GetLeadingTrivia(), keywordKind, crefSyntax.GetTrailingTrivia());

            return(TypeCref(PredefinedType(token)).WithAdditionalAnnotations(annotation));
        }