private static void InitMember(JsDocComment comment, IMember member, IEmitter emitter, object value) { if (member != null) { var method = member as IMethod; if (method != null) { comment.This = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter); if (method.IsConstructor) { comment.Constructs = comment.This; } if (method.TypeParameters != null && method.TypeParameters.Count > 0) { foreach (var param in method.TypeParameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = "Function"; comment.Parameters.Add(jsParam); } } } comment.Override = member.IsOverride; if (member is IParameterizedMember) { var parameters = ((IParameterizedMember)member).Parameters; if (parameters != null && parameters.Count > 0) { foreach (var param in parameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter); comment.Parameters.Add(jsParam); } } } var variable = member as IVariable; if (variable != null) { comment.MemberType = XmlToJsDoc.ToJavascriptName(variable.Type, emitter); } else if (!(member is IField || member is IProperty)) { comment.Returns.Add(new JsDocParam { Type = XmlToJsDoc.ToJavascriptName(member.ReturnType, emitter) }); } var field = member as IField; if (field != null) { comment.ReadOnly = field.IsReadOnly; comment.Const = field.IsConst; comment.Default = value ?? field.ConstantValue; comment.MemberType = XmlToJsDoc.ToJavascriptName(field.Type, emitter); } var property = member as IProperty; if (property != null) { comment.ReadOnly = !property.CanSet; comment.Default = value; comment.MemberType = XmlToJsDoc.ToJavascriptName(property.ReturnType, emitter); } var ev = member as IEvent; if (ev != null) { comment.Event = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter) + "#" + member.Name; } comment.MemberOf = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter); comment.IsPublic = member.IsPublic; comment.IsPrivate = member.IsPrivate; comment.IsProtected = member.IsProtected; var entity = member as ICSharpCode.NRefactory.TypeSystem.Implementation.AbstractResolvedEntity; if (entity != null) { comment.IsAbstract = entity.IsAbstract; comment.IsStatic = entity.IsStatic; } } }
public static string ToJavascriptName(IType type, IEmitter emitter) { if (type.Kind == TypeKind.Delegate) { var delegateName = BridgeTypes.ConvertName(type.FullName); if (!emitter.JsDoc.Callbacks.Contains(delegateName)) { var method = type.GetDelegateInvokeMethod(); JsDocComment comment = new JsDocComment(); var parameters = method.Parameters; if (parameters != null && parameters.Count > 0) { foreach (var param in parameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter); comment.Parameters.Add(jsParam); } } comment.Returns.Add(new JsDocParam { Type = XmlToJsDoc.ToJavascriptName(method.ReturnType, emitter) }); comment.Callback = delegateName; comment.MemberOf = type.Namespace; if (!emitter.JsDoc.Namespaces.Contains(type.Namespace)) { emitter.JsDoc.Namespaces.Add(type.Namespace); comment.Namespace = type.Namespace; } emitter.JsDoc.Callbacks.Add(delegateName); emitter.WriteIndented(comment.ToString() + newLine + newLine, 0); } return(delegateName); } if (type.IsKnownType(KnownTypeCode.String)) { return("string"); } if (type.IsKnownType(KnownTypeCode.Boolean)) { return("boolean"); } if (type.IsKnownType(KnownTypeCode.Void)) { return("void"); } if (type.IsKnownType(KnownTypeCode.Byte) || type.IsKnownType(KnownTypeCode.Char) || type.IsKnownType(KnownTypeCode.Double) || type.IsKnownType(KnownTypeCode.Int16) || type.IsKnownType(KnownTypeCode.Int32) || type.IsKnownType(KnownTypeCode.SByte) || type.IsKnownType(KnownTypeCode.Single) || type.IsKnownType(KnownTypeCode.UInt16) || type.IsKnownType(KnownTypeCode.UInt32)) { return("number"); } if (type.Kind == TypeKind.Array) { ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type; return(JS.Types.ARRAY + ".<" + XmlToJsDoc.ToJavascriptName(arrayType.ElementType, emitter) + ">"); } if (type.Kind == TypeKind.Dynamic) { return("object"); } if (type.Kind == TypeKind.Enum && type.DeclaringType != null) { return("number"); } if (NullableType.IsNullable(type)) { return("?" + XmlToJsDoc.ToJavascriptName(NullableType.GetUnderlyingType(type), emitter)); } BridgeType bridgeType = emitter.BridgeTypes.Get(type, true); //string name = BridgeTypes.ConvertName(type.FullName); var name = type.Namespace; var hasTypeDef = bridgeType != null && bridgeType.TypeDefinition != null; bool isNested = false; if (hasTypeDef) { var typeDef = bridgeType.TypeDefinition; if (typeDef.IsNested) { name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(emitter, typeDef); isNested = true; } name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(typeDef.Name); } else { if (type.DeclaringType != null) { name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(emitter, type); if (type.DeclaringType.TypeArguments.Count > 0) { name += Helpers.PrefixDollar(type.TypeArguments.Count); } isNested = true; } name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(type.Name); } bool isCustomName = false; if (bridgeType != null) { name = BridgeTypes.AddModule(name, bridgeType, false, isNested, out isCustomName); } if (!hasTypeDef && !isCustomName && type.TypeArguments.Count > 0) { name += Helpers.PrefixDollar(type.TypeArguments.Count); } return(name); }
private static string ReadComment(string source, ResolveResult rr, IEmitter emitter, JsDocComment comment) { var xml = new StringBuilder("<comment>" + newLine); if (source != null) { foreach (var line in source.Split(newLine)) { var trimmedLine = line.Trim(); if (string.IsNullOrEmpty(trimmedLine)) { continue; } xml.Append(System.Text.RegularExpressions.Regex.Replace(line, @"\/\/\/\s*", "") + newLine); } xml.Append("</comment>"); var doc = new System.Xml.XmlDocument(); try { doc.LoadXml(xml.ToString()); } catch (XmlException) { return(""); } foreach (XmlNode node in doc.GetElementsByTagName("summary")) { comment.Descriptions.Add(HandleNode(node)); } foreach (XmlNode node in doc.GetElementsByTagName("remark")) { comment.Remarks.Add(HandleNode(node)); } foreach (XmlNode node in doc.GetElementsByTagName("typeparam")) { string name = null; var attr = node.Attributes["name"]; if (attr != null) { name = attr.Value.Trim(); } var param = comment.Parameters.FirstOrDefault(p => p.Name == name); if (param == null) { param = new JsDocParam { Name = "[name]", Type = "[type]" }; comment.Parameters.Add(param); } attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value; } else if (rr != null) { param.Type = "Function"; } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("param")) { string name = null; var attr = node.Attributes["name"]; if (attr != null) { name = attr.Value.Trim(); } var param = comment.Parameters.FirstOrDefault(p => p.Name == name); if (param == null) { continue; } attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value; } else if (rr != null) { param.Type = XmlToJsDoc.GetParamTypeName(param.Name, rr, emitter); } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("returns")) { JsDocParam param = null; if (comment.Returns.Any()) { param = comment.Returns.FirstOrDefault(); } else { param = new JsDocParam { Type = "[type]" }; comment.Returns.Add(param); } var attr = node.Attributes["name"]; if (attr != null) { param.Name = attr.Value.Trim(); } if (string.IsNullOrWhiteSpace(param.Type)) { attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value.Trim(); } else if (rr != null) { param.Type = XmlToJsDoc.GetParamTypeName(null, rr, emitter); } } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("example")) { var codeNodes = node.SelectNodes("code"); StringBuilder sb = new StringBuilder(); foreach (XmlNode codeNode in codeNodes) { sb.Append(codeNode.InnerText + newLine); node.RemoveChild(codeNode); } var code = sb.ToString(); var caption = HandleNode(node); comment.Examples.Add(new Tuple <string, string>(caption, code)); } foreach (XmlNode node in doc.GetElementsByTagName("exception")) { var attr = node.Attributes["cref"]; var exceptionType = ""; if (attr != null) { try { exceptionType = XmlToJsDoc.ToJavascriptName(emitter.BridgeTypes.Get(attr.InnerText).Type, emitter); } catch { // ignored } } var caption = HandleNode(node); comment.Throws.Add(new Tuple <string, string>(caption, exceptionType)); } foreach (XmlNode node in doc.GetElementsByTagName("seealso")) { var attr = node.Attributes["cref"]; var cref = ""; if (attr != null) { cref = attr.InnerText; } comment.SeeAlso.Add(cref); } foreach (XmlNode node in doc.GetElementsByTagName("value")) { var valueParam = comment.Parameters.FirstOrDefault(p => p.Name == "value"); if (valueParam != null) { valueParam.Desc = HandleNode(node); } } } return(comment.ToString()); }
public static string ToJavascriptName(IType type, IEmitter emitter) { if (type.Kind == TypeKind.Delegate) { var delegateName = BridgeTypes.ConvertName(type.FullName); if (!emitter.JsDoc.Callbacks.Contains(delegateName)) { var method = type.GetDelegateInvokeMethod(); JsDocComment comment = new JsDocComment(); var parameters = method.Parameters; if (parameters != null && parameters.Count > 0) { foreach (var param in parameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter); comment.Parameters.Add(jsParam); } } comment.Returns.Add(new JsDocParam { Type = XmlToJsDoc.ToJavascriptName(method.ReturnType, emitter) }); comment.Callback = delegateName; comment.MemberOf = type.Namespace; if (!emitter.JsDoc.Namespaces.Contains(type.Namespace)) { emitter.JsDoc.Namespaces.Add(type.Namespace); comment.Namespace = type.Namespace; } emitter.JsDoc.Callbacks.Add(delegateName); emitter.Output.Insert(0, comment.ToString() + newLine + newLine); } return delegateName; } if (type.IsKnownType(KnownTypeCode.String)) { return "string"; } if (type.IsKnownType(KnownTypeCode.Boolean)) { return "boolean"; } if (type.IsKnownType(KnownTypeCode.Void)) { return "void"; } if (type.IsKnownType(KnownTypeCode.Byte) || type.IsKnownType(KnownTypeCode.Char) || type.IsKnownType(KnownTypeCode.Decimal) || type.IsKnownType(KnownTypeCode.Double) || type.IsKnownType(KnownTypeCode.Int16) || type.IsKnownType(KnownTypeCode.Int32) || type.IsKnownType(KnownTypeCode.Int64) || type.IsKnownType(KnownTypeCode.SByte) || type.IsKnownType(KnownTypeCode.Single) || type.IsKnownType(KnownTypeCode.UInt16) || type.IsKnownType(KnownTypeCode.UInt32) || type.IsKnownType(KnownTypeCode.UInt64)) { return "number"; } if (type.Kind == TypeKind.Array) { ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type; return "Array.<" + XmlToJsDoc.ToJavascriptName(arrayType.ElementType, emitter) + ">"; } if (type.Kind == TypeKind.Dynamic) { return "object"; } if (type.Kind == TypeKind.Enum && type.DeclaringType != null) { return "number"; } if (NullableType.IsNullable(type)) { return "?" + XmlToJsDoc.ToJavascriptName(NullableType.GetUnderlyingType(type), emitter); } BridgeType bridgeType = emitter.BridgeTypes.Get(type, true); //string name = BridgeTypes.ConvertName(type.FullName); var name = type.Namespace; var hasTypeDef = bridgeType != null && bridgeType.TypeDefinition != null; if (hasTypeDef) { var typeDef = bridgeType.TypeDefinition; if (typeDef.IsNested) { name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(typeDef); } name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(typeDef.Name); } else { if (type.DeclaringType != null) { name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.GetParentNames(type); if (type.DeclaringType.TypeArguments.Count > 0) { name += "$" + type.TypeArguments.Count; } } name = (string.IsNullOrEmpty(name) ? "" : (name + ".")) + BridgeTypes.ConvertName(type.Name); } bool isCustomName = false; if (bridgeType != null) { name = BridgeTypes.AddModule(name, bridgeType, out isCustomName); } if (!hasTypeDef && !isCustomName && type.TypeArguments.Count > 0) { name += "$" + type.TypeArguments.Count; } return name; }
private static void InitMember(JsDocComment comment, IMember member, IEmitter emitter, object value) { if (member != null) { var method = member as IMethod; if (method != null) { comment.This = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter); if (method.IsConstructor) { comment.Constructs = comment.This; } if (method.TypeParameters != null && method.TypeParameters.Count > 0) { foreach (var param in method.TypeParameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = "Function"; comment.Parameters.Add(jsParam); } } } comment.Override = member.IsOverride; if (member is IParameterizedMember) { var parameters = ((IParameterizedMember)member).Parameters; if (parameters != null && parameters.Count > 0) { foreach (var param in parameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter); comment.Parameters.Add(jsParam); } } } var variable = member as IVariable; if (variable != null) { comment.MemberType = XmlToJsDoc.ToJavascriptName(variable.Type, emitter); } else { comment.Returns.Add(new JsDocParam { Type = XmlToJsDoc.ToJavascriptName(member.ReturnType, emitter) }); } var field = member as DefaultResolvedField; if (field != null) { comment.ReadOnly = field.IsReadOnly; comment.Const = field.IsConst; comment.Default = value ?? field.ConstantValue; } var ev = member as IEvent; if (ev != null) { comment.Event = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter) + "#" + member.Name; } comment.MemberOf = XmlToJsDoc.ToJavascriptName(member.DeclaringType, emitter); comment.IsPublic = member.IsPublic; comment.IsPrivate = member.IsPrivate; comment.IsProtected = member.IsProtected; var entity = member as ICSharpCode.NRefactory.TypeSystem.Implementation.AbstractResolvedEntity; if (entity != null) { comment.IsAbstract = entity.IsAbstract; comment.IsStatic = entity.IsStatic; } } }
private static string ReadComment(string source, ResolveResult rr, IEmitter emitter, JsDocComment comment) { var xml = new StringBuilder("<comment>" + newLine); if (source != null) { foreach (var line in source.Split(newLine)) { var trimmedLine = line.Trim(); if (string.IsNullOrEmpty(trimmedLine)) { continue; } xml.Append(System.Text.RegularExpressions.Regex.Replace(line, @"\/\/\/\s*", "") + newLine); } xml.Append("</comment>"); var doc = new System.Xml.XmlDocument(); try { doc.LoadXml(xml.ToString()); } catch (XmlException) { return ""; } foreach (XmlNode node in doc.GetElementsByTagName("summary")) { comment.Descriptions.Add(HandleNode(node)); } foreach (XmlNode node in doc.GetElementsByTagName("remark")) { comment.Remarks.Add(HandleNode(node)); } foreach (XmlNode node in doc.GetElementsByTagName("typeparam")) { string name = null; var attr = node.Attributes["name"]; if (attr != null) { name = attr.Value.Trim(); } var param = comment.Parameters.FirstOrDefault(p => p.Name == name); if (param == null) { param = new JsDocParam { Name = "[name]", Type = "[type]" }; comment.Parameters.Add(param); } attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value; } else if (rr != null) { param.Type = "Function"; } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("param")) { string name = null; var attr = node.Attributes["name"]; if (attr != null) { name = attr.Value.Trim(); } var param = comment.Parameters.FirstOrDefault(p => p.Name == name); if (param == null) { param = new JsDocParam { Name = "[name]", Type = "[type]" }; comment.Parameters.Add(param); } attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value; } else if (rr != null) { param.Type = XmlToJsDoc.GetParamTypeName(param.Name, rr, emitter); } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("returns")) { JsDocParam param = null; if (comment.Returns.Any()) { param = comment.Returns.FirstOrDefault(); } else { param = new JsDocParam { Type = "[type]" }; comment.Returns.Add(param); } var attr = node.Attributes["name"]; if (attr != null) { param.Name = attr.Value.Trim(); } attr = node.Attributes["type"]; if (attr != null) { param.Type = attr.Value.Trim(); } else if (rr != null) { param.Type = XmlToJsDoc.GetParamTypeName(null, rr, emitter); } var text = HandleNode(node); if (!string.IsNullOrEmpty(text)) { param.Desc = text; } } foreach (XmlNode node in doc.GetElementsByTagName("example")) { var codeNodes = node.SelectNodes("code"); StringBuilder sb = new StringBuilder(); foreach (XmlNode codeNode in codeNodes) { sb.Append(codeNode.InnerText + newLine); node.RemoveChild(codeNode); } var code = sb.ToString(); var caption = HandleNode(node); comment.Examples.Add(new Tuple<string, string>(caption, code)); } foreach (XmlNode node in doc.GetElementsByTagName("exception")) { var attr = node.Attributes["cref"]; var exceptionType = ""; if (attr != null) { try { exceptionType = XmlToJsDoc.ToJavascriptName(emitter.BridgeTypes.Get(attr.InnerText).Type, emitter); } catch { // ignored } } var caption = HandleNode(node); comment.Throws.Add(new Tuple<string, string>(caption, exceptionType)); } foreach (XmlNode node in doc.GetElementsByTagName("seealso")) { var attr = node.Attributes["cref"]; var cref = ""; if (attr != null) { cref = attr.InnerText; } comment.SeeAlso.Add(cref); } foreach (XmlNode node in doc.GetElementsByTagName("value")) { var valueParam = comment.Parameters.FirstOrDefault(p => p.Name == "value"); if (valueParam != null) { valueParam.Desc = HandleNode(node); } } } return comment.ToString(); }
public static string ToJavascriptName(IType type, IEmitter emitter) { if (type.Kind == TypeKind.Delegate) { var delegateName = BridgeTypes.ConvertName(type.FullName); if (!emitter.JsDoc.Callbacks.Contains(delegateName)) { var method = type.GetDelegateInvokeMethod(); JsDocComment comment = new JsDocComment(); var parameters = method.Parameters; if (parameters != null && parameters.Count > 0) { foreach (var param in parameters) { var jsParam = new JsDocParam(); jsParam.Name = param.Name; jsParam.Type = XmlToJsDoc.ToJavascriptName(param.Type, emitter); comment.Parameters.Add(jsParam); } } comment.Returns.Add(new JsDocParam { Type = XmlToJsDoc.ToJavascriptName(method.ReturnType, emitter) }); comment.Callback = delegateName; comment.MemberOf = type.Namespace; if (!emitter.JsDoc.Namespaces.Contains(type.Namespace)) { emitter.JsDoc.Namespaces.Add(type.Namespace); comment.Namespace = type.Namespace; } emitter.JsDoc.Callbacks.Add(delegateName); emitter.Output.Insert(0, comment.ToString() + newLine + newLine); } return(delegateName); } if (type.IsKnownType(KnownTypeCode.String)) { return("string"); } if (type.IsKnownType(KnownTypeCode.Boolean)) { return("boolean"); } if (type.IsKnownType(KnownTypeCode.Void)) { return("void"); } if (type.IsKnownType(KnownTypeCode.Byte) || type.IsKnownType(KnownTypeCode.Char) || type.IsKnownType(KnownTypeCode.Decimal) || type.IsKnownType(KnownTypeCode.Double) || type.IsKnownType(KnownTypeCode.Int16) || type.IsKnownType(KnownTypeCode.Int32) || type.IsKnownType(KnownTypeCode.Int64) || type.IsKnownType(KnownTypeCode.SByte) || type.IsKnownType(KnownTypeCode.Single) || type.IsKnownType(KnownTypeCode.UInt16) || type.IsKnownType(KnownTypeCode.UInt32) || type.IsKnownType(KnownTypeCode.UInt64)) { return("number"); } if (type.Kind == TypeKind.Array) { ICSharpCode.NRefactory.TypeSystem.ArrayType arrayType = (ICSharpCode.NRefactory.TypeSystem.ArrayType)type; return("Array.<" + XmlToJsDoc.ToJavascriptName(arrayType.ElementType, emitter) + ">"); } if (type.Kind == TypeKind.Dynamic) { return("object"); } if (type.Kind == TypeKind.Enum && type.DeclaringType != null) { return("number"); } if (NullableType.IsNullable(type)) { return("?" + XmlToJsDoc.ToJavascriptName(NullableType.GetUnderlyingType(type), emitter)); } BridgeType bridgeType = emitter.BridgeTypes.Get(type, true); string name = BridgeTypes.ConvertName(type.FullName); bool isCustomName = false; if (bridgeType != null) { name = BridgeTypes.AddModule(name, bridgeType, out isCustomName); } if (!isCustomName && type.TypeArguments.Count > 0) { name += "$" + type.TypeArguments.Count; } return(name); }