зеркало из https://github.com/dotnet/razor.git
Родитель
22063f297d
Коммит
e4d1b9e72a
|
@ -83,7 +83,7 @@ internal class ExtractToComponentCodeActionProvider() : IRazorCodeActionProvider
|
|||
|
||||
private static (SyntaxNode? Start, SyntaxNode? End) GetStartAndEndElements(RazorCodeActionContext context, RazorSyntaxTree syntaxTree)
|
||||
{
|
||||
var owner = syntaxTree.Root.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: true);
|
||||
var owner = syntaxTree.Root.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: !context.HasSelection);
|
||||
if (owner is null)
|
||||
{
|
||||
return (null, null);
|
||||
|
@ -112,25 +112,13 @@ internal class ExtractToComponentCodeActionProvider() : IRazorCodeActionProvider
|
|||
|
||||
private static SyntaxNode? GetEndElementNode(RazorCodeActionContext context, RazorSyntaxTree syntaxTree)
|
||||
{
|
||||
var endOwner = syntaxTree.Root.FindInnermostNode(context.EndAbsoluteIndex, includeWhitespace: true);
|
||||
var endOwner = syntaxTree.Root.FindInnermostNode(context.EndAbsoluteIndex, includeWhitespace: false);
|
||||
if (endOwner is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Correct selection to include the current node if the selection ends immediately after a closing tag.
|
||||
if (endOwner is MarkupTextLiteralSyntax markupTextLiteral
|
||||
&& SelectionShouldBePrevious(markupTextLiteral, context.EndAbsoluteIndex)
|
||||
&& endOwner.TryGetPreviousSibling(out var previousSibling))
|
||||
{
|
||||
endOwner = previousSibling;
|
||||
}
|
||||
|
||||
return GetBlockOrTextNode(endOwner);
|
||||
|
||||
static bool SelectionShouldBePrevious(MarkupTextLiteralSyntax markupTextLiteral, int absoluteIndex)
|
||||
=> markupTextLiteral.Span.Start == absoluteIndex
|
||||
|| markupTextLiteral.ContainsOnlyWhitespace();
|
||||
}
|
||||
|
||||
private static SyntaxNode? GetBlockOrTextNode(SyntaxNode node)
|
||||
|
|
|
@ -22,8 +22,10 @@ using Microsoft.CodeAnalysis.Testing;
|
|||
using Microsoft.CodeAnalysis.Text;
|
||||
using Microsoft.VisualStudio.LanguageServer.Protocol;
|
||||
using Moq;
|
||||
using Roslyn.Test.Utilities;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using WorkItemAttribute = Roslyn.Test.Utilities.WorkItemAttribute;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.LanguageServer.Test.CodeActions.Razor;
|
||||
|
||||
|
@ -465,6 +467,144 @@ public class ExtractToComponentCodeActionProviderTest(ITestOutputHelper testOutp
|
|||
</div>|}
|
||||
""");
|
||||
|
||||
[Fact]
|
||||
[WorkItem("https://github.com/dotnet/razor/issues/11261")]
|
||||
public Task Handle_Inside_ElseBlock()
|
||||
=> TestAsync("""
|
||||
@page "/weather"
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
{|selection: {|result:<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th aria-label="Temperature in Celsius">Temp. (C)</th>
|
||||
<th aria-label="Temperature in Farenheit">Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>|}|}
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate a loading indicator
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
[Fact]
|
||||
[WorkItem("https://github.com/dotnet/razor/issues/11261")]
|
||||
public Task Handle_Inside_ElseBlock_NoSelection()
|
||||
=> TestAsync("""
|
||||
@page "/weather"
|
||||
|
||||
<PageTitle>Weather</PageTitle>
|
||||
|
||||
<h1>Weather</h1>
|
||||
|
||||
<p>This component demonstrates showing data.</p>
|
||||
|
||||
@if (forecasts == null)
|
||||
{
|
||||
<p><em>Loading...</em></p>
|
||||
}
|
||||
else
|
||||
{
|
||||
{|selection:|} <table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th aria-label="Temperature in Celsius">Temp. (C)</th>
|
||||
<th aria-label="Temperature in Farenheit">Temp. (F)</th>
|
||||
<th>Summary</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var forecast in forecasts)
|
||||
{
|
||||
<tr>
|
||||
<td>@forecast.Date.ToShortDateString()</td>
|
||||
<td>@forecast.TemperatureC</td>
|
||||
<td>@forecast.TemperatureF</td>
|
||||
<td>@forecast.Summary</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
|
||||
@code {
|
||||
private WeatherForecast[]? forecasts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Simulate asynchronous loading to demonstrate a loading indicator
|
||||
await Task.Delay(500);
|
||||
|
||||
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
||||
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
|
||||
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = startDate.AddDays(index),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = summaries[Random.Shared.Next(summaries.Length)]
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
public int TemperatureC { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
private static RazorCodeActionContext CreateRazorCodeActionContext(
|
||||
VSCodeActionParams request,
|
||||
TextSpan selectionSpan,
|
||||
|
@ -557,7 +697,12 @@ public class ExtractToComponentCodeActionProviderTest(ITestOutputHelper testOutp
|
|||
Assert.NotNull(razorCodeActionResolutionParams);
|
||||
var actionParams = ((JsonElement)razorCodeActionResolutionParams.Data!).Deserialize<ExtractToComponentCodeActionParams>();
|
||||
Assert.NotNull(actionParams);
|
||||
Assert.Equal(resultSpan.Start, actionParams.Start);
|
||||
Assert.Equal(resultSpan.End, actionParams.End);
|
||||
|
||||
if (resultSpan.Start != actionParams.Start || resultSpan.End != actionParams.End)
|
||||
{
|
||||
var resultText = context.SourceText.GetSubTextString(resultSpan);
|
||||
var actualText = context.SourceText.GetSubTextString(TextSpan.FromBounds(actionParams.Start, actionParams.End));
|
||||
AssertEx.EqualOrDiff(resultText, actualText, "Code action span does not match expected");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче