Skip to content

Commit

Permalink
Some Cleanup For ExtractToComponent (#11008)
Browse files Browse the repository at this point in the history
Clean up some of the extract to component code. Move more work to the resolver, use pooled objects where applicable, and try to make the provider cleaner when creating the ExtractToComponentCodeActionParams
  • Loading branch information
ryzngard authored Oct 15, 2024
1 parent 17dbcdb commit e060ce0
Show file tree
Hide file tree
Showing 17 changed files with 329 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
}

var tree = context.CodeDocument.GetSyntaxTree();
var node = tree.Root.FindInnermostNode(context.Location.AbsoluteIndex);
var node = tree.Root.FindInnermostNode(context.StartLocation.AbsoluteIndex);
var isInImplicitExpression = node?.AncestorsAndSelf().Any(n => n is CSharpImplicitExpressionSyntax) ?? false;

var allowList = isInImplicitExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static bool TryGetOwner(RazorCodeActionContext context, [NotNullWhen(true)] out
return false;
}

owner = syntaxTree.Root.FindInnermostNode(context.Location.AbsoluteIndex);
owner = syntaxTree.Root.FindInnermostNode(context.StartLocation.AbsoluteIndex);
if (owner is null)
{
Debug.Fail("Owner should never be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,22 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
request.Range = vsCodeActionContext.SelectionRange;
}

if (!sourceText.TryGetSourceLocation(request.Range.Start, out var location))
if (!sourceText.TryGetSourceLocation(request.Range.Start, out var startLocation))
{
return null;
}

if (!sourceText.TryGetSourceLocation(request.Range.End, out var endLocation))
{
endLocation = startLocation;
}

var context = new RazorCodeActionContext(
request,
documentSnapshot,
codeDocument,
location,
startLocation,
endLocation,
sourceText,
_languageServerFeatureOptions.SupportsFileManipulation,
_supportsCodeActionResolve);
Expand All @@ -177,7 +183,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V

private async Task<ImmutableArray<RazorVSInternalCodeAction>> GetDelegatedCodeActionsAsync(DocumentContext documentContext, RazorCodeActionContext context, Guid correlationId, CancellationToken cancellationToken)
{
var languageKind = context.CodeDocument.GetLanguageKind(context.Location.AbsoluteIndex, rightAssociative: false);
var languageKind = context.CodeDocument.GetLanguageKind(context.StartLocation.AbsoluteIndex, rightAssociative: false);

// No point delegating if we're in a Razor context
if (languageKind == RazorLanguageKind.Razor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;

namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models;

// NOTE: As mentioned before, these have changed in future PRs, where much of the Provider logic was moved to the resolver.
// The last three properties are not used in the current implementation.
internal sealed class ExtractToComponentCodeActionParams
{
[JsonPropertyName("uri")]
public required Uri Uri { get; set; }

[JsonPropertyName("extractStart")]
public int ExtractStart { get; set; }
[JsonPropertyName("start")]
public int Start { get; set; }

[JsonPropertyName("extractEnd")]
public int ExtractEnd { get; set; }
[JsonPropertyName("end")]
public int End { get; set; }

[JsonPropertyName("namespace")]
public required string Namespace { get; set; }

[JsonPropertyName("usingDirectives")]
public required List<string> usingDirectives { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed class ComponentAccessibilityCodeActionProvider : IRazorCodeActio
public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeActionContext context, CancellationToken cancellationToken)
{
// Locate cursor
var node = context.CodeDocument.GetSyntaxTree().Root.FindInnermostNode(context.Location.AbsoluteIndex);
var node = context.CodeDocument.GetSyntaxTree().Root.FindInnermostNode(context.StartLocation.AbsoluteIndex);
if (node is null)
{
return [];
Expand All @@ -44,7 +44,7 @@ public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorC
return [];
}

if (context.Location.AbsoluteIndex < startTag.SpanStart)
if (context.StartLocation.AbsoluteIndex < startTag.SpanStart)
{
// Cursor is before the start tag, so we shouldn't show a light bulb. This can happen
// in cases where the cursor is in whitespace at the beginning of the document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
}

var owner = syntaxTree.Root.FindInnermostNode(context.Location.AbsoluteIndex);
var owner = syntaxTree.Root.FindInnermostNode(context.StartLocation.AbsoluteIndex);
if (owner is null)
{
_logger.LogWarning($"Owner should never be null.");
Expand Down Expand Up @@ -84,7 +84,7 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
}

// Do not provide code action if the cursor is inside the code block
if (context.Location.AbsoluteIndex > csharpCodeBlockNode.SpanStart)
if (context.StartLocation.AbsoluteIndex > csharpCodeBlockNode.SpanStart)
{
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
}
Expand Down
Loading

0 comments on commit e060ce0

Please sign in to comment.