Simplify ChunkTreeBuilder

This commit is contained in:
Ryan Nowak 2015-11-18 09:35:06 -08:00
parent 6ff10c4d6c
commit 5ce8740f70
7 changed files with 28 additions and 33 deletions

View File

@ -1,17 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNet.Razor.Chunks
{
public class ChunkTree
public class ChunkTree : ParentChunk
{
public ChunkTree()
{
Chunks = new List<Chunk>();
}
public IList<Chunk> Chunks { get; set; }
}
}

View File

@ -13,11 +13,14 @@ namespace Microsoft.AspNet.Razor.Chunks
public ChunkTreeBuilder()
{
ChunkTree = new ChunkTree();
Root = new ChunkTree();
_parentStack = new Stack<ParentChunk>();
_parentStack.Push(Root);
}
public ChunkTree ChunkTree { get; private set; }
public ParentChunk Current => _parentStack.Peek();
public ChunkTree Root { get; }
public void AddChunk(Chunk chunk, SyntaxTreeNode association, bool topLevel = false)
{
@ -27,13 +30,13 @@ namespace Microsoft.AspNet.Razor.Chunks
chunk.Association = association;
// If we're not in the middle of a parent chunk
if (_parentStack.Count == 0 || topLevel == true)
if (topLevel)
{
ChunkTree.Chunks.Add(chunk);
Root.Children.Add(chunk);
}
else
{
_parentStack.Peek().Children.Add(chunk);
Current.Children.Add(chunk);
}
}

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
// Populates the chunk tree with chunks associated with attributes
attribute.Value.Accept(chunkGenerator);
var chunks = chunkGenerator.Context.ChunkTreeBuilder.ChunkTree.Chunks;
var chunks = chunkGenerator.Context.ChunkTreeBuilder.Root.Children;
var first = chunks.FirstOrDefault();
attributeChunkValue = new ParentChunk

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators
{
}
private ChunkTree Tree { get { return Context.ChunkTreeBuilder.ChunkTree; } }
private ChunkTree Tree { get { return Context.ChunkTreeBuilder.Root; } }
public RazorEngineHost Host { get { return Context.Host; } }
/// <summary>
@ -64,10 +64,10 @@ namespace Microsoft.AspNet.Razor.CodeGenerators
var csharpCodeVisitor = CreateCSharpCodeVisitor(writer, Context);
new CSharpTypeMemberVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Chunks);
new CSharpTypeMemberVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Children);
CreateCSharpDesignTimeCodeVisitor(csharpCodeVisitor, writer, Context)
.AcceptTree(Tree);
new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Children);
BuildConstructor(writer);
@ -78,8 +78,8 @@ namespace Microsoft.AspNet.Razor.CodeGenerators
{
using (writer.BuildMethodDeclaration("public override async", "Task", Host.GeneratedClassContext.ExecuteMethodName))
{
new CSharpTagHelperRunnerInitializationVisitor(writer, Context).Accept(Tree.Chunks);
csharpCodeVisitor.Accept(Tree.Chunks);
new CSharpTagHelperRunnerInitializationVisitor(writer, Context).Accept(Tree.Children);
csharpCodeVisitor.Accept(Tree.Children);
}
}
}
@ -131,7 +131,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators
protected virtual CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
{
var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context);
baseTypeVisitor.Accept(Tree.Chunks);
baseTypeVisitor.Accept(Tree.Children);
var baseType = baseTypeVisitor.CurrentBaseType ?? Host.DefaultBaseClass;
@ -153,7 +153,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators
{
// Write out using directives
var usingVisitor = new CSharpUsingVisitor(writer, Context);
foreach (Chunk chunk in Tree.Chunks)
foreach (var chunk in Tree.Children)
{
usingVisitor.Accept(chunk);
}

View File

@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Razor.CodeGenerators.Visitors
protected virtual void AcceptTreeCore(ChunkTree tree)
{
Accept(tree.Chunks);
Accept(tree.Children);
}
protected override void Visit(SetBaseTypeChunk chunk)

View File

@ -402,7 +402,7 @@ namespace Microsoft.AspNet.Razor
var codeGeneratorResult = codeGenerator.Generate();
// Collect results and return
return new GeneratorResults(results, codeGeneratorResult, codeGeneratorContext.ChunkTreeBuilder.ChunkTree);
return new GeneratorResults(results, codeGeneratorResult, codeGeneratorContext.ChunkTreeBuilder.Root);
}
protected internal virtual RazorChunkGenerator CreateChunkGenerator(

View File

@ -26,12 +26,12 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
builder.EndParentChunk();
// Assert
Assert.Equal(2, builder.ChunkTree.Chunks.Count);
Assert.Equal(2, builder.Root.Children.Count);
var parentChunk = Assert.IsType<ExpressionBlockChunk>(builder.ChunkTree.Chunks.First());
var parentChunk = Assert.IsType<ExpressionBlockChunk>(builder.Root.Children.First());
Assert.Empty(parentChunk.Children);
var addTagHelperChunk = Assert.IsType<AddTagHelperChunk>(builder.ChunkTree.Chunks.Last());
var addTagHelperChunk = Assert.IsType<AddTagHelperChunk>(builder.Root.Children.Last());
Assert.Equal(addTagHelperChunk.LookupText, "some text");
}
@ -50,12 +50,12 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
builder.EndParentChunk();
// Assert
Assert.Equal(2, builder.ChunkTree.Chunks.Count);
Assert.Equal(2, builder.Root.Children.Count);
var parentChunk = Assert.IsType<ExpressionBlockChunk>(builder.ChunkTree.Chunks.First());
var parentChunk = Assert.IsType<ExpressionBlockChunk>(builder.Root.Children.First());
Assert.Empty(parentChunk.Children);
var removeTagHelperChunk = Assert.IsType<RemoveTagHelperChunk>(builder.ChunkTree.Chunks.Last());
var removeTagHelperChunk = Assert.IsType<RemoveTagHelperChunk>(builder.Root.Children.Last());
Assert.Equal(removeTagHelperChunk.LookupText, "some text");
}
@ -71,7 +71,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
builder.AddLiteralChunk("some text", previousSpan);
// Assert
var chunk = Assert.Single(builder.ChunkTree.Chunks);
var chunk = Assert.Single(builder.Root.Children);
var literalChunk = Assert.IsType<LiteralChunk>(chunk);
Assert.Equal("some text", literalChunk.Text);
Assert.Same(previousSpan, literalChunk.Association);
@ -91,7 +91,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
builder.AddLiteralChunk("<p>", newSpan);
// Assert
var chunk = Assert.Single(builder.ChunkTree.Chunks);
var chunk = Assert.Single(builder.Root.Children);
var literalChunk = Assert.IsType<LiteralChunk>(chunk);
Assert.Equal("<a><p>", literalChunk.Text);
var span = Assert.IsType<Span>(literalChunk.Association);
@ -114,7 +114,7 @@ namespace Microsoft.AspNet.Razor.Chunks.Generators
builder.AddLiteralChunk("<p>", literalSpan);
// Assert
var chunks = builder.ChunkTree.Chunks;
var chunks = builder.Root.Children;
Assert.Equal(2, chunks.Count);
var statementChunk = Assert.IsType<StatementChunk>(chunks[0]);
Assert.Equal("int a = 10;", statementChunk.Code);