|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Razor.Language; |
| 10 | +using Microsoft.AspNetCore.Razor.Language.Components; |
| 11 | +using Microsoft.AspNetCore.Razor.Language.Syntax; |
| 12 | +using Microsoft.AspNetCore.Razor.Threading; |
| 13 | +using Microsoft.CodeAnalysis.Razor.CodeActions.Models; |
| 14 | +using Microsoft.CodeAnalysis.Razor.GoToDefinition; |
| 15 | +using Microsoft.CodeAnalysis.Razor.Logging; |
| 16 | +using Microsoft.CodeAnalysis.Razor.Protocol; |
| 17 | +using Microsoft.VisualStudio.LanguageServer.Protocol; |
| 18 | + |
| 19 | +namespace Microsoft.CodeAnalysis.Razor.CodeActions.Razor; |
| 20 | + |
| 21 | +internal class SimplifyTagToSelfClosingCodeActionProvider(ILoggerFactory loggerFactory) : IRazorCodeActionProvider |
| 22 | +{ |
| 23 | + private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<SimplifyTagToSelfClosingCodeActionProvider>(); |
| 24 | + |
| 25 | + private const string RenderFragmentTypeName = "Microsoft.AspNetCore.Components.RenderFragment"; |
| 26 | + private const string GenericRenderFragmentTypeName = "Microsoft.AspNetCore.Components.RenderFragment<"; |
| 27 | + |
| 28 | + public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeActionContext context, CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + if (context.HasSelection) |
| 31 | + { |
| 32 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 33 | + } |
| 34 | + |
| 35 | + // Make sure we're in the right kind and part of file |
| 36 | + if (!FileKinds.IsComponent(context.CodeDocument.FileKind)) |
| 37 | + { |
| 38 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 39 | + } |
| 40 | + |
| 41 | + if (context.LanguageKind != RazorLanguageKind.Html) |
| 42 | + { |
| 43 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 44 | + } |
| 45 | + |
| 46 | + // Caret must be inside a markup element |
| 47 | + if (context.ContainsDiagnostic(ComponentDiagnosticFactory.UnexpectedMarkupElement.Id) || |
| 48 | + context.ContainsDiagnostic(ComponentDiagnosticFactory.UnexpectedClosingTag.Id)) |
| 49 | + { |
| 50 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 51 | + } |
| 52 | + |
| 53 | + var syntaxTree = context.CodeDocument.GetSyntaxTree(); |
| 54 | + if (syntaxTree?.Root is null) |
| 55 | + { |
| 56 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 57 | + } |
| 58 | + |
| 59 | + var owner = syntaxTree.Root.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: !context.HasSelection)?.FirstAncestorOrSelf<MarkupTagHelperElementSyntax>(); |
| 60 | + if (owner is not MarkupTagHelperElementSyntax markupElementSyntax) |
| 61 | + { |
| 62 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 63 | + } |
| 64 | + |
| 65 | + // Check whether the code action is applicable to the element |
| 66 | + if (!IsApplicableTo(context, markupElementSyntax)) |
| 67 | + { |
| 68 | + return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); |
| 69 | + } |
| 70 | + |
| 71 | + // Provide code action to simplify |
| 72 | + var actionParams = new SimplifyTagToSelfClosingCodeActionParams |
| 73 | + { |
| 74 | + Start = markupElementSyntax.StartTag.CloseAngle.SpanStart, |
| 75 | + End = markupElementSyntax.EndTag.CloseAngle.EndPosition, |
| 76 | + }; |
| 77 | + |
| 78 | + var resolutionParams = new RazorCodeActionResolutionParams() |
| 79 | + { |
| 80 | + TextDocument = context.Request.TextDocument, |
| 81 | + Action = LanguageServerConstants.CodeActions.SimplifyTagToSelfClosingAction, |
| 82 | + Language = RazorLanguageKind.Razor, |
| 83 | + DelegatedDocumentUri = context.DelegatedDocumentUri, |
| 84 | + Data = actionParams, |
| 85 | + }; |
| 86 | + |
| 87 | + var codeAction = RazorCodeActionFactory.CreateSimplifyTagToSelfClosing(resolutionParams); |
| 88 | + return Task.FromResult<ImmutableArray<RazorVSInternalCodeAction>>([codeAction]); |
| 89 | + } |
| 90 | + |
| 91 | + internal bool IsApplicableTo(RazorCodeActionContext context, MarkupTagHelperElementSyntax markupElementSyntax) |
| 92 | + { |
| 93 | + // Check whether the element is self-closing |
| 94 | + if (markupElementSyntax is not ( |
| 95 | + { EndTag.CloseAngle.IsMissing: false } and |
| 96 | + { StartTag.ForwardSlash: null } and |
| 97 | + { StartTag.CloseAngle.IsMissing: false } |
| 98 | + )) |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + // Check whether the element has any non-whitespace content |
| 104 | + if (markupElementSyntax is { Body: { } body } && body.Any(static n => !n.ContainsOnlyWhitespace())) |
| 105 | + { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + // Get symbols for the markup element |
| 110 | + if (!RazorComponentDefinitionHelpers.TryGetBoundTagHelpers(context.CodeDocument, markupElementSyntax.StartTag.Name.SpanStart, true, _logger, out var boundTagHelper, out _)) |
| 111 | + { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + if (!boundTagHelper.IsComponentTagHelper) |
| 116 | + { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + // Check whether the Component must have children |
| 121 | + if (boundTagHelper.BoundAttributes.Any(attribute => |
| 122 | + // Attribute has `EditorRequired` flag |
| 123 | + attribute is { TypeName: string typeName, IsEditorRequired: true } && |
| 124 | + |
| 125 | + // It has type of a `RenderFragment` |
| 126 | + (typeName == RenderFragmentTypeName || typeName.StartsWith(GenericRenderFragmentTypeName, StringComparison.Ordinal)) && |
| 127 | + |
| 128 | + // It is not set or bound as an attribute |
| 129 | + !markupElementSyntax.TagHelperInfo!.BindingResult.Attributes.Any(a => |
| 130 | + a.Key == attribute.Name || |
| 131 | + (a.Key.StartsWith("@bind-", StringComparison.Ordinal) && a.Key.AsSpan("@bind-".Length).Equals(attribute.Name, StringComparison.Ordinal)) |
| 132 | + ) |
| 133 | + )) |
| 134 | + { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | +} |
0 commit comments