| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library docgen.models.indexable; |
| 6 |
| 7 import 'package:markdown/markdown.dart' as markdown; |
| 8 |
| 9 import '../exports/mirrors_util.dart' as dart2js_util; |
| 10 import '../exports/source_mirrors.dart'; |
| 11 |
| 12 import '../library_helpers.dart'; |
| 13 import 'dummy_mirror.dart'; |
| 14 import 'library.dart'; |
| 15 import 'mirror_based.dart'; |
| 16 import 'model_helpers.dart'; |
| 17 |
| 18 /// An item that is categorized in our mirrorToDocgen map, as a distinct, |
| 19 /// searchable element. |
| 20 /// |
| 21 /// These are items that refer to concrete entities (a Class, for example, |
| 22 /// but not a Type, which is a "pointer" to a class) that we wish to be |
| 23 /// globally resolvable. This includes things such as class methods and |
| 24 /// variables, but parameters for methods are not "Indexable" as we do not want |
| 25 /// the user to be able to search for a method based on its parameter names! |
| 26 /// The set of indexable items also includes Typedefs, since the user can refer |
| 27 /// to them as concrete entities in a particular scope. |
| 28 abstract class Indexable<TMirror extends DeclarationMirror> |
| 29 extends MirrorBased<TMirror> { |
| 30 |
| 31 Library get owningLibrary => owner.owningLibrary; |
| 32 |
| 33 String get qualifiedName => fileName; |
| 34 final TMirror mirror; |
| 35 final bool isPrivate; |
| 36 /// The comment text pre-resolution. We keep this around because inherited |
| 37 /// methods need to resolve links differently from the superclass. |
| 38 String unresolvedComment = ''; |
| 39 |
| 40 Indexable(TMirror mirror) |
| 41 : this.mirror = mirror, |
| 42 this.isPrivate = isHidden(mirror) { |
| 43 |
| 44 var map = mirrorToDocgen[dart2js_util.qualifiedNameOf(this.mirror)]; |
| 45 if (map == null) map = new Map<String, Set<Indexable>>(); |
| 46 |
| 47 var set = map[owner.docName]; |
| 48 if (set == null) set = new Set<Indexable>(); |
| 49 set.add(this); |
| 50 map[owner.docName] = set; |
| 51 mirrorToDocgen[dart2js_util.qualifiedNameOf(this.mirror)] = map; |
| 52 } |
| 53 |
| 54 /// Returns this object's qualified name, but following the conventions |
| 55 /// we're using in Dartdoc, which is that library names with dots in them |
| 56 /// have them replaced with hyphens. |
| 57 String get docName; |
| 58 |
| 59 /// Converts all [foo] references in comments to <a>libraryName.foo</a>. |
| 60 markdown.Node fixReference(String name) { |
| 61 // Attempt the look up the whole name up in the scope. |
| 62 String elementName = findElementInScope(name); |
| 63 if (elementName != null) { |
| 64 return new markdown.Element.text('a', elementName); |
| 65 } |
| 66 return fixComplexReference(name); |
| 67 } |
| 68 |
| 69 /// Look for the specified name starting with the current member, and |
| 70 /// progressively working outward to the current library scope. |
| 71 String findElementInScope(String name) => |
| 72 findElementInScopeWithPrefix(name, packagePrefix); |
| 73 |
| 74 /// The reference to this element based on where it is printed as a |
| 75 /// documentation file and also the unique URL to refer to this item. |
| 76 /// |
| 77 /// The qualified name (for URL purposes) and the file name are the same, |
| 78 /// of the form packageName/ClassName or packageName/ClassName.methodName. |
| 79 /// This defines both the URL and the directory structure. |
| 80 String get fileName => packagePrefix + ownerPrefix + name; |
| 81 |
| 82 /// The full docName of the owner element, appended with a '.' for this |
| 83 /// object's name to be appended. |
| 84 String get ownerPrefix => owner.docName != '' ? owner.docName + '.' : ''; |
| 85 |
| 86 /// The prefix String to refer to the package that this item is in, for URLs |
| 87 /// and comment resolution. |
| 88 /// |
| 89 /// The prefix can be prepended to a qualified name to get a fully unique |
| 90 /// name among all packages. |
| 91 String get packagePrefix; |
| 92 |
| 93 /// Documentation comment with converted markdown and all links resolved. |
| 94 String commentField; |
| 95 |
| 96 /// Accessor to documentation comment with markdown converted to html and all |
| 97 /// links resolved. |
| 98 String get comment { |
| 99 if (commentField != null) return commentField; |
| 100 |
| 101 commentField = commentToHtml(); |
| 102 if (commentField.isEmpty) { |
| 103 commentField = getMdnComment(); |
| 104 } |
| 105 return commentField; |
| 106 } |
| 107 |
| 108 void set comment(x) { |
| 109 commentField = x; |
| 110 } |
| 111 |
| 112 /// The simple name to refer to this item. |
| 113 String get name => dart2js_util.nameOf(mirror); |
| 114 |
| 115 /// Accessor to the parent item that owns this item. |
| 116 /// |
| 117 /// "Owning" is defined as the object one scope-level above which this item |
| 118 /// is defined. Ex: The owner for a top level class, would be its enclosing |
| 119 /// library. The owner of a local variable in a method would be the enclosing |
| 120 /// method. |
| 121 Indexable get owner => new DummyMirror(mirror.owner); |
| 122 |
| 123 /// Generates MDN comments from database.json. |
| 124 String getMdnComment(); |
| 125 |
| 126 /// The type of this member to be used in index.txt. |
| 127 String get typeName => ''; |
| 128 |
| 129 /// Creates a [Map] with this [Indexable]'s name and a preview comment. |
| 130 Map get previewMap { |
| 131 var finalMap = { 'name' : name, 'qualifiedName' : qualifiedName }; |
| 132 var pre = preview; |
| 133 if (pre != null) finalMap['preview'] = pre; |
| 134 return finalMap; |
| 135 } |
| 136 |
| 137 String get preview { |
| 138 if (comment != '') { |
| 139 var index = comment.indexOf('</p>'); |
| 140 return index > 0 ? |
| 141 '${comment.substring(0, index)}</p>' : |
| 142 '<p><i>Comment preview not available</i></p>'; |
| 143 } |
| 144 return null; |
| 145 } |
| 146 |
| 147 /// Accessor to obtain the raw comment text for a given item, _without_ any |
| 148 /// of the links resolved. |
| 149 String get _commentText { |
| 150 String commentText; |
| 151 mirror.metadata.forEach((metadata) { |
| 152 if (metadata is CommentInstanceMirror) { |
| 153 CommentInstanceMirror comment = metadata; |
| 154 if (comment.isDocComment) { |
| 155 if (commentText == null) { |
| 156 commentText = comment.trimmedText; |
| 157 } else { |
| 158 commentText = '$commentText\n${comment.trimmedText}'; |
| 159 } |
| 160 } |
| 161 } |
| 162 }); |
| 163 return commentText; |
| 164 } |
| 165 |
| 166 /// Returns any documentation comments associated with a mirror with |
| 167 /// simple markdown converted to html. |
| 168 /// |
| 169 /// By default we resolve any comment references within our own scope. |
| 170 /// However, if a method is inherited, we want the inherited comments, but |
| 171 /// links to the subclasses's version of the methods. |
| 172 String commentToHtml([Indexable resolvingScope]) { |
| 173 if (resolvingScope == null) resolvingScope = this; |
| 174 var commentText = _commentText; |
| 175 unresolvedComment = commentText; |
| 176 |
| 177 commentText = commentText == null ? '' : |
| 178 markdown.markdownToHtml(commentText.trim(), |
| 179 linkResolver: resolvingScope.fixReference, |
| 180 inlineSyntaxes: MARKDOWN_SYNTAXES); |
| 181 return commentText; |
| 182 } |
| 183 |
| 184 /// Return a map representation of this type. |
| 185 Map toMap(); |
| 186 |
| 187 /// Accessor to determine if this item and all of its owners are visible. |
| 188 bool get isVisible => isFullChainVisible(this); |
| 189 |
| 190 /// Returns true if [mirror] is the correct type of mirror that this Docgen |
| 191 /// object wraps. (Workaround for the fact that Types are not first class.) |
| 192 bool isValidMirror(DeclarationMirror mirror); |
| 193 } |
| OLD | NEW |