@@ -265,8 +265,7 @@ public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescrip
265
265
{
266
266
// If this is a child content tag helper, we want to add it if it's original type is in scope.
267
267
// E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in scope.
268
- TrySplitNamespaceAndType ( tagHelper , out var typeNamespace ) ;
269
- if ( ! typeNamespace . IsEmpty && IsTypeInScope ( typeNamespace , currentNamespace ) )
268
+ if ( IsTypeInScope ( tagHelper , currentNamespace ) )
270
269
{
271
270
Matches . Add ( tagHelper ) ;
272
271
}
@@ -352,8 +351,7 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node)
352
351
{
353
352
// If this is a child content tag helper, we want to add it if it's original type is in scope of the given namespace.
354
353
// E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in this namespace.
355
- TrySplitNamespaceAndType ( tagHelper , out var typeName ) ;
356
- if ( ! typeName . IsEmpty && IsTypeInNamespace ( typeName , @namespace ) )
354
+ if ( IsTypeInNamespace ( tagHelper , @namespace ) )
357
355
{
358
356
Matches . Add ( tagHelper ) ;
359
357
}
@@ -368,26 +366,15 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node)
368
366
}
369
367
}
370
368
371
- internal static bool IsTypeInNamespace ( StringSegment typeName , string @namespace )
372
- {
373
- if ( ! TrySplitNamespaceAndType ( typeName , out var typeNamespace , out var _ ) || typeNamespace . IsEmpty )
374
- {
375
- // Either the typeName is not the full type name or this type is at the top level.
376
- return true ;
377
- }
378
-
379
- return typeNamespace . Equals ( @namespace , StringComparison . Ordinal ) ;
380
- }
381
-
382
369
internal static bool IsTypeInNamespace ( TagHelperDescriptor tagHelper , string @namespace )
383
370
{
384
- if ( ! TrySplitNamespaceAndType ( tagHelper , out var typeNamespace ) || typeNamespace . IsEmpty )
371
+ return IsTypeInNamespace ( tagHelper . GetTypeNamespace ( ) , @namespace ) ;
372
+
373
+ static bool IsTypeInNamespace ( string typeNamespace , string @namespace )
385
374
{
386
375
// Either the typeName is not the full type name or this type is at the top level.
387
- return true ;
376
+ return string . IsNullOrEmpty ( typeNamespace ) || typeNamespace . Equals ( @namespace , StringComparison . Ordinal ) ;
388
377
}
389
-
390
- return typeNamespace . Equals ( @namespace , StringComparison . Ordinal ) ;
391
378
}
392
379
393
380
// Check if the given type is already in scope given the namespace of the current document.
@@ -397,34 +384,18 @@ internal static bool IsTypeInNamespace(TagHelperDescriptor tagHelper, string @na
397
384
// Whereas `MyComponents.SomethingElse.OtherComponent` is not in scope.
398
385
internal static bool IsTypeInScope ( TagHelperDescriptor descriptor , string currentNamespace )
399
386
{
400
- if ( ! TrySplitNamespaceAndType ( descriptor , out var typeNamespace , out _ ) || typeNamespace . IsEmpty )
401
- {
402
- // Either the typeName is not the full type name or this type is at the top level.
403
- return true ;
404
- }
405
-
406
- return IsTypeInScopeCore ( currentNamespace , typeNamespace ) ;
387
+ return IsTypeInScopeCore ( currentNamespace , descriptor . GetTypeNamespace ( ) ) ;
407
388
}
408
389
409
- // Check if the given type is already in scope given the namespace of the current document.
410
- // E.g,
411
- // If the namespace of the document is `MyComponents.Components.Shared`,
412
- // then the types `MyComponents.FooComponent`, `MyComponents.Components.BarComponent`, `MyComponents.Components.Shared.BazComponent` are all in scope.
413
- // Whereas `MyComponents.SomethingElse.OtherComponent` is not in scope.
414
- internal static bool IsTypeInScope ( StringSegment typeName , string currentNamespace )
390
+ private static bool IsTypeInScopeCore ( string currentNamespace , string typeNamespace )
415
391
{
416
- if ( ! TrySplitNamespaceAndType ( typeName , out var typeNamespace , out _ ) || typeNamespace . IsEmpty )
392
+ if ( string . IsNullOrEmpty ( typeNamespace ) )
417
393
{
418
394
// Either the typeName is not the full type name or this type is at the top level.
419
395
return true ;
420
396
}
421
397
422
- return IsTypeInScopeCore ( currentNamespace , typeNamespace ) ;
423
- }
424
-
425
- private static bool IsTypeInScopeCore ( string currentNamespace , StringSegment typeNamespace )
426
- {
427
- if ( ! new StringSegment ( currentNamespace ) . StartsWith ( typeNamespace , StringComparison . Ordinal ) )
398
+ if ( ! currentNamespace . StartsWith ( typeNamespace , StringComparison . Ordinal ) )
428
399
{
429
400
// typeName: MyComponents.Shared.SomeCoolNamespace
430
401
// currentNamespace: MyComponents.Shared
@@ -445,83 +416,7 @@ private static bool IsTypeInScopeCore(string currentNamespace, StringSegment typ
445
416
// open file in the editor. We mangle the class name for its generated code, so using that here to filter these out.
446
417
internal static bool IsTagHelperFromMangledClass ( TagHelperDescriptor tagHelper )
447
418
{
448
- StringSegment className ;
449
- if ( tagHelper . IsChildContentTagHelper ( ) )
450
- {
451
- // If this is a child content tag helper, we want to look at it's original type.
452
- // E.g, if the type name is `Test.__generated__MyComponent.ChildContent`, we want to look at `Test.__generated__MyComponent`.
453
- TrySplitNamespaceAndType ( tagHelper , out var typeNamespace ) ;
454
- return TrySplitNamespaceAndType ( typeNamespace , out var _ , out className )
455
- && ComponentMetadata . IsMangledClass ( className ) ;
456
- }
457
-
458
- return TrySplitNamespaceAndType ( tagHelper , out var _ , out className ) &&
459
- ComponentMetadata . IsMangledClass ( className ) ;
460
- }
461
-
462
- internal static bool TrySplitNamespaceAndType ( TagHelperDescriptor tagHelperDescriptor , out StringSegment @namespace )
463
- => TrySplitNamespaceAndType ( tagHelperDescriptor , out @namespace , out _ ) ;
464
-
465
- internal static bool TrySplitNamespaceAndType ( TagHelperDescriptor tagHelperDescriptor , out StringSegment @namespace , out StringSegment typeName )
466
- {
467
- if ( tagHelperDescriptor . ParsedTypeInfo is { } value )
468
- {
469
- @namespace = value . Namespace ;
470
- typeName = value . TypeName ;
471
- return value . Success ;
472
- }
473
-
474
- var success = TrySplitNamespaceAndType ( tagHelperDescriptor . GetTypeName ( ) , out @namespace , out typeName ) ;
475
- tagHelperDescriptor . ParsedTypeInfo = new ( success , @namespace , typeName ) ;
476
- return success ;
477
- }
478
-
479
- // Internal for testing.
480
- internal static bool TrySplitNamespaceAndType ( StringSegment fullTypeName , out StringSegment @namespace , out StringSegment typeName )
481
- {
482
- @namespace = StringSegment . Empty ;
483
- typeName = StringSegment . Empty ;
484
-
485
- if ( fullTypeName . IsEmpty )
486
- {
487
- return false ;
488
- }
489
-
490
- var nestingLevel = 0 ;
491
- var splitLocation = - 1 ;
492
- for ( var i = fullTypeName . Length - 1 ; i >= 0 ; i -- )
493
- {
494
- var c = fullTypeName [ i ] ;
495
- if ( c == Type . Delimiter && nestingLevel == 0 )
496
- {
497
- splitLocation = i ;
498
- break ;
499
- }
500
- else if ( c == '>' )
501
- {
502
- nestingLevel ++ ;
503
- }
504
- else if ( c == '<' )
505
- {
506
- nestingLevel -- ;
507
- }
508
- }
509
-
510
- if ( splitLocation == - 1 )
511
- {
512
- typeName = fullTypeName ;
513
- return true ;
514
- }
515
-
516
- @namespace = fullTypeName . Subsegment ( 0 , splitLocation ) ;
517
-
518
- var typeNameStartLocation = splitLocation + 1 ;
519
- if ( typeNameStartLocation < fullTypeName . Length )
520
- {
521
- typeName = fullTypeName . Subsegment ( typeNameStartLocation , fullTypeName . Length - typeNameStartLocation ) ;
522
- }
523
-
524
- return true ;
419
+ return ComponentMetadata . IsMangledClass ( tagHelper . GetTypeNameIdentifier ( ) ) ;
525
420
}
526
421
}
527
422
}
0 commit comments