From ec40ebcd23d2e094f1f338bdef6285b0828ccb98 Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Fri, 22 May 2026 17:05:29 -0700 Subject: [PATCH] Fix a bug in textDocument/definition for primary constructors Change-Id: Id24cdfda1c84322b5fa77cca0c87df0784482ec6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/506080 Commit-Queue: Samuel Rawlins Auto-Submit: Brian Wilkerson Reviewed-by: Samuel Rawlins --- .../keyword_navigation_computer.dart | 9 ++++--- .../test/lsp/definition_test.dart | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pkg/analysis_server/lib/src/utilities/navigation/keyword_navigation_computer.dart b/pkg/analysis_server/lib/src/utilities/navigation/keyword_navigation_computer.dart index b99cc8c5bc5..2ccd6ca7fd7 100644 --- a/pkg/analysis_server/lib/src/utilities/navigation/keyword_navigation_computer.dart +++ b/pkg/analysis_server/lib/src/utilities/navigation/keyword_navigation_computer.dart @@ -65,9 +65,12 @@ class KeywordNavigationComputer { MethodDeclaration() => function.name, ConstructorDeclaration() => // For named constructors, return the name. - // For unnamed constructors, use the return type / class name. - // TODO(scheglov): support primary constructors - function.name ?? function.typeName!.beginToken, + // For unnamed constructors, use the return type / class name, or the + // `new` or `factory` keyword. + function.name ?? + function.typeName?.beginToken ?? + function.newKeyword ?? + function.factoryKeyword, _ => null, }; } diff --git a/pkg/analysis_server/test/lsp/definition_test.dart b/pkg/analysis_server/test/lsp/definition_test.dart index 2785b40a189..69b50757f96 100644 --- a/pkg/analysis_server/test/lsp/definition_test.dart +++ b/pkg/analysis_server/test/lsp/definition_test.dart @@ -1057,6 +1057,31 @@ class MyClass { await testContents(contents); } + Future test_keywordNavigation_return_toConstructor_withFactory() async { + var contents = ''' +class MyClass { + [!factory!] () { + ret^urn MyClass.n(); + } + MyClass.n(); +} +'''; + + await testContents(contents); + } + + Future test_keywordNavigation_return_toConstructor_withNew() async { + var contents = ''' +class MyClass { + [!new!] () { + ret^urn; + } +} +'''; + + await testContents(contents); + } + Future test_keywordNavigation_return_toFunction() async { var contents = ''' int [!foo!]() {