Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: pkg/docgen/lib/docgen.dart

Issue 62963005: Add package name for resolved names in packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 /// **docgen** is a tool for creating machine readable representations of Dart 5 /// **docgen** is a tool for creating machine readable representations of Dart
6 /// code metadata, including: classes, members, comments and annotations. 6 /// code metadata, including: classes, members, comments and annotations.
7 /// 7 ///
8 /// docgen is run on a `.dart` file or a directory containing `.dart` files. 8 /// docgen is run on a `.dart` file or a directory containing `.dart` files.
9 /// 9 ///
10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR] 10 /// $ dart docgen.dart [OPTIONS] [FILE/DIR]
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 (each) => librariesWeGotByPath.putIfAbsent(each, 130 (each) => librariesWeGotByPath.putIfAbsent(each,
131 () => throw "Missing library $each")).toList(); 131 () => throw "Missing library $each")).toList();
132 librariesToDocument.addAll((includeSdk || parseSdk) ? _sdkLibraries : []); 132 librariesToDocument.addAll((includeSdk || parseSdk) ? _sdkLibraries : []);
133 _documentLibraries(librariesToDocument, includeSdk: includeSdk, 133 _documentLibraries(librariesToDocument, includeSdk: includeSdk,
134 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk, 134 outputToYaml: outputToYaml, append: append, parseSdk: parseSdk,
135 introduction: introduction); 135 introduction: introduction);
136 return true; 136 return true;
137 }); 137 });
138 } 138 }
139 139
140 /// For a [library] and its corresponding [mirror] that we believe come 140 /// For a library's [mirror], determine the name of the package (if any) we
141 /// from a package (because it has a file 141 /// believe it came from (because of its file URI).
142 /// URI) look for the package name and set it on [library]. 142 ///
143 void _findPackage(Library library, LibraryMirror mirror) { 143 /// If [library] is specified, we set the packageName field. If no package could
144 if (mirror.uri.scheme != 'file') return; 144 /// be determined, we return an empty string.
145 var filePath = mirror.uri.toFilePath(); 145 String _findPackage(LibraryMirror mirror, [Library library]) {
146 // We assume that we are documenting only libraries under package/lib 146 if (mirror.uri.scheme != 'file') return '';
147 var rootdir = path.dirname((path.dirname(filePath))); 147 var filePath = mirror.uri.toFilePath();
148 var pubspec = path.join(rootdir, 'pubspec.yaml'); 148 // We assume that we are documenting only libraries under package/lib
149 library.packageName = _packageName(pubspec); 149 var rootdir = path.dirname((path.dirname(filePath)));
150 var pubspec = path.join(rootdir, 'pubspec.yaml');
151 var packageName = _packageName(pubspec);
152 if (library != null) {
153 library.packageName = packageName;
150 // If we are the main library in a package, associate the package readme 154 // If we are the main library in a package, associate the package readme
151 // with us. 155 // with us.
152 // TODO(alanknight): We can't really rely on all packages having a library 156 // TODO(alanknight): We can't really rely on all packages having a library
153 // that matches the package name. Need a better way to store this. 157 // that matches the package name. Need a better way to store this.
154 if (library.packageName == library.name) { 158 if (library.packageName == library.name) {
155 library.packageIntro = _packageIntro(rootdir); 159 library.packageIntro = _packageIntro(rootdir);
156 } 160 }
161 }
162 return packageName + '/';
157 } 163 }
158 164
159 String _packageIntro(packageDir) { 165 String _packageIntro(packageDir) {
160 var dir = new Directory(packageDir); 166 var dir = new Directory(packageDir);
161 var files = dir.listSync(); 167 var files = dir.listSync();
162 var readmes = files.where((FileSystemEntity each) => (each is File && 168 var readmes = files.where((FileSystemEntity each) => (each is File &&
163 each.path.substring(packageDir.length + 1, each.path.length) 169 each.path.substring(packageDir.length + 1, each.path.length)
164 .startsWith('README'))).toList(); 170 .startsWith('README'))).toList();
165 if (readmes.isEmpty) return ''; 171 if (readmes.isEmpty) return '';
166 // If there are multiples, pick the shortest name. 172 // If there are multiples, pick the shortest name.
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 _writeToFile(JSON.encode(index), 'index.json'); 383 _writeToFile(JSON.encode(index), 'index.json');
378 } 384 }
379 385
380 Library generateLibrary(dart2js.Dart2JsLibraryMirror library) { 386 Library generateLibrary(dart2js.Dart2JsLibraryMirror library) {
381 _currentLibrary = library; 387 _currentLibrary = library;
382 var result = new Library(docName(library), _commentToHtml(library), 388 var result = new Library(docName(library), _commentToHtml(library),
383 _classes(library.classes), 389 _classes(library.classes),
384 _methods(library.functions), 390 _methods(library.functions),
385 _variables(library.variables), 391 _variables(library.variables),
386 _isHidden(library)); 392 _isHidden(library));
387 _findPackage(result, library); 393 _findPackage(library, result);
388 logger.fine('Generated library for ${result.name}'); 394 logger.fine('Generated library for ${result.name}');
389 return result; 395 return result;
390 } 396 }
391 397
392 void _writeIndexableToFile(Indexable result, bool outputToYaml) { 398 void _writeIndexableToFile(Indexable result, bool outputToYaml) {
393 var outputFile = result.fileName; 399 var outputFile = result.fileName;
394 var output; 400 var output;
395 if (outputToYaml) { 401 if (outputToYaml) {
396 output = getYamlString(result.toMap()); 402 output = getYamlString(result.toMap());
397 outputFile = outputFile + '.yaml'; 403 outputFile = outputFile + '.yaml';
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 538
533 String _htmlMdn(String content, String url) { 539 String _htmlMdn(String content, String url) {
534 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">' 540 return '<div class="mdn">' + content.trim() + '<p class="mdn-note">'
535 '<a href="' + url.trim() + '">from Mdn</a></p></div>'; 541 '<a href="' + url.trim() + '">from Mdn</a></p></div>';
536 } 542 }
537 543
538 /// Look for the specified name starting with the current member, and 544 /// Look for the specified name starting with the current member, and
539 /// progressively working outward to the current library scope. 545 /// progressively working outward to the current library scope.
540 String findElementInScope(String name, LibraryMirror currentLibrary, 546 String findElementInScope(String name, LibraryMirror currentLibrary,
541 ClassMirror currentClass, MemberMirror currentMember) { 547 ClassMirror currentClass, MemberMirror currentMember) {
548 var packagePrefix = _findPackage(currentLibrary);
549
542 determineLookupFunc(name) => name.contains('.') ? 550 determineLookupFunc(name) => name.contains('.') ?
543 dart2js_util.lookupQualifiedInScope : 551 dart2js_util.lookupQualifiedInScope :
544 (mirror, name) => mirror.lookupInScope(name); 552 (mirror, name) => mirror.lookupInScope(name);
545 var lookupFunc = determineLookupFunc(name); 553 var lookupFunc = determineLookupFunc(name);
546 554
547 var memberScope = currentMember == null ? 555 var memberScope = currentMember == null ?
548 null : lookupFunc(currentMember, name); 556 null : lookupFunc(currentMember, name);
549 if (memberScope != null) return docName(memberScope); 557 if (memberScope != null) return packagePrefix + docName(memberScope);
550 558
551 var classScope = currentClass; 559 var classScope = currentClass;
552 while (classScope != null) { 560 while (classScope != null) {
553 var classFunc = lookupFunc(currentClass, name); 561 var classFunc = lookupFunc(currentClass, name);
554 if (classFunc != null) return docName(classFunc); 562 if (classFunc != null) return packagePrefix + docName(classFunc);
555 classScope = classScope.superclass; 563 classScope = classScope.superclass;
556 } 564 }
557 565
558 var libraryScope = currentLibrary == null ? 566 var libraryScope = currentLibrary == null ?
559 null : lookupFunc(currentLibrary, name); 567 null : lookupFunc(currentLibrary, name);
560 if (libraryScope != null) return docName(libraryScope); 568 if (libraryScope != null) return packagePrefix + docName(libraryScope);
561 569
562 // Look in the dart core library scope. 570 // Look in the dart core library scope.
563 var coreScope = _coreLibrary == null? null : lookupFunc(_coreLibrary, name); 571 var coreScope = _coreLibrary == null? null : lookupFunc(_coreLibrary, name);
564 if (coreScope != null) return docName(_coreLibrary); 572 if (coreScope != null) return packagePrefix + docName(_coreLibrary);
565 573
566 // If it's a reference that starts with a another library name, then it 574 // If it's a reference that starts with a another library name, then it
567 // looks for a match of that library name in the other sdk libraries. 575 // looks for a match of that library name in the other sdk libraries.
568 if(name.contains('.')) { 576 if(name.contains('.')) {
569 var index = name.indexOf('.'); 577 var index = name.indexOf('.');
570 var libraryName = name.substring(0, index); 578 var libraryName = name.substring(0, index);
571 var remainingName = name.substring(index + 1); 579 var remainingName = name.substring(index + 1);
572 foundLibraryName(library) => library.uri.pathSegments[0] == libraryName; 580 foundLibraryName(library) => library.uri.pathSegments[0] == libraryName;
573 581
574 if (_sdkLibraries.any(foundLibraryName)) { 582 if (_sdkLibraries.any(foundLibraryName)) {
575 var library = _sdkLibraries.singleWhere(foundLibraryName); 583 var library = _sdkLibraries.singleWhere(foundLibraryName);
576 // Look to see if it's a fully qualified library name. 584 // Look to see if it's a fully qualified library name.
577 var scope = determineLookupFunc(remainingName)(library, remainingName); 585 var scope = determineLookupFunc(remainingName)(library, remainingName);
578 if (scope != null) return docName(scope); 586 if (scope != null) return packagePrefix + docName(scope);
579 } 587 }
580 } 588 }
581 return null; 589 return null;
582 } 590 }
583 591
584 // HTML escaped version of '<' character. 592 // HTML escaped version of '<' character.
585 final _LESS_THAN = '&lt;'; 593 final _LESS_THAN = '&lt;';
586 594
587 /// Chunk the provided name into individual parts to be resolved. We take a 595 /// Chunk the provided name into individual parts to be resolved. We take a
588 /// simplistic approach to chunking, though, we break at " ", ",", "&lt;" 596 /// simplistic approach to chunking, though, we break at " ", ",", "&lt;"
(...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 /// Remove statics from the map of inherited items before adding them. 1392 /// Remove statics from the map of inherited items before adding them.
1385 Map _filterStatics(Map items) { 1393 Map _filterStatics(Map items) {
1386 var result = {}; 1394 var result = {};
1387 items.forEach((name, item) { 1395 items.forEach((name, item) {
1388 if (!item.isStatic) { 1396 if (!item.isStatic) {
1389 result[name] = item; 1397 result[name] = item;
1390 } 1398 }
1391 }); 1399 });
1392 return result; 1400 return result;
1393 } 1401 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698