Support top level declarations with templates (#1984)

* Support top level declarations with templates

* Add a comment
diff --git a/lib/src/model.dart b/lib/src/model.dart
index a417a57..d838f60 100644
--- a/lib/src/model.dart
+++ b/lib/src/model.dart
@@ -4912,6 +4912,12 @@
             !precachedElements.contains(d)) {
           precachedElements.add(d);
           yield d._precacheLocalDocs();
+          // TopLevelVariables get their documentation from getters and setters,
+          // so should be precached if either has a template.
+          if (m is TopLevelVariable) {
+            precachedElements.add(m);
+            yield m._precacheLocalDocs();
+          }
         }
       }
     }
diff --git a/test/model_test.dart b/test/model_test.dart
index 0710a86..b186657 100644
--- a/test/model_test.dart
+++ b/test/model_test.dart
@@ -3095,7 +3095,7 @@
     });
 
     test('found five properties', () {
-      expect(exLibrary.publicProperties, hasLength(5));
+      expect(exLibrary.publicProperties, hasLength(7));
     });
 
     test('linked return type is a double', () {
diff --git a/testing/test_package/lib/example.dart b/testing/test_package/lib/example.dart
index 5344182..ea392c6 100644
--- a/testing/test_package/lib/example.dart
+++ b/testing/test_package/lib/example.dart
@@ -31,6 +31,22 @@
 /// This should [not work].
 const incorrectDocReferenceFromEx = 'doh';
 
+/// top level declarations with templates/macros
+///
+/// {@template ex1}
+/// ex2 macro content
+/// {@endtemplate}
+int myNumber = 3;
+
+/// {@macro ex1}
+void testMacro() {}
+
+/// {@template ex2}
+/// ex2 macro content
+/// {@endtemplate}
+bool get isCheck => true;
+
+
 /// A custom annotation.
 class aThingToDo {
   final String who;