Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Resolving Symbolic References in a CodeDOM (Part 7)

, 2 Dec 2012
Resolving symbolic references in a CodeDOM.
Nova.0.6.exe.zip
Nova.0.6.zip
Nova.CLI
Properties
Nova.CodeDOM
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Interfaces
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Assemblies
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Parsing
Base
Properties
Rendering
Resolving
Utilities
Mono.Cecil
Reflection
Nova.Examples
Properties
Nova.Studio
Images
About.png
Configuration.png
EditCopy.png
EditCut.png
EditDelete.png
EditPaste.png
EditRedo.png
EditUndo.png
Error.png
Exit.png
FileNew.png
FileOpen.png
FileSave.png
FileSaveAll.png
FileSaveAs.png
Find.png
Help.png
Info.png
Logo.png
Options.png
Print.png
PrintPreview.png
Properties.png
Todo.png
Warning.png
Objects.ico
Properties
Settings.settings
Nova.Test
Properties
Nova.UI
CodeDOM
Annotations
Base
Comments
Base
DocComments
CodeRef
Base
List
Name
Base
Other
Simple
CompilerDirectives
Base
Conditionals
Base
Messages
Base
Pragmas
Base
Symbols
Base
Base
Expressions
AnonymousMethods
Base
Operators
Base
Binary
Arithmetic
Base
Assignment
Base
Bitwise
Base
Conditional
Relational
Base
Shift
Base
Other
Base
Unary
Base
Other
References
Base
GotoTargets
Base
Methods
Namespaces
Other
Properties
Types
Base
Variables
Base
Projects
Namespaces
References
Base
Statements
Base
Conditionals
Base
Exceptions
Generics
Constraints
Base
Iterators
Base
Jumps
Loops
Methods
OperatorDecls
Miscellaneous
Namespaces
Properties
Base
Events
Types
Base
Variables
Base
Properties
Resolving
Utilties
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Media;
using Mono.Cecil;
using Mono.Collections.Generic;

