Ejemplo n.º 1
0
        private XamlFormatter.XmlElementFormatting GetElementFormatting(ElementNode elementNode)
        {
            XamlFormatter.XmlElementFormatting elementFormatting = new XamlFormatter.XmlElementFormatting();
            XmlElementReference elementReference = elementNode.SourceContextReference.SourceContext as XmlElementReference;

            if (elementReference != null && elementReference.TextRange != null)
            {
                IReadableTextBuffer textBuffer = (IReadableTextBuffer)TextBufferHelper.GetHostBuffer(elementReference.TextBuffer);
                elementFormatting.LeadingWhitespace          = XamlFormatter.GetLeadingWhitespace((XamlSourceContext)elementReference);
                elementFormatting.TrailingWhitespace         = XamlFormatter.GetTrailingWhitespace((XamlSourceContext)elementReference);
                elementFormatting.AttributeLeadingWhitespace = XamlFormatter.GetTrailingWhitespace(textBuffer, elementReference.TextRange.Offset);
                if (elementReference.StartTagRange != null)
                {
                    elementFormatting.AttributeTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, elementReference.StartTagRange.Offset + elementReference.StartTagRange.Length - 1);
                    elementFormatting.ContentLeadingWhitespace    = XamlFormatter.GetTrailingWhitespace(textBuffer, elementReference.StartTagRange.Offset + elementReference.StartTagRange.Length);
                    int num = elementReference.TextRange.Offset + elementReference.TextRange.Length;
                    elementFormatting.ContentTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, num - (elementNode.Name.FullName.Length + 3));
                }
                else
                {
                    elementFormatting.AttributeTrailingWhitespace = XamlFormatter.GetLeadingWhitespace(textBuffer, elementReference.TextRange.Offset + elementReference.TextRange.Length - 2);
                    elementFormatting.ContentLeadingWhitespace    = (string)null;
                    elementFormatting.ContentTrailingWhitespace   = (string)null;
                }
            }
            return(elementFormatting);
        }
Ejemplo n.º 2
0
        private void WriteElement(string indent, ElementNode element)
        {
            ElementPersistenceSettings elementSettings = this.settings.GetElementSettings(typeof(object));

            XamlFormatter.XmlElementFormatting elementFormatting = this.GetElementFormatting(element);
            if (elementFormatting.LeadingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.LeadingWhitespace);
                if (!this.writer.Started || this.writer.LineBreakCount > 0)
                {
                    if (element.IndentingBehavior == IndentingBehavior.FromContainer)
                    {
                        this.writer.SetIndent(indent);
                    }
                    else
                    {
                        indent = this.writer.Indent;
                    }
                }
            }
            else
            {
                this.writer.SetMinimumLineBreaks(elementSettings.LinesOutside);
                this.writer.SetIndent(indent);
            }
            XmlElementReference elementReference = (XmlElementReference)null;

            if (this.shouldUpdateSourceContext && element.SourceContextReference != null)
            {
                elementReference = element.SourceContextReference.BuildXmlElementReference();
                element.SourceContextReference.SourceContext = (XamlSourceContext)elementReference;
            }
            int start = this.writer.WriteBeginStartElement(element.Name.FullName);

            if (elementFormatting.AttributeLeadingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.AttributeLeadingWhitespace);
            }
            string indent1 = indent + this.settings.IndentString;

            element.Attributes.EnsureOrdering();
            for (int index = 0; index < element.Attributes.Count; ++index)
            {
                this.WriteNode(indent1, element.Attributes[index]);
            }
            if (elementFormatting.AttributeTrailingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.AttributeTrailingWhitespace);
            }
            int end;
            int length;

            if (element.Children.Count > 0)
            {
                bool flag = element.IsWhitespaceSignificant;
                for (int index = 0; index < element.Children.Count; ++index)
                {
                    switch (element.Children[index].NodeType)
                    {
                    case NodeType.Literal:
                    case NodeType.WhitespaceLiteral:
                        flag = true;
                        break;
                    }
                }
                this.writer.WriteEndStartElement();
                end = this.writer.Length;
                if (elementFormatting.ContentLeadingWhitespace != null)
                {
                    this.writer.SetWhitespace(elementFormatting.ContentLeadingWhitespace);
                }
                else
                {
                    this.writer.SetMinimumLineBreaks(elementSettings.LinesInside);
                }
                for (int index = 0; index < element.Children.Count; ++index)
                {
                    FormattedNode node = element.Children[index];
                    if (flag)
                    {
                        this.writer.SuppressWhitespace();
                    }
                    this.WriteNode(indent1, node);
                    if (flag)
                    {
                        this.writer.SuppressWhitespace();
                    }
                }
                if (elementFormatting.ContentTrailingWhitespace != null)
                {
                    this.writer.SetWhitespace(elementFormatting.ContentTrailingWhitespace);
                    if (element.IndentingBehavior == IndentingBehavior.FromContainer && this.writer.LineBreakCount > 0)
                    {
                        this.writer.SetIndent(indent);
                    }
                }
                else
                {
                    this.writer.SetMinimumLineBreaks(elementSettings.LinesInside);
                    this.writer.SetIndent(indent);
                }
                this.writer.WriteEndElement(true);
                length = this.writer.Length;
            }
            else
            {
                this.writer.WriteEndElement(false);
                end    = -1;
                length = this.writer.Length;
            }
            if (this.shouldUpdateSourceContext && elementReference != null)
            {
                elementReference.InitialTextSpan = (ITextRange) new TextRange(start, length);
                if (end != -1)
                {
                    elementReference.InitialStartTagSpan = (ITextRange) new TextRange(start, end);
                }
                element.SourceContextReference.SourceContext = (XamlSourceContext)elementReference;
            }
            if (elementFormatting.TrailingWhitespace != null)
            {
                this.writer.SetWhitespace(elementFormatting.TrailingWhitespace);
            }
            else
            {
                this.writer.SetMinimumLineBreaks(elementSettings.LinesOutside);
            }
        }