33using System . Threading ;
44using System . Threading . Tasks ;
55using Microsoft . CodeAnalysis ;
6+ using Microsoft . CodeAnalysis . CSharp ;
67using Microsoft . CodeAnalysis . CSharp . Syntax ;
78using static Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
89using static Roslynator . CSharp . CSharpFactory ;
@@ -16,7 +17,16 @@ public static async Task<Document> RefactorAsync(
1617 TypeDeclarationSyntax typeDeclaration ,
1718 CancellationToken cancellationToken )
1819 {
19- int position = typeDeclaration . OpenBraceToken . Span . End ;
20+ TypeDeclarationSyntax newTypeDeclaration = typeDeclaration ;
21+
22+ if ( typeDeclaration . OpenBraceToken . IsKind ( SyntaxKind . None ) )
23+ {
24+ newTypeDeclaration = typeDeclaration . WithSemicolonToken ( default )
25+ . WithOpenBraceToken ( OpenBraceToken ( ) )
26+ . WithCloseBraceToken ( CloseBraceToken ( ) ) ;
27+ }
28+
29+ int position = newTypeDeclaration . Identifier . Span . End ;
2030
2131 SemanticModel semanticModel = await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
2232
@@ -27,9 +37,15 @@ public static async Task<Document> RefactorAsync(
2737 ParseName ( "System.Diagnostics.DebuggerDisplayAttribute" ) . WithSimplifierAnnotation ( ) ,
2838 AttributeArgument ( LiteralExpression ( $ "{{{propertyName},nq}}") ) ) ) ;
2939
30- PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration ( propertyName , InvocationExpression ( IdentifierName ( "ToString" ) ) ) ;
40+ INamedTypeSymbol typeSymbol = semanticModel . GetDeclaredSymbol ( typeDeclaration , cancellationToken ) ! ;
41+
42+ ExpressionSyntax returnExpression = ( typeSymbol . TypeKind == TypeKind . Struct && typeSymbol . IsRefLikeType )
43+ ? StringLiteralExpression ( "" )
44+ : InvocationExpression ( IdentifierName ( "ToString" ) ) ;
45+
46+ PropertyDeclarationSyntax propertyDeclaration = DebuggerDisplayPropertyDeclaration ( propertyName , returnExpression ) ;
3147
32- TypeDeclarationSyntax newTypeDeclaration = SyntaxRefactorings . AddAttributeLists ( typeDeclaration , keepDocumentationCommentOnTop : true , attributeList ) ;
48+ newTypeDeclaration = SyntaxRefactorings . AddAttributeLists ( newTypeDeclaration , keepDocumentationCommentOnTop : true , attributeList ) ;
3349
3450 newTypeDeclaration = MemberDeclarationInserter . Default . Insert ( newTypeDeclaration , propertyDeclaration ) ;
3551
0 commit comments