[analysis_server] Fix navigation for the offset between a type name and generic type args

Fixes https://github.com/Dart-Code/Dart-Code/issues/4619

Change-Id: I5cee17037fe4531183dc5e2cb566e9f020e1b541
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316223
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/test/lsp/definition_test.dart b/pkg/analysis_server/test/lsp/definition_test.dart
index 4f46baa..4bb3f1f 100644
--- a/pkg/analysis_server/test/lsp/definition_test.dart
+++ b/pkg/analysis_server/test/lsp/definition_test.dart
@@ -533,6 +533,18 @@
     await testContents(contents);
   }
 
+  Future<void> test_type_generic_end() async {
+    final contents = '''
+f() {
+  final a = A^<String>();
+}
+
+class [[A]]<T> {}
+''';
+
+    await testContents(contents);
+  }
+
   Future<void> test_unopenFile() async {
     final contents = '''
 [[foo]]() {
diff --git a/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart b/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart
index e2694a2..d5c0cbc 100644
--- a/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart
+++ b/pkg/analyzer_plugin/lib/utilities/navigation/navigation_dart.dart
@@ -62,6 +62,17 @@
     current = parent;
   }
 
+  // Consider the angle brackets for type arguments part of the leading type,
+  // otherwise we don't navigate in the common situation of having the type name
+  // selected, where VS Code provides the end of the selection as the position
+  // to search.
+  //
+  // In `A^<String>` node will be TypeArgumentList and we will never find A if
+  // we start visiting from there.
+  if (current is TypeArgumentList && parent != null) {
+    current = parent;
+  }
+
   return current;
 }