/// <summary> /// This is used to write a string followed by an optional line break if needed (the writer position is /// past the maximum position afterwards). /// </summary> /// <param name="writer">The syntax writer to use</param> /// <param name="text">An optional text string to write before the new line</param> /// <param name="indent">An optional indent to write after the line break</param> /// <returns>True if a new line was written, false if not</returns> public static bool WriteWithLineBreakIfNeeded(SyntaxWriter writer, string text, string indent) { if (!String.IsNullOrEmpty(text)) { writer.WriteString(text); } if (writer.Position > MaxPosition) { writer.WriteLine(); if (!String.IsNullOrEmpty(indent)) { writer.WriteString(indent); } return(true); } return(false); }
/// <summary> /// This is used to write a string followed by an optional line break if needed (the writer position is /// past the maximum position afterwards). /// </summary> /// <param name="writer">The syntax writer to use</param> /// <param name="text">An optional text string to write before the new line</param> /// <param name="indent">An optional indent to write after the line break</param> /// <returns>True if a new line was written, false if not</returns> public static bool WriteWithLineBreakIfNeeded(SyntaxWriter writer, string text, string indent) { if(!String.IsNullOrEmpty(text)) writer.WriteString(text); if(writer.Position > MaxPosition) { writer.WriteLine(); if(!String.IsNullOrEmpty(indent)) writer.WriteString(indent); return true; } return false; }
/// <summary> /// Write attached property syntax /// </summary> /// <param name="reflection">The reflection data used to produce the syntax</param> /// <param name="writer">The writer to which the syntax is written</param> public virtual void WriteAttachedPropertySyntax(XPathNavigator reflection, SyntaxWriter writer) { string getterId = (string)reflection.Evaluate(attachedPropertyGetterExpression); string setterId = (string)reflection.Evaluate(attachedPropertySetterExpression); if(!string.IsNullOrEmpty(getterId)) { writer.WriteString("See "); writer.WriteReferenceLink(getterId); } if(!string.IsNullOrEmpty(setterId)) { if(!string.IsNullOrEmpty(getterId)) writer.WriteString(", "); writer.WriteReferenceLink(setterId); } }
/// <summary> /// Write attached event syntax /// </summary> /// <param name="reflection">The reflection data used to produce the syntax</param> /// <param name="writer">The writer to which the syntax is written</param> public virtual void WriteAttachedEventSyntax(XPathNavigator reflection, SyntaxWriter writer) { string adderId = (string)reflection.Evaluate(attachedEventAdderExpression); string removerId = (string)reflection.Evaluate(attachedEventRemoverExpression); if(!(string.IsNullOrEmpty(adderId) && string.IsNullOrEmpty(removerId))) { writer.WriteString("See "); writer.WriteReferenceLink(adderId); writer.WriteString(", "); writer.WriteReferenceLink(removerId); } }
/// <summary> /// Write out a constant's value /// </summary> /// <param name="parent">The parent node</param> /// <param name="writer">The syntax writer</param> protected virtual void WriteConstantValue(XPathNavigator parent, SyntaxWriter writer) { XPathNavigator type = parent.SelectSingleNode(returnsTypeExpression); XPathNavigator value = parent.SelectSingleNode(returnsValueExpression); switch(value.LocalName) { case "nullValue": writer.WriteKeyword("null"); break; case "enumValue": XPathNodeIterator fields = value.SelectChildren(XPathNodeType.Element); while(fields.MoveNext()) { string name = fields.Current.GetAttribute("name", String.Empty); if(fields.CurrentPosition > 1) writer.WriteString("|"); this.WriteTypeReference(type, writer); writer.WriteString("."); writer.WriteString(name); } break; case "value": string text = value.Value; string typeId = type.GetAttribute("api", String.Empty); switch(typeId) { case "T:System.String": writer.WriteString("\""); writer.WriteString(text); writer.WriteString("\""); break; case "T:System.Boolean": writer.WriteKeyword(Convert.ToBoolean(text, CultureInfo.InvariantCulture) ? "true" : "false"); break; case "T:System.Char": writer.WriteString("'"); writer.WriteString(text); writer.WriteString("'"); break; // Decimal constants get converted to static read-only fields so no need to handle them here case "T:System.Byte": case "T:System.Double": case "T:System.SByte": case "T:System.Int16": case "T:System.Int64": case "T:System.Int32": case "T:System.UInt16": case "T:System.UInt32": case "T:System.UInt64": writer.WriteString(text); break; case "T:System.Single": writer.WriteString(text); writer.WriteString("f"); break; default: // If not a recognized type, just write out the value so that something shows writer.WriteString(text); break; } break; } }
/// <summary> /// Write out a type reference /// </summary> /// <param name="reference">The type reference to output</param> /// <param name="writer">The syntax writer to which the type reference is written</param> protected virtual void WriteTypeReference(XPathNavigator reference, SyntaxWriter writer) { switch(reference.LocalName) { case "arrayOf": int rank = Convert.ToInt32(reference.GetAttribute("rank", String.Empty), CultureInfo.InvariantCulture); XPathNavigator element = reference.SelectSingleNode(typeExpression); WriteTypeReference(element, writer); writer.WriteString("["); for(int i = 1; i < rank; i++) writer.WriteString(","); writer.WriteString("]"); break; case "pointerTo": XPathNavigator pointee = reference.SelectSingleNode(typeExpression); WriteTypeReference(pointee, writer); writer.WriteString("*"); break; case "referenceTo": XPathNavigator referee = reference.SelectSingleNode(typeExpression); WriteTypeReference(referee, writer); break; case "type": string id = reference.GetAttribute("api", String.Empty); WriteNormalTypeReference(id, writer); XPathNodeIterator typeModifiers = reference.Select(typeModifiersExpression); while(typeModifiers.MoveNext()) WriteTypeReference(typeModifiers.Current, writer); break; case "template": string name = reference.GetAttribute("name", String.Empty); writer.WriteString(name); XPathNodeIterator modifiers = reference.Select(typeModifiersExpression); while(modifiers.MoveNext()) WriteTypeReference(modifiers.Current, writer); break; case "specialization": writer.WriteString("<"); XPathNodeIterator arguments = reference.Select(specializationArgumentsExpression); while(arguments.MoveNext()) { if(arguments.CurrentPosition > 1) writer.WriteString(", "); WriteTypeReference(arguments.Current, writer); } writer.WriteString(">"); break; } }
/// <summary> /// Write out a constant's value /// </summary> /// <param name="parent">The parent node</param> /// <param name="writer">The syntax writer</param> protected virtual void WriteConstantValue(XPathNavigator parent, SyntaxWriter writer) { XPathNavigator type = parent.SelectSingleNode(returnsTypeExpression); XPathNavigator value = parent.SelectSingleNode(returnsValueExpression); switch (value.LocalName) { case "nullValue": writer.WriteKeyword("null"); break; case "enumValue": XPathNodeIterator fields = value.SelectChildren(XPathNodeType.Element); while (fields.MoveNext()) { string name = fields.Current.GetAttribute("name", String.Empty); if (fields.CurrentPosition > 1) { writer.WriteString("|"); } this.WriteTypeReference(type, writer); writer.WriteString("."); writer.WriteString(name); } break; case "value": string text = value.Value; string typeId = type.GetAttribute("api", String.Empty); switch (typeId) { case "T:System.String": writer.WriteString("\""); writer.WriteString(text); writer.WriteString("\""); break; case "T:System.Boolean": writer.WriteKeyword(Convert.ToBoolean(text, CultureInfo.InvariantCulture) ? "true" : "false"); break; case "T:System.Char": writer.WriteString("'"); writer.WriteString(text); writer.WriteString("'"); break; // Decimal constants get converted to static read-only fields so no need to handle them here case "T:System.Byte": case "T:System.Double": case "T:System.SByte": case "T:System.Int16": case "T:System.Int64": case "T:System.Int32": case "T:System.UInt16": case "T:System.UInt32": case "T:System.UInt64": writer.WriteString(text); break; case "T:System.Single": writer.WriteString(text); writer.WriteString("f"); break; default: // If not a recognized type, just write out the value so that something shows writer.WriteString(text); break; } break; } }
/// <summary> /// Write out a type reference /// </summary> /// <param name="reference">The type reference to output</param> /// <param name="writer">The syntax writer to which the type reference is written</param> protected virtual void WriteTypeReference(XPathNavigator reference, SyntaxWriter writer) { switch (reference.LocalName) { case "arrayOf": int rank = Convert.ToInt32(reference.GetAttribute("rank", String.Empty), CultureInfo.InvariantCulture); XPathNavigator element = reference.SelectSingleNode(typeExpression); WriteTypeReference(element, writer); writer.WriteString("["); for (int i = 1; i < rank; i++) { writer.WriteString(","); } writer.WriteString("]"); break; case "pointerTo": XPathNavigator pointee = reference.SelectSingleNode(typeExpression); WriteTypeReference(pointee, writer); writer.WriteString("*"); break; case "referenceTo": XPathNavigator referee = reference.SelectSingleNode(typeExpression); WriteTypeReference(referee, writer); break; case "type": string id = reference.GetAttribute("api", String.Empty); WriteNormalTypeReference(id, writer); XPathNodeIterator typeModifiers = reference.Select(typeModifiersExpression); while (typeModifiers.MoveNext()) { WriteTypeReference(typeModifiers.Current, writer); } break; case "template": string name = reference.GetAttribute("name", String.Empty); writer.WriteString(name); XPathNodeIterator modifiers = reference.Select(typeModifiersExpression); while (modifiers.MoveNext()) { WriteTypeReference(modifiers.Current, writer); } break; case "specialization": writer.WriteString("<"); XPathNodeIterator arguments = reference.Select(specializationArgumentsExpression); while (arguments.MoveNext()) { if (arguments.CurrentPosition > 1) { writer.WriteString(", "); } WriteTypeReference(arguments.Current, writer); } writer.WriteString(">"); break; } }
/// <summary> /// Write out a type reference /// </summary> /// <param name="reference">The type reference to output</param> /// <param name="writer">The syntax writer to which the type reference is written</param> protected virtual void WriteTypeReference(XPathNavigator reference, SyntaxWriter writer) { switch (reference.LocalName) { case "arrayOf": int rank = Convert.ToInt32(reference.GetAttribute("rank", String.Empty), CultureInfo.InvariantCulture); XPathNavigator element = reference.SelectSingleNode(typeExpression); WriteTypeReference(element, writer); writer.WriteString("["); for (int i = 1; i < rank; i++) { writer.WriteString(","); } writer.WriteString("]"); break; case "pointerTo": XPathNavigator pointee = reference.SelectSingleNode(typeExpression); WriteTypeReference(pointee, writer); writer.WriteString("*"); break; case "referenceTo": XPathNavigator referee = reference.SelectSingleNode(typeExpression); WriteTypeReference(referee, writer); break; case "type": string id = reference.GetAttribute("api", String.Empty); XPathNodeIterator typeModifiers = reference.Select(typeModifiersExpression); // !EFW - Support value tuple syntax if (id.StartsWith("T:System.ValueTuple`", StringComparison.Ordinal)) { writer.WriteString("("); while (typeModifiers.MoveNext()) { XPathNodeIterator args = typeModifiers.Current.Select(specializationArgumentsExpression); while (args.MoveNext()) { if (args.CurrentPosition > 1) { writer.WriteString(", "); } WriteTypeReference(args.Current, writer); var elementName = args.Current.GetAttribute("elementName", String.Empty); if (elementName != null) { writer.WriteString(" "); writer.WriteString(elementName); } } } writer.WriteString(")"); } else { WriteNormalTypeReference(id, writer); while (typeModifiers.MoveNext()) { WriteTypeReference(typeModifiers.Current, writer); } } break; case "template": string name = reference.GetAttribute("name", String.Empty); writer.WriteString(name); XPathNodeIterator modifiers = reference.Select(typeModifiersExpression); while (modifiers.MoveNext()) { WriteTypeReference(modifiers.Current, writer); } break; case "specialization": writer.WriteString("<"); XPathNodeIterator arguments = reference.Select(specializationArgumentsExpression); while (arguments.MoveNext()) { if (arguments.CurrentPosition > 1) { writer.WriteString(", "); } WriteTypeReference(arguments.Current, writer); } writer.WriteString(">"); break; } }