|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using Microsoft.CodeAnalysis; |
5 | 6 |
|
6 | 7 | namespace System.ClientModel.SourceGeneration; |
7 | 8 |
|
8 | 9 | internal sealed class TypeRef : IEquatable<TypeRef> |
9 | 10 | { |
10 | | - public TypeRef(string name, string nameSpace, string assembly, TypeRef? itemType = default, int arrayRank = 0) |
| 11 | + public TypeRef(string name, string nameSpace, string assembly, TypeRef? itemType = default, int arrayRank = 0, string? alias = null) |
11 | 12 | { |
12 | 13 | Name = name; |
13 | 14 | Namespace = nameSpace; |
14 | 15 | ItemType = itemType; |
15 | 16 | Assembly = assembly; |
16 | 17 | ArrayRank = arrayRank; |
| 18 | + Alias = alias; |
17 | 19 | } |
18 | 20 |
|
19 | 21 | public string Name { get; } |
20 | 22 | public string Namespace { get; } |
21 | 23 | public TypeRef? ItemType { get; } |
22 | 24 | public string Assembly { get; } |
23 | 25 | public int ArrayRank { get; } |
| 26 | + public string? Alias { get; } |
24 | 27 |
|
25 | | - internal static TypeRef FromINamedTypeSymbol(ITypeSymbol symbol, TypeSymbolKindCache symbolToKindCache) |
| 28 | + private string? _typeCaseName; |
| 29 | + public string TypeCaseName => _typeCaseName ??= Name.ToIdentifier(false); |
| 30 | + |
| 31 | + private string? _camelCaseName; |
| 32 | + public string CamelCaseName => _camelCaseName ??= TypeCaseName.ToCamelCase(); |
| 33 | + |
| 34 | + private string? _typeCaseAlias; |
| 35 | + public string? TypeCaseAlias => _typeCaseAlias ??= Alias?.ToIdentifier(false); |
| 36 | + |
| 37 | + private string? _camelCaseAlias; |
| 38 | + public string? CamelCaseAlias => _camelCaseAlias ??= TypeCaseAlias?.ToCamelCase(); |
| 39 | + |
| 40 | + internal static TypeRef FromTypeSymbol(ITypeSymbol symbol, TypeSymbolKindCache symbolToKindCache, Dictionary<ITypeSymbol, string> dupes) |
26 | 41 | { |
| 42 | + dupes.TryGetValue(symbol, out string alias); |
27 | 43 | if (symbol is INamedTypeSymbol namedTypeSymbol) |
28 | 44 | { |
| 45 | + var name = symbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat); |
29 | 46 | var itemSymbol = namedTypeSymbol.GetItemSymbol(symbolToKindCache); |
| 47 | + TypeRef? itemTypeRef = null; |
| 48 | + if (itemSymbol is not null) |
| 49 | + { |
| 50 | + itemTypeRef = FromTypeSymbol(itemSymbol, symbolToKindCache, dupes); |
| 51 | + if (itemTypeRef.Alias is not null) |
| 52 | + { |
| 53 | + alias = name.Replace(itemSymbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat), itemTypeRef.Alias); |
| 54 | + } |
| 55 | + } |
30 | 56 |
|
31 | 57 | return new TypeRef( |
32 | 58 | symbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat), |
33 | 59 | symbol.ContainingNamespace.ToDisplayString(), |
34 | 60 | symbol.ContainingAssembly.ToDisplayString(), |
35 | | - itemSymbol is null ? null : FromINamedTypeSymbol(itemSymbol, symbolToKindCache)); |
| 61 | + itemSymbol is null ? null : FromTypeSymbol(itemSymbol, symbolToKindCache, dupes), |
| 62 | + alias: alias); |
36 | 63 | } |
37 | 64 | else if (symbol is IArrayTypeSymbol arrayTypeSymbol) |
38 | 65 | { |
39 | | - var elementType = FromINamedTypeSymbol(arrayTypeSymbol.ElementType, symbolToKindCache); |
| 66 | + var name = arrayTypeSymbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).RemoveAsterisks(); |
| 67 | + var elementType = FromTypeSymbol(arrayTypeSymbol.ElementType, symbolToKindCache, dupes); |
| 68 | + if (elementType.Alias is not null) |
| 69 | + { |
| 70 | + alias = name.Replace(arrayTypeSymbol.ElementType.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat), elementType.Alias); |
| 71 | + } |
| 72 | + |
40 | 73 | return new TypeRef( |
41 | 74 | arrayTypeSymbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).RemoveAsterisks(), |
42 | 75 | elementType.Namespace, |
43 | 76 | elementType.Assembly, |
44 | 77 | elementType, |
45 | | - arrayTypeSymbol.Rank); |
| 78 | + arrayTypeSymbol.Rank, |
| 79 | + alias: alias); |
46 | 80 | } |
47 | 81 | else |
48 | 82 | { |
|
0 commit comments