示例#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);
        }
示例#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));
        }
        public string GetIndent(string childIndent)
        {
            if (this.documentNode == null)
            {
                return(string.Empty);
            }
            DocumentCompositeNode parent = this.documentNode.Parent;

            if (parent == null)
            {
                return(string.Empty);
            }
            XamlSourceContext xamlSourceContext = (XamlSourceContext)null;

            if (this.documentNode.IsChild)
            {
                for (int index = this.documentNode.SiteChildIndex - 1; index >= 0; --index)
                {
                    xamlSourceContext = parent.Children[index].SourceContext as XamlSourceContext;
                    if (xamlSourceContext != null && xamlSourceContext.TextRange != null)
                    {
                        break;
                    }
                }
            }
            bool flag = false;

            while (parent != null && (xamlSourceContext == null || xamlSourceContext.TextRange == null))
            {
                xamlSourceContext = parent.SourceContext as XamlSourceContext ?? parent.ContainerSourceContext as XamlSourceContext;
                parent            = parent.Parent;
                flag = true;
            }
            if (xamlSourceContext == null)
            {
                return(string.Empty);
            }
            IReadableSelectableTextBuffer hostBuffer = TextBufferHelper.GetHostBuffer(xamlSourceContext.TextBuffer);
            ITextRange textRange = TextBufferHelper.ExpandSpanLeftToFillWhitespace((IReadableTextBuffer)hostBuffer, (ITextRange) new TextRange(xamlSourceContext.TextRange.Offset, xamlSourceContext.TextRange.Offset));
            string     str       = hostBuffer.GetText(textRange.Offset, textRange.Length);
            int        num       = str.LastIndexOf('\n');

            if (num != -1)
            {
                str = str.Substring(num + 1);
            }
            if (flag)
            {
                str += childIndent;
            }
            return(str);
        }
示例#4
0
        private static ITextRange CaculateTextRange(XamlSourceContext sourceContext, ITextBuffer textBuffer)
        {
            ITextRange        textRange        = sourceContext.TextRange;
            IMappingTextRange mappingTextRange = textRange as IMappingTextRange;

            if (mappingTextRange != null)
            {
                textRange = mappingTextRange.GetRangeForBuffer(textBuffer);
            }
            else if (TextBufferHelper.GetHostBuffer(sourceContext.TextBuffer) != TextBufferHelper.GetHostBuffer((IReadableSelectableTextBuffer)textBuffer))
            {
                return(TextRange.Null);
            }
            int offset = textRange.Offset;

            return((ITextRange) new TextRange(offset, offset + textRange.Length));
        }
示例#5
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);
        }