Beispiel #1
0
        private LoveBlock VisitBlock(Block block)
        {
            // Handle regular block
            var lBlock = new LoveBlock(block.Start.AbsoluteIndex, block.Length);

            foreach (var c in block.Children)
            {
                var node = VisitNode(c);

                if (node != null)
                {
                    lBlock.Children.Add(node);
                }
            }

            // Merge blocks of same type
            var      newChildren = new List <LoveNode>();
            LoveNode newChild    = null;

            foreach (var c in lBlock.Children)
            {
                if (newChild != null && newChild.GetType() == c.GetType() && c is LoveSpan)
                {
                    var cSpan   = c as LoveSpan;
                    var newSpan = newChild as LoveSpan;

                    if (cSpan is LoveBinding)
                    {
                        newChild = new LoveBinding(newSpan.Start, newSpan.Length + cSpan.Length, newSpan.Content + cSpan.Content);
                    }
                    else if (cSpan is LoveMarkup)
                    {
                        newChild = new LoveMarkup(newSpan.Start, newSpan.Length + cSpan.Length, newSpan.Content + cSpan.Content);
                    }
                    else if (cSpan is LoveMarkupExpression)
                    {
                        newChild = new LoveMarkupExpression(newSpan.Start, newSpan.Length + cSpan.Length, newSpan.Content + cSpan.Content);
                    }
                }
                else
                {
                    if (newChild != null)
                    {
                        newChildren.Add(newChild);
                        newChild = null;
                    }

                    newChild = c;
                }
            }

            if (newChild != null)
            {
                newChildren.Add(newChild);
                newChild = null;
            }

            lBlock.Children.Clear();
            lBlock.Children.AddRange(newChildren);

            if (lBlock.Children.Count == 0)
            {
                return(null);
            }

            // Simplify children
            for (int i = 0; i < lBlock.Children.Count; i++)
            {
                var child = lBlock.Children[i];

                // Convert bindings to specific bindings if possible
                if (child is LoveBinding)
                {
                    lBlock.Children[i] = ConvertBinding(child as LoveBinding);
                }
            }

            ConvertChildrenToControlBlocks(lBlock);

            //// Convert to control blocks if Possible
            //var cBlock = ConvertToControlBlock(lBlock);
            //if (cBlock != null) { return cBlock; }

            return(lBlock);
        }