|
| 1 | +using System.Collections.Generic; |
1 | 2 | using System.Linq; |
2 | 3 | using System.Text; |
3 | 4 | using Microsoft.CodeAnalysis; |
@@ -206,35 +207,72 @@ private static string GenerateSource( |
206 | 207 | sb.AppendLine(); |
207 | 208 | } |
208 | 209 |
|
209 | | - sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' '); |
210 | | - if (type.IsStatic) |
211 | | - sb.Append("static "); |
212 | | - else if (type.IsAbstract && type.TypeKind == TypeKind.Class) |
213 | | - sb.Append("abstract "); |
214 | | - else if (type.IsSealed && type.TypeKind == TypeKind.Class) |
215 | | - sb.Append("sealed "); |
216 | | - sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name).AppendLine(); |
217 | | - sb.AppendLine("{"); |
218 | | - sb.Append(" public static global::PatternKit.Cloud.Ambassador.Ambassador<") |
| 210 | + var containingTypes = GetContainingTypes(type); |
| 211 | + var indentLevel = 0; |
| 212 | + foreach (var containingType in containingTypes) |
| 213 | + { |
| 214 | + AppendTypeDeclaration(sb, containingType, indentLevel); |
| 215 | + sb.AppendLine(); |
| 216 | + sb.AppendLine(new string(' ', indentLevel * 4) + "{"); |
| 217 | + indentLevel++; |
| 218 | + } |
| 219 | + |
| 220 | + AppendTypeDeclaration(sb, type, indentLevel); |
| 221 | + sb.AppendLine(); |
| 222 | + var indent = new string(' ', indentLevel * 4); |
| 223 | + sb.AppendLine(indent + "{"); |
| 224 | + var memberIndent = indent + " "; |
| 225 | + var bodyIndent = memberIndent + " "; |
| 226 | + var chainIndent = bodyIndent + " "; |
| 227 | + sb.Append(memberIndent).Append("public static global::PatternKit.Cloud.Ambassador.Ambassador<") |
219 | 228 | .Append(requestTypeName).Append(", ").Append(responseTypeName).Append("> ").Append(factoryMethodName).AppendLine("()"); |
220 | | - sb.AppendLine(" {"); |
221 | | - sb.Append(" return global::PatternKit.Cloud.Ambassador.Ambassador<") |
| 229 | + sb.Append(memberIndent).AppendLine("{"); |
| 230 | + sb.Append(bodyIndent).Append("return global::PatternKit.Cloud.Ambassador.Ambassador<") |
222 | 231 | .Append(requestTypeName).Append(", ").Append(responseTypeName).Append(">.Create(\"").Append(Escape(ambassadorName)).AppendLine("\")"); |
223 | 232 | foreach (var transform in transforms) |
224 | | - sb.Append(" .Transform(").Append(transform.Name).AppendLine(")"); |
| 233 | + sb.Append(chainIndent).Append(".Transform(").Append(transform.Name).AppendLine(")"); |
225 | 234 | if (policyName is not null) |
226 | | - sb.Append(" .ConnectionPolicy(").Append(policyName).AppendLine(")"); |
| 235 | + sb.Append(chainIndent).Append(".ConnectionPolicy(").Append(policyName).AppendLine(")"); |
227 | 236 | foreach (var item in telemetry) |
228 | | - sb.Append(" .Telemetry(\"").Append(Escape(item.Name)).Append("\", ").Append(item.Method.Name).AppendLine(")"); |
229 | | - sb.Append(" .Call(").Append(callName).AppendLine(")"); |
| 237 | + sb.Append(chainIndent).Append(".Telemetry(\"").Append(Escape(item.Name)).Append("\", ").Append(item.Method.Name).AppendLine(")"); |
| 238 | + sb.Append(chainIndent).Append(".Call(").Append(callName).AppendLine(")"); |
230 | 239 | if (fallbackName is not null) |
231 | | - sb.Append(" .Fallback(").Append(fallbackName).AppendLine(")"); |
232 | | - sb.AppendLine(" .Build();"); |
233 | | - sb.AppendLine(" }"); |
234 | | - sb.AppendLine("}"); |
| 240 | + sb.Append(chainIndent).Append(".Fallback(").Append(fallbackName).AppendLine(")"); |
| 241 | + sb.Append(chainIndent).AppendLine(".Build();"); |
| 242 | + sb.Append(memberIndent).AppendLine("}"); |
| 243 | + sb.AppendLine(indent + "}"); |
| 244 | + for (var i = containingTypes.Length - 1; i >= 0; i--) |
| 245 | + { |
| 246 | + sb.AppendLine(new string(' ', i * 4) + "}"); |
| 247 | + } |
| 248 | + |
235 | 249 | return sb.ToString(); |
236 | 250 | } |
237 | 251 |
|
| 252 | + private static INamedTypeSymbol[] GetContainingTypes(INamedTypeSymbol type) |
| 253 | + { |
| 254 | + var containingTypes = new Stack<INamedTypeSymbol>(); |
| 255 | + for (var current = type.ContainingType; current is not null; current = current.ContainingType) |
| 256 | + { |
| 257 | + containingTypes.Push(current); |
| 258 | + } |
| 259 | + |
| 260 | + return containingTypes.ToArray(); |
| 261 | + } |
| 262 | + |
| 263 | + private static void AppendTypeDeclaration(StringBuilder sb, INamedTypeSymbol type, int indentLevel) |
| 264 | + { |
| 265 | + sb.Append(new string(' ', indentLevel * 4)); |
| 266 | + sb.Append(GetAccessibility(type.DeclaredAccessibility)).Append(' '); |
| 267 | + if (type.IsStatic) |
| 268 | + sb.Append("static "); |
| 269 | + else if (type.IsAbstract && type.TypeKind == TypeKind.Class) |
| 270 | + sb.Append("abstract "); |
| 271 | + else if (type.IsSealed && type.TypeKind == TypeKind.Class) |
| 272 | + sb.Append("sealed "); |
| 273 | + sb.Append("partial ").Append(type.TypeKind == TypeKind.Struct ? "struct" : "class").Append(' ').Append(type.Name); |
| 274 | + } |
| 275 | + |
238 | 276 | private static string? GetNamedString(AttributeData attribute, string name) |
239 | 277 | => attribute.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value as string; |
240 | 278 |
|
|
0 commit comments