11using Microsoft . CodeAnalysis ;
22using System . Collections . Generic ;
3+ using System . Collections . Immutable ;
34using System . Linq ;
45using System . Text ;
56
@@ -18,7 +19,7 @@ internal static string GenerateWrapperTypeSource(StringBuilder source, INamedTyp
1819 source . AppendLine ( """namespace Bunit.Web.AngleSharp;""" ) ;
1920 source . AppendLine ( ) ;
2021 source . AppendLine ( "/// <inheritdoc/>" ) ;
21- source . AppendLine ( """ [System.Diagnostics.DebuggerDisplay("{OuterHtml,nq}")]"" " ) ;
22+ source . AppendLine ( "[System.Diagnostics.DebuggerDisplay(\ " {OuterHtml,nq}\ " )]" ) ;
2223 source . AppendLine ( "[System.Diagnostics.DebuggerNonUserCode]" ) ;
2324 source . AppendLine ( "[System.CodeDom.Compiler.GeneratedCodeAttribute(\" Bunit.Web.AngleSharp\" , \" 1.0.0.0\" )]" ) ;
2425 source . AppendLine ( $ "internal sealed class { name } : WrapperBase<{ wrappedTypeName } >, { wrappedTypeName } ") ;
@@ -60,64 +61,59 @@ internal static string GenerateWrapperTypeSource(StringBuilder source, INamedTyp
6061
6162 private static void GenerateOrdinaryMethod ( StringBuilder source , IMethodSymbol method )
6263 {
63- // Determine the return type of the method
64- string returnType = method . ReturnType . ToDisplayString ( GeneratorConfig . SymbolFormat ) ;
65-
66- // Start building the method signature
67- source . AppendLine ( ) ;
68- source . AppendLine ( "\t /// <inheritdoc/>" ) ;
69- source . AppendLine ( "\t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
70- source . AppendLine ( "\t [System.Diagnostics.DebuggerHidden]" ) ;
71- source . AppendLine ( "\t [System.Diagnostics.DebuggerStepThrough]" ) ;
72- source . Append ( $ "\t public { returnType } { method . Name } (") ;
73-
74- // Append parameters directly to the StringBuilder using a for loop
75- var parameters = method . Parameters ;
76- for ( int i = 0 ; i < parameters . Length ; i ++ )
64+ var methodParts = method . ToDisplayParts ( GeneratorConfig . SymbolFormat ) ;
65+
66+ // It seems that the ToDisplayParts will return ...
67+ //
68+ // public global::AngleSharp.Dom.IShadowRoot AttachShadow(global::AngleSharp.Dom.ShadowRootMode mode = 0)
69+ //
70+ // when called on a method with a default enum parameters specified.
71+ // However, the C# compiler changes this afterword to ...
72+ //
73+ // public global::AngleSharp.Dom.IShadowRoot AttachShadow(global::AngleSharp.Dom.ShadowRootMode mode = Open)
74+ //
75+ // which doesn't compile.
76+ // So this entire IF is there to fix this, and instead generate ...
77+ //
78+ // public global::AngleSharp.Dom.IShadowRoot AttachShadow(global::AngleSharp.Dom.ShadowRootMode mode = global::AngleSharp.Dom.ShadowRootMode.Open)
79+ if ( method . Parameters . SingleOrDefault ( x => x . HasExplicitDefaultValue && x . Type . BaseType ? . ToString ( ) == "System.Enum" ) is IParameterSymbol parameter )
7780 {
78- if ( i > 0 )
79- source . Append ( ", " ) ;
80-
81- source . Append ( $ "{ parameters [ i ] . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } { parameters [ i ] . Name } ") ;
81+ var defaultValue = parameter . Type
82+ . GetMembers ( )
83+ . OfType < IFieldSymbol > ( )
84+ . Single ( x => x . ConstantValue == parameter . ExplicitDefaultValue ) ;
85+
86+ methodParts = methodParts [ 0 ..^ 2 ]
87+ . AddRange ( methodParts [ 0 ..2 ] )
88+ . AddRange ( defaultValue . ToDisplayParts ( ) )
89+ . Add ( methodParts [ ^ 1 ] ) ;
8290 }
8391
84- // Complete the method signature and start the method body
85- source . Append ( $ ") => WrappedElement.{ method . Name } (") ;
86-
87- // Append method invocation parameters using a for loop
88- for ( int i = 0 ; i < parameters . Length ; i ++ )
89- {
90- if ( i > 0 )
91- source . Append ( ", " ) ;
92-
93- source . Append ( parameters [ i ] . Name ) ;
94- }
95-
96- // Close the method invocation
97- source . AppendLine ( ");" ) ;
92+ source . AppendLine ( ) ;
93+ source . AppendInheritDoc ( ) ;
94+ source . AppendDefaultAttributes ( "\t " ) ;
95+ source . Append ( "\t public " ) . AppendLine ( methodParts . ToDisplayString ( ) ) ;
96+ source . Append ( $ "\t \t => WrappedElement.{ method . Name } (")
97+ . AppendCallParameters ( method . Parameters )
98+ . AppendLine ( ");" ) ;
9899 }
99100
100101 private static void GenerateRegularProperty ( StringBuilder source , IPropertySymbol property )
101102 {
102103 source . AppendLine ( ) ;
103- source . AppendLine ( "\t /// <inheritdoc/>" ) ;
104- source . Append ( $ "\t public { property . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } { property . Name } ") ;
105- source . AppendLine ( ) ;
104+ source . AppendInheritDoc ( ) ;
105+ source . Append ( "\t public " ) . AppendLine ( property . ToDisplayString ( GeneratorConfig . SymbolFormat ) ) ;
106106 source . AppendLine ( "\t {" ) ;
107107
108108 if ( property . GetMethod is IMethodSymbol )
109109 {
110- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerHidden]" ) ;
111- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerStepThrough]" ) ;
112- source . AppendLine ( "\t \t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
110+ source . AppendDefaultAttributes ( ) ;
113111 source . AppendLine ( $ "\t \t get => WrappedElement.{ property . Name } ;") ;
114112 }
115113
116114 if ( property . SetMethod is IMethodSymbol )
117115 {
118- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerHidden]" ) ;
119- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerStepThrough]" ) ;
120- source . AppendLine ( "\t \t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
116+ source . AppendDefaultAttributes ( ) ;
121117 source . AppendLine ( $ "\t \t set => WrappedElement.{ property . Name } = value;") ;
122118 }
123119
@@ -127,55 +123,32 @@ private static void GenerateRegularProperty(StringBuilder source, IPropertySymbo
127123 private static void GenerateIndexerProperty ( StringBuilder source , IPropertySymbol property )
128124 {
129125 source . AppendLine ( ) ;
130- source . AppendLine ( "\t /// <inheritdoc/>" ) ;
131- source . Append ( $ "\t public { property . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } this[") ;
132-
133- foreach ( var p in property . Parameters )
134- {
135- source . Append ( $ "{ p . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } { p . Name } ") ;
136- }
137-
138- source . Append ( "]" ) ;
139- source . AppendLine ( ) ;
126+ source . AppendInheritDoc ( ) ;
127+ source . Append ( "\t public " ) . AppendLine ( property . ToDisplayString ( GeneratorConfig . SymbolFormat ) ) ;
140128 source . AppendLine ( "\t {" ) ;
141129
142130 if ( property . GetMethod is IMethodSymbol )
143131 {
144- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerHidden]" ) ;
145- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerStepThrough]" ) ;
146- source . AppendLine ( "\t \t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
132+ source . AppendDefaultAttributes ( ) ;
147133 source . Append ( "\t \t get => WrappedElement[" ) ;
148- PrintCallParameters ( ) ;
134+ source . AppendCallParameters ( property . Parameters ) ;
149135 source . AppendLine ( "];" ) ;
150136 }
151137
152138 if ( property . SetMethod is IMethodSymbol )
153139 {
154- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerHidden]" ) ;
155- source . AppendLine ( "\t \t [System.Diagnostics.DebuggerStepThrough]" ) ;
156- source . AppendLine ( "\t \t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
140+ source . AppendDefaultAttributes ( ) ;
157141 source . Append ( "\t \t set => WrappedElement[" ) ;
158- PrintCallParameters ( ) ;
142+ source . AppendCallParameters ( property . Parameters ) ;
159143 source . AppendLine ( "] = value;" ) ;
160144 }
161145 source . AppendLine ( "\t }" ) ;
162-
163- void PrintCallParameters ( )
164- {
165- for ( int i = 0 ; i < property . Parameters . Length ; i ++ )
166- {
167- if ( i > 0 )
168- source . Append ( ", " ) ;
169-
170- source . Append ( property . Parameters [ i ] . Name ) ;
171- }
172- }
173146 }
174147
175148 private static void GenerateEventProperty ( StringBuilder source , IEventSymbol eventSymbol )
176149 {
177150 source . AppendLine ( ) ;
178- source . AppendLine ( " \t /// <inheritdoc/>" ) ;
151+ source . AppendInheritDoc ( ) ;
179152 source . AppendLine ( $ "\t public event { eventSymbol . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } { eventSymbol . Name } ") ;
180153 source . AppendLine ( "\t {" ) ;
181154 source . AppendLine ( "\t \t [System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
@@ -200,6 +173,44 @@ private static IEnumerable<ISymbol> GetAllMembers(this INamedTypeSymbol symbol)
200173 }
201174 }
202175
176+ private static StringBuilder AppendInputArguments ( this StringBuilder source , ImmutableArray < IParameterSymbol > parameters )
177+ {
178+ for ( int i = 0 ; i < parameters . Length ; i ++ )
179+ {
180+ if ( i > 0 )
181+ source . Append ( ", " ) ;
182+
183+ source . Append ( $ "{ parameters [ i ] . Type . ToDisplayString ( GeneratorConfig . SymbolFormat ) } { parameters [ i ] . Name } ") ;
184+ }
185+ return source ;
186+ }
187+
188+ private static StringBuilder AppendCallParameters ( this StringBuilder source , ImmutableArray < IParameterSymbol > parameters )
189+ {
190+ for ( int i = 0 ; i < parameters . Length ; i ++ )
191+ {
192+ if ( i > 0 )
193+ source . Append ( ", " ) ;
194+
195+ source . Append ( parameters [ i ] . Name ) ;
196+ }
197+ return source ;
198+ }
199+
200+ private static StringBuilder AppendInheritDoc ( this StringBuilder source )
201+ {
202+ source . AppendLine ( "\t /// <inheritdoc/>" ) ;
203+ return source ;
204+ }
205+
206+ private static StringBuilder AppendDefaultAttributes ( this StringBuilder source , string tabs = "\t \t " )
207+ {
208+ source . Append ( tabs ) . AppendLine ( "[System.Diagnostics.DebuggerHidden]" ) ;
209+ source . Append ( tabs ) . AppendLine ( "[System.Diagnostics.DebuggerStepThrough]" ) ;
210+ source . Append ( tabs ) . AppendLine ( "[System.Runtime.CompilerServices.MethodImpl(MethodImplOptions.AggressiveInlining)]" ) ;
211+ return source ;
212+ }
213+
203214 private static bool IsSpecialMethod ( string methodName )
204215 {
205216 return methodName . StartsWith ( "add_" ) || methodName . StartsWith ( "remove_" ) ||
0 commit comments