Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add extensibility point for TH attribute code gen.
Browse files Browse the repository at this point in the history
- Followed the "decorate" pattern that we use throughout Razor.
- Enabled the CSharpCodeBuilder to decorate the CSharpCodeVisitor which exposes a CSharpTagHelperCodeRenderer which is indirectly used to render attribute values.
- Fixed up some existing classes that just new'd up the CSharpCodeVisitor class to instead take in the decorated version.

#119
  • Loading branch information
NTaylorMullen committed Sep 30, 2014
1 parent 23ddd12 commit 48cdc40
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public override CodeBuilderResult Build()
writer.WriteLine("private static object @__o;");
}

var csharpCodeVisitor = DecorateCSharpCodeVisitor(writer,
Context,
new CSharpCodeVisitor(writer, Context));

new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpHelperVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpHelperVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Chunks);
new CSharpTypeMemberVisitor(csharpCodeVisitor, writer, Context).Accept(Tree.Chunks);
new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);

BuildConstructor(writer);
Expand All @@ -53,7 +57,7 @@ public override CodeBuilderResult Build()
{
using (writer.BuildMethodDeclaration("public override async", "Task", Host.GeneratedClassContext.ExecuteMethodName))
{
new CSharpCodeVisitor(writer, Context).Accept(Tree.Chunks);
csharpCodeVisitor.Accept(Tree.Chunks);
}
}
}
Expand All @@ -62,6 +66,13 @@ public override CodeBuilderResult Build()
return new CodeBuilderResult(writer.GenerateCode(), writer.LineMappingManager.Mappings);
}

protected virtual CSharpCodeVisitor DecorateCSharpCodeVisitor([NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context,
[NotNull] CSharpCodeVisitor incomingVisitor)
{
return incomingVisitor;
}

protected virtual CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
{
var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context);
Expand Down Expand Up @@ -102,7 +113,7 @@ private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<
string taskNamespace = typeof(Task).Namespace;

// We need to add the task namespace but ONLY if it hasn't been added by the default imports or using imports yet.
if(!defaultImports.Contains(taskNamespace) && !usingVisitor.ImportedUsings.Contains(taskNamespace))
if (!defaultImports.Contains(taskNamespace) && !usingVisitor.ImportedUsings.Contains(taskNamespace))
{
writer.WriteUsing(taskNamespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public class CSharpTagHelperCodeRenderer
private static readonly TagHelperAttributeDescriptorComparer AttributeDescriptorComparer =
new TagHelperAttributeDescriptorComparer();

// TODO: The work to properly implement this will be done in: https://github.com/aspnet/Razor/issues/74
private readonly TagHelperAttributeValueCodeRenderer _attributeValueCodeRenderer =
new TagHelperAttributeValueCodeRenderer();
private readonly TagHelperAttributeValueCodeRenderer _attributeValueCodeRenderer;
private readonly CSharpCodeWriter _writer;
private readonly CodeBuilderContext _context;
private readonly IChunkVisitor _bodyVisitor;
Expand All @@ -40,12 +38,14 @@ public class CSharpTagHelperCodeRenderer
/// <param name="context">A <see cref="CodeBuilderContext"/> instance that contains information about
/// the current code generation process.</param>
public CSharpTagHelperCodeRenderer([NotNull] IChunkVisitor bodyVisitor,
[NotNull] TagHelperAttributeValueCodeRenderer attributeValueCodeRenderer,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context)
{
_bodyVisitor = bodyVisitor;
_attributeValueCodeRenderer = attributeValueCodeRenderer;
_writer = writer;
_context = context;
_bodyVisitor = bodyVisitor;
_tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ public class CSharpCodeVisitor : CodeVisitor<CSharpCodeWriter>
private const string TemplateWriterName = "__razor_template_writer";

private CSharpPaddingBuilder _paddingBuilder;
private CSharpTagHelperCodeRenderer _tagHelperCodeRenderer;

public CSharpCodeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
: base(writer, context)
{
_paddingBuilder = new CSharpPaddingBuilder(context.Host);
_tagHelperCodeRenderer = new CSharpTagHelperCodeRenderer(this, writer, context);
TagHelperRenderer = new CSharpTagHelperCodeRenderer(this,
new TagHelperAttributeValueCodeRenderer(),
writer,
context);
}

public CSharpTagHelperCodeRenderer TagHelperRenderer { get; set; }

protected override void Visit(TagHelperChunk chunk)
{
_tagHelperCodeRenderer.RenderTagHelper(chunk);
if (TagHelperRenderer != null)
{
TagHelperRenderer.RenderTagHelper(chunk);
}
}

protected override void Visit(ChunkBlock chunk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public class CSharpHelperVisitor : CodeVisitor<CSharpCodeWriter>
{
private const string HelperWriterName = "__razor_helper_writer";

private CSharpCodeVisitor _codeVisitor;
private CSharpCodeVisitor _csharpCodeVisitor;

public CSharpHelperVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
public CSharpHelperVisitor([NotNull] CSharpCodeVisitor csharpCodeVisitor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context)
: base(writer, context)
{
_codeVisitor = new CSharpCodeVisitor(writer, context);
_csharpCodeVisitor = csharpCodeVisitor;
}

protected override void Visit(HelperChunk chunk)
Expand Down Expand Up @@ -46,7 +48,7 @@ protected override void Visit(HelperChunk chunk)
Context.TargetWriterName = HelperWriterName;

// Generate children code
_codeVisitor.Accept(chunk.Children);
_csharpCodeVisitor.Accept(chunk.Children);

Context.TargetWriterName = currentTargetWriterName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class CSharpTypeMemberVisitor : CodeVisitor<CSharpCodeWriter>
{
private CSharpCodeVisitor _csharpCodeVisitor;

public CSharpTypeMemberVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
public CSharpTypeMemberVisitor([NotNull] CSharpCodeVisitor csharpCodeVisitor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context)
: base(writer, context)
{
_csharpCodeVisitor = new CSharpCodeVisitor(writer, context);
_csharpCodeVisitor = csharpCodeVisitor;
}

protected override void Visit(TypeMemberChunk chunk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;

Expand All @@ -31,10 +28,10 @@ public class TagHelperAttributeValueCodeRenderer
/// Writes the string: "new MyPropertyType(...)" to the output where the "..." is rendered by calling the
/// <paramref name="renderAttributeValue"/> <see cref="Action"/>.
/// </remarks>
public void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context,
[NotNull] Action<CSharpCodeWriter> renderAttributeValue)
public virtual void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext context,
[NotNull] Action<CSharpCodeWriter> renderAttributeValue)
{
renderAttributeValue(writer);
}
Expand Down

0 comments on commit 48cdc40

Please sign in to comment.