1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . Threading ;
5
- using System . Threading . Tasks ;
6
4
using Microsoft . AspNetCore . Razor . Language ;
7
5
using Microsoft . AspNetCore . Razor . Language . Syntax ;
8
6
using Microsoft . CodeAnalysis . Razor . Protocol ;
9
7
using Microsoft . CodeAnalysis . Text ;
10
- using Roslyn . LanguageServer . Protocol ;
11
8
12
9
namespace Microsoft . CodeAnalysis . Razor . Workspaces . Utilities ;
13
10
14
11
internal static class WrapWithTagHelper
15
12
{
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 )
17
18
{
18
- var result = await ValidateAndAdjustRangeInternalAsync ( codeDocument , range , cancellationToken ) . ConfigureAwait ( false ) ;
19
- return result . IsValid ;
20
- }
19
+ wrappingRange = range ;
21
20
22
- public static async Task < ( bool IsValid , LspRange ? AdjustedRange ) > ValidateAndAdjustRangeAsync ( RazorCodeDocument codeDocument , LspRange range , CancellationToken cancellationToken )
23
- {
24
21
var sourceText = codeDocument . Source . Text ;
25
22
26
- if ( range ? . Start is not { } start ||
27
- ! sourceText . TryGetAbsoluteIndex ( start , out var hostDocumentIndex ) )
23
+ if ( ! sourceText . TryGetAbsoluteIndex ( range . Start , out var hostDocumentIndex ) )
28
24
{
29
- return ( false , null ) ;
25
+ return false ;
30
26
}
31
27
32
28
// 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
38
34
//
39
35
// Limiting this to only whitespace on the same line, as it's not clear what user expectation would be otherwise.
40
36
var requestSpan = sourceText . GetTextSpan ( range ) ;
41
- var adjustedRange = range ;
42
37
if ( sourceText . TryGetFirstNonWhitespaceOffset ( requestSpan , out var offset , out var newLineCount ) &&
43
38
newLineCount == 0 )
44
39
{
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 ) ;
55
46
hostDocumentIndex += offset ;
56
47
}
57
48
@@ -89,29 +80,22 @@ public static async Task<bool> IsValidWrappingRangeAsync(RazorCodeDocument codeD
89
80
//
90
81
// <p>[|@currentCount|]</p>
91
82
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 ) ;
94
85
if ( node ? . FirstAncestorOrSelf < CSharpImplicitExpressionSyntax > ( ) is { Parent : CSharpCodeBlockSyntax codeBlock } &&
95
86
( requestSpan == codeBlock . Span || requestSpan . Length == 0 ) )
96
87
{
97
88
// 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 ) ;
99
90
languageKind = RazorLanguageKind . Html ;
100
91
}
101
92
}
102
93
103
94
if ( languageKind is not RazorLanguageKind . Html )
104
95
{
105
- return ( false , null ) ;
96
+ return false ;
106
97
}
107
98
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 ;
116
100
}
117
- }
101
+ }
0 commit comments