public TypeCompletionData(ICSharpCode.NRefactory.TypeSystem.IType type, bool fullName, bool isInAttributeContext, TypeSystemAstBuilder builder, bool addForTypeCreation) { Type = type; FullName = fullName; IsInAttributeContext = isInAttributeContext; // Confirm that this doesn't also include other types, such as enums, structs, etc. //DeclarationCategory = DeclarationCategory.Class; DeclarationCategory = type.Kind.ResolveDeclarationCategoryFromTypeKind(); string typeName = fullName ? builder.ConvertType(type).GetText() : type.Name; if (isInAttributeContext && typeName.EndsWith("Attribute") && typeName.Length > "Attribute".Length) { typeName = typeName.Substring(0, typeName.Length - "Attribute".Length); } SetDefaultText(typeName); // for documentation, see type.GetDefinition().Documentation Documentation = type.GetDefinition().Documentation; }
public virtual string GetCustomTypeName(ICSharpCode.NRefactory.TypeSystem.IType type) { var attrs = type.GetDefinition().Attributes; string name = null; var nameAttr = this.GetAttribute(attrs, Translator.Bridge_ASSEMBLY + ".NameAttribute"); if (nameAttr != null) { name = (string)nameAttr.PositionalArguments[0].ConstantValue; } if (!string.IsNullOrEmpty(name)) { return name; } var nsAtrr = this.GetAttribute(attrs, Translator.Bridge_ASSEMBLY + ".NamespaceAttribute"); if (nsAtrr != null && nsAtrr.PositionalArguments.Count > 0) { var arg = nsAtrr.PositionalArguments[0]; name = Helpers.ReplaceSpecialChars(type.Name); if (arg.ConstantValue is bool && !((bool)arg.ConstantValue)) { return name; } if (arg.ConstantValue is string) { string ns = arg.ConstantValue.ToString(); return (!string.IsNullOrWhiteSpace(ns) ? (ns + ".") : "") + name; } } if (this.HasAttribute(attrs, Translator.Bridge_ASSEMBLY + ".ObjectLiteralAttribute")) { return "Object"; } return null; }
protected override string GetTypeString(ICSharpCode.NRefactory.TypeSystem.IType t, OutputSettings settings) { if (t.Kind == TypeKind.Unknown) { return settings.IncludeMarkup ? settings.Markup (t.Name) : t.Name; } if (t.Kind == TypeKind.TypeParameter) return settings.IncludeMarkup ? settings.Markup (t.FullName) : t.FullName; var typeWithElementType = t as TypeWithElementType; if (typeWithElementType != null) { var sb = new StringBuilder (); if (typeWithElementType is PointerType) { sb.Append (settings.Markup ("*")); } AppendType (sb, typeWithElementType.ElementType, settings); if (typeWithElementType is ArrayType) { sb.Append (settings.Markup ("[")); sb.Append (settings.Markup (new string (',', ((ArrayType)t).Dimensions - 1))); sb.Append (settings.Markup ("]")); } return sb.ToString (); } ITypeDefinition type = t.GetDefinition (); if (type == null) return ""; // No short names in TypeScript /* if (!settings.UseNETTypeNames && type.Namespace == "System" && type.TypeParameterCount == 0) { switch (type.Name) { case "Object": return "object"; case "Boolean": return "bool"; case "Char": return "char"; case "SByte": return "sbyte"; case "Byte": return "byte"; case "Int16": return "short"; case "UInt16": return "ushort"; case "Int32": return "int"; case "UInt32": return "uint"; case "Int64": return "long"; case "UInt64": return "ulong"; case "Single": return "float"; case "Double": return "double"; case "Decimal": return "decimal"; case "String": return "string"; case "Void": return "void"; } } */ // output anonymous type if (type.IsSynthetic && type.Name == "$Anonymous$") return GetTypeReferenceString (type, settings); var result = new StringBuilder (); var def = type; AppendModifiers (result, settings, def); if (settings.IncludeKeywords) result.Append (GetString (def.Kind)); if (result.Length > 0 && !result.ToString ().EndsWith (" ")) result.Append (settings.Markup (" ")); if (type.Kind == TypeKind.Delegate && settings.ReformatDelegates && settings.IncludeReturnType) { var invoke = type.GetDelegateInvokeMethod (); result.Append (GetTypeReferenceString (invoke.ReturnType, settings)); result.Append (settings.Markup (" ")); } if (settings.UseFullName && !string.IsNullOrEmpty (type.Namespace)) result.Append ((settings.IncludeMarkup ? settings.Markup (t.Namespace) : type.Namespace) + "."); if (settings.UseFullInnerTypeName && type.DeclaringTypeDefinition != null) { bool includeGenerics = settings.IncludeGenerics; settings.OutputFlags |= OutputFlags.IncludeGenerics; string typeString = GetTypeReferenceString (type.DeclaringTypeDefinition, settings); if (!includeGenerics) settings.OutputFlags &= ~OutputFlags.IncludeGenerics; result.Append (typeString); result.Append (settings.Markup (".")); } result.Append (settings.EmitName (type, settings.IncludeMarkup ? settings.Markup (t.Name) : type.Name)); // No generics in TypeScript so far. /* if (settings.IncludeGenerics && type.TypeParameterCount > 0) { result.Append (settings.Markup ("<")); for (int i = 0; i < type.TypeParameterCount; i++) { if (i > 0) result.Append (settings.Markup (settings.HideGenericParameterNames ? "," : ", ")); if (!settings.HideGenericParameterNames) { if (t is ParameterizedType) { result.Append (GetTypeReferenceString (((ParameterizedType)t).TypeArguments [i], settings)); } else { AppendVariance (result, type.TypeParameters [i].Variance); result.Append (NetToCSharpTypeName (type.TypeParameters [i].FullName)); } } } result.Append (settings.Markup (">")); } */ // No delegate in TypeScript so far. /* if (t.Kind == TypeKind.Delegate && settings.ReformatDelegates) { // var policy = GetPolicy (settings); // if (policy.BeforeMethodCallParentheses) // result.Append (settings.Markup (" ")); result.Append (settings.Markup ("(")); var invoke = type.GetDelegateInvokeMethod (); if (invoke != null) AppendParameterList (result, settings, invoke.Parameters); result.Append (settings.Markup (")")); return result.ToString (); } */ if (settings.IncludeBaseTypes && type.DirectBaseTypes.Any ()) { bool first = true; foreach (var baseType in type.DirectBaseTypes) { // if (baseType.FullName == "System.Object" || baseType.FullName == "System.Enum") // continue; result.Append (settings.Markup (first ? " : " : ", ")); first = false; result.Append (GetTypeReferenceString (baseType, settings)); } } // OutputConstraints (result, settings, type.TypeParameters); return result.ToString (); }