From 77a480ca4938d97fa848ff3a364fef5d8dac0e68 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 25 Oct 2022 16:31:10 +0000 Subject: [PATCH] [analysis_server] Handle type arguments on named type references for initial Call Hierarchy item Change-Id: I5e30c808ddda7ff7817f7be7dd0ecff9a725854c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/265501 Commit-Queue: Brian Wilkerson Reviewed-by: Brian Wilkerson --- .../computer/computer_lazy_type_hierarchy.dart | 16 ++++++++-------- .../computer/type_hierarchy_computer_test.dart | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/analysis_server/lib/src/computer/computer_lazy_type_hierarchy.dart b/pkg/analysis_server/lib/src/computer/computer_lazy_type_hierarchy.dart index 2ad21309f38..b290667296f 100644 --- a/pkg/analysis_server/lib/src/computer/computer_lazy_type_hierarchy.dart +++ b/pkg/analysis_server/lib/src/computer/computer_lazy_type_hierarchy.dart @@ -83,22 +83,22 @@ class DartLazyTypeHierarchyComputer { TypeHierarchyItem? findTarget(int offset) { final node = NodeLocator2(offset).searchWithin(_result.unit); - Element? element; + DartType? type; // Try named types. - final type = node?.thisOrAncestorOfType(); - element = type?.name.staticElement; + type = node?.thisOrAncestorOfType()?.type; - if (element == null) { + if (type == null) { // Try enclosing class/mixins. final Declaration? declaration = node ?.thisOrAncestorMatching((node) => _isValidTargetDeclaration(node)); - element = declaration?.declaredElement; + final element = declaration?.declaredElement; + if (element is InterfaceElement) { + type = element.thisType; + } } - return element is InterfaceElement - ? TypeHierarchyItem.forType(element.thisType) - : null; + return type is InterfaceType ? TypeHierarchyItem.forType(type) : null; } /// Locate the [Element] referenced by [location]. diff --git a/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart b/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart index 5e9df760714..bdef622ffc5 100644 --- a/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart +++ b/pkg/analysis_server/test/src/computer/type_hierarchy_computer_test.dart @@ -728,4 +728,22 @@ int? b; ), ); } + + /// Ensure invocations directly on a type with type args retain those args. + Future test_typeReference_generic() async { + final content = ''' +/*[0*/class /*[1*/MyClass1/*1]*/ {}/*0]*/ +MyCl^ass1? a; + '''; + + addTestSource(content); + await expectTarget( + _isItem( + 'MyClass1', + testFile, + codeRange: code.ranges[0].sourceRange, + nameRange: code.ranges[1].sourceRange, + ), + ); + } }