Beispiel #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);
        }
Beispiel #2
0
        private static string GetTrailingWhitespace(XamlSourceContext sourceContext)
        {
            IReadableTextBuffer textBuffer = (IReadableTextBuffer)TextBufferHelper.GetHostBuffer(sourceContext.TextBuffer);

            if (textBuffer.Length == 0)
            {
                return(string.Empty);
            }
            return(XamlFormatter.GetTrailingWhitespace(textBuffer, sourceContext.TextRange.Offset + sourceContext.TextRange.Length));
        }
Beispiel #3
0
        private XamlFormatter.UnprocessedNodeFormatting GetNodeFormatting(UnprocessedNode unprocessedNode)
        {
            XamlFormatter.UnprocessedNodeFormatting unprocessedNodeFormatting = new XamlFormatter.UnprocessedNodeFormatting();
            XamlSourceContext sourceContext = unprocessedNode.SourceContextReference.SourceContext;

            if (sourceContext != null && sourceContext.TextRange != null)
            {
                unprocessedNodeFormatting.LeadingWhitespace  = XamlFormatter.GetLeadingWhitespace(sourceContext);
                unprocessedNodeFormatting.TrailingWhitespace = XamlFormatter.GetTrailingWhitespace(sourceContext);
            }
            return(unprocessedNodeFormatting);
        }
Beispiel #4
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);
        }