Пример #1
0
        private static SyntaxToken CreateNewIdentifierTokenFromToken(SyntaxToken originalToken, bool escape)
        {
            var isVerbatimIdentifier = originalToken.IsVerbatimIdentifier();

            if (isVerbatimIdentifier == escape)
            {
                return(originalToken);
            }

            var unescapedText = isVerbatimIdentifier ? originalToken.ToString().Substring(1) : originalToken.ToString();

            return(escape
                ? originalToken.CopyAnnotationsTo(SyntaxFactory.VerbatimIdentifier(originalToken.LeadingTrivia, unescapedText, originalToken.ValueText, originalToken.TrailingTrivia))
                : originalToken.CopyAnnotationsTo(SyntaxFactory.Identifier(originalToken.LeadingTrivia, SyntaxKind.IdentifierToken, unescapedText, originalToken.ValueText, originalToken.TrailingTrivia)));
        }
        public static SyntaxToken WithSourceMappingFrom(this SyntaxToken converted, SyntaxToken fromToken)
        {
            var origLinespan = fromToken.SyntaxTree.GetLineSpan(fromToken.Span);

            return(fromToken.CopyAnnotationsTo(converted)
                   .WithSourceStartLineAnnotation(origLinespan)
                   .WithSourceEndLineAnnotation(origLinespan));
        }
Пример #3
0
        public static SyntaxToken TryEscapeIdentifierToken(
            SyntaxToken syntaxToken,
            SyntaxNode parentOfToken
            )
        {
            // do not escape an already escaped identifier
            if (syntaxToken.IsVerbatimIdentifier())
            {
                return(syntaxToken);
            }

            if (
                SyntaxFacts.GetKeywordKind(syntaxToken.ValueText) == SyntaxKind.None &&
                SyntaxFacts.GetContextualKeywordKind(syntaxToken.ValueText) == SyntaxKind.None
                )
            {
                return(syntaxToken);
            }

            if (
                SyntaxFacts.GetContextualKeywordKind(syntaxToken.ValueText)
                == SyntaxKind.UnderscoreToken
                )
            {
                return(syntaxToken);
            }

            var parent = parentOfToken.Parent;

            if (parentOfToken is SimpleNameSyntax && parent.Kind() == SyntaxKind.XmlNameAttribute)
            {
                // do not try to escape XML name attributes
                return(syntaxToken);
            }

            // do not escape global in a namespace qualified name
            if (parent.Kind() == SyntaxKind.AliasQualifiedName && syntaxToken.ValueText == "global")
            {
                return(syntaxToken);
            }

            // safe to escape identifier
            return(syntaxToken
                   .CopyAnnotationsTo(
                       SyntaxFactory.VerbatimIdentifier(
                           syntaxToken.LeadingTrivia,
                           syntaxToken.ToString(),
                           syntaxToken.ValueText,
                           syntaxToken.TrailingTrivia
                           )
                       )
                   .WithAdditionalAnnotations(Simplifier.Annotation));
        }
