-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge rendermode feature to main. Code flow only PR.
- Loading branch information
Showing
120 changed files
with
3,985 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...mpiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentRenderModeDirective.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
#nullable enable | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language.Components; | ||
|
||
internal sealed class ComponentRenderModeDirective | ||
{ | ||
public static readonly DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective( | ||
"rendermode", | ||
DirectiveKind.SingleLine, | ||
builder => | ||
{ | ||
builder.AddIdentifierOrExpression(ComponentResources.RenderModeDirective_Token_Name, ComponentResources.RenderModeDirective_Token_Description); | ||
builder.Usage = DirectiveUsage.FileScopedSinglyOccurring; | ||
builder.Description = ComponentResources.RenderModeDirective_Documentation; | ||
}); | ||
|
||
public static void Register(RazorProjectEngineBuilder builder) | ||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
builder.AddDirective(Directive, FileKinds.Component); | ||
builder.Features.Add(new ComponentRenderModeDirectivePass()); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...er/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentRenderModeDirectivePass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
#nullable enable | ||
|
||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Razor.Language.Intermediate; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language.Components; | ||
|
||
internal sealed class ComponentRenderModeDirectivePass : IntermediateNodePassBase, IRazorDirectiveClassifierPass | ||
{ | ||
private const string GeneratedRenderModeAttributeName = "__PrivateComponentRenderModeAttribute"; | ||
|
||
protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) | ||
{ | ||
var @namespace = documentNode.FindPrimaryNamespace(); | ||
var @class = documentNode.FindPrimaryClass(); | ||
if (@namespace == null || @class == null) | ||
{ | ||
return; | ||
} | ||
|
||
var directives = documentNode.FindDirectiveReferences(ComponentRenderModeDirective.Directive); | ||
if (directives.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
// We don't need to worry about duplicate attributes as we have already replaced any multiples with MalformedDirective | ||
Debug.Assert(directives.Count == 1); | ||
|
||
var child = ((DirectiveIntermediateNode)directives[0].Node).Children.FirstOrDefault(); | ||
if (child == null) | ||
{ | ||
return; | ||
} | ||
|
||
// generate the inner attribute class | ||
var classDecl = new ClassDeclarationIntermediateNode() | ||
{ | ||
ClassName = GeneratedRenderModeAttributeName, | ||
BaseType = $"global::{ComponentsApi.RenderModeAttribute.FullTypeName}", | ||
}; | ||
classDecl.Modifiers.Add("private"); | ||
classDecl.Modifiers.Add("sealed"); | ||
classDecl.Children.Add(new CSharpCodeIntermediateNode() | ||
{ | ||
Children = | ||
{ | ||
new IntermediateToken() | ||
{ | ||
Kind = TokenKind.CSharp, | ||
Content = $"private static global::{ComponentsApi.IComponentRenderMode.FullTypeName} ModeImpl => " | ||
}, | ||
new CSharpCodeIntermediateNode() | ||
{ | ||
Source = child.Source, | ||
Children = | ||
{ | ||
child is not DirectiveTokenIntermediateNode directiveToken | ||
? child | ||
: new IntermediateToken() | ||
{ | ||
Kind = TokenKind.CSharp, | ||
Content = directiveToken.Content | ||
} | ||
} | ||
}, | ||
new IntermediateToken() | ||
{ | ||
Kind = TokenKind.CSharp, | ||
Content = ";" | ||
} | ||
} | ||
}); | ||
classDecl.Children.Add(new CSharpCodeIntermediateNode() | ||
{ | ||
Children = | ||
{ | ||
new IntermediateToken() | ||
{ | ||
Kind = TokenKind.CSharp, | ||
Content = $"public override global::{ComponentsApi.IComponentRenderMode.FullTypeName} Mode => ModeImpl;" | ||
} | ||
} | ||
}); | ||
@class.Children.Add(classDecl); | ||
|
||
// generate the attribute usage on top of the class | ||
var attributeNode = new CSharpCodeIntermediateNode(); | ||
attributeNode.Children.Add(new IntermediateToken() | ||
{ | ||
Kind = TokenKind.CSharp, | ||
Content = $"[global::{@namespace.Content}.{@class.ClassName}.{GeneratedRenderModeAttributeName}]", | ||
}); | ||
|
||
// Insert the new attribute on top of the class | ||
var childCount = @namespace.Children.Count; | ||
for (var i = 0; i < childCount; i++) | ||
{ | ||
if (object.ReferenceEquals(@namespace.Children[i], @class)) | ||
{ | ||
@namespace.Children.Insert(i, attributeNode); | ||
break; | ||
} | ||
} | ||
Debug.Assert(@namespace.Children.Count == childCount + 1); | ||
} | ||
} |
Oops, something went wrong.