public int Compare(string x, string y) { var xParsed = NameUtils.ParseGenericArity(x); var yParsed = NameUtils.ParseGenericArity(y); var result = string.Compare(xParsed.name, yParsed.name, StringComparison.Ordinal); return(result != 0 ? result : xParsed.arity.CompareTo(yParsed.arity)); }
private static ImmutableNode <string> BuildNameWithArity(string rawName) { var(name, arity) = NameUtils.ParseGenericArity(rawName); var arityBuilder = (ImmutableNode <string>)null; if (arity != 0) { arityBuilder = GenericParameterListEnd; for (var i = 1; i < arity; i++) { arityBuilder = new ImmutableNode <string>(null, ",", arityBuilder); } arityBuilder = new ImmutableNode <string>(null, "<", arityBuilder); } return(new ImmutableNode <string>(null, name, arityBuilder)); }
public ImmutableNode <string> Visit(GenericInstantiationTypeReference genericInstantiationTypeReference) { var args = genericInstantiationTypeReference.GenericTypeArguments; if (args.Count == 1 && genericInstantiationTypeReference.TypeDefinition is TopLevelTypeReference topLevel && topLevel.Name == "Nullable`1" && topLevel.Namespace == "System") { return(new ImmutableNode <string>(args[0].Accept(this), "?", null)); } if (IsValueTuple(genericInstantiationTypeReference, minArgs: 2) && TryUseTupleSyntax(args, out var tupleSyntax)) { return(tupleSyntax); } var builder = (ImmutableNode <string>)null; var argumentsLeft = genericInstantiationTypeReference.GenericTypeArguments.Count; void BuildNext(string typeName) { var(name, arity) = NameUtils.ParseGenericArity(typeName); if (arity != 0) { if (argumentsLeft < arity) { throw new InvalidOperationException("Number of generic arguments provided does not match combined type arity."); } builder = new ImmutableNode <string>(null, ">", builder); for (var i = 0; i < arity; i++) { argumentsLeft--; builder = new ImmutableNode <string>( genericInstantiationTypeReference.GenericTypeArguments[argumentsLeft].Accept(this), i == 0 ? null : ", ", builder); } builder = new ImmutableNode <string>(null, "<", builder); } builder = new ImmutableNode <string>(null, name, builder); } var currentType = genericInstantiationTypeReference.TypeDefinition; for (; currentType is NestedTypeReference nested; currentType = nested.DeclaringType) { BuildNext(nested.Name); builder = new ImmutableNode <string>(null, ".", builder); } topLevel = currentType as TopLevelTypeReference ?? throw new InvalidOperationException("Nested types must be declared by either a top-level type or another nested type."); BuildNext(topLevel.Name); if (argumentsLeft != 0) { throw new InvalidOperationException("Number of generic arguments provided does not match combined type arity."); } return(AddNamespace(topLevel, builder)); }