using Nova.CodeDOM;
using Nova.Utilities;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.TypeRefBase"/>.
    /// </summary>
    public abstract class TypeRefBaseVM : SymbolicRefVM
    {
        #region /* FIELDS */

        /// <summary>
        /// Optional type arguments (for generic types only).
        /// </summary>
        protected ChildListVM<ExpressionVM> _typeArgumentVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.TypeRefBase"/>.
        /// </summary>
        protected TypeRefBaseVM(TypeRefBase typeRefBase, bool createTypeArgumentVMs, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(typeRefBase, dictionary)
        {
            if (createTypeArgumentVMs)
                _typeArgumentVMs = CreateListVM<Expression, ExpressionVM>(typeRefBase.TypeArguments, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.TypeRefBase"/> model.
        /// </summary>
        public TypeRefBase TypeRefBase
        {
            get { return (TypeRefBase)CodeObject; }
        }

        /// <summary>
        /// The type argument <see cref="Expression"/>s of the reference (if any).
        /// </summary>
        public virtual ChildListVM<ExpressionVM> TypeArguments
        {
            get { return _typeArgumentVMs; }
        }

        /// <summary>
        /// True if there are any type arguments.
        /// </summary>
        public bool HasTypeArguments
        {
            get { return (TypeArguments != null && TypeArguments.Count > 0); }
        }

        /// <summary>
        /// The number of type arguments.
        /// </summary>
        public int TypeArgumentCount
        {
            get { return (TypeArguments != null ? TypeArguments.Count : 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            RenderType(renderer, CodeObject.GetType(), flags, this);
            renderer.RenderRightArrow(NORMAL_BRUSH, this);
            RenderReferenceDescription(renderer, TypeRefBase.GetTypeWithoutConstant().Reference);
            RenderSummaryCommentInToolTip(renderer);
            RenderMessagesInToolTip(renderer, flags);
        }

        /// <summary>
        /// Render a type.
        /// </summary>
        public static void RenderTypeReference(CodeRenderer renderer, TypeReference typeReference, RenderFlags flags, CodeObjectVM tag)
        {
            RenderFlags passFlags = flags & ~RenderFlags.Description;

            // Dereference (remove the trailing '&' or '*') if it's a reference type or pointer type
            if (typeReference.IsByReference)
                typeReference = ((ByReferenceType)typeReference).ElementType;

            bool hasBorder = false;
            bool isDelegate = false;
            TypeDefinition typeDefinition = null;
            try { typeDefinition = typeReference.Resolve(); }
            catch { }

            bool isDescription = flags.HasFlag(RenderFlags.Description);
            if (isDescription)
            {
                string declType = null;
                MethodDefinition delegateInvokeMethodInfo = null;
                Brush borderBrush = DarkPalerGreen;
                Brush backgroundBrush = PalerGreen;
                if (typeDefinition != null)
                {
                    // For Mono, IsValueType and IsClass are also true for enums, so we have to check IsEnum first
                    if (typeDefinition.IsEnum)
                    {
                        declType = EnumDecl.ParseToken;
                        borderBrush = EnumDeclVM.StaticBorderBrush;
                        backgroundBrush = EnumDeclVM.StaticBackgroundBrush;
                    }
                    else if (typeDefinition.IsValueType)
                    {
                        declType = StructDecl.ParseToken;
                        borderBrush = StructDeclVM.StaticBorderBrush;
                        backgroundBrush = StructDeclVM.StaticBackgroundBrush;
                    }
                    else if (typeDefinition.IsClass)
                    {
                        if (TypeDefinitionUtil.IsDelegateType(typeDefinition))
                        {
                            declType = DelegateDecl.ParseToken;
                            borderBrush = DelegateDeclVM.StaticBorderBrush;
                            backgroundBrush = DelegateDeclVM.StaticBackgroundBrush;
                            isDelegate = true;
                        }
                        else
                        {
                            declType = ClassDecl.ParseToken;
                            borderBrush = ClassDeclVM.StaticBorderBrush;
                            backgroundBrush = ClassDeclVM.StaticBackgroundBrush;
                        }
                    }
                    else if (typeDefinition.IsInterface)
                    {
                        declType = InterfaceDecl.ParseToken;
                        borderBrush = InterfaceDeclVM.StaticBorderBrush;
                        backgroundBrush = InterfaceDeclVM.StaticBackgroundBrush;
                    }

                    hasBorder = (flags.HasFlag(RenderFlags.ForceBorder) && !flags.HasFlag(RenderFlags.NoBorder) && !typeDefinition.IsGenericParameter);
                    if (hasBorder)
                        renderer.CreateBorder(borderBrush, backgroundBrush, tag);

                    delegateInvokeMethodInfo = (isDelegate ? TypeDefinitionUtil.GetInvokeMethod(typeDefinition) : null);
                    if (!flags.HasFlag(RenderFlags.NoPreAnnotations))
                    {
                        AttributeVM.RenderAttributes(renderer, typeDefinition, tag);
                        if (delegateInvokeMethodInfo != null)
                            AttributeVM.RenderAttributes(renderer, delegateInvokeMethodInfo.MethodReturnType, tag, AttributeTarget.Return);
                    }
                }
                if (typeReference.IsGenericParameter)
                {
                    renderer.RenderText("(type parameter) ", NORMAL_BRUSH, TextStyle.Proportional);
                    IGenericParameterProvider owner = ((GenericParameter)typeReference).Owner;
                    if (owner is TypeReference)
                        RenderTypeReferenceAsTypeRefVM(renderer, (TypeReference)owner, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                    else if (owner is MethodDefinition)
                        RenderGenericMember(renderer, ((MethodDefinition)owner).Name, (IEnumerable<TypeReference>)((MethodDefinition)owner).GenericParameters, passFlags, IDENTIFIER_BRUSH, tag);
                    renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                }
                else
                {
                    if (typeDefinition != null)
                        renderer.RenderText(ModifiersHelpers.AsString(TypeRefBase.GetTypeModifiers(typeDefinition)), KEYWORD_BRUSH, tag);
                    renderer.RenderText(declType, KEYWORD_BRUSH, tag);
                    renderer.RenderText(" ", PUNC_BRUSH, tag);
                }
                if (delegateInvokeMethodInfo != null)
                {
                    TypeReference returnType = delegateInvokeMethodInfo.ReturnType;
                    RenderTypeReferenceAsTypeRefVM(renderer, returnType, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    renderer.RenderText(" ", PUNC_BRUSH, tag);
                }
            }

            bool hasDotPrefix = flags.HasFlag(RenderFlags.HasDotPrefix);

            // Go by local generic arguments instead of IsGenericType so that nested types with generic
            // enclosing types aren't treated as generic unless they have their own type arguments.
            string keyword;
            IEnumerable<TypeReference> genericArguments = TypeDefinitionUtil.GetLocalGenericArguments(typeReference);
            if (genericArguments != null)
            {
                // Render "Nullable<Type>" as "Type?"
                if (TypeDefinitionUtil.IsNullableType(typeReference))
                {
                    TypeReference typeParam = Enumerable.First(genericArguments);
                    if (!hasDotPrefix && TypeRefBase.TypeNameToKeywordMap.TryGetValue(typeParam.Name, out keyword) && typeParam.Namespace == "System")
                        renderer.RenderText(keyword + TypeRefBase.ParseTokenNullable, KEYWORD_BRUSH, tag);
                    else
                    {
                        RenderTypeReferenceAsTypeRefVM(renderer, typeParam, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                        renderer.RenderText(TypeRefBase.ParseTokenNullable, TYPE_BRUSH, tag);
                    }
                }
                else
                {
                    if (typeReference.IsNested && !hasDotPrefix && flags.HasFlag(RenderFlags.ShowParentTypes))
                    {
                        RenderTypeReferenceAsTypeRefVM(renderer, typeReference.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                        renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                    }
                    RenderGenericMember(renderer, TypeDefinitionUtil.NonGenericName(typeReference), genericArguments, flags, TYPE_BRUSH, tag);
                }
            }
            else if (typeReference.IsGenericParameter)
            {
                // If there is an 'open' type parameter, render it as an error (or warning if in a doc comment)
                Brush brush = (tag is OpenTypeParameterRefVM ? (flags.HasFlag(RenderFlags.InDocComment) ? WARNING_BRUSH : ERROR_BRUSH) : IDENTIFIER_BRUSH);
                renderer.RenderText(typeReference.Name, brush, tag);
            }
            else if (typeReference.IsArray)
            {
                ArrayType arrayType = (ArrayType)typeReference;
                RenderTypeReferenceAsTypeRefVM(renderer, arrayType.ElementType, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                renderer.RenderText(TypeRefBase.ArrayRankToString(arrayType.Rank), PUNC_BRUSH, tag);
            }
            else if (!hasDotPrefix && !isDescription && TypeRefBase.TypeNameToKeywordMap.TryGetValue(typeReference.Name, out keyword) && typeReference.Namespace == "System")
            {
                renderer.RenderText(keyword, KEYWORD_BRUSH, tag);
            }
            else
            {
                if (typeReference.IsNested && !hasDotPrefix && flags.HasFlag(RenderFlags.ShowParentTypes))
                {
                    RenderTypeReferenceAsTypeRefVM(renderer, typeReference.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                    renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                }
                renderer.RenderName(typeReference.Name, (typeDefinition == null ? ERROR_BRUSH : TYPE_BRUSH), tag, flags);
            }

            if (isDescription && typeDefinition != null)
            {
                if (isDelegate)
                    MethodRefVM.RenderMethodParameters(renderer, TypeDefinitionUtil.GetInvokeMethod(typeDefinition), RenderFlags.None, tag);
                else
                {
                    bool hasBaseItem = false;

                    // Render base type (if any)
                    if (!typeDefinition.IsValueType)  // Don't show the implicit ValueType base for structs
                    {
                        TypeReference baseType = typeDefinition.IsEnum ? TypeDefinitionUtil.GetUnderlyingTypeOfEnum(typeDefinition) : typeDefinition.BaseType;
                        if (baseType != null && baseType.FullName != "System.Object" && (!typeDefinition.IsEnum || baseType.FullName != "System.Int32"))
                        {
                            renderer.RenderText(" " + BaseListTypeDecl.ParseToken + " ", PUNC_BRUSH, tag);
                            RenderTypeReferenceAsTypeRefVM(renderer, baseType, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                            hasBaseItem = true;
                        }
                    }

                    // Render implemented interfaces (if any)
                    if (!typeDefinition.IsEnum)  // Don't show the interfaces of the implicit Enum base for enums
                    {
                        Collection<TypeReference> interfaces = typeDefinition.Interfaces;
                        if (interfaces != null && interfaces.Count > 0)
                        {
                            // There's no way to tell which interfaces are implemented by the current type as
                            // opposed to one of it's base types.  We could subtract all interfaces from the base
                            // type, but that wouldn't be perfect since the same interface can be implemented at
                            // multiple levels.  Instead, display them all, since an external user really just
                            // cares that they're implemented by the type, and doesn't care if it's really by a
                            // base type.
                            foreach (TypeReference @interface in interfaces)
                            {
                                renderer.RenderText((hasBaseItem ? Expression.ParseTokenSeparator : (" " + BaseListTypeDecl.ParseToken)) + " ", PUNC_BRUSH, tag);
                                RenderTypeReferenceAsTypeRefVM(renderer, @interface, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                                hasBaseItem = true;
                            }
                        }
                    }

                    // Render any type constraints for local type arguments
                    if (typeDefinition.HasGenericParameters)
                        RenderConstraints(renderer, TypeDefinitionUtil.GetLocalGenericArguments(typeDefinition), passFlags, tag);
                }
            }

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(tag);
        }

        /// <summary>
        /// Render a TypeReference as a TypeRefVM for nested tooltips.
        /// </summary>
        public static void RenderTypeReferenceAsTypeRefVM(CodeRenderer renderer, TypeReference typeReference, RenderFlags flags, CodeObjectVM parentVM)
        {
            // Treat GenericParameters as TypeParameter definitions
            CodeObject codeObject;
            if (typeReference is GenericParameter)
                codeObject = new TypeParameter(typeReference.Name);
            else
            {
                codeObject = TypeRef.Create(typeReference);
                // Evaluate type parameters some day, but also need to evaluate the type arguments in the declaration
                //TypeRefBase typeRefBase = TypeRef.Create(typeReference).EvaluateTypeArgumentTypes(parentVM.CodeObject);
            }

            // Use parentVM to render if possible, but it can be null if we're inside a tooltip
            CodeObjectVM codeObjectVM;
            if (parentVM != null)
                codeObjectVM = parentVM.CreateVM(codeObject, true);
            else
                codeObjectVM = CreateVM(codeObject, null, true);
            codeObjectVM.Render(renderer, flags | RenderFlags.NoBorder);
        }

        /// <summary>
        /// Render a generic member.
        /// </summary>
        public static void RenderGenericMember<T>(CodeRenderer renderer, string name, IEnumerable<T> args, RenderFlags flags, Brush brush, CodeObjectVM tag) where T : TypeReference
        {
            renderer.RenderText(name, brush, tag);
            if (!flags.HasFlag(RenderFlags.SuppressTypeArgs) && Enumerable.Any(args))
            {
                // Render the angle brackets normally in the GUI, even if we're inside a documentation comment
                renderer.RenderText(TypeRefBase.ParseTokenArgumentStart, PUNC_BRUSH, tag);
                bool first = true;
                foreach (T typeArg in args)
                {
                    if (!first)
                        renderer.RenderText(Expression.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
                    RenderTypeReferenceAsTypeRefVM(renderer, typeArg, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    first = false;
                }
                renderer.RenderText(TypeRefBase.ParseTokenArgumentEnd, PUNC_BRUSH, tag);
            }
        }

        /// <summary>
        /// Render a <see cref="Type"/>.
        /// </summary>
        public static void RenderType(CodeRenderer renderer, Type type, RenderFlags flags, CodeObjectVM tag)
        {
            RenderFlags passFlags = flags & ~RenderFlags.Description;

            // Dereference (remove the trailing '&' or '*') if it's a reference type or pointer type
            if (type.IsByRef)
                type = type.GetElementType();

            bool hasBorder = false;
            bool isDelegate = false;
            bool isDescription = flags.HasFlag(RenderFlags.Description);
            if (isDescription)
            {
                string declType = null;
                Brush borderBrush = DarkPalerGreen;
                Brush backgroundBrush = PalerGreen;
                if (type.IsClass)
                {
                    if (TypeUtil.IsDelegateType(type))
                    {
                        declType = DelegateDecl.ParseToken;
                        borderBrush = DelegateDeclVM.StaticBorderBrush;
                        backgroundBrush = DelegateDeclVM.StaticBackgroundBrush;
                        isDelegate = true;
                    }
                    else if (!type.IsGenericParameter)
                    {
                        declType = ClassDecl.ParseToken;
                        borderBrush = ClassDeclVM.StaticBorderBrush;
                        backgroundBrush = ClassDeclVM.StaticBackgroundBrush;
                    }
                }
                else if (type.IsInterface)
                {
                    declType = InterfaceDecl.ParseToken;
                    borderBrush = InterfaceDeclVM.StaticBorderBrush;
                    backgroundBrush = InterfaceDeclVM.StaticBackgroundBrush;
                }
                else if (type.IsEnum)
                {
                    declType = EnumDecl.ParseToken;
                    borderBrush = EnumDeclVM.StaticBorderBrush;
                    backgroundBrush = EnumDeclVM.StaticBackgroundBrush;
                }
                else if (type.IsValueType)
                {
                    declType = StructDecl.ParseToken;
                    borderBrush = StructDeclVM.StaticBorderBrush;
                    backgroundBrush = StructDeclVM.StaticBackgroundBrush;
                }

                hasBorder = (flags.HasFlag(RenderFlags.ForceBorder) && !flags.HasFlag(RenderFlags.NoBorder) && !type.IsGenericParameter);
                if (hasBorder)
                    renderer.CreateBorder(borderBrush, backgroundBrush, tag);

                MethodInfo delegateInvokeMethodInfo = (isDelegate ? TypeUtil.GetInvokeMethod(type) : null);
                if (!flags.HasFlag(RenderFlags.NoPreAnnotations))
                {
                    AttributeVM.RenderAttributes(renderer, type, tag);
                    if (delegateInvokeMethodInfo != null)
                        AttributeVM.RenderAttributes(renderer, delegateInvokeMethodInfo.ReturnParameter, tag, AttributeTarget.Return);
                }
                if (type.IsGenericParameter)
                {
                    renderer.RenderText("(type parameter) ", NORMAL_BRUSH, TextStyle.Proportional);
                    if (type.DeclaringMethod != null)
                        RenderGenericMember(renderer, type.DeclaringMethod.Name, type.DeclaringMethod.GetGenericArguments(), passFlags, TYPE_BRUSH, tag);
                    else if (type.DeclaringType != null)
                        RenderTypeAsTypeRefVM(renderer, type.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                    renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                }
                else
                {
                    renderer.RenderText(ModifiersHelpers.AsString(TypeRefBase.GetTypeModifiers(type)), KEYWORD_BRUSH, tag);
                    renderer.RenderText(declType, KEYWORD_BRUSH, tag);
                    renderer.RenderText(" ", PUNC_BRUSH, tag);
                }
                if (delegateInvokeMethodInfo != null)
                {
                    Type returnType = delegateInvokeMethodInfo.ReturnType;
                    RenderTypeAsTypeRefVM(renderer, returnType, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    renderer.RenderText(" ", PUNC_BRUSH, tag);
                }
            }

            bool hasDotPrefix = flags.HasFlag(RenderFlags.HasDotPrefix);

            // Go by local generic arguments instead of IsGenericType so that nested types with generic
            // enclosing types aren't treated as generic unless they have their own type arguments.
            string keyword;
            Type[] genericArguments = TypeUtil.GetLocalGenericArguments(type);
            if (genericArguments != null && genericArguments.Length > 0)
            {
                // Render "Nullable<Type>" as "Type?"
                if (TypeUtil.IsNullableType(type))
                {
                    Type typeParam = genericArguments[0];
                    if (!hasDotPrefix && TypeRefBase.TypeNameToKeywordMap.TryGetValue(typeParam.Name, out keyword) && typeParam.Namespace == "System")
                        renderer.RenderText(keyword + TypeRefBase.ParseTokenNullable, KEYWORD_BRUSH, tag);
                    else
                    {
                        RenderTypeAsTypeRefVM(renderer, typeParam, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                        renderer.RenderText(TypeRefBase.ParseTokenNullable, TYPE_BRUSH, tag);
                    }
                }
                else
                {
                    if (type.IsNested && !hasDotPrefix && flags.HasFlag(RenderFlags.ShowParentTypes))
                    {
                        RenderTypeAsTypeRefVM(renderer, type.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                        renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                    }
                    RenderGenericMember(renderer, TypeUtil.NonGenericName(type), genericArguments, flags, TYPE_BRUSH, tag);
                }
            }
            else if (type.IsGenericParameter)
            {
                // If there is an 'open' type parameter, render it as an error (or warning if in a doc comment)
                Brush brush = (tag is OpenTypeParameterRefVM ? (flags.HasFlag(RenderFlags.InDocComment) ? WARNING_BRUSH : ERROR_BRUSH) : IDENTIFIER_BRUSH);
                renderer.RenderText(type.Name, brush, tag);
            }
            else if (type.IsArray)
            {
                RenderTypeAsTypeRefVM(renderer, type.GetElementType(), RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                renderer.RenderText(TypeRefBase.ArrayRankToString(type.GetArrayRank()), PUNC_BRUSH, tag);
            }
            else if (!hasDotPrefix && !isDescription && TypeRefBase.TypeNameToKeywordMap.TryGetValue(type.Name, out keyword) && type.Namespace == "System")
            {
                renderer.RenderText(keyword, KEYWORD_BRUSH, tag);
            }
            else
            {
                if (type.IsNested && !hasDotPrefix && flags.HasFlag(RenderFlags.ShowParentTypes))
                {
                    RenderTypeAsTypeRefVM(renderer, type.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
                    renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
                }
                renderer.RenderName(type.Name, TYPE_BRUSH, tag, flags);
            }

            if (isDescription)
            {
                if (isDelegate)
                {
                    MethodInfo delegateInvokeMethodInfo = TypeUtil.GetInvokeMethod(type);
                    if (delegateInvokeMethodInfo != null)
                        MethodRefVM.RenderMethodParameters(renderer, delegateInvokeMethodInfo, RenderFlags.None, tag);
                }
                else
                {
                    bool hasBaseItem = false;

                    // Render base type (if any)
                    if (!type.IsValueType)  // Don't show the implicit ValueType base for structs
                    {
                        Type baseType = type.IsEnum ? Enum.GetUnderlyingType(type) : type.BaseType;
                        if (baseType != null && baseType != typeof(object) && (!type.IsEnum || baseType != typeof(int)))
                        {
                            renderer.RenderText(" " + BaseListTypeDecl.ParseToken + " ", PUNC_BRUSH, tag);
                            RenderTypeAsTypeRefVM(renderer, baseType, flags, tag);  // Render as TypeRefVM for nested tooltips
                            hasBaseItem = true;
                        }
                    }

                    // Render implemented interfaces (if any)
                    if (!type.IsEnum)  // Don't show the interfaces of the implicit Enum base for enums
                    {
                        Type[] interfaces = type.GetInterfaces();
                        if (interfaces.Length > 0)
                        {
                            // There's no way to tell which interfaces are implemented by the current type as
                            // opposed to one of it's base types.  We could subtract all interfaces from the base
                            // type, but that wouldn't be perfect since the same interface can be implemented at
                            // multiple levels.  Instead, display them all, since an external user really just
                            // cares that they're implemented by the type, and doesn't care if it's really by a
                            // base type.
                            foreach (Type @interface in interfaces)
                            {
                                renderer.RenderText((hasBaseItem ? Expression.ParseTokenSeparator : (" " + BaseListTypeDecl.ParseToken)) + " ", PUNC_BRUSH, tag);
                                RenderTypeAsTypeRefVM(renderer, @interface, flags, tag);  // Render as TypeRefVM for nested tooltips
                                hasBaseItem = true;
                            }
                        }
                    }

                    // Render any type constraints for local type arguments
                    if (type.IsGenericType)
                        RenderConstraints(renderer, TypeUtil.GetLocalGenericArguments(type.GetGenericTypeDefinition()), passFlags, tag);
                }
            }

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(tag);
        }

        /// <summary>
        /// Render a Type as a TypeRefVM for nested tooltips.
        /// </summary>
        public static void RenderTypeAsTypeRefVM(CodeRenderer renderer, Type type, RenderFlags flags, CodeObjectVM parentVM)
        {
            // Treat generic parameters as TypeParameter definitions
            CodeObject codeObject;
            if (type.IsGenericParameter)
                codeObject = new TypeParameter(type.Name);
            else
            {
                codeObject = TypeRef.Create(type);
                // Evaluate type parameters some day, but also need to evaluate the type arguments in the declaration
                //TypeRefBase typeRefBase = TypeRef.Create(typeReference).EvaluateTypeArgumentTypes(parentVM.CodeObject);
            }

            // Use parentVM to render if possible, but it can be null if we're inside a tooltip
            CodeObjectVM codeObjectVM;
            if (parentVM != null)
                codeObjectVM = parentVM.CreateVM(codeObject, true);
            else
                codeObjectVM = CreateVM(codeObject, null, true);
            codeObjectVM.Render(renderer, flags | RenderFlags.NoBorder);
        }

        /// <summary>
        /// Render a generic member.
        /// </summary>
        public static void RenderGenericMember(CodeRenderer renderer, string name, Type[] args, RenderFlags flags, Brush brush, CodeObjectVM tag)
        {
            renderer.RenderText(name, brush, tag);
            if (!flags.HasFlag(RenderFlags.SuppressTypeArgs))
            {
                // Render the angle brackets normally in the GUI, even if we're inside a documentation comment
                renderer.RenderText(TypeRefBase.ParseTokenArgumentStart, PUNC_BRUSH, tag);
                bool first = true;
                foreach (Type typeArg in args)
                {
                    if (!first)
                        renderer.RenderText(Expression.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
                    RenderTypeAsTypeRefVM(renderer, typeArg, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    first = false;
                }
                renderer.RenderText(TypeRefBase.ParseTokenArgumentEnd, PUNC_BRUSH, tag);
            }
        }

        /// <summary>
        /// Render type arguments (if any).
        /// </summary>
        public static void RenderTypeArguments(CodeRenderer renderer, IEnumerable<ExpressionVM> typeArguments, RenderFlags flags, CodeObjectVM tag)
        {
            if (typeArguments != null)
            {
                // Don't pass Description, SuppressBrackets, or ShowParentTypes on to any type arguments, and don't display EOL comments if this is a Description
                RenderFlags passFlags = (flags & (RenderFlags.PassMask & ~(RenderFlags.Description | RenderFlags.SuppressBrackets | RenderFlags.ShowParentTypes)))
                    | (flags.HasFlag(RenderFlags.Description) ? RenderFlags.NoEOLComments : 0);

                // Render the angle brackets normally in the GUI, even if we're inside a documentation comment
                renderer.RenderText(TypeRefBase.ParseTokenArgumentStart, PUNC_BRUSH, tag);
                renderer.RenderList(typeArguments, passFlags, tag);
                renderer.RenderText(TypeRefBase.ParseTokenArgumentEnd, PUNC_BRUSH, tag);
            }
        }

        /// <summary>
        /// Render array rank brackets (if any).
        /// </summary>
        public void RenderArrayRanks(CodeRenderer renderer, RenderFlags flags)
        {
            if (!flags.HasFlag(RenderFlags.SuppressBrackets))
            {
                // Use the array ranks if we have any, otherwise use any on the referenced type
                if (TypeRefBase.ArrayRanks != null)
                {
                    foreach (int rank in TypeRefBase.ArrayRanks)
                    {
                        // Render with a null object tag for now (no special tooltip handling)
                        renderer.RenderText(TypeRefBase.ArrayRankToString(rank), PUNC_BRUSH, null);
                    }
                }
                //else if (TypeRefBase.Reference is TypeDefinition)  // A TypeDefinition won't have any ranks
                else if (TypeRefBase.Reference is Type)
                    RenderArrayRank(renderer, (Type)TypeRefBase.Reference);
            }
        }

        private static void RenderArrayRank(CodeRenderer renderer, Type type)
        {
            if (type.IsArray)
            {
                RenderArrayRank(renderer, type.GetElementType());
                // Render with a null object tag for now (no special tooltip handling)
                renderer.RenderText(TypeRefBase.ArrayRankToString(type.GetArrayRank()), PUNC_BRUSH, null);
            }
        }

        /// <summary>
        /// Render type constraints.
        /// </summary>
        public static void RenderConstraints(CodeRenderer renderer, IEnumerable<GenericParameter> genericParameters, RenderFlags flags, CodeObjectVM tag)
        {
            foreach (GenericParameter genericParameter in genericParameters)
            {
                bool isValueType = false;
                Mono.Cecil.GenericParameterAttributes attributes = genericParameter.Attributes;
                bool hasBorder = false;

                if (attributes.HasFlag(Mono.Cecil.GenericParameterAttributes.ReferenceTypeConstraint))
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, genericParameter, flags, tag);
                    renderer.RenderText(ClassConstraint.ParseToken, KEYWORD_BRUSH, tag);
                }
                if (attributes.HasFlag(Mono.Cecil.GenericParameterAttributes.NotNullableValueTypeConstraint))
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, genericParameter, flags, tag);
                    renderer.RenderText(StructConstraint.ParseToken, KEYWORD_BRUSH, tag);
                    isValueType = true;
                }

                foreach (TypeReference constraint in genericParameter.Constraints)
                {
                    if (constraint.FullName != "System.ValueType")  // Already handled above
                    {
                        RenderConstraintPrefix(renderer, ref hasBorder, genericParameter, flags, tag);
                        RenderTypeReferenceAsTypeRefVM(renderer, constraint, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    }
                }

                if (attributes.HasFlag(Mono.Cecil.GenericParameterAttributes.DefaultConstructorConstraint) && !isValueType)
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, genericParameter, flags, tag);
                    renderer.RenderText(NewConstraint.ParseToken, KEYWORD_BRUSH, tag);
                    if (!MethodDeclBaseVM.HideMethodParens)
                        renderer.RenderText(Expression.ParseTokenStartGroup + Expression.ParseTokenEndGroup, PUNC_BRUSH, tag);
                }

                // If we have a border, return to the previous one
                if (hasBorder)
                    renderer.ReturnToBorderParent(tag);
            }
        }

        private static void RenderConstraintPrefix(CodeRenderer renderer, ref bool hasBorder, GenericParameter genericParameter, RenderFlags flags, CodeObjectVM tag)
        {
            if (!hasBorder)
            {
                renderer.RenderText(" ", PUNC_BRUSH, tag);
                hasBorder = !flags.HasFlag(RenderFlags.NoBorder);
                if (hasBorder)
                    renderer.CreateBorder(StaticBorderBrush, StaticBackgroundBrush, tag);

                renderer.RenderText(ConstraintClause.ParseToken + " ", KEYWORD_BRUSH, tag);
                RenderTypeReference(renderer, genericParameter, RenderFlags.None, tag);
                renderer.RenderText(" " + ConstraintClause.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
            }
            else
                renderer.RenderText(Expression.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
        }

        /// <summary>
        /// Render type constraints.
        /// </summary>
        public static void RenderConstraints(CodeRenderer renderer, Type[] typeParameters, RenderFlags flags, CodeObjectVM tag)
        {
            foreach (Type typeParameter in typeParameters)
            {
                bool isValueType = false;
                System.Reflection.GenericParameterAttributes attributes = typeParameter.GenericParameterAttributes;
                bool hasBorder = false;

                if (attributes.HasFlag(System.Reflection.GenericParameterAttributes.ReferenceTypeConstraint))
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, typeParameter, flags, tag);
                    renderer.RenderText(ClassConstraint.ParseToken, KEYWORD_BRUSH, tag);
                }
                if (attributes.HasFlag(System.Reflection.GenericParameterAttributes.NotNullableValueTypeConstraint))
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, typeParameter, flags, tag);
                    renderer.RenderText(StructConstraint.ParseToken, KEYWORD_BRUSH, tag);
                    isValueType = true;
                }

                foreach (Type constraint in typeParameter.GetGenericParameterConstraints())
                {
                    if (constraint != typeof(ValueType))  // Already handled above
                    {
                        RenderConstraintPrefix(renderer, ref hasBorder, typeParameter, flags, tag);
                        RenderTypeAsTypeRefVM(renderer, constraint, RenderFlags.None, tag);  // Render as TypeRefVM for nested tooltips
                    }
                }

                if (attributes.HasFlag(System.Reflection.GenericParameterAttributes.DefaultConstructorConstraint) && !isValueType)
                {
                    RenderConstraintPrefix(renderer, ref hasBorder, typeParameter, flags, tag);
                    renderer.RenderText(NewConstraint.ParseToken, KEYWORD_BRUSH, tag);
                    if (!MethodDeclBaseVM.HideMethodParens)
                        renderer.RenderText(Expression.ParseTokenStartGroup + Expression.ParseTokenEndGroup, PUNC_BRUSH, tag);
                }

                // If we have a border, return to the previous one
                if (hasBorder)
                    renderer.ReturnToBorderParent(tag);
            }
        }

        private static void RenderConstraintPrefix(CodeRenderer renderer, ref bool hasBorder, Type typeParameter, RenderFlags flags, CodeObjectVM tag)
        {
            if (!hasBorder)
            {
                renderer.RenderText(" ", PUNC_BRUSH, tag);
                hasBorder = !flags.HasFlag(RenderFlags.NoBorder);
                if (hasBorder)
                     renderer.CreateBorder(StaticBorderBrush, StaticBackgroundBrush, tag);

                renderer.RenderText(ConstraintClause.ParseToken + " ", KEYWORD_BRUSH, tag);
                RenderType(renderer, typeParameter, RenderFlags.None, tag);
                renderer.RenderText(" " + ConstraintClause.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
            }
            else
                renderer.RenderText(Expression.ParseTokenSeparator + " ", PUNC_BRUSH, tag);
        }

        public override void UnRender()
        {
            ChildListHelpers.UnRender(_typeArgumentVMs);
            base.UnRender();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

Share

About the Author

KenBeckett
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 2 Dec 2012
Article Copyright 2012 by KenBeckett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid