Skip to content

Commit

Permalink
Add support for defining sections
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Nov 29, 2023
1 parent 25be9cf commit 5e4a9db
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/RazorBlade.Analyzers.Tests/RazorBladeSourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ public Task should_reject_tag_helper_directives()
);
}

[Test]
public Task should_handle_sections()
{
return Verify(
"""
Before section
@section SectionName { Section content }
After section
@section OtherSectionName { Answer is @(42) }
"""
);
}

private static GeneratorDriverRunResult Generate(string input,
string? csharpCode,
bool embeddedLibrary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//HintName: TestNamespace.TestFile.Razor.g.cs
#pragma checksum "./TestFile.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "c5ddc67791758895b8ee73dd2a45225d3418acdb"
// <auto-generated/>
#pragma warning disable 1591
namespace TestNamespace
{
#line hidden
#nullable restore
internal partial class TestFile : global::RazorBlade.HtmlTemplate
#nullable disable
{
#pragma warning disable 1998
protected async override global::System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("Before section\r\n");
DefineSection("SectionName", async() => {
WriteLiteral(" Section content ");
}
);
WriteLiteral("After section\r\n");
DefineSection("OtherSectionName", async() => {
WriteLiteral(" Answer is ");
#nullable restore
#line (4,41)-(4,43) 6 "./TestFile.cshtml"
Write(42);

#line default
#line hidden
#nullable disable
WriteLiteral(" ");
}
);
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
2 changes: 2 additions & 0 deletions src/RazorBlade.Analyzers/RazorBladeSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -112,6 +113,7 @@ private static RazorCSharpDocument GenerateRazorCode(SourceText sourceText, Inpu
cfg =>
{
ModelDirective.Register(cfg);
SectionDirective.Register(cfg);

cfg.SetCSharpLanguageVersion(globalOptions.ParseOptions.LanguageVersion);

Expand Down
26 changes: 25 additions & 1 deletion src/RazorBlade.Library/RazorTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,6 +14,8 @@ namespace RazorBlade;
/// </summary>
public abstract class RazorTemplate : IEncodedContent
{
private readonly Dictionary<string, Func<Task>> _sections = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The <see cref="TextWriter"/> which receives the output.
/// </summary>
Expand Down Expand Up @@ -169,6 +173,26 @@ protected internal virtual void Write(IEncodedContent? content)
[EditorBrowsable(EditorBrowsableState.Never)]
protected internal abstract void EndWriteAttribute();

/// <summary>
/// Defines a section.
/// </summary>
/// <param name="name">The name of the section.</param>
/// <param name="action">The action which renders the section.</param>
[PublicAPI]
[EditorBrowsable(EditorBrowsableState.Never)]
protected internal void DefineSection(string name, Func<Task> action)
{
#if NETCOREAPP
if (!_sections.TryAdd(name, action))
throw new InvalidOperationException($"Section '{name}' is already defined.");
#else
if (_sections.ContainsKey(name))
throw new InvalidOperationException($"Section '{name}' is already defined.");

_sections[name] = action;
#endif
}

void IEncodedContent.WriteTo(TextWriter textWriter)
=> Render(textWriter, CancellationToken.None);
}

0 comments on commit 5e4a9db

Please sign in to comment.