/VsIntegration/Nemerle.Compiler.Utils/Hints/SubHintForType.n

http://github.com/xxVisorxx/nemerle · Unknown · 132 lines · 114 code · 18 blank · 0 comment · 0 complexity · ce9c54eb0891753c9ebe338f63517b02 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Nemerle.Collections;
  6. using BFlg = System.Reflection.BindingFlags;
  7. namespace Nemerle.Compiler.Utils
  8. {
  9. class SubHintForType : Utils.Convert
  10. {
  11. public this()
  12. {
  13. base(true, null, false);
  14. ConvertTypeName = ConvertTypeNameImpl;
  15. }
  16. mutable _currentId = 1;
  17. _idToTypeMap : Hashtable[int, FixedType] = Hashtable();
  18. mutable _ignoringType : FixedType;
  19. GetTypeId(ty : FixedType) : int
  20. {
  21. _currentId++;
  22. _idToTypeMap.Add(_currentId, ty);
  23. _currentId
  24. }
  25. ConvertTypeNameImpl(ty : FixedType, name : string) : string
  26. {
  27. if (ty.Equals(_ignoringType))
  28. $"<font color='DarkCyan'>$name</font>"
  29. else
  30. $"<font color='DarkCyan'><b><hint value='$name' key='$(GetTypeId(ty))' /></b></font>"
  31. }
  32. public GetDelayedHintHendler() : Func[string, string]
  33. {
  34. def makeTypeStr(key : string) : string
  35. {
  36. AddNamespaces = true;
  37. ExpandTypeArgs = false;
  38. def ty = _idToTypeMap[int.Parse(key)];
  39. def makeDelegateExt(ti)
  40. {
  41. def m = ti.LookupMember("Invoke").Head;
  42. "<lb/>Signature: " + FixedTypeToString(m.GetMemType()) + "<lb/>"
  43. + "Inferred: " + TypeVarToString(ty.TypeOfMember(m))
  44. }
  45. def baseTypesExt(ti : TypeInfo, baseTypeName = "Base type") : string
  46. {
  47. def implementsInfo(itfs : list[FixedType.Class]) : string
  48. {
  49. def plural = if (itfs.Length > 1) "s" else "";
  50. def prompt = if (ti.IsInterface) $"Base interface$plural" else "Implements";
  51. if (itfs.IsEmpty) ""
  52. else $<#<lb/>$prompt: ..$(itfs; ", "; FixedClassTypeToString)#>
  53. }
  54. def old = AddNamespaces;
  55. AddNamespaces = false;
  56. def itfs1 = ti.GetDirectSuperTypes();
  57. def baseType = ti.BaseType;
  58. def itfs = if (baseType == null) itfs1 else itfs1.Tail;
  59. def res =
  60. if (baseType != null)
  61. $"<lb/>$baseTypeName: " + TypeVarToString(ty.GetInstantiatedSuperType(baseType)) + implementsInfo(itfs)
  62. else implementsInfo(itfs);
  63. AddNamespaces = old;
  64. res
  65. }
  66. def (kind, ext) =
  67. match (ty)
  68. {
  69. | Class(ti, _args) =>
  70. def (kind, ext) =
  71. if (ti.IsDelegate) ("delegate ", makeDelegateExt(ti))
  72. else if (ti.IsModule) ("module ", "")
  73. else if (ti.IsValueType) ("", "")
  74. else match (ti.GetTydecl())
  75. {
  76. | Class with kind = "class "
  77. | Interface with kind = "interface " => (kind, baseTypesExt(ti))
  78. | VariantOption =>
  79. def old = AddNamespaces;
  80. AddNamespaces = false;
  81. def flds = ti.GetFields(BFlg.DeclaredOnly | BFlg.Instance | BFlg.Public)
  82. .Map(f => $"$(f.Name) : $(TypeVarToString(ty.TypeOfMember(f)));");
  83. def flds = if (flds.IsEmpty) "" else $<#<lb/>Fields: ..$flds#>;
  84. AddNamespaces = old;
  85. ("variant option ", flds + baseTypesExt(ti, "Declaring variant"))
  86. | Variant(members) when members.IsEmpty => ("variant ", baseTypesExt(ti))
  87. | Variant(members) =>
  88. def old = AddNamespaces;
  89. AddNamespaces = false;
  90. def ops = members.Map(o => FixedClassTypeToString(o.GetMemType()));
  91. AddNamespaces = old;
  92. ("variant ", $<#<lb/>Variant options: ..$ops<lb/>$(baseTypesExt(ti))#>)
  93. | Enum => ("enum ", "")
  94. | Alias => assert(false);
  95. };
  96. def obsolete = if (ti.IsObsolete) "[Obsolete] " else "";
  97. (obsolete + "<keyword>" + HintHelper.MakeAccessModifiers(ti) + kind + "</keyword>", ext)
  98. | StaticTypeVarRef => ("type parameter ", "")
  99. | Fun => ("function type ", "")
  100. | Tuple => ("tuple ", "")
  101. | Array(_, rank) => ($"$rank dimention ", "")
  102. | Ref => ("ref parameter ", "")
  103. | Out => ("out parameter ", "")
  104. | Void => ("", "")
  105. | Intersection => ("", "")
  106. };
  107. def res = $"<hint>$kind$(FixedTypeToString(ty))$ext</hint>";
  108. _ignoringType = ty;
  109. res
  110. }
  111. makeTypeStr
  112. }
  113. }
  114. }