Пример #4
0
        public static SyntaxToken CopyAnnotations(SyntaxToken from, SyntaxToken to)
        {
            // Because we are removing a node that may have annotations (i.e. formatting), we need
            // to copy those annotations to the new node. However, we can only copy all annotations
            // which will mean that the new node will include a ParenthesesSimplification annotation,
            // even if didn't have one before. That results in potentially removing parentheses that
            // weren't annotated by the user. To address this, we add *another* annotation to indicate
            // that the new node shouldn't be simplified. This is to work around the
            // fact that there is no way to remove an annotation from a node in the current API. If
            // that gets added, we can clean this up.

            var dontSimplifyResult = !to.HasAnnotation(Simplifier.Annotation);

            to = from.CopyAnnotationsTo(to);

            if (dontSimplifyResult)
            {
                to = to.WithAdditionalAnnotations(DontSimplifyAnnotation);
            }

            return(to);
        }
        private static bool TryReduceAttributeSuffix(
            NameSyntax name,
            SyntaxToken identifierToken,
            SemanticModel semanticModel,
            out TypeSyntax replacementNode,
            out TextSpan issueSpan,
            CancellationToken cancellationToken)
        {
            issueSpan = default(TextSpan);
            replacementNode = default(TypeSyntax);

            // we can try to remove the Attribute suffix if this is the attribute name
            if (SyntaxFacts.IsAttributeName(name))
            {
                if (name.Parent.Kind() == SyntaxKind.Attribute || name.IsRightSideOfDotOrColonColon())
                {
                    const string AttributeName = "Attribute";

                    // an attribute that should keep it (unnecessary "Attribute" suffix should be annotated with a DontSimplifyAnnotation
                    if (identifierToken.ValueText != AttributeName && identifierToken.ValueText.EndsWith(AttributeName, StringComparison.Ordinal) && !identifierToken.HasAnnotation(SimplificationHelpers.DontSimplifyAnnotation))
                    {
                        // weird. the semantic model is able to bind attribute syntax like "[as()]" although it's not valid code.
                        // so we need another check for keywords manually.
                        var newAttributeName = identifierToken.ValueText.Substring(0, identifierToken.ValueText.Length - 9);
                        if (SyntaxFacts.GetKeywordKind(newAttributeName) != SyntaxKind.None)
                        {
                            return false;
                        }

                        // if this attribute name in source contained unicode escaping, we will loose it now
                        // because there is no easy way to determine the substring from identifier->ToString() 
                        // which would be needed to pass to SyntaxFactory.Identifier
                        // The result is an unescaped unicode character in source.

                        // once we remove the Attribute suffix, we can't use an escaped identifier
                        var newIdentifierToken = identifierToken.CopyAnnotationsTo(
                            SyntaxFactory.Identifier(
                                identifierToken.LeadingTrivia,
                                newAttributeName,
                                identifierToken.TrailingTrivia));

                        replacementNode = SyntaxFactory.IdentifierName(newIdentifierToken)
                            .WithLeadingTrivia(name.GetLeadingTrivia());
                        issueSpan = new TextSpan(identifierToken.Span.End - 9, 9);

                        return true;
                    }
                }
            }

            return false;
        }
            private SyntaxToken RenameWithinToken(SyntaxToken oldToken, SyntaxToken newToken)
            {
                if (_isProcessingComplexifiedSpans ||
                    (_isProcessingTrivia == 0 &&
                    !_stringAndCommentTextSpans.Contains(oldToken.Span)))
                {
                    return newToken;
                }

                if (_isRenamingInStrings)
                {
                    if (newToken.IsKind(SyntaxKind.StringLiteralToken))
                    {
                        newToken = RenameInStringLiteral(oldToken, newToken, SyntaxFactory.Literal);
                    }
                    else if (newToken.IsKind(SyntaxKind.InterpolatedStringTextToken))
                    {
                        newToken = RenameInStringLiteral(oldToken, newToken, (leadingTrivia, text, value, trailingTrivia) =>
                            SyntaxFactory.Token(newToken.LeadingTrivia, SyntaxKind.InterpolatedStringTextToken, text, value, newToken.TrailingTrivia));
                    }
                }

                if (_isRenamingInComments)
                {
                    if (newToken.IsKind(SyntaxKind.XmlTextLiteralToken))
                    {
                        newToken = RenameInStringLiteral(oldToken, newToken, SyntaxFactory.XmlTextLiteral);
                    }
                    else if (newToken.IsKind(SyntaxKind.IdentifierToken) && newToken.Parent.IsKind(SyntaxKind.XmlName) && newToken.ValueText == _originalText)
                    {
                        var newIdentifierToken = SyntaxFactory.Identifier(newToken.LeadingTrivia, _replacementText, newToken.TrailingTrivia);
                        newToken = newToken.CopyAnnotationsTo(_renameAnnotations.WithAdditionalAnnotations(newIdentifierToken, new RenameTokenSimplificationAnnotation() { OriginalTextSpan = oldToken.Span }));
                        AddModifiedSpan(oldToken.Span, newToken.Span);
                    }

                    if (newToken.HasLeadingTrivia)
                    {
                        var updatedToken = RenameInTrivia(oldToken, oldToken.LeadingTrivia);
                        if (updatedToken != oldToken)
                        {
                            newToken = newToken.WithLeadingTrivia(updatedToken.LeadingTrivia);
                        }
                    }

                    if (newToken.HasTrailingTrivia)
                    {
                        var updatedToken = RenameInTrivia(oldToken, oldToken.TrailingTrivia);
                        if (updatedToken != oldToken)
                        {
                            newToken = newToken.WithTrailingTrivia(updatedToken.TrailingTrivia);
                        }
                    }
                }

                return newToken;
            }
            private SyntaxToken RenameInStringLiteral(SyntaxToken oldToken, SyntaxToken newToken, Func<SyntaxTriviaList, string, string, SyntaxTriviaList, SyntaxToken> createNewStringLiteral)
            {
                var originalString = newToken.ToString();
                string replacedString = RenameLocations.ReferenceProcessing.ReplaceMatchingSubStrings(originalString, _originalText, _replacementText);
                if (replacedString != originalString)
                {
                    var oldSpan = oldToken.Span;
                    newToken = createNewStringLiteral(newToken.LeadingTrivia, replacedString, replacedString, newToken.TrailingTrivia);
                    AddModifiedSpan(oldSpan, newToken.Span);
                    return newToken.CopyAnnotationsTo(_renameAnnotations.WithAdditionalAnnotations(newToken, new RenameTokenSimplificationAnnotation() { OriginalTextSpan = oldSpan }));
                }

                return newToken;
            }
            private SyntaxToken RenameToken(SyntaxToken oldToken, SyntaxToken newToken, string prefix, string suffix)
            {
                var parent = oldToken.Parent;
                string currentNewIdentifier = _isVerbatim ? _replacementText.Substring(1) : _replacementText;
                var oldIdentifier = newToken.ValueText;
                var isAttributeName = SyntaxFacts.IsAttributeName(parent);

                if (isAttributeName)
                {
                    Debug.Assert(_renamedSymbol.IsAttribute() || _aliasSymbol.Target.IsAttribute());
                    if (oldIdentifier != _renamedSymbol.Name)
                    {
                        string withoutSuffix;
                        if (currentNewIdentifier.TryGetWithoutAttributeSuffix(out withoutSuffix))
                        {
                            currentNewIdentifier = withoutSuffix;
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(prefix))
                    {
                        currentNewIdentifier = prefix + currentNewIdentifier;
                    }

                    if (!string.IsNullOrEmpty(suffix))
                    {
                        currentNewIdentifier = currentNewIdentifier + suffix;
                    }
                }

                // determine the canonical identifier name (unescaped, no unicode escaping, ...)
                string valueText = currentNewIdentifier;
                var kind = SyntaxFacts.GetKeywordKind(currentNewIdentifier);
                if (kind != SyntaxKind.None)
                {
                    valueText = SyntaxFacts.GetText(kind);
                }
                else
                {
                    var parsedIdentifier = SyntaxFactory.ParseName(currentNewIdentifier);
                    if (parsedIdentifier.IsKind(SyntaxKind.IdentifierName))
                    {
                        valueText = ((IdentifierNameSyntax)parsedIdentifier).Identifier.ValueText;
                    }
                }

                // TODO: we can't use escaped unicode characters in xml doc comments, so we need to pass the valuetext as text as well.
                // <param name="\u... is invalid.

                // if it's an attribute name we don't mess with the escaping because it might change overload resolution
                newToken = _isVerbatim || (isAttributeName && oldToken.IsVerbatimIdentifier())
                    ? newToken = newToken.CopyAnnotationsTo(SyntaxFactory.VerbatimIdentifier(newToken.LeadingTrivia, currentNewIdentifier, valueText, newToken.TrailingTrivia))
                    : newToken = newToken.CopyAnnotationsTo(SyntaxFactory.Identifier(newToken.LeadingTrivia, SyntaxKind.IdentifierToken, currentNewIdentifier, valueText, newToken.TrailingTrivia));

                if (_replacementTextValid)
                {
                    if (newToken.IsVerbatimIdentifier())
                    {
                        // a reference location should always be tried to be unescaped, whether it was escaped before rename 
                        // or the replacement itself is escaped.
                        newToken = newToken.WithAdditionalAnnotations(Simplifier.Annotation);
                    }
                    else
                    {
                        var semanticModel = GetSemanticModelForNode(parent, _speculativeModel ?? _semanticModel);
                        newToken = Simplification.CSharpSimplificationService.TryEscapeIdentifierToken(newToken, parent, semanticModel);
                    }
                }

                return newToken;
            }
Пример #9
0
        private SyntaxToken CreateNewIdentifierTokenFromToken(SyntaxToken originalToken, bool escape)
        {
            var isVerbatimIdentifier = originalToken.IsVerbatimIdentifier();
            if (isVerbatimIdentifier == escape)
            {
                return originalToken;
            }

            var unescapedText = isVerbatimIdentifier ? originalToken.ToString().Substring(1) : originalToken.ToString();

            return escape
                ? originalToken.CopyAnnotationsTo(SyntaxFactory.VerbatimIdentifier(originalToken.LeadingTrivia, unescapedText, originalToken.ValueText, originalToken.TrailingTrivia))
                : originalToken.CopyAnnotationsTo(SyntaxFactory.Identifier(originalToken.LeadingTrivia, SyntaxKind.IdentifierToken, unescapedText, originalToken.ValueText, originalToken.TrailingTrivia));
        }