private static async Task <Document> RefactorAsync(
            Document document,
            AttributeSyntax attribute,
            CancellationToken cancellationToken)
        {
            TypeDeclarationSyntax typeDeclaration = attribute.FirstAncestor <TypeDeclarationSyntax>();

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            string propertyName = NameGenerator.Default.EnsureUniqueName(DefaultNames.DebuggerDisplayPropertyName, semanticModel, typeDeclaration.OpenBraceToken.Span.End);

            AttributeArgumentSyntax argument = attribute.ArgumentList.Arguments.First();

            TypeDeclarationSyntax newTypeDeclaration = typeDeclaration.ReplaceNode(
                argument,
                argument.WithExpression(
                    StringLiteralExpression($"{{{propertyName},nq}}")).WithTriviaFrom(argument.Expression));

            string value = semanticModel
                           .GetDeclaredSymbol(typeDeclaration, cancellationToken)
                           .GetAttribute(MetadataNames.System_Diagnostics_DebuggerDisplayAttribute)
                           .ConstructorArguments[0]
                           .Value
                           .ToString();

            bool isVerbatim = SyntaxInfo.StringLiteralExpressionInfo(argument.Expression).IsVerbatim;

            ExpressionSyntax returnExpression = GetReturnExpression(value, isVerbatim);

            PropertyDeclarationSyntax propertyDeclaration = MarkTypeWithDebuggerDisplayAttributeRefactoring.DebuggerDisplayPropertyDeclaration(propertyName, returnExpression);

            newTypeDeclaration = MemberDeclarationInserter.Default.Insert(newTypeDeclaration, propertyDeclaration);

            return(await document.ReplaceNodeAsync(typeDeclaration, newTypeDeclaration, cancellationToken).ConfigureAwait(false));
        }
Example #2
0
        internal static bool TryGetTearDownMethod(AttributeSyntax setupAttribute, SemanticModel semanticModel, CancellationToken cancellationToken, out MethodDeclarationSyntax result)
        {
            result = null;
            var typeDeclarationSyntax = setupAttribute.FirstAncestor <TypeDeclarationSyntax>();

            if (typeDeclarationSyntax == null)
            {
                return(false);
            }

            var attributeType = semanticModel.GetTypeInfoSafe(setupAttribute, cancellationToken).Type;

            var teardOwnAttributeType = attributeType == KnownSymbol.NUnitSetUpAttribute
                ? KnownSymbol.NUnitTearDownAttribute
                : KnownSymbol.NUnitOneTimeTearDownAttribute;

            foreach (var member in typeDeclarationSyntax.Members)
            {
                if (member is MethodDeclarationSyntax methodDeclaration)
                {
                    if (Attribute.TryFind(methodDeclaration, teardOwnAttributeType, semanticModel, cancellationToken, out _))
                    {
                        result = methodDeclaration;
                        return(true);
                    }
                }
            }

            return(false);
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            AttributeSyntax attribute,
            CancellationToken cancellationToken)
        {
            TypeDeclarationSyntax typeDeclaration = attribute.FirstAncestor <TypeDeclarationSyntax>();

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            string propertyName = NameGenerator.Default.EnsureUniqueMemberName(PropertyName, semanticModel, typeDeclaration.OpenBraceToken.Span.End, cancellationToken: cancellationToken);

            AttributeArgumentSyntax argument = attribute.ArgumentList.Arguments.First();

            TypeDeclarationSyntax newTypeDeclaration = typeDeclaration.ReplaceNode(
                argument,
                argument.WithExpression(
                    StringLiteralExpression($"{{{propertyName},nq}}")).WithTriviaFrom(argument.Expression));

            string value = semanticModel
                           .GetDeclaredSymbol(typeDeclaration, cancellationToken)
                           .GetAttribute(semanticModel.GetTypeByMetadataName(MetadataNames.System_Diagnostics_DebuggerDisplayAttribute))
                           .ConstructorArguments[0]
                           .Value
                           .ToString();

            ExpressionSyntax returnExpression = GetReturnExpression(value, SyntaxInfo.StringLiteralExpressionInfo(argument.Expression).IsVerbatim);

            PropertyDeclarationSyntax propertyDeclaration = PropertyDeclaration(
                SingletonList(
                    AttributeList(
                        Attribute(
                            ParseName("System.Diagnostics.DebuggerBrowsableAttribute"),
                            AttributeArgument(
                                SimpleMemberAccessExpression(
                                    ParseName("System.Diagnostics.DebuggerBrowsableState").WithSimplifierAnnotation(),
                                    IdentifierName("Never"))
                                )
                            ).WithSimplifierAnnotation()
                        )
                    ),
                Modifiers.Private(),
                CSharpTypeFactory.StringType(),
                default(ExplicitInterfaceSpecifierSyntax),
                Identifier(propertyName).WithRenameAnnotation(),
                AccessorList(
                    GetAccessorDeclaration(
                        Block(
                            ReturnStatement(returnExpression)))));

            propertyDeclaration = propertyDeclaration.WithFormatterAnnotation();

            newTypeDeclaration = MemberDeclarationInserter.Default.Insert(newTypeDeclaration, propertyDeclaration);

            return(await document.ReplaceNodeAsync(typeDeclaration, newTypeDeclaration, cancellationToken).ConfigureAwait(false));
        }
Example #4
0
        private static Task <Document> SpecifyNameAsync(
            Document document,
            AttributeSyntax attribute,
            CancellationToken cancellationToken)
        {
            AttributeArgumentSyntax newAttributeArgument = AttributeArgument(
                NameEquals("Name"),
                NameOfExpression(
                    IdentifierName(attribute.FirstAncestor <ClassDeclarationSyntax>().Identifier.WithNavigationAnnotation())));

            AttributeArgumentListSyntax argumentList = attribute.ArgumentList;

            SeparatedSyntaxList <AttributeArgumentSyntax> arguments = argumentList.Arguments.Add(newAttributeArgument);

            AttributeArgumentListSyntax newArgumentList = argumentList.WithArguments(arguments);

            AttributeSyntax newAttribute = attribute.WithArgumentList(newArgumentList);

            return(document.ReplaceNodeAsync(attribute, newAttribute, cancellationToken));
        }