| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library docgen.models.annotation; | 5 library docgen.models.annotation; |
| 6 | 6 |
| 7 import '../exports/source_mirrors.dart'; | 7 import '../exports/source_mirrors.dart'; |
| 8 | 8 |
| 9 import '../exports/dart2js_mirrors.dart' show ResolvedNode; | |
| 10 | |
| 11 import '../library_helpers.dart'; | 9 import '../library_helpers.dart'; |
| 12 | 10 |
| 13 import 'library.dart'; | 11 import 'library.dart'; |
| 14 import 'mirror_based.dart'; | 12 import 'mirror_based.dart'; |
| 15 | 13 |
| 16 import 'dart:mirrors'; | 14 import 'dart:mirrors'; |
| 17 import 'package:compiler/src/tree/tree.dart'; | |
| 18 | 15 |
| 19 /// Holds the name of the annotation, and its parameters. | 16 /// Holds the name of the annotation, and its parameters. |
| 20 class Annotation extends MirrorBased<ClassMirror> { | 17 class Annotation extends MirrorBased<ClassMirror> { |
| 21 /// The class of this annotation. | 18 /// The class of this annotation. |
| 22 DeclarationMirror mirror; | 19 DeclarationMirror mirror; |
| 23 Send node; | |
| 24 final Library owningLibrary; | 20 final Library owningLibrary; |
| 25 List<String> parameters; | 21 List<String> parameters; |
| 26 | 22 |
| 27 Annotation(ResolvedNode resolvedNode, this.owningLibrary) { | 23 Annotation(this.owningLibrary, this.mirror, |
| 28 parameters = []; | 24 [List<String> this.parameters = const <String>[]]); |
| 29 getMirrorForResolvedNode(resolvedNode, (m, n) { mirror = m; node = n;}, | |
| 30 (String param) => parameters.add(param)); | |
| 31 } | |
| 32 | |
| 33 String getMirrorForResolvedNode(ResolvedNode node, callbackFunc, | |
| 34 paramCallbackFunc) { | |
| 35 ResolvedNodeMirrorFinder finder = new ResolvedNodeMirrorFinder(node, | |
| 36 callbackFunc, paramCallbackFunc); | |
| 37 finder.unparse(node.node); | |
| 38 return finder.result; | |
| 39 } | |
| 40 | 25 |
| 41 Map toMap() => { | 26 Map toMap() => { |
| 42 'name': owningLibrary.packagePrefix + | 27 'name': owningLibrary.packagePrefix + |
| 43 getDocgenObject(mirror, owningLibrary).docName, | 28 getDocgenObject(mirror, owningLibrary).docName, |
| 44 'parameters': parameters | 29 'parameters': parameters |
| 45 }; | 30 }; |
| 46 } | 31 } |
| 47 | 32 |
| 48 class ResolvedNodeMirrorFinder extends Unparser { | |
| 49 final ResolvedNode resolvedNode; | |
| 50 final Function annotationMirrorCallback; | |
| 51 final Function parameterValueCallback; | |
| 52 int recursionLevel; | |
| 53 | |
| 54 ResolvedNodeMirrorFinder(this.resolvedNode, this.annotationMirrorCallback, | |
| 55 this.parameterValueCallback) : recursionLevel = 0; | |
| 56 | |
| 57 visitSend(Send node) { | |
| 58 if (recursionLevel == 0) { | |
| 59 var m = resolvedNode.resolvedMirror(node.selector); | |
| 60 annotationMirrorCallback(m, node); | |
| 61 } else { | |
| 62 Operator op = node.selector.asOperator(); | |
| 63 String opString = op != null ? op.source : null; | |
| 64 bool spacesNeeded = | |
| 65 identical(opString, 'is') || identical(opString, 'as'); | |
| 66 if (node.isPrefix) visit(node.selector); | |
| 67 unparseSendReceiver(node, spacesNeeded: spacesNeeded); | |
| 68 if (!node.isPrefix && !node.isIndex) visit(node.selector); | |
| 69 if (spacesNeeded) sb.write(' '); | |
| 70 // Also add a space for sequences like x + +1 and y - -y. | |
| 71 // TODO(ahe): remove case for '+' when we drop the support for it. | |
| 72 if (node.argumentsNode != null && (identical(opString, '-') | |
| 73 || identical(opString, '+'))) { | |
| 74 var beginToken = node.argumentsNode.getBeginToken(); | |
| 75 if (beginToken != null && | |
| 76 identical(beginToken.stringValue, opString)) { | |
| 77 sb.write(' '); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 recursionLevel++; | |
| 82 visit(node.argumentsNode); | |
| 83 recursionLevel--; | |
| 84 } | |
| 85 | |
| 86 unparseNodeListFrom(NodeList node, var from, {bool spaces: true}) { | |
| 87 if (from.isEmpty) return; | |
| 88 | |
| 89 visit(from.head); | |
| 90 | |
| 91 for (var link = from.tail; !link.isEmpty; link = link.tail) { | |
| 92 if (recursionLevel >= 2) { | |
| 93 parameterValueCallback(sb.toString()); | |
| 94 sb.clear(); | |
| 95 } | |
| 96 visit(link.head); | |
| 97 } | |
| 98 if (recursionLevel >= 2) { | |
| 99 parameterValueCallback(sb.toString()); | |
| 100 sb.clear(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 visitNodeList(NodeList node) { | |
| 105 addToken(node.beginToken); | |
| 106 if (recursionLevel == 1) sb.clear(); | |
| 107 if (node.nodes != null) { | |
| 108 recursionLevel++; | |
| 109 unparseNodeListFrom(node, node.nodes); | |
| 110 recursionLevel--; | |
| 111 } | |
| 112 if (node.endToken != null) write(node.endToken.value); | |
| 113 } | |
| 114 } | |
| OLD | NEW |