Skip to content

Commit b4d8345

Browse files
committed
Add component mapping
1 parent 96a1ad3 commit b4d8345

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeRenderingContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public abstract class CodeRenderingContext : IDisposable
3737

3838
public abstract void AddSourceMappingFor(SourceSpan node);
3939

40+
public abstract void AddComponentMapping(int length);
41+
4042
public abstract void RenderNode(IntermediateNode node);
4143

4244
public abstract void RenderNode(IntermediateNode node, IntermediateNodeWriter writer);

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ public static CSharpCodeWritingScope BuildClassDeclaration(
422422
}
423423

424424
writer.Write("class ");
425+
context.AddComponentMapping(name.Length);
425426
writer.Write(name);
426427

427428
if (typeParameters != null && typeParameters.Count > 0)

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultCodeRenderingContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class DefaultCodeRenderingContext : CodeRenderingContext
1919
private readonly List<ScopeInternal> _scopes;
2020

2121
private readonly PooledObject<ImmutableArray<SourceMapping>.Builder> _sourceMappingsBuilder;
22+
private readonly PooledObject<ImmutableArray<SourceSpan>.Builder> _componentMappingsBuilder;
2223

2324
public DefaultCodeRenderingContext(
2425
CodeWriter codeWriter,
@@ -61,6 +62,7 @@ public DefaultCodeRenderingContext(
6162
Diagnostics = new RazorDiagnosticCollection();
6263
Items = new ItemCollection();
6364
_sourceMappingsBuilder = ArrayBuilderPool<SourceMapping>.GetPooledObject();
65+
_componentMappingsBuilder = ArrayBuilderPool<SourceSpan>.GetPooledObject();
6466
LinePragmas = new List<LinePragma>();
6567

6668
var diagnostics = _documentNode.GetAllDiagnostics();
@@ -100,6 +102,8 @@ public DefaultCodeRenderingContext(
100102

101103
public ImmutableArray<SourceMapping>.Builder SourceMappings => _sourceMappingsBuilder.Object;
102104

105+
public ImmutableArray<SourceSpan>.Builder ComponentMappings => _componentMappingsBuilder.Object;
106+
103107
internal List<LinePragma> LinePragmas { get; }
104108

105109
public override IntermediateNodeWriter NodeWriter => Current.Writer;
@@ -142,6 +146,11 @@ public override void AddSourceMappingFor(SourceSpan source)
142146
SourceMappings.Add(sourceMapping);
143147
}
144148

149+
public override void AddComponentMapping(int length)
150+
{
151+
ComponentMappings.Add(new SourceSpan(CodeWriter.Location, length));
152+
}
153+
145154
public override void RenderChildren(IntermediateNode node)
146155
{
147156
if (node == null)
@@ -220,6 +229,7 @@ public override void AddLinePragma(LinePragma linePragma)
220229
public override void Dispose()
221230
{
222231
_sourceMappingsBuilder.Dispose();
232+
_componentMappingsBuilder.Dispose();
223233
}
224234

225235
private struct ScopeInternal

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/DefaultDocumentWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument
5353
_options,
5454
allOrderedDiagnostics.ToArray(),
5555
context.SourceMappings.DrainToImmutable(),
56+
context.ComponentMappings.DrainToImmutable(),
5657
context.LinePragmas.ToArray());
5758
}
5859

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/DefaultRazorCSharpDocument.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class DefaultRazorCSharpDocument : RazorCSharpDocument
1515
private readonly string _generatedCode;
1616
private readonly RazorDiagnostic[] _diagnostics;
1717
private readonly ImmutableArray<SourceMapping> _sourceMappings;
18+
private readonly ImmutableArray<SourceSpan> _componentMappings;
1819
private readonly LinePragma[] _linePragmas;
1920
private readonly RazorCodeGenerationOptions _options;
2021
private readonly RazorCodeDocument _codeDocument;
@@ -25,6 +26,7 @@ public DefaultRazorCSharpDocument(
2526
RazorCodeGenerationOptions options,
2627
RazorDiagnostic[] diagnostics,
2728
ImmutableArray<SourceMapping> sourceMappings,
29+
ImmutableArray<SourceSpan> componentMappings,
2830
LinePragma[] linePragmas)
2931
{
3032
if (generatedCode == null)
@@ -43,7 +45,9 @@ public DefaultRazorCSharpDocument(
4345

4446
_diagnostics = diagnostics ?? Array.Empty<RazorDiagnostic>();
4547
_sourceMappings = sourceMappings;
48+
_componentMappings = componentMappings;
4649
_linePragmas = linePragmas ?? Array.Empty<LinePragma>();
50+
_componentMappings = componentMappings;
4751
}
4852

4953
public override IReadOnlyList<RazorDiagnostic> Diagnostics => _diagnostics;
@@ -52,6 +56,8 @@ public DefaultRazorCSharpDocument(
5256

5357
public override ImmutableArray<SourceMapping> SourceMappings => _sourceMappings;
5458

59+
public override ImmutableArray<SourceSpan> ComponentMappings => _componentMappings;
60+
5561
internal override IReadOnlyList<LinePragma> LinePragmas => _linePragmas;
5662

5763
public override RazorCodeGenerationOptions Options => _options;

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*REMOVED*~Microsoft.AspNetCore.Razor.Language.TagHelperDescriptor.TagMatchingRules.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Razor.Language.TagMatchingRuleDescriptor>
99
*REMOVED*~Microsoft.AspNetCore.Razor.Language.TagMatchingRuleDescriptor.Attributes.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Razor.Language.RequiredAttributeDescriptor>
1010
*REMOVED*~virtual Microsoft.AspNetCore.Razor.Language.BoundAttributeDescriptor.BoundAttributeParameters.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Razor.Language.BoundAttributeParameterDescriptor>
11+
abstract Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeRenderingContext.AddComponentMapping(int length) -> void
1112
abstract Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeRenderingContext.AddSourceMappingFor(Microsoft.AspNetCore.Razor.Language.SourceSpan node) -> void
1213
abstract Microsoft.AspNetCore.Razor.Language.CodeGeneration.CodeRenderingContext.Dispose() -> void
1314
abstract Microsoft.AspNetCore.Razor.Language.MetadataCollection.ContainsKey(string! key) -> bool
@@ -17,6 +18,7 @@ abstract Microsoft.AspNetCore.Razor.Language.MetadataCollection.Keys.get -> Syst
1718
abstract Microsoft.AspNetCore.Razor.Language.MetadataCollection.this[string! key].get -> string?
1819
abstract Microsoft.AspNetCore.Razor.Language.MetadataCollection.TryGetValue(string! key, out string? value) -> bool
1920
abstract Microsoft.AspNetCore.Razor.Language.MetadataCollection.Values.get -> System.Collections.Generic.IEnumerable<string?>!
21+
abstract Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument.ComponentMappings.get -> System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.SourceSpan>
2022
abstract Microsoft.AspNetCore.Razor.Language.RazorSourceDocumentProperties.FilePath.get -> string?
2123
abstract Microsoft.AspNetCore.Razor.Language.RazorSourceDocumentProperties.RelativePath.get -> string?
2224
abstract Microsoft.AspNetCore.Razor.Language.TagHelperCollector<T>.Collect(Microsoft.CodeAnalysis.ISymbol! symbol, System.Collections.Generic.ICollection<Microsoft.AspNetCore.Razor.Language.TagHelperDescriptor!>! results) -> void
@@ -365,7 +367,7 @@ virtual Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.OnInitialized()
365367
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.SetImportSyntaxTrees(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree> syntaxTrees) -> void
366368
~static Microsoft.AspNetCore.Razor.Language.RazorCodeDocumentExtensions.SetPreTagHelperSyntaxTree(this Microsoft.AspNetCore.Razor.Language.RazorCodeDocument document, Microsoft.AspNetCore.Razor.Language.RazorSyntaxTree syntaxTree) -> void
367369
~static Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument.Create(Microsoft.AspNetCore.Razor.Language.RazorCodeDocument codeDocument, string generatedCode, Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions options, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.RazorDiagnostic> diagnostics) -> Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument
368-
~static Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument.Create(Microsoft.AspNetCore.Razor.Language.RazorCodeDocument codeDocument, string generatedCode, Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions options, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.RazorDiagnostic> diagnostics, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.SourceMapping> sourceMappings, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.CodeGeneration.LinePragma> linePragmas) -> Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument
370+
~static Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument.Create(Microsoft.AspNetCore.Razor.Language.RazorCodeDocument codeDocument, string generatedCode, Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions options, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.RazorDiagnostic> diagnostics, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.SourceMapping> sourceMappings, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.SourceSpan> componentMappings, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.CodeGeneration.LinePragma> linePragmas) -> Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument
369371
~static Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument.Create(string generatedCode, Microsoft.AspNetCore.Razor.Language.RazorCodeGenerationOptions options, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.RazorDiagnostic> diagnostics, System.Collections.Immutable.ImmutableArray<Microsoft.AspNetCore.Razor.Language.SourceMapping> sourceMappings, System.Collections.Generic.IEnumerable<Microsoft.AspNetCore.Razor.Language.CodeGeneration.LinePragma> linePragmas) -> Microsoft.AspNetCore.Razor.Language.RazorCSharpDocument
370372
~static Microsoft.AspNetCore.Razor.Language.TagHelperDescriptorBuilderExtensions.SetMetadata(this Microsoft.AspNetCore.Razor.Language.TagHelperDescriptorBuilder builder, System.Collections.Generic.KeyValuePair<string, string> pair) -> void
371373
~static Microsoft.AspNetCore.Razor.Language.TagHelperDescriptorBuilderExtensions.SetMetadata(this Microsoft.AspNetCore.Razor.Language.TagHelperDescriptorBuilder builder, System.Collections.Generic.KeyValuePair<string, string> pair1, System.Collections.Generic.KeyValuePair<string, string> pair2) -> void

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/RazorCSharpDocument.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public abstract class RazorCSharpDocument : IRazorGeneratedDocument
1717

1818
public abstract ImmutableArray<SourceMapping> SourceMappings { get; }
1919

20+
/// <summary>
21+
/// Maps spans from the generated source to the whole component.
22+
/// </summary>
23+
/// <remarks>
24+
/// Used to map the component class name, so go-to-definition navigates to the .razor file.
25+
/// </remarks>
26+
public abstract ImmutableArray<SourceSpan> ComponentMappings { get; }
27+
2028
public abstract IReadOnlyList<RazorDiagnostic> Diagnostics { get; }
2129

2230
public abstract RazorCodeGenerationOptions Options { get; }
@@ -31,7 +39,7 @@ public static RazorCSharpDocument Create(string generatedCode, RazorCodeGenerati
3139

3240
[Obsolete("For backwards compatibility only. Use the overload that takes a RazorCodeDocument")]
3341
public static RazorCSharpDocument Create(string generatedCode, RazorCodeGenerationOptions options, IEnumerable<RazorDiagnostic> diagnostics, ImmutableArray<SourceMapping> sourceMappings, IEnumerable<LinePragma> linePragmas)
34-
=> Create(codeDocument: null, generatedCode, options, diagnostics, sourceMappings, linePragmas);
42+
=> Create(codeDocument: null, generatedCode, options, diagnostics, sourceMappings, ImmutableArray<SourceSpan>.Empty, linePragmas);
3543

3644
public static RazorCSharpDocument Create(RazorCodeDocument codeDocument, string generatedCode, RazorCodeGenerationOptions options, IEnumerable<RazorDiagnostic> diagnostics)
3745
{
@@ -50,7 +58,7 @@ public static RazorCSharpDocument Create(RazorCodeDocument codeDocument, string
5058
throw new ArgumentNullException(nameof(diagnostics));
5159
}
5260

53-
return new DefaultRazorCSharpDocument(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings: ImmutableArray<SourceMapping>.Empty, linePragmas: null);
61+
return new DefaultRazorCSharpDocument(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings: ImmutableArray<SourceMapping>.Empty, componentMappings: ImmutableArray<SourceSpan>.Empty, linePragmas: null);
5462
}
5563

5664
public static RazorCSharpDocument Create(
@@ -59,6 +67,7 @@ public static RazorCSharpDocument Create(
5967
RazorCodeGenerationOptions options,
6068
IEnumerable<RazorDiagnostic> diagnostics,
6169
ImmutableArray<SourceMapping> sourceMappings,
70+
ImmutableArray<SourceSpan> componentMappings,
6271
IEnumerable<LinePragma> linePragmas)
6372
{
6473
if (generatedCode == null)
@@ -76,6 +85,6 @@ public static RazorCSharpDocument Create(
7685
throw new ArgumentNullException(nameof(diagnostics));
7786
}
7887

79-
return new DefaultRazorCSharpDocument(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings, linePragmas.ToArray());
88+
return new DefaultRazorCSharpDocument(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings, componentMappings, linePragmas.ToArray());
8089
}
8190
}

0 commit comments

Comments
 (0)