Exemplo n.º 1
0
        public void ReplaceWith_NotifiesParentChildHasChanged()
        {
            // Arrange
            var spanBuilder = new SpanBuilder(SourceLocation.Zero);

            spanBuilder.Accept(SyntaxFactory.Token(SyntaxKind.HtmlTextLiteral, "hello"));
            var span         = spanBuilder.Build();
            var blockBuilder = new BlockBuilder()
            {
                Type = BlockKindInternal.Markup,
            };

            blockBuilder.Children.Add(span);
            var block = blockBuilder.Build();

            span.Parent = block;
            var originalBlockLength = block.Length;
            var newSpanBuilder      = new SpanBuilder(SourceLocation.Zero);

            newSpanBuilder.Accept(SyntaxFactory.Token(SyntaxKind.HtmlTextLiteral, "hi"));

            // Act
            span.ReplaceWith(newSpanBuilder);

            // Assert
            Assert.Equal(5, originalBlockLength);
            Assert.Equal(2, block.Length);
        }
Exemplo n.º 2
0
        protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
        {
            var newBlock = new BlockBuilder(block);

            newBlock.Children.Clear();
            var ws = block.Children.FirstOrDefault() as Span;
            IEnumerable <SyntaxTreeNode> newNodes = block.Children;

            if (ws.Content.All(char.IsWhiteSpace))
            {
                // Add this node to the parent
                var builder = new SpanBuilder(ws);
                builder.ClearSymbols();
                FillSpan(builder, ws.Start, ws.Content);
                parent.Children.Add(builder.Build());

                // Remove the old whitespace node
                newNodes = block.Children.Skip(1);
            }

            foreach (SyntaxTreeNode node in newNodes)
            {
                newBlock.Children.Add(node);
            }
            return(newBlock.Build());
        }
        private static Span GetSpan(SourceLocation start, string content)
        {
            var spanBuilder = new SpanBuilder(start);
            var tokens      = CSharpLanguageCharacteristics.Instance.TokenizeString(content).ToArray();

            foreach (var token in tokens)
            {
                spanBuilder.Accept(token);
            }
            var span = spanBuilder.Build();

            return(span);
        }
Exemplo n.º 4
0
        public void ReplaceWith_ResetsLength()
        {
            // Arrange
            var builder = new SpanBuilder(SourceLocation.Zero);

            builder.Accept(SyntaxFactory.Token(SyntaxKind.HtmlTextLiteral, "hello"));
            var span       = builder.Build();
            var newBuilder = new SpanBuilder(SourceLocation.Zero);

            newBuilder.Accept(SyntaxFactory.Token(SyntaxKind.HtmlTextLiteral, "hi"));
            var originalLength = span.Length;

            // Act
            span.ReplaceWith(newBuilder);

            // Assert
            Assert.Equal(5, originalLength);
            Assert.Equal(2, span.Length);
        }
Exemplo n.º 5
0
        public void ReplaceWith_ResetsLength()
        {
            // Arrange
            var builder = new SpanBuilder(SourceLocation.Zero);

            builder.Accept(new HtmlToken("hello", HtmlTokenType.Text));
            var span       = builder.Build();
            var newBuilder = new SpanBuilder(SourceLocation.Zero);

            newBuilder.Accept(new HtmlToken("hi", HtmlTokenType.Text));
            var originalLength = span.Length;

            // Act
            span.ReplaceWith(newBuilder);

            // Assert
            Assert.Equal(5, originalLength);
            Assert.Equal(2, span.Length);
        }
Exemplo n.º 6
0
        public void Clone_ClonesSpan()
        {
            // Arrange
            var spanBuilder = new SpanBuilder(new SourceLocation(1, 2, 3))
            {
                EditHandler    = new SpanEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString),
                Kind           = SpanKindInternal.Transition,
                ChunkGenerator = new ExpressionChunkGenerator(),
            };

            spanBuilder.Accept(new CSharpSymbol("@", CSharpSymbolType.Transition));
            var span = spanBuilder.Build();

            // Act
            var copy = (Span)span.Clone();

            // Assert
            Assert.Equal(span, copy);
            Assert.NotSame(span, copy);
        }
Exemplo n.º 7
0
        protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
        {
            // Collect the content of this node
            var builder = new StringBuilder();

            for (var i = 0; i < block.Children.Count; i++)
            {
                var childSpan = (Span)block.Children[i];
                builder.Append(childSpan.Content);
            }

            // Create a new span containing this content
            var span = new SpanBuilder(block.Children[0].Start);

            span.EditHandler = SpanEditHandler.CreateDefault(HtmlLanguageCharacteristics.Instance.TokenizeString);
            Debug.Assert(block.Children.Count > 0);
            var start = ((Span)block.Children[0]).Start;

            FillSpan(span, start, builder.ToString());
            return(span.Build());
        }
Exemplo n.º 8
0
        public void ChildChanged_NotifiesParent()
        {
            // Arrange
            var spanBuilder = new SpanBuilder(SourceLocation.Zero);

            spanBuilder.Accept(new HtmlSymbol("hello", HtmlSymbolType.Text));
            var span         = spanBuilder.Build();
            var blockBuilder = new BlockBuilder()
            {
                Type = BlockKindInternal.Markup,
            };

            blockBuilder.Children.Add(span);
            var childBlock = blockBuilder.Build();

            blockBuilder = new BlockBuilder()
            {
                Type = BlockKindInternal.Markup,
            };
            blockBuilder.Children.Add(childBlock);
            var parentBlock         = blockBuilder.Build();
            var originalBlockLength = parentBlock.Length;

            spanBuilder = new SpanBuilder(SourceLocation.Zero);
            spanBuilder.Accept(new HtmlSymbol("hi", HtmlSymbolType.Text));
            span.ReplaceWith(spanBuilder);

            // Wire up parents now so we can re-trigger ChildChanged to cause cache refresh.
            span.Parent       = childBlock;
            childBlock.Parent = parentBlock;

            // Act
            childBlock.ChildChanged();

            // Assert
            Assert.Equal(5, originalBlockLength);
            Assert.Equal(2, parentBlock.Length);
        }
Exemplo n.º 9
0
        public override SyntaxTreeNode Clone()
        {
            var spanBuilder = new SpanBuilder(this);

            return(spanBuilder.Build());
        }