Exemplo n.º 1
0
        /// <summary>
        /// Returns the root nodes of all subtrees that are fully contained between <paramref name="start"/> and <paramref name="end"/> (inclusive).
        /// </summary>
        public IEnumerable <AstNode> GetNodesBetween(TextLocation start, TextLocation end)
        {
            AstNode node = this;

            while (node != null)
            {
                AstNode next;
                if (start <= node.StartLocation && node.EndLocation <= end)
                {
                    // Remember next before yielding node.
                    // This allows iteration to continue when the caller removes/replaces the node.
                    next = node.GetNextNode();
                    yield return(node);
                }
                else
                {
                    if (node.EndLocation <= start)
                    {
                        next = node.GetNextNode();
                    }
                    else
                    {
                        next = node.FirstChild;
                    }
                }

                if (next != null && next.StartLocation > end)
                {
                    yield break;
                }
                node = next;
            }
        }
        void FixAttributesAndDocComment(EntityDeclaration entity)
        {
            var node = entity.FirstChild;

            while (node != null && node.Role == Roles.Comment)
            {
                node = node.GetNextSibling(NoWhitespacePredicate);
                FixIndentation(node);
            }
            if (entity.Attributes.Count > 0)
            {
                AstNode n = null;
                entity.Attributes.First().AcceptVisitor(this);
                foreach (var attr in entity.Attributes.Skip(1))
                {
                    FixIndentation(attr);
                    attr.AcceptVisitor(this);
                    n = attr;
                }
                if (n != null)
                {
                    FixIndentation(n.GetNextNode(NoWhitespacePredicate));
                }
                else
                {
                    FixIndentation(entity.Attributes.First().GetNextNode(NoWhitespacePredicate));
                }
            }
        }
Exemplo n.º 3
0
        public void EnsureNewLinesAfter(AstNode node, int blankLines)
        {
            if (formatter.FormattingMode != FormattingMode.Intrusive)
            {
                blankLines = 1;
            }
            int     foundBlankLines = 0;
            var     nextNode        = node.GetNextNode();
            AstNode lastNewLine     = null;

            while (nextNode != null)
            {
                if (!(nextNode is NewLineNode))
                {
                    break;
                }
                lastNewLine = nextNode;
                foundBlankLines++;
                nextNode = nextNode.GetNextNode();
            }
            if (nextNode == null)
            {
                return;
            }
            var start = document.GetOffset(node.EndLocation);
            var end   = document.GetOffset((lastNewLine ?? nextNode).StartLocation);
            var sb    = new StringBuilder(options.EolMarker.Length * blankLines);

            for (int i = 0; i < blankLines + (lastNewLine != null ? -1 : 0); i++)
            {
                sb.Append(options.EolMarker);
            }
            AddChange(start, end - start, sb.ToString());
        }
Exemplo n.º 4
0
        void FixOpenBrace(BraceStyle braceStyle, AstNode lbrace)
        {
            if (lbrace.IsNull)
            {
                return;
            }
            switch (braceStyle)
            {
            case BraceStyle.DoNotChange:
                return;

            case BraceStyle.BannerStyle:
            case BraceStyle.EndOfLine:
                var prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                int prevOffset = document.GetOffset(prev.EndLocation);

                if (prev is Comment || prev is PreProcessorDirective)
                {
                    int next = document.GetOffset(lbrace.GetNextNode().StartLocation);
                    EnsureText(prevOffset, next, "");
                    while (prev is Comment || prev is PreProcessorDirective)
                    {
                        prev = prev.GetPrevNode();
                    }
                    prevOffset = document.GetOffset(prev.EndLocation);
                    AddChange(prevOffset, 0, " {");
                }
                else
                {
                    int braceOffset2 = document.GetOffset(lbrace.StartLocation);
                    EnsureText(prevOffset, braceOffset2, " ");
                }
                break;

            case BraceStyle.EndOfLineWithoutSpace:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset = document.GetOffset(prev.EndLocation);
                int braceOffset = document.GetOffset(lbrace.StartLocation);
                EnsureText(prevOffset, braceOffset, "");
                break;

            case BraceStyle.NextLine:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                break;

            case BraceStyle.NextLineShifted:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;

            case BraceStyle.NextLineShifted2:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;
            }
        }