/// <summary> /// Adds a new attribute to this directive. /// </summary> /// <param name="attribute">The attribute to add.</param> /// <returns>A new instance of <see cref="IT4DirectiveAttribute"/>, representing <paramref name="attribute"/> in the T4 file.</returns> public IT4DirectiveAttribute AddAttribute(IT4DirectiveAttribute attribute) { if (attribute == null) { throw new ArgumentNullException("attribute"); } using (WriteLockCookie.Create(IsPhysical())) { ITreeNode lastNode = LastChild; Assertion.AssertNotNull(lastNode, "lastNode != null"); ITreeNode anchor = IsEndNode(lastNode) ? lastNode.PrevSibling : lastNode; Assertion.AssertNotNull(anchor, "anchor != null"); bool addSpaceAfter = anchor.GetTokenType() == T4TokenNodeTypes.Space; bool addSpaceBefore = !addSpaceAfter; if (addSpaceBefore) { anchor = ModificationUtil.AddChildAfter(anchor, T4TokenNodeTypes.Space.CreateLeafElement()); } IT4DirectiveAttribute result = ModificationUtil.AddChildAfter(anchor, attribute); if (addSpaceAfter) { ModificationUtil.AddChildAfter(result, T4TokenNodeTypes.Space.CreateLeafElement()); } return(result); } }
/// <summary> /// Adds a new attribute to this directive. /// </summary> /// <param name="attribute">The attribute to add.</param> /// <returns>A new instance of <see cref="IT4DirectiveAttribute"/>, representing <paramref name="attribute"/> in the T4 file.</returns> public IT4DirectiveAttribute AddAttribute(IT4DirectiveAttribute attribute) { if (attribute == null) throw new ArgumentNullException("attribute"); using (WriteLockCookie.Create(IsPhysical())) { ITreeNode lastNode = LastChild; Assertion.AssertNotNull(lastNode, "lastNode != null"); ITreeNode anchor = IsEndNode(lastNode) ? lastNode.PrevSibling : lastNode; Assertion.AssertNotNull(anchor, "anchor != null"); bool addSpaceAfter = anchor.GetTokenType() == T4TokenNodeTypes.Space; bool addSpaceBefore = !addSpaceAfter; if (addSpaceBefore) anchor = ModificationUtil.AddChildAfter(anchor, T4TokenNodeTypes.Space.CreateLeafElement()); IT4DirectiveAttribute result = ModificationUtil.AddChildAfter(anchor, attribute); if (addSpaceAfter) ModificationUtil.AddChildAfter(result, T4TokenNodeTypes.Space.CreateLeafElement()); return result; } }
public static IT4Token GetAttributeValueToken([CanBeNull] this IT4Directive directive, [CanBeNull] string attributeName) { if (directive == null || String.IsNullOrEmpty(attributeName)) { return(null); } IT4DirectiveAttribute attribute = directive.GetAttribute(attributeName); if (attribute == null) { return(null); } return(attribute.GetValueToken()); }