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 25, 2014
1 parent cfffc06 commit 84e32d5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public override CodeBuilderResult Build()
writer.WriteLine("private static object @__o;");
}

new CSharpHelperVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
var csharpCodeVisitor = DecorateCSharpCodeVisitor(writer,
Context,
new CSharpCodeVisitor(writer, Context));

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

Expand All @@ -55,7 +59,7 @@ public override CodeBuilderResult Build()
{
new CSharpTagHelperDeclarationVisitor(writer, Context).Accept(Tree.Chunks);

new CSharpCodeVisitor(writer, Context).Accept(Tree.Chunks);
csharpCodeVisitor.Accept(Tree.Chunks);
}
}
}
Expand All @@ -64,6 +68,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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,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 @@ -41,12 +39,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 Expand Up @@ -363,7 +363,7 @@ private void RenderBufferedAttributeValueAccessor()

private void RenderBufferedAttributeValue(TagHelperAttributeDescriptor attributeDescriptor)
{
RenderAttribute(
RenderAttributeValue(
attributeDescriptor,
valueRenderer: (writer) =>
{
Expand All @@ -373,7 +373,7 @@ private void RenderBufferedAttributeValue(TagHelperAttributeDescriptor attribute

private void RenderRawAttributeValue(string value, TagHelperAttributeDescriptor attributeDescriptor)
{
RenderAttribute(
RenderAttributeValue(
attributeDescriptor,
valueRenderer: (writer) =>
{
Expand All @@ -383,7 +383,7 @@ private void RenderRawAttributeValue(string value, TagHelperAttributeDescriptor

private void RenderQuotedAttributeValue(string value, TagHelperAttributeDescriptor attributeDescriptor)
{
RenderAttribute(
RenderAttributeValue(
attributeDescriptor,
valueRenderer: (writer) =>
{
Expand Down Expand Up @@ -417,8 +417,8 @@ private void BuildBufferedWritingScope(Action renderCode)
}
}

private void RenderAttribute(TagHelperAttributeDescriptor attributeDescriptor,
Action<CSharpCodeWriter> valueRenderer)
private void RenderAttributeValue(TagHelperAttributeDescriptor attributeDescriptor,
Action<CSharpCodeWriter> valueRenderer)
{
_attributeValueCodeRenderer.RenderAttributeValue(attributeDescriptor, _writer, _context, valueRenderer);
}
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 84e32d5

Please sign in to comment.