Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use incremental source generator #60

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ C# and Razor compiler playground in the browser via Blazor WebAssembly. https://
- VSCode Monaco Editor.
- Multiple input sources (especially useful for interlinked Razor components).
- C# Language Services (completions, live diagnostics) - experimental.
- Configuring any C# parse options (e.g., LangVersion, Features).
- Configuring any C# options (e.g., LangVersion, Features, OptimizationLevel, AllowUnsafe).

## Development

Expand Down
40 changes: 20 additions & 20 deletions src/App/Lab/InitialCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

internal sealed record InitialCode
{
public static readonly InitialCode CSharp = new("Program.cs", """
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

class Program
{
static void Main()
{
Console.WriteLine("Hello.");
}
}

""");

public static readonly InitialCode Razor = new("TestComponent.razor", """
<TestComponent Param="1" />

Expand All @@ -25,26 +45,6 @@ @using Microsoft.JSInterop

""");

public static readonly InitialCode CSharp = new("Program.cs", """
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

class Program
{
static void Main()
{
Console.WriteLine("Hello.");
}
}

""");

public static readonly InitialCode Cshtml = new("TestPage.cshtml", """
@page
@using System.ComponentModel.DataAnnotations
Expand Down
16 changes: 8 additions & 8 deletions src/App/Lab/SavedState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ static WellKnownSlugs()
{
IEnumerable<(string Shorthand, string Title, SavedState State)> entries =
[
("razor", "Razor", SavedState.Razor),
("csharp", "C#", SavedState.CSharp),
("razor", "Razor", SavedState.Razor),
("cshtml", "CSHTML", SavedState.Cshtml),
];

Expand Down Expand Up @@ -190,20 +190,20 @@ internal sealed record SavedState
RazorToolchain = RazorToolchain.SourceGeneratorOrInternalApi,
};

public static SavedState Initial => Razor;

public static SavedState Razor { get; } = defaults with
{
Inputs = [InitialCode.Razor.ToInputCode(), InitialCode.RazorImports.ToInputCode()],
SelectedOutputType = "gcs",
};
public static SavedState Initial => CSharp;

public static SavedState CSharp { get; } = defaults with
{
Inputs = [InitialCode.CSharp.ToInputCode()],
SelectedOutputType = "run",
};

public static SavedState Razor { get; } = defaults with
{
Inputs = [InitialCode.Razor.ToInputCode(), InitialCode.RazorImports.ToInputCode()],
SelectedOutputType = "gcs",
};

public static SavedState Cshtml { get; } = defaults with
{
Inputs = [InitialCode.Cshtml.ToInputCode()],
Expand Down
10 changes: 5 additions & 5 deletions src/App/Lab/TemplateCache.cs

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions src/Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ You can try selecting different Razor toolchain in Settings / Advanced.

private (CompilationInput Input, CompiledAssembly Output)? lastResult;

/// <summary>
/// Reused for incremental source generation.
/// </summary>
private GeneratorDriver? generatorDriver;

public CompiledAssembly Compile(
CompilationInput input,
ImmutableDictionary<string, ImmutableArray<byte>>? assemblies,
Expand Down Expand Up @@ -338,18 +343,28 @@ static CSharpCompilationOptions createCompilationOptions(OutputKind outputKind)
references: references,
options: options);

var driver = CSharpGeneratorDriver.Create(
generators: [new RazorSourceGenerator().AsSourceGenerator()],
additionalTexts: additionalTextsBuilder.ToImmutable(),
parseOptions: parseOptions,
optionsProvider: optionsProvider);
if (generatorDriver is null)
{
generatorDriver = CSharpGeneratorDriver.Create(
generators: [new RazorSourceGenerator().AsSourceGenerator()],
additionalTexts: additionalTextsBuilder.ToImmutable(),
parseOptions: parseOptions,
optionsProvider: optionsProvider);
}
else
{
generatorDriver = generatorDriver
.ReplaceAdditionalTexts(additionalTextsBuilder.ToImmutable())
.WithUpdatedParseOptions(parseOptions)
.WithUpdatedAnalyzerConfigOptions(optionsProvider);
}

driver = (CSharpGeneratorDriver)driver.RunGeneratorsAndUpdateCompilation(
generatorDriver = (CSharpGeneratorDriver)generatorDriver.RunGeneratorsAndUpdateCompilation(
initialCompilation,
out var finalCommonCompilation,
out var generatorDiagnostics);

razorResult = driver.GetRunResult().Results.FirstOrDefault();
razorResult = generatorDriver.GetRunResult().Results.FirstOrDefault();

var finalCompilation = (CSharpCompilation)finalCommonCompilation;

Expand Down