|
8 | 8 | using System.Text; |
9 | 9 | using Microsoft.AspNetCore.Razor; |
10 | 10 | using Microsoft.AspNetCore.Razor.Language; |
| 11 | +using Microsoft.AspNetCore.Razor.Language.Components; |
11 | 12 | using Microsoft.AspNetCore.Razor.Language.Syntax; |
12 | 13 | using Microsoft.AspNetCore.Razor.PooledObjects; |
13 | 14 | using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features; |
@@ -214,12 +215,7 @@ public void Generate() |
214 | 215 | var node = root.FindInnermostNode(originalSpan.AbsoluteIndex); |
215 | 216 | if (node is CSharpExpressionLiteralSyntax) |
216 | 217 | { |
217 | | - // Rather than bother to store more data about the formatted file, since we don't actually know where |
218 | | - // these will end up in that file once it's all said and done, we are just going to use a simple comment |
219 | | - // format that we can easily parse. |
220 | | - additionalLinesBuilder.AppendLine(GetAdditionalLineComment(originalSpan)); |
221 | | - additionalLinesBuilder.AppendLine(_sourceText.GetSubTextString(originalSpan.ToTextSpan())); |
222 | | - additionalLinesBuilder.AppendLine(";"); |
| 218 | + AddAdditionalLineFormattingContent(additionalLinesBuilder, node, originalSpan); |
223 | 219 | } |
224 | 220 |
|
225 | 221 | iMapping++; |
@@ -254,6 +250,34 @@ public void Generate() |
254 | 250 | _builder.AppendLine(additionalLinesBuilder.ToString()); |
255 | 251 | } |
256 | 252 |
|
| 253 | + private void AddAdditionalLineFormattingContent(StringBuilder additionalLinesBuilder, RazorSyntaxNode node, SourceSpan originalSpan) |
| 254 | + { |
| 255 | + // Rather than bother to store more data about the formatted file, since we don't actually know where |
| 256 | + // these will end up in that file once it's all said and done, we are just going to use a simple comment |
| 257 | + // format that we can easily parse. |
| 258 | + |
| 259 | + // Special case, for attributes that represent generic type parameters, we want to output something such |
| 260 | + // that Roslyn knows to format it as a type. For example, the meaning and spacing around "?"s should be |
| 261 | + // what the user expects. |
| 262 | + if (node is { Parent.Parent: MarkupTagHelperAttributeSyntax attribute } && |
| 263 | + attribute is { Parent.Parent: MarkupTagHelperElementSyntax element } && |
| 264 | + element.TagHelperInfo.BindingResult.Descriptors is [{ } descriptor] && |
| 265 | + descriptor.IsGenericTypedComponent() && |
| 266 | + descriptor.BoundAttributes.FirstOrDefault(d => d.Name == attribute.TagHelperAttributeInfo.Name) is { } boundAttribute && |
| 267 | + boundAttribute.IsTypeParameterProperty()) |
| 268 | + { |
| 269 | + additionalLinesBuilder.AppendLine("F<"); |
| 270 | + additionalLinesBuilder.AppendLine(GetAdditionalLineComment(originalSpan)); |
| 271 | + additionalLinesBuilder.AppendLine(_sourceText.GetSubTextString(originalSpan.ToTextSpan())); |
| 272 | + additionalLinesBuilder.AppendLine("> x;"); |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + additionalLinesBuilder.AppendLine(GetAdditionalLineComment(originalSpan)); |
| 277 | + additionalLinesBuilder.AppendLine(_sourceText.GetSubTextString(originalSpan.ToTextSpan())); |
| 278 | + additionalLinesBuilder.AppendLine(";"); |
| 279 | + } |
| 280 | + |
257 | 281 | public override LineInfo Visit(RazorSyntaxNode? node) |
258 | 282 | { |
259 | 283 | // Sometimes we are in a block where we want to do no formatting at all |
|
0 commit comments