From aced0523a4afbb0ef4fac7685cd8dcdeda72d0d3 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 1 Apr 2024 23:06:19 +0000 Subject: [PATCH] Augment. Fixes for navigation for constructor annotations. ...and for import prefixes in augmentations. Change-Id: I4c2f358d5bd1819c8691990a0b86a37d04449213 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/360505 Commit-Queue: Konstantin Shcheglov Reviewed-by: Brian Wilkerson --- .../notification_navigation_test.dart | 67 +++++++++++++++++++ .../lib/utilities/analyzer_converter.dart | 5 +- .../utilities/navigation/navigation_dart.dart | 3 + 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pkg/analysis_server/test/analysis/notification_navigation_test.dart b/pkg/analysis_server/test/analysis/notification_navigation_test.dart index e53bedc75064..ac727cae0d40 100644 --- a/pkg/analysis_server/test/analysis/notification_navigation_test.dart +++ b/pkg/analysis_server/test/analysis/notification_navigation_test.dart @@ -463,6 +463,44 @@ void f() { assertHasTarget('foo', targetFile: aFile); } + Future + test_class_constructor_annotationConstructor_importPrefix() async { + final a = newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); + const A.named(); +} +'''); + + addTestFile(''' +library augment 'b.dart'; +import 'a.dart' as prefix; + +class B { + @prefix.A() + @prefix.A.named() + B().foo(); +} +'''); + + await prepareNavigation(); + + assertHasRegion('prefix.A()'); + assertHasTarget('prefix;'); + + assertHasRegion('A()'); + assertHasTarget('A();', targetFile: a); + + assertHasRegion('prefix.A.named()'); + assertHasTarget('prefix;'); + + assertHasRegion('A.named()'); + assertHasTarget('named()', targetFile: a); + + assertHasRegion('named()'); + assertHasTarget('named()', targetFile: a); + } + Future test_class_constructor_named() async { addTestFile(''' class A { @@ -1381,6 +1419,35 @@ library my.lib; assertHasTargetString('my.lib'); } + Future + test_libraryAugmentation_topLevelFunction_annotationConstructor_importPrefix() async { + final a = newFile('$testPackageLibPath/a.dart', r''' +class A { + const A(); +} +'''); + + newFile('$testPackageLibPath/b.dart', r''' +import augment 'test.dart'; +'''); + + addTestFile(''' +library augment 'b.dart'; +import 'a.dart' as prefix; + +@prefix.A() +external B().foo(); +'''); + + await prepareNavigation(); + + assertHasRegion('prefix.A()'); + assertHasTarget('prefix;'); + + assertHasRegion('A()'); + assertHasTarget('A();', targetFile: a); + } + Future test_multiplyDefinedElement() async { newFile('$testPackageLibPath/libA.dart', 'library A; int TEST = 1;'); newFile('$testPackageLibPath/libB.dart', 'library B; int TEST = 2;'); diff --git a/pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart b/pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart index a5f85959e39f..86e209ef2c2a 100644 --- a/pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart +++ b/pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart @@ -321,10 +321,11 @@ extension ElementExtensions on analyzer.Element? { if (currentElement is analyzer.CompilationUnitElement) { return currentElement; } - if (currentElement?.enclosingElement is analyzer.LibraryElement) { + if (currentElement?.enclosingElement + is analyzer.LibraryOrAugmentationElement) { currentElement = currentElement?.enclosingElement; } - if (currentElement is analyzer.LibraryElement) { + if (currentElement is analyzer.LibraryOrAugmentationElement) { return currentElement.definingCompilationUnit; } for (; diff --git a/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart b/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart index 425419c0779f..3f7f5585d872 100644 --- a/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart +++ b/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart @@ -293,6 +293,8 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor { @override void visitConstructorDeclaration(ConstructorDeclaration node) { + node.metadata.accept(this); + // For a default constructor, override the class name to be the declaration // itself rather than linking to the class. var nameToken = node.name; @@ -302,6 +304,7 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor { node.returnType.accept(this); computer._addRegionForToken(nameToken, node.declaredElement); } + node.parameters.accept(this); node.initializers.accept(this); node.redirectedConstructor?.accept(this);