@@ -9,7 +9,8 @@ import 'dart:convert';
99import 'dart:io' ;
1010
1111import 'package:analyzer/dart/ast/ast.dart'
12- show AnnotatedNode, Annotation, Declaration;
12+ show AnnotatedNode, Declaration, FormalParameter, FieldDeclaration,
13+ VariableDeclaration, VariableDeclarationList;
1314import 'package:analyzer/dart/element/element.dart' ;
1415import 'package:analyzer/dart/element/type.dart' ;
1516import 'package:analyzer/src/generated/resolver.dart'
@@ -913,9 +914,23 @@ class Field extends ModelElement
913914
914915 bool get writeOnly => hasSetter && ! hasGetter;
915916
917+ @override
918+ List <String > get annotations {
919+ List <String > all_annotations = new List <String >();
920+ all_annotations.addAll (super .annotations);
921+
922+ if (element is PropertyInducingElement ) {
923+ var pie = element as PropertyInducingElement ;
924+ all_annotations.addAll (annotationsFromMetadata (pie.getter? .metadata));
925+ all_annotations.addAll (annotationsFromMetadata (pie.setter? .metadata));
926+ }
927+ return all_annotations.toList (growable: false );
928+ }
929+
916930 @override
917931 Set <String > get features {
918932 Set <String > all_features = super .features;
933+ all_features.addAll (annotations);
919934 /// final/const implies read-only, so don't display both strings.
920935 if (readOnly && ! isFinal && ! isConst) all_features.add ('read-only' );
921936 if (writeOnly) all_features.add ('write-only' );
@@ -1475,43 +1490,56 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
14751490 }
14761491
14771492 List <String > get annotations {
1478- // Check https://code.google.com/p/dart/issues/detail?id=23181
1479- // If that is fixed, this code might get a lot easier
1480- if (element.computeNode () != null &&
1481- element.computeNode () is AnnotatedNode ) {
1482- return (element.computeNode () as AnnotatedNode )
1483- .metadata
1484- .map ((Annotation a) {
1485- var annotationString = a.toSource ().substring (1 ); // remove the @
1486- var e = a.element;
1487- if (e != null && (e is ConstructorElement )) {
1488- var me = new ModelElement .from (
1489- e.enclosingElement, package._getLibraryFor (e.enclosingElement));
1490- if (me.href != null ) {
1491- return annotationString.replaceAll (me.name, me.linkedName);
1492- }
1493- }
1494- return annotationString;
1495- }).toList (growable: false );
1496- } else {
1497- return element.metadata.map ((ElementAnnotation a) {
1498- // TODO link to the element's href
1499- return a.element.name;
1500- }).toList (growable: false );
1501- }
1493+ List <dynamic > metadata;
1494+ if (element.computeNode () is AnnotatedNode ) {
1495+ AnnotatedNode node = element.computeNode () as AnnotatedNode ;
1496+
1497+ // Declarations are contained inside FieldDeclarations, and that is where
1498+ // the actual annotations are.
1499+ while ((node is VariableDeclaration || node is VariableDeclarationList ) &&
1500+ node is ! FieldDeclaration ) {
1501+ assert (null != (node as AnnotatedNode ).parent);
1502+ node = node.parent;
1503+ }
1504+ metadata = node.metadata;
1505+ } else if (element.computeNode () is ! FormalParameter ) {
1506+ // TODO(jcollins-g): This is special cased to suppress annotations for
1507+ // parameters in constructor documentation. Do we
1508+ // want to do this?
1509+ metadata = element.metadata;
1510+ }
1511+ return annotationsFromMetadata (metadata);
1512+ }
1513+
1514+ /// Returns annotations from a given metadata set, with escaping.
1515+ /// md is a dynamic parameter since ElementAnnotation and Annotation have no
1516+ /// common class for calling toSource() and element.
1517+ List <String > annotationsFromMetadata (List <dynamic > md) {
1518+ if (md == null ) md = new List <dynamic >();
1519+ return md.map ((dynamic a) {
1520+ String annotation = (const HtmlEscape ()).convert (a.toSource ());
1521+ if (a.element is ConstructorElement ) {
1522+ var me = new ModelElement .from (a.element.enclosingElement,
1523+ package._getLibraryFor (a.element.enclosingElement));
1524+ annotation = annotation.replaceFirst (me.name, me.linkedName);
1525+ }
1526+ return annotation;
1527+ }).toList (growable: false );
15021528 }
15031529
1504- /// const and static are not needed here because const/static elements get
1505- /// their own sections in the doc.
15061530 Set <String > get features {
15071531 Set <String > all_features = new Set <String >();
15081532 all_features.addAll (annotations);
1509- /// override as an annotation should be replaced with direct information
1510- /// from the analyzer if we decide to display it at this level.
1511- all_features.remove ('override' );
1512- /// Drop the plain "deprecated" annotation, that's indicated via
1513- /// strikethroughs. Custom @Deprecated() will still appear.
1514- all_features.remove ('deprecated' );
1533+
1534+ // override as an annotation should be replaced with direct information
1535+ // from the analyzer if we decide to display it at this level.
1536+ all_features.remove ('@override' );
1537+
1538+ // Drop the plain "deprecated" annotation, that's indicated via
1539+ // strikethroughs. Custom @Deprecated() will still appear.
1540+ all_features.remove ('@deprecated' );
1541+ // const and static are not needed here because const/static elements get
1542+ // their own sections in the doc.
15151543 if (isFinal) all_features.add ('final' );
15161544 return all_features;
15171545 }
@@ -1749,7 +1777,7 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
17491777 buf.write ('<span class="parameter" id="${param .htmlId }">' );
17501778 if (showMetadata && param.hasAnnotations) {
17511779 param.annotations.forEach ((String annotation) {
1752- buf.write ('<span>@ $annotation </span> ' );
1780+ buf.write ('<span>$annotation </span> ' );
17531781 });
17541782 }
17551783 if (param.modelType.isFunctionType) {
@@ -2575,6 +2603,7 @@ class TopLevelVariable extends ModelElement
25752603 @override
25762604 Set <String > get features {
25772605 Set <String > all_features = super .features;
2606+
25782607 /// final/const implies read-only, so don't display both strings.
25792608 if (readOnly && ! isFinal && ! isConst) all_features.add ('read-only' );
25802609 if (writeOnly) all_features.add ('write-only' );
0 commit comments