13
13
using Microsoft . AspNetCore . Razor . Test . Common . LanguageServer ;
14
14
using Microsoft . CodeAnalysis . Razor . ProjectSystem ;
15
15
using Microsoft . CodeAnalysis . Razor . Protocol . CodeActions ;
16
- using Microsoft . CodeAnalysis . Razor . Workspaces ;
17
16
using Microsoft . CodeAnalysis . Testing ;
18
17
using Microsoft . CodeAnalysis . Text ;
19
18
using Microsoft . VisualStudio . LanguageServer . Protocol ;
20
19
using Moq ;
21
20
using Xunit ;
22
21
using Xunit . Abstractions ;
22
+ using Range = Microsoft . VisualStudio . LanguageServer . Protocol . Range ;
23
23
24
24
namespace Microsoft . AspNetCore . Razor . LanguageServer . Test . CodeActions . Razor ;
25
25
@@ -55,7 +55,7 @@ public async Task Handle_InvalidFileKind()
55
55
var request = new VSCodeActionParams ( )
56
56
{
57
57
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri ( documentPath ) } ,
58
- Range = new Range ( ) ,
58
+ Range = VsLspFactory . DefaultRange ,
59
59
Context = new VSInternalCodeActionContext ( )
60
60
} ;
61
61
@@ -76,7 +76,7 @@ public async Task Handle_InvalidFileKind()
76
76
public async Task Handle_SinglePointSelection_ReturnsNotEmpty ( )
77
77
{
78
78
// Arrange
79
- var documentPath = "c:/Test.cs " ;
79
+ var documentPath = "c:/Test.razor " ;
80
80
var contents = """
81
81
@page "/"
82
82
@@ -102,12 +102,12 @@ public async Task Handle_SinglePointSelection_ReturnsNotEmpty()
102
102
var request = new VSCodeActionParams ( )
103
103
{
104
104
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri ( documentPath ) } ,
105
- Range = new Range ( ) ,
105
+ Range = VsLspFactory . DefaultRange ,
106
106
Context = new VSInternalCodeActionContext ( )
107
107
} ;
108
108
109
109
var location = new SourceLocation ( cursorPosition , - 1 , - 1 ) ;
110
- var context = CreateRazorCodeActionContext ( request , location , documentPath , contents ) ;
110
+ var context = CreateRazorCodeActionContext ( request , location , documentPath , contents , supportsFileCreation : true ) ;
111
111
112
112
var provider = new ExtractToNewComponentCodeActionProvider ( LoggerFactory ) ;
113
113
@@ -122,7 +122,7 @@ public async Task Handle_SinglePointSelection_ReturnsNotEmpty()
122
122
public async Task Handle_MultiPointSelection_ReturnsNotEmpty ( )
123
123
{
124
124
// Arrange
125
- var documentPath = "c:/Test.cs " ;
125
+ var documentPath = "c:/Test.razor " ;
126
126
var contents = """
127
127
@page "/"
128
128
@@ -148,14 +148,15 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
148
148
var request = new VSCodeActionParams ( )
149
149
{
150
150
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri ( documentPath ) } ,
151
- Range = new Range ( ) ,
151
+ Range = VsLspFactory . DefaultRange ,
152
152
Context = new VSInternalCodeActionContext ( )
153
153
} ;
154
154
155
155
var location = new SourceLocation ( cursorPosition , - 1 , - 1 ) ;
156
156
var context = CreateRazorCodeActionContext ( request , location , documentPath , contents ) ;
157
157
158
- AddMultiPointSelectionToContext ( context , selectionSpan ) ;
158
+ var lineSpan = context . SourceText . GetLinePositionSpan ( selectionSpan ) ;
159
+ request . Range = VsLspFactory . CreateRange ( lineSpan ) ;
159
160
160
161
var provider = new ExtractToNewComponentCodeActionProvider ( LoggerFactory ) ;
161
162
@@ -175,7 +176,7 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
175
176
public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentElement ( )
176
177
{
177
178
// Arrange
178
- var documentPath = "c:/Test.cs " ;
179
+ var documentPath = "c:/Test.razor " ;
179
180
var contents = """
180
181
@page "/"
181
182
@@ -201,14 +202,15 @@ public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentEl
201
202
var request = new VSCodeActionParams ( )
202
203
{
203
204
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri ( documentPath ) } ,
204
- Range = new Range ( ) ,
205
+ Range = VsLspFactory . DefaultRange ,
205
206
Context = new VSInternalCodeActionContext ( )
206
207
} ;
207
208
208
209
var location = new SourceLocation ( cursorPosition , - 1 , - 1 ) ;
209
210
var context = CreateRazorCodeActionContext ( request , location , documentPath , contents ) ;
210
211
211
- AddMultiPointSelectionToContext ( context , selectionSpan ) ;
212
+ var lineSpan = context . SourceText . GetLinePositionSpan ( selectionSpan ) ;
213
+ request . Range = VsLspFactory . CreateRange ( lineSpan ) ;
212
214
213
215
var provider = new ExtractToNewComponentCodeActionProvider ( LoggerFactory ) ;
214
216
@@ -222,6 +224,8 @@ public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentEl
222
224
Assert . NotNull ( razorCodeActionResolutionParams ) ;
223
225
var actionParams = ( ( JsonElement ) razorCodeActionResolutionParams . Data ) . Deserialize < ExtractToNewComponentCodeActionParams > ( ) ;
224
226
Assert . NotNull ( actionParams ) ;
227
+ Assert . Equal ( selectionSpan . Start , actionParams . ExtractStart ) ;
228
+ Assert . Equal ( selectionSpan . End , actionParams . ExtractEnd ) ;
225
229
}
226
230
227
231
private static RazorCodeActionContext CreateRazorCodeActionContext ( VSCodeActionParams request , SourceLocation location , string filePath , string text , bool supportsFileCreation = true )
@@ -241,34 +245,18 @@ private static RazorCodeActionContext CreateRazorCodeActionContext(VSCodeActionP
241
245
codeDocument . SetFileKind ( FileKinds . Component ) ;
242
246
codeDocument . SetCodeGenerationOptions ( RazorCodeGenerationOptions . Create ( o =>
243
247
{
244
- o . RootNamespace = "ExtractToCodeBehindTest " ;
248
+ o . RootNamespace = "ExtractToComponentTest " ;
245
249
} ) ) ;
246
250
codeDocument . SetSyntaxTree ( syntaxTree ) ;
247
251
248
252
var documentSnapshot = Mock . Of < IDocumentSnapshot > ( document =>
249
253
document . GetGeneratedOutputAsync ( ) == Task . FromResult ( codeDocument ) &&
250
- document . GetTextAsync ( ) == Task . FromResult ( codeDocument . GetSourceText ( ) ) , MockBehavior . Strict ) ;
254
+ document . GetTextAsync ( ) == Task . FromResult ( codeDocument . Source . Text ) , MockBehavior . Strict ) ;
251
255
252
256
var sourceText = SourceText . From ( text ) ;
253
257
254
258
var context = new RazorCodeActionContext ( request , documentSnapshot , codeDocument , location , sourceText , supportsFileCreation , SupportsCodeActionResolve : true ) ;
255
259
256
260
return context ;
257
261
}
258
-
259
- private static void AddMultiPointSelectionToContext ( RazorCodeActionContext context , TextSpan selectionSpan )
260
- {
261
- var sourceText = context . CodeDocument . Source . Text ;
262
- var startLinePosition = sourceText . Lines . GetLinePosition ( selectionSpan . Start ) ;
263
- var startPosition = new Position ( startLinePosition . Line , startLinePosition . Character ) ;
264
-
265
- var endLinePosition = sourceText . Lines . GetLinePosition ( selectionSpan . End ) ;
266
- var endPosition = new Position ( endLinePosition . Line , endLinePosition . Character ) ;
267
-
268
- context . Request . Range = new Range
269
- {
270
- Start = startPosition ,
271
- End = endPosition
272
- } ;
273
- }
274
262
}
0 commit comments