[DAS] Fixes comment references for enum constants

Fixes: https://github.com/dart-lang/sdk/issues/60760
Change-Id: I1a6dc74f1265ac4195f772a3e0374856008cbc6f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430000
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Auto-Submit: Felipe Morschel <git@fmorschel.dev>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
index c7428b8..7437c91 100644
--- a/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
+++ b/pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart
@@ -399,7 +399,10 @@
       return;
     }
     AstNode? parent = containingMember.parent ?? containingMember;
-    if (parent is ClassMember) {
+    if (parent is EnumConstantDeclaration) {
+      assert(node is CommentReference);
+      parent = parent.parent;
+    } else if (parent is ClassMember) {
       assert(node is CommentReference);
       parent = parent.parent;
     } else if (parent is Directive) {
diff --git a/pkg/analysis_server/test/services/completion/dart/location/dart_doc_test.dart b/pkg/analysis_server/test/services/completion/dart/location/dart_doc_test.dart
index 0805e8e..e444707 100644
--- a/pkg/analysis_server/test/services/completion/dart/location/dart_doc_test.dart
+++ b/pkg/analysis_server/test/services/completion/dart/location/dart_doc_test.dart
@@ -76,6 +76,23 @@
 ''');
   }
 
+  Future<void> test_enumConstant() async {
+    allowedIdentifiers = const {'value1'};
+    await computeSuggestions('''
+enum MyEnum {
+  /// This doc should suggest the commented enum constant name [val^].
+  value1
+}
+''');
+    assertResponse(r'''
+replacement
+  left: 3
+suggestions
+  value1
+    kind: enumConstant
+''');
+  }
+
   Future<void> test_extension() async {
     allowedIdentifiers = const {'MyExt'};
     await computeSuggestions('''
diff --git a/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart b/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart
index 3f65159..c30c1e7 100644
--- a/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart
+++ b/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart
@@ -227,7 +227,10 @@
   void visitComment(Comment node) {}
 
   @override
-  void visitCommentReference(CommentReference node) {}
+  void visitCommentReference(CommentReference node) {
+    sink.write(node.newKeyword?.lexeme ?? '');
+    _visitNode(prefix: '[', node.expression, suffix: ']');
+  }
 
   @override
   void visitCompilationUnit(CompilationUnit node) {
diff --git a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
index 7ed687d..2776945 100644
--- a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
+++ b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart
@@ -634,7 +634,7 @@
 /// [$code]
 void f() {}
 ''');
-    _assertSource('', findNode.commentReference(code));
+    _assertSource('[x]', findNode.commentReference(code));
   }
 
   void test_visitCompilationUnit_declaration() {