-
Notifications
You must be signed in to change notification settings - Fork 209
#11793 Simplify tag to self-closing code action #11802
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
Merged
davidwengier
merged 22 commits into
dotnet:main
from
Peter-Juhasz:simplifytagtoselfclosing
Jul 1, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e15fe5b
fuse on by default for 17.14 (#11426)
0424172
Update dependencies from https://github.com/dotnet/arcade build 20250…
dotnet-maestro[bot] 632a3f3
[release/dev17.14] Update dependencies from dotnet/arcade (#11515)
akhera99 6e4b433
Merge remote-tracking branch 'upstream/main' into merge_main_to_17_14
akhera99 bfd5764
Snap main to 17.14 P3 (#11549)
akhera99 efda00b
Move static accessor into seperate method so we don't JIT it unless w…
chsienki 1874a91
[release/dev17.14] Fix extra JITing (#11559)
akhera99 a51562b
Move cohost options to explicit class (#11566)
github-actions[bot] 10596b2
Snap 17.14p3 (merge `main` commits to `release/dev17.14`) (#11688)
jjonescz e5361d0
Fuse/fix debugging info (#11658)
chsienki 4ccba78
Add back serialization tests
DustinCampbell 4a69788
[release/dev17.14] Add serialization tests back (#11695)
DustinCampbell 4fb2a6b
#11793 Simplify tag to self-closing code action
Peter-Juhasz 0102275
Improvements:
Peter-Juhasz 83bca1d
Simplify `IsChildContentProperty` call
Peter-Juhasz 152b091
Simplify descriptor access on markup element
Peter-Juhasz 7acb7ec
Move parsing attribute name to syntax facts
Peter-Juhasz f7df559
Fix build warnings
Peter-Juhasz 91f6d31
Review findings
Peter-Juhasz e92382f
Merge remote-tracking branch 'upstream/main' into simplifytagtoselfcl…
davidwengier d648574
Simplify self-closing tag check
davidwengier cc5272c
Merge remote-tracking branch 'upstream/main' into simplifytagtoselfcl…
davidwengier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
...oft.CodeAnalysis.Razor.Workspaces/CodeActions/Models/SimplifyComponentCodeActionParams.cs
This file contains hidden or 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,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.CodeActions.Models; | ||
|
||
internal sealed class SimplifyTagToSelfClosingCodeActionParams | ||
{ | ||
[JsonPropertyName("startTagCloseAngleIndex")] | ||
public int StartTagCloseAngleIndex { get; set; } | ||
|
||
[JsonPropertyName("endTagCloseAngleIndex")] | ||
public int EndTagCloseAngleIndex { get; set; } | ||
} |
This file contains hidden or 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
137 changes: 137 additions & 0 deletions
137
...Analysis.Razor.Workspaces/CodeActions/Razor/SimplifyTagToSelfClosingCodeActionProvider.cs
This file contains hidden or 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,137 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.Components; | ||
using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
using Microsoft.AspNetCore.Razor.Threading; | ||
using Microsoft.CodeAnalysis.Razor.CodeActions.Models; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.CodeActions.Razor; | ||
|
||
internal class SimplifyTagToSelfClosingCodeActionProvider : IRazorCodeActionProvider | ||
{ | ||
public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeActionContext context, CancellationToken cancellationToken) | ||
{ | ||
if (context.HasSelection) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
// Make sure we're in the right kind and part of file | ||
if (!FileKinds.IsComponent(context.CodeDocument.FileKind)) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
if (context.LanguageKind != RazorLanguageKind.Html) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
// Caret must be inside a markup element | ||
if (context.ContainsDiagnostic(ComponentDiagnosticFactory.UnexpectedMarkupElement.Id) || | ||
context.ContainsDiagnostic(ComponentDiagnosticFactory.UnexpectedClosingTag.Id)) | ||
davidwengier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
var syntaxTree = context.CodeDocument.GetSyntaxTree(); | ||
if (syntaxTree?.Root is null) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
var owner = syntaxTree.Root.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: false)?.FirstAncestorOrSelf<MarkupTagHelperElementSyntax>(); | ||
if (owner is not MarkupTagHelperElementSyntax markupElementSyntax) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
// Check whether the code action is applicable to the element | ||
if (!IsApplicableTo(markupElementSyntax)) | ||
{ | ||
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
} | ||
|
||
// Provide code action to simplify | ||
var actionParams = new SimplifyTagToSelfClosingCodeActionParams | ||
{ | ||
StartTagCloseAngleIndex = markupElementSyntax.StartTag.CloseAngle.SpanStart, | ||
EndTagCloseAngleIndex = markupElementSyntax.EndTag.CloseAngle.EndPosition, | ||
}; | ||
|
||
var resolutionParams = new RazorCodeActionResolutionParams() | ||
{ | ||
TextDocument = context.Request.TextDocument, | ||
Action = LanguageServerConstants.CodeActions.SimplifyTagToSelfClosingAction, | ||
Language = RazorLanguageKind.Razor, | ||
DelegatedDocumentUri = context.DelegatedDocumentUri, | ||
Data = actionParams, | ||
}; | ||
|
||
var codeAction = RazorCodeActionFactory.CreateSimplifyTagToSelfClosing(resolutionParams); | ||
return Task.FromResult<ImmutableArray<RazorVSInternalCodeAction>>([codeAction]); | ||
} | ||
|
||
internal static bool IsApplicableTo(MarkupTagHelperElementSyntax markupElementSyntax) | ||
{ | ||
// If there is no end tag, then the element is either already self-closing, or invalid. Either way, don't offer. | ||
if (markupElementSyntax.EndTag is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (markupElementSyntax is not { TagHelperInfo.BindingResult.Descriptors: [.. var descriptors] }) | ||
{ | ||
return false; | ||
} | ||
|
||
// Check whether the element has any non-whitespace content | ||
if (markupElementSyntax is { Body: { } body } && body.Any(static n => !n.ContainsOnlyWhitespace())) | ||
{ | ||
return false; | ||
} | ||
|
||
// Get symbols for the markup element | ||
var boundTagHelper = descriptors.FirstOrDefault(static d => d.IsComponentTagHelper); | ||
if (boundTagHelper == null) | ||
{ | ||
return false; | ||
} | ||
|
||
// Check whether the Component must have children | ||
foreach (var attribute in boundTagHelper.BoundAttributes) | ||
{ | ||
// Parameter is not required | ||
if (attribute is { IsEditorRequired: false }) | ||
{ | ||
continue; | ||
} | ||
|
||
// Parameter is not a `RenderFragment` or `RenderFragment<T>` | ||
if (!attribute.IsChildContentProperty()) | ||
{ | ||
continue; | ||
} | ||
|
||
// Parameter is not set or bound as an attribute | ||
if (!markupElementSyntax.TagHelperInfo!.BindingResult.Attributes.Any(a => | ||
RazorSyntaxFacts.TryGetComponentParameterNameFromFullAttributeName(a.Key, out var componentParameterName, out var directiveAttributeParameter) && | ||
componentParameterName.SequenceEqual(attribute.Name) && | ||
directiveAttributeParameter is { IsEmpty: true } or "get" | ||
)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...Analysis.Razor.Workspaces/CodeActions/Razor/SimplifyTagToSelfClosingCodeActionResolver.cs
This file contains hidden or 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,57 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Razor.CodeActions.Models; | ||
using Microsoft.CodeAnalysis.Razor.Formatting; | ||
using Microsoft.CodeAnalysis.Razor.ProjectSystem; | ||
using Microsoft.CodeAnalysis.Razor.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.CodeActions.Razor; | ||
|
||
internal class SimplifyTagToSelfClosingCodeActionResolver : IRazorCodeActionResolver | ||
{ | ||
public string Action => LanguageServerConstants.CodeActions.SimplifyTagToSelfClosingAction; | ||
|
||
public async Task<WorkspaceEdit?> ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) | ||
{ | ||
if (data.ValueKind == JsonValueKind.Undefined) | ||
{ | ||
return null; | ||
} | ||
|
||
var actionParams = JsonSerializer.Deserialize<SimplifyTagToSelfClosingCodeActionParams>(data.GetRawText()); | ||
if (actionParams is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var componentDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
var text = componentDocument.Source.Text; | ||
var removeRange = text.GetRange(actionParams.StartTagCloseAngleIndex, actionParams.EndTagCloseAngleIndex); | ||
|
||
var documentChanges = new TextDocumentEdit[] | ||
{ | ||
new TextDocumentEdit | ||
{ | ||
TextDocument = new OptionalVersionedTextDocumentIdentifier { DocumentUri= new(documentContext.Uri) }, | ||
Edits = | ||
[ | ||
new TextEdit | ||
{ | ||
NewText = " />", | ||
Range = removeRange, | ||
} | ||
], | ||
} | ||
}; | ||
|
||
return new WorkspaceEdit | ||
{ | ||
DocumentChanges = documentChanges, | ||
}; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...sis.Razor.Workspaces/Completion/DirectiveAttributeEventParameterCompletionItemProvider.cs
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.