public static PropertyDeclarationSyntax DebuggerDisplayPropertyDeclaration(string name, ExpressionSyntax returnExpression) { return(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(name).WithRenameAnnotation(), AccessorList( GetAccessorDeclaration( Block( ReturnStatement(returnExpression)) ) ) ).WithFormatterAnnotation()); }
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)); }
private static Task <Document> UseStringEmptyInsteadOfEmptyStringLiteralAsync( Document document, ExpressionSyntax expression, CancellationToken cancellationToken = default) { MemberAccessExpressionSyntax memberAccessExpression = CSharpFactory.SimpleMemberAccessExpression( CSharpTypeFactory.StringType(), SyntaxFactory.IdentifierName("Empty")); memberAccessExpression = memberAccessExpression.WithTriviaFrom(expression); return(document.ReplaceNodeAsync(expression, memberAccessExpression, cancellationToken)); }
private static async Task <Document> UseStringIsNullOrEmptyMethodAsync( Document document, BinaryExpressionSyntax binaryExpression, CancellationToken cancellationToken) { if (binaryExpression.IsKind(SyntaxKind.EqualsExpression)) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); BinaryExpressionInfo binaryExpressionInfo = SyntaxInfo.BinaryExpressionInfo(binaryExpression); ExpressionSyntax expression = (CSharpUtility.IsEmptyStringExpression(binaryExpressionInfo.Left, semanticModel, cancellationToken)) ? binaryExpressionInfo.Right : binaryExpressionInfo.Left; ExpressionSyntax newNode = SimpleMemberInvocationExpression( CSharpTypeFactory.StringType(), IdentifierName("IsNullOrEmpty"), Argument(expression)); newNode = newNode .WithTriviaFrom(binaryExpression) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken).ConfigureAwait(false)); } else { NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(binaryExpression.Left); ExpressionSyntax newNode = SimpleMemberInvocationExpression( CSharpTypeFactory.StringType(), IdentifierName("IsNullOrEmpty"), Argument(nullCheck.Expression)); if (nullCheck.IsCheckingNotNull) { newNode = LogicalNotExpression(newNode); } newNode = newNode .WithTriviaFrom(binaryExpression) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken).ConfigureAwait(false)); } }
private static Task <Document> UseStringIsNullOrEmptyMethodAsync( Document document, BinaryExpressionSyntax binaryExpression, CancellationToken cancellationToken) { NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(binaryExpression.Left); ExpressionSyntax newNode = SimpleMemberInvocationExpression( CSharpTypeFactory.StringType(), IdentifierName("IsNullOrEmpty"), Argument(nullCheck.Expression)); if (nullCheck.IsCheckingNotNull) { newNode = LogicalNotExpression(newNode); } newNode = newNode .WithTriviaFrom(binaryExpression) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken)); }
private static Task <Document> RefactorAsync( Document document, InterpolatedStringExpressionSyntax interpolatedString, CancellationToken cancellationToken) { StringBuilder sb = StringBuilderCache.GetInstance(); var b = new SyntaxNodeTextBuilder(interpolatedString, sb); var arguments = new List <ArgumentSyntax>() { null }; if (interpolatedString.IsVerbatim()) { b.Append("@"); } b.Append("\""); int index = 0; foreach (InterpolatedStringContentSyntax content in interpolatedString.Contents) { switch (content.Kind()) { case SyntaxKind.Interpolation: { var interpolation = (InterpolationSyntax)content; b.Append("{"); b.Append(index.ToString(CultureInfo.InvariantCulture)); index++; InterpolationAlignmentClauseSyntax alignmentClause = interpolation.AlignmentClause; if (alignmentClause != null) { b.Append(","); b.AppendSpan(alignmentClause.Value); } InterpolationFormatClauseSyntax formatClause = interpolation.FormatClause; if (formatClause != null) { b.Append(":"); b.AppendSpan(formatClause.FormatStringToken); } b.Append("}"); arguments.Add(Argument(interpolation.Expression)); break; } case SyntaxKind.InterpolatedStringText: { b.AppendSpan(content); break; } } } b.Append("\""); arguments[0] = Argument(ParseExpression(StringBuilderCache.GetStringAndFree(sb))); InvocationExpressionSyntax invocation = SimpleMemberInvocationExpression( CSharpTypeFactory.StringType(), IdentifierName("Format"), ArgumentList(SeparatedList(arguments))); invocation = invocation.WithTriviaFrom(interpolatedString).WithFormatterAnnotation(); return(document.ReplaceNodeAsync(interpolatedString, invocation, cancellationToken)); }