Fix for indexing constructor references in documentation comments.

Bug: https://github.com/Dart-Code/Dart-Code/issues/2816
Change-Id: I4515b6887f5d8bd93fc6ba59812c5a9ec14f0d3c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181260
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart b/pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart
index 52da455..50f73a3 100644
--- a/pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/rename_constructor_test.dart
@@ -95,6 +95,7 @@
 
   Future<void> test_createChange_add() async {
     await indexTestUnit('''
+/// Documentation for [new A]
 class A {
   A() {} // marker
   factory A._() = A;
@@ -114,6 +115,7 @@
     // validate change
     refactoring.newName = 'newName';
     return assertSuccessfulRefactoring('''
+/// Documentation for [new A.newName]
 class A {
   A.newName() {} // marker
   factory A._() = A.newName;
@@ -129,6 +131,7 @@
 
   Future<void> test_createChange_add_toSynthetic() async {
     await indexTestUnit('''
+/// Documentation for [new A]
 class A {
 }
 class B extends A {
@@ -146,6 +149,7 @@
     // validate change
     refactoring.newName = 'newName';
     return assertSuccessfulRefactoring('''
+/// Documentation for [new A.newName]
 class A {
   A.newName();
 }
@@ -160,6 +164,7 @@
 
   Future<void> test_createChange_change() async {
     await indexTestUnit('''
+/// Documentation for [A.test] and [new A.test]
 class A {
   A.test() {} // marker
   factory A._() = A.test;
@@ -179,6 +184,7 @@
     // validate change
     refactoring.newName = 'newName';
     return assertSuccessfulRefactoring('''
+/// Documentation for [A.newName] and [new A.newName]
 class A {
   A.newName() {} // marker
   factory A._() = A.newName;
@@ -194,6 +200,7 @@
 
   Future<void> test_createChange_remove() async {
     await indexTestUnit('''
+/// Documentation for [A.test] and [new A.test]
 class A {
   A.test() {} // marker
   factory A._() = A.test;
@@ -213,6 +220,7 @@
     // validate change
     refactoring.newName = '';
     return assertSuccessfulRefactoring('''
+/// Documentation for [A] and [new A]
 class A {
   A() {} // marker
   factory A._() = A;
diff --git a/pkg/analyzer/lib/src/dart/analysis/index.dart b/pkg/analyzer/lib/src/dart/analysis/index.dart
index c664f80..1df5522 100644
--- a/pkg/analyzer/lib/src/dart/analysis/index.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/index.dart
@@ -584,6 +584,28 @@
   }
 
   @override
+  visitCommentReference(CommentReference node) {
+    var identifier = node.identifier;
+    var element = identifier.staticElement;
+    if (element is ConstructorElement) {
+      if (identifier is PrefixedIdentifier) {
+        var offset = identifier.prefix.end;
+        var length = identifier.end - offset;
+        recordRelationOffset(
+            element, IndexRelationKind.IS_REFERENCED_BY, offset, length, true);
+        return;
+      } else {
+        var offset = identifier.end;
+        recordRelationOffset(
+            element, IndexRelationKind.IS_REFERENCED_BY, offset, 0, true);
+        return;
+      }
+    }
+
+    return super.visitCommentReference(node);
+  }
+
+  @override
   void visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
     SimpleIdentifier fieldName = node.fieldName;
     if (fieldName != null) {
diff --git a/pkg/analyzer/test/src/dart/analysis/index_test.dart b/pkg/analyzer/test/src/dart/analysis/index_test.dart
index 21fcfc4..1f5499e 100644
--- a/pkg/analyzer/test/src/dart/analysis/index_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/index_test.dart
@@ -602,33 +602,39 @@
 
   test_isReferencedBy_ConstructorElement() async {
     await _indexTestUnit('''
+/// [new A.foo] 1
+/// [A.foo] 2
+/// [new A] 3
 class A implements B {
   A() {}
   A.foo() {}
 }
 class B extends A {
-  B() : super(); // 1
-  B.foo() : super.foo(); // 2
-  factory B.bar() = A.foo; // 3
+  B() : super(); // 4
+  B.foo() : super.foo(); // 5
+  factory B.bar() = A.foo; // 6
 }
 main() {
-  new A(); // 4
-  new A.foo(); // 5
+  new A(); // 7
+  new A.foo(); // 8
 }
 ''');
     var constA = findElement.unnamedConstructor('A');
     var constA_foo = findElement.constructor('foo', of: 'A');
     // A()
     assertThat(constA)
-      ..hasRelationCount(2)
-      ..isReferencedAt('(); // 1', true, length: 0)
-      ..isReferencedAt('(); // 4', true, length: 0);
+      ..hasRelationCount(3)
+      ..isReferencedAt('] 3', true, length: 0)
+      ..isReferencedAt('(); // 4', true, length: 0)
+      ..isReferencedAt('(); // 7', true, length: 0);
     // A.foo()
     assertThat(constA_foo)
-      ..hasRelationCount(3)
-      ..isReferencedAt('.foo(); // 2', true, length: 4)
-      ..isReferencedAt('.foo; // 3', true, length: 4)
-      ..isReferencedAt('.foo(); // 5', true, length: 4);
+      ..hasRelationCount(5)
+      ..isReferencedAt('.foo] 1', true, length: 4)
+      ..isReferencedAt('.foo] 2', true, length: 4)
+      ..isReferencedAt('.foo(); // 5', true, length: 4)
+      ..isReferencedAt('.foo; // 6', true, length: 4)
+      ..isReferencedAt('.foo(); // 8', true, length: 4);
   }
 
   test_isReferencedBy_ConstructorElement_classTypeAlias() async {