Example #1
0
        protected override StringBuilder AppendGenericType(StringBuilder buf, TypeReference type, DynamicParserContext context, bool appendGeneric = true)
        {
            List <TypeReference> decls = DocUtils.GetDeclaringTypes(
                type is GenericInstanceType ? type.GetElementType() : type);
            List <TypeReference> genArgs = GetGenericArguments(type);
            int  argIdx = 0;
            int  displayedInParentArguments = 0;
            bool insertNested = false;

            foreach (var decl in decls)
            {
                TypeReference declDef = decl.Resolve() ?? decl;
                if (insertNested)
                {
                    buf.Append(NestedTypeSeparator);
                }
                insertNested = true;

                bool isTuple          = IsTuple(type);
                bool isFSharpFunction = IsFSharpFunction(type);
                if (!isTuple && !isFSharpFunction)
                {
                    AppendTypeName(buf, declDef, context);
                }

                int argumentCount            = DocUtils.GetGenericArgumentCount(declDef);
                int notYetDisplayedArguments = argumentCount - displayedInParentArguments;
                displayedInParentArguments = argumentCount;// nested TypeReferences have parents' generic arguments, but we shouldn't display them
                if (notYetDisplayedArguments > 0)
                {
                    if (!isTuple && !isFSharpFunction)
                    {
                        buf.Append(GenericTypeContainer[0]);
                    }
                    if (isFSharpFunction && genericParameterState == GenericParameterState.WithinTuple)
                    {
                        buf.Append("(");
                    }
                    var origState = MemberFormatterState;
                    var genericParameterStateOrigState = genericParameterState;
                    MemberFormatterState  = MemberFormatterState.WithinGenericTypeParameters;
                    genericParameterState = isTuple ? GenericParameterState.WithinTuple : GenericParameterState.None;

                    for (int i = 0; i < notYetDisplayedArguments; ++i)
                    {
                        if (i > 0)
                        {
                            buf.Append(isTuple ? " * " : isFSharpFunction ? " -> " : ", ");
                        }
                        var genArg           = genArgs[argIdx++];
                        var genericParameter = genArg as GenericParameter;
                        if (genericParameter != null && IsFlexibleType(genericParameter))
                        {
                            buf.Append("#");// replace genericParameter which is a flexible type with its constraint type
                            _AppendTypeName(buf, genericParameter.Constraints[0], context);
                        }
                        else
                        {
                            _AppendTypeName(buf, genArg, context);
                        }
                    }
                    MemberFormatterState  = origState;
                    genericParameterState = genericParameterStateOrigState;

                    if (MemberFormatterState == MemberFormatterState.None)
                    {
                        AppendConstraints(buf,
                                          genArgs.GetRange(0, notYetDisplayedArguments)
                                          .Where(i => i is GenericParameter)
                                          .Cast <GenericParameter>()
                                          .ToList());
                    }
                    if (!isTuple && !isFSharpFunction)
                    {
                        buf.Append(GenericTypeContainer[1]);
                    }
                    if (isFSharpFunction && genericParameterState == GenericParameterState.WithinTuple)
                    {
                        buf.Append(")");
                    }
                }
            }
            return(buf);
        }