Skip to content

Commit ca2f314

Browse files
committed
Fix wrap with tag helper methods to just operate on a LinePositionSpan
1 parent 521741a commit ca2f314

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/WrapWithTag/WrapWithTagEndpoint.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Threading;
55
using System.Threading.Tasks;
6+
using ICSharpCode.Decompiler.Util;
67
using Microsoft.AspNetCore.Razor.Language;
78
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
89
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
@@ -41,19 +42,21 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(WrapWithTagParams reques
4142

4243
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
4344

44-
var validationResult = await WrapWithTagHelper.ValidateAndAdjustRangeAsync(codeDocument, request.Range, cancellationToken).ConfigureAwait(false);
45-
if (!validationResult.IsValid)
45+
if (request.Range is null)
4646
{
47-
_logger.LogInformation($"Unsupported language at the requested range.");
47+
_logger.LogInformation($"WrapWithTag request for {request.TextDocument.DocumentUri} has a null range.");
4848
return null;
4949
}
5050

51-
// Update the request range if it was adjusted
52-
if (validationResult.AdjustedRange is not null)
51+
if (!WrapWithTagHelper.TryGetValidWrappingRange(codeDocument, request.Range.ToLinePositionSpan(), out var adjustedRange))
5352
{
54-
request.Range = validationResult.AdjustedRange;
53+
_logger.LogInformation($"Unsupported language at the requested range.");
54+
return null;
5555
}
5656

57+
// Update the request range if it was adjusted
58+
request.Range = adjustedRange.ToRange();
59+
5760
cancellationToken.ThrowIfCancellationRequested();
5861

5962
var versioned = new VersionedTextDocumentIdentifier
Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Threading;
5-
using System.Threading.Tasks;
64
using Microsoft.AspNetCore.Razor.Language;
75
using Microsoft.AspNetCore.Razor.Language.Syntax;
86
using Microsoft.CodeAnalysis.Razor.Protocol;
97
using Microsoft.CodeAnalysis.Text;
10-
using Roslyn.LanguageServer.Protocol;
118

129
namespace Microsoft.CodeAnalysis.Razor.Workspaces.Utilities;
1310

1411
internal static class WrapWithTagHelper
1512
{
16-
public static async Task<bool> IsValidWrappingRangeAsync(RazorCodeDocument codeDocument, LinePositionSpan range, CancellationToken cancellationToken)
13+
/// <summary>
14+
/// Returns the range to use for wrapping a selection with a tag.
15+
/// </summary>
16+
/// <returns>Returns null if the range specified is not valid</returns>
17+
public static bool TryGetValidWrappingRange(RazorCodeDocument codeDocument, LinePositionSpan range, out LinePositionSpan wrappingRange)
1718
{
18-
var result = await ValidateAndAdjustRangeInternalAsync(codeDocument, range, cancellationToken).ConfigureAwait(false);
19-
return result.IsValid;
20-
}
19+
wrappingRange = range;
2120

22-
public static async Task<(bool IsValid, LspRange? AdjustedRange)> ValidateAndAdjustRangeAsync(RazorCodeDocument codeDocument, LspRange range, CancellationToken cancellationToken)
23-
{
2421
var sourceText = codeDocument.Source.Text;
2522

26-
if (range?.Start is not { } start ||
27-
!sourceText.TryGetAbsoluteIndex(start, out var hostDocumentIndex))
23+
if (!sourceText.TryGetAbsoluteIndex(range.Start, out var hostDocumentIndex))
2824
{
29-
return (false, null);
25+
return false;
3026
}
3127

3228
// First thing we do is make sure we start at a non-whitespace character. This is important because in some
@@ -38,20 +34,15 @@ public static async Task<bool> IsValidWrappingRangeAsync(RazorCodeDocument codeD
3834
//
3935
// Limiting this to only whitespace on the same line, as it's not clear what user expectation would be otherwise.
4036
var requestSpan = sourceText.GetTextSpan(range);
41-
var adjustedRange = range;
4237
if (sourceText.TryGetFirstNonWhitespaceOffset(requestSpan, out var offset, out var newLineCount) &&
4338
newLineCount == 0)
4439
{
45-
adjustedRange = new LspRange
46-
{
47-
Start = new Position
48-
{
49-
Line = range.Start.Line,
50-
Character = range.Start.Character + offset
51-
},
52-
End = range.End
53-
};
54-
requestSpan = sourceText.GetTextSpan(adjustedRange);
40+
wrappingRange = new LinePositionSpan(
41+
start: new LinePosition(
42+
line: range.Start.Line,
43+
character: range.Start.Character + offset),
44+
end: range.End);
45+
requestSpan = sourceText.GetTextSpan(wrappingRange);
5546
hostDocumentIndex += offset;
5647
}
5748

@@ -89,29 +80,22 @@ public static async Task<bool> IsValidWrappingRangeAsync(RazorCodeDocument codeD
8980
//
9081
// <p>[|@currentCount|]</p>
9182

92-
var tree = codeDocument.GetSyntaxTree();
93-
var node = tree.Root.FindNode(requestSpan, includeWhitespace: false, getInnermostNodeForTie: true);
83+
var root = codeDocument.GetRequiredSyntaxRoot();
84+
var node = root.FindNode(requestSpan, includeWhitespace: false, getInnermostNodeForTie: true);
9485
if (node?.FirstAncestorOrSelf<CSharpImplicitExpressionSyntax>() is { Parent: CSharpCodeBlockSyntax codeBlock } &&
9586
(requestSpan == codeBlock.Span || requestSpan.Length == 0))
9687
{
9788
// Pretend we're in Html so the rest of the logic can continue
98-
adjustedRange = sourceText.GetRange(codeBlock.Span);
89+
wrappingRange = sourceText.GetLinePositionSpan(codeBlock.Span);
9990
languageKind = RazorLanguageKind.Html;
10091
}
10192
}
10293

10394
if (languageKind is not RazorLanguageKind.Html)
10495
{
105-
return (false, null);
96+
return false;
10697
}
10798

108-
return (true, adjustedRange);
109-
}
110-
111-
private static async Task<(bool IsValid, LinePositionSpan? AdjustedRange)> ValidateAndAdjustRangeInternalAsync(RazorCodeDocument codeDocument, LinePositionSpan range, CancellationToken cancellationToken)
112-
{
113-
var lspRange = range.ToRange();
114-
var result = await ValidateAndAdjustRangeAsync(codeDocument, lspRange, cancellationToken).ConfigureAwait(false);
115-
return (result.IsValid, result.AdjustedRange?.ToLinePositionSpan());
99+
return true;
116100
}
117-
}
101+
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/WrapWithTag/RemoteWrapWithTagService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private async ValueTask<Response> IsValidWrapWithTagLocationAsync(
5252
CancellationToken cancellationToken)
5353
{
5454
var codeDocument = await context.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
55-
var isValid = await WrapWithTagHelper.IsValidWrappingRangeAsync(codeDocument, range, cancellationToken).ConfigureAwait(false);
55+
var isValid = WrapWithTagHelper.TryGetValidWrappingRange(codeDocument, range, out var adjustedRange);
5656
return Response.Results(isValid);
5757
}
5858

0 commit comments

Comments
 (0)