Esempio n. 1
0
        private void WriteAttribute(string indent, AttributeNode attribute)
        {
            XamlFormatter.XmlAttributeFormatting attributeFormatting = this.GetAttributeFormatting(attribute);
            if (attributeFormatting.LeadingWhitespace != null)
            {
                this.writer.SetWhitespace(attributeFormatting.LeadingWhitespace);
                if (attribute.IndentingBehavior == IndentingBehavior.FromContainer && this.writer.LineBreakCount > 0)
                {
                    this.writer.SetIndent(indent);
                }
            }
            else if (this.writer.LineBreakCount == 0 && string.IsNullOrEmpty(this.writer.Indent))
            {
                this.writer.SetIndent(this.settings.AttributeIndentString);
            }
            StringBuilder builder = new StringBuilder(attribute.Name.FullName);
            char          attributeQuoteCharacter;

            if (attributeFormatting.ValuePrefix != null)
            {
                attributeQuoteCharacter = attributeFormatting.AttributeQuoteCharacter;
                builder.Append(attributeFormatting.ValuePrefix);
            }
            else
            {
                attributeQuoteCharacter = this.settings.AttributeQuoteCharacter;
                builder.Append('=');
                builder.Append(attributeQuoteCharacter);
            }
            this.WriteAttributeValue(attributeQuoteCharacter, builder, attribute.Value, attribute.EscapeWhitespace);
            if (attributeFormatting.ValueSuffix != null)
            {
                builder.Append(attributeFormatting.ValueSuffix);
            }
            else
            {
                builder.Append(attributeQuoteCharacter);
            }
            int start  = this.writer.WriteAttribute(builder.ToString());
            int length = this.writer.Length;

            if (this.shouldUpdateSourceContext && attribute.SourceContextReference != null)
            {
                XmlAttributeReference attributeReference = attribute.SourceContextReference.BuildXmlAttributeReference();
                attributeReference.InitialTextSpan             = (ITextRange) new TextRange(start, length);
                attribute.SourceContextReference.SourceContext = (XamlSourceContext)attributeReference;
            }
            if (attributeFormatting.TrailingWhitespace == null)
            {
                return;
            }
            this.writer.SetWhitespace(attributeFormatting.TrailingWhitespace);
        }
Esempio n. 2
0
        private XamlFormatter.XmlAttributeFormatting GetAttributeFormatting(AttributeNode attributeNode)
        {
            XamlFormatter.XmlAttributeFormatting attributeFormatting = new XamlFormatter.XmlAttributeFormatting();
            if (attributeNode.SourceContextReference == null)
            {
                return(attributeFormatting);
            }
            XmlAttributeReference attributeReference = attributeNode.SourceContextReference.SourceContext as XmlAttributeReference;

            if (attributeReference != null && attributeReference.TextRange != null)
            {
                string text = TextBufferHelper.GetHostBuffer(attributeReference.TextBuffer).GetText(attributeReference.TextRange.Offset, attributeReference.TextRange.Length);
                int    num  = text.IndexOf('=');
                if (num == -1)
                {
                    return(attributeFormatting);
                }
                int index1 = num - 1;
                while (index1 >= 0 && Scanner.IsXmlWhitespace(text[index1]))
                {
                    --index1;
                }
                int index2 = num + 1;
                while (index2 < text.Length && Scanner.IsXmlWhitespace(text[index2]))
                {
                    ++index2;
                }
                int startIndex = text.LastIndexOf(text[index2]);
                if (index2 >= 0 && startIndex > index2)
                {
                    attributeFormatting.LeadingWhitespace       = XamlFormatter.GetLeadingWhitespace((XamlSourceContext)attributeReference);
                    attributeFormatting.TrailingWhitespace      = XamlFormatter.GetTrailingWhitespace((XamlSourceContext)attributeReference);
                    attributeFormatting.ValuePrefix             = text.Substring(index1 + 1, index2 - index1);
                    attributeFormatting.ValueSuffix             = text.Substring(startIndex, 1);
                    attributeFormatting.AttributeQuoteCharacter = text[index2];
                }
            }
            return(attributeFormatting);
        }