@@ -124,7 +124,7 @@ internal sealed class ExtractToComponentCodeActionResolver(
124
124
125
125
var newComponentContent = newComponentResult . NewContents ;
126
126
var componentNameAndParams = GenerateComponentNameAndParameters ( newComponentResult . Methods , componentName ) ;
127
-
127
+
128
128
var componentDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = actionParams . Uri } ;
129
129
var newComponentDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = newComponentUri } ;
130
130
@@ -175,7 +175,7 @@ internal sealed record SelectionAnalysisResult
175
175
public int ExtractStart ;
176
176
public int ExtractEnd ;
177
177
public HashSet < string > ? ComponentDependencies ;
178
- public HashSet < string > ? VariableDependencies ;
178
+ public HashSet < string > ? TentativeVariableDependencies ;
179
179
}
180
180
181
181
private static SelectionAnalysisResult TryAnalyzeSelection ( RazorCodeDocument codeDocument , ExtractToComponentCodeActionParams actionParams )
@@ -191,20 +191,20 @@ private static SelectionAnalysisResult TryAnalyzeSelection(RazorCodeDocument cod
191
191
var ( success , extractStart , extractEnd ) = TryProcessMultiPointSelection ( startElementNode , endElementNode , codeDocument , actionParams ) ;
192
192
193
193
var dependencyScanRoot = FindNearestCommonAncestor ( startElementNode , endElementNode ) ?? startElementNode ;
194
- var methodDependencies = AddComponentDependenciesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
194
+ var componentDependencies = AddComponentDependenciesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
195
195
var variableDependencies = AddVariableDependenciesInRange ( dependencyScanRoot , extractStart , extractEnd ) ;
196
196
197
197
return new SelectionAnalysisResult
198
198
{
199
199
Success = success ,
200
200
ExtractStart = extractStart ,
201
201
ExtractEnd = extractEnd ,
202
- ComponentDependencies = methodDependencies ,
203
- VariableDependencies = variableDependencies ,
202
+ ComponentDependencies = componentDependencies ,
203
+ TentativeVariableDependencies = variableDependencies ,
204
204
} ;
205
205
}
206
206
207
- private static ( MarkupElementSyntax ? Start , MarkupElementSyntax ? End ) GetStartAndEndElements ( RazorCodeDocument codeDocument , ExtractToComponentCodeActionParams actionParams )
207
+ private static ( MarkupSyntaxNode ? Start , MarkupSyntaxNode ? End ) GetStartAndEndElements ( RazorCodeDocument codeDocument , ExtractToComponentCodeActionParams actionParams )
208
208
{
209
209
var syntaxTree = codeDocument . GetSyntaxTree ( ) ;
210
210
if ( syntaxTree is null )
@@ -218,8 +218,8 @@ private static (MarkupElementSyntax? Start, MarkupElementSyntax? End) GetStartAn
218
218
return ( null , null ) ;
219
219
}
220
220
221
- var startElementNode = owner . FirstAncestorOrSelf < MarkupElementSyntax > ( ) ;
222
- if ( startElementNode is null || IsInsideProperHtmlContent ( actionParams . AbsoluteIndex , startElementNode ) )
221
+ var startElementNode = owner . FirstAncestorOrSelf < MarkupSyntaxNode > ( node => node is MarkupTagHelperElementSyntax or MarkupElementSyntax ) ;
222
+ if ( startElementNode is null )
223
223
{
224
224
return ( null , null ) ;
225
225
}
@@ -235,19 +235,7 @@ private static (MarkupElementSyntax? Start, MarkupElementSyntax? End) GetStartAn
235
235
return ( startElementNode , endElementNode ) ;
236
236
}
237
237
238
- private static bool IsInsideProperHtmlContent ( int absoluteIndex , MarkupElementSyntax startElementNode )
239
- {
240
- // If the provider executes before the user/completion inserts an end tag, the below return fails
241
- if ( startElementNode . EndTag . IsMissing )
242
- {
243
- return true ;
244
- }
245
-
246
- return absoluteIndex > startElementNode . StartTag . Span . End &&
247
- absoluteIndex < startElementNode . EndTag . SpanStart ;
248
- }
249
-
250
- private static MarkupElementSyntax ? TryGetEndElementNode ( Position selectionStart , Position selectionEnd , RazorSyntaxTree syntaxTree , SourceText sourceText )
238
+ private static MarkupSyntaxNode ? TryGetEndElementNode ( Position selectionStart , Position selectionEnd , RazorSyntaxTree syntaxTree , SourceText sourceText )
251
239
{
252
240
if ( selectionStart == selectionEnd )
253
241
{
@@ -273,7 +261,7 @@ private static bool IsInsideProperHtmlContent(int absoluteIndex, MarkupElementSy
273
261
endOwner = previousSibling ;
274
262
}
275
263
276
- return endOwner . FirstAncestorOrSelf < MarkupElementSyntax > ( ) ;
264
+ return endOwner . FirstAncestorOrSelf < MarkupSyntaxNode > ( node => node is MarkupTagHelperElementSyntax or MarkupElementSyntax ) ;
277
265
}
278
266
279
267
private static SourceLocation ? GetEndLocation ( Position selectionEnd , SourceText sourceText )
@@ -295,7 +283,7 @@ private static bool IsInsideProperHtmlContent(int absoluteIndex, MarkupElementSy
295
283
/// <param name="actionParams">The parameters for the extraction action, which will be updated.</param>
296
284
/// one more line for output
297
285
/// <returns>A tuple containing a boolean indicating success, the start of the extraction range, and the end of the extraction range.</returns>
298
- private static ( bool success , int extractStart , int extractEnd ) TryProcessMultiPointSelection ( MarkupElementSyntax startElementNode , MarkupElementSyntax endElementNode , RazorCodeDocument codeDocument , ExtractToComponentCodeActionParams actionParams )
286
+ private static ( bool success , int extractStart , int extractEnd ) TryProcessMultiPointSelection ( MarkupSyntaxNode startElementNode , MarkupSyntaxNode endElementNode , RazorCodeDocument codeDocument , ExtractToComponentCodeActionParams actionParams )
299
287
{
300
288
var extractStart = startElementNode . Span . Start ;
301
289
var extractEnd = endElementNode . Span . End ;
@@ -419,11 +407,9 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
419
407
private static SyntaxNode ? FindNearestCommonAncestor ( SyntaxNode node1 , SyntaxNode node2 )
420
408
{
421
409
var current = node1 ;
422
- var secondNodeIsCodeBlock = node2 is CSharpCodeBlockSyntax ;
423
-
424
410
while ( current is not null )
425
411
{
426
- if ( ShouldCheckNode ( current , secondNodeIsCodeBlock ) && current . Span . Contains ( node2 . Span ) )
412
+ if ( CheckNode ( current ) && current . Span . Contains ( node2 . Span ) )
427
413
{
428
414
return current ;
429
415
}
@@ -434,21 +420,11 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
434
420
return null ;
435
421
}
436
422
437
- // Whenever the multi point selection includes a code block at the end, the logic for finding the nearest common ancestor and containing sibling pair
438
- // should accept nodes of type MarkupBlockSyntax and CSharpCodeBlock each, respectively. ShouldCheckNode() and IsValidNode() handle these cases.
439
- private static bool ShouldCheckNode ( SyntaxNode node , bool isCodeBlock )
440
- {
441
- if ( isCodeBlock )
442
- {
443
- return node is MarkupElementSyntax or MarkupBlockSyntax ;
444
- }
445
-
446
- return node is MarkupElementSyntax ;
447
- }
423
+ private static bool CheckNode ( SyntaxNode node ) => node is MarkupElementSyntax or MarkupTagHelperElementSyntax or MarkupBlockSyntax ;
448
424
449
425
private static bool IsValidNode ( SyntaxNode node , bool isCodeBlock )
450
426
{
451
- return node is MarkupElementSyntax || ( isCodeBlock && node is CSharpCodeBlockSyntax ) ;
427
+ return node is MarkupElementSyntax or MarkupTagHelperElementSyntax || ( isCodeBlock && node is CSharpCodeBlockSyntax ) ;
452
428
}
453
429
454
430
private static HashSet < string > AddComponentDependenciesInRange ( SyntaxNode root , int extractStart , int extractEnd )
0 commit comments