Skip to content

Deemphasize reexported symbols [#1158] #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions lib/src/export_graph.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'package:analyzer/dart/element/element.dart';

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments to methods to explain functionality.

/// Node in the directed export graph.
///
/// It wraps a library element, and also contains links to the nodes/libs, which export this lib,
/// and nodes/libs, which are exported by this lib.
class _ExportGraphNode {
final LibraryElement libraryElement;
Set<_ExportGraphNode> exportedBy = new Set();
Set<_ExportGraphNode> exports = new Set();

_ExportGraphNode(this.libraryElement);

/// It returns a "canonical" library element
///
/// That's one of the passed in the arguments, which is the closest if we go up the graph.
_ExportGraphNode canonicalLibraryElement(Iterable<LibraryElement> libraryElements) {
if (libraryElements.contains(libraryElement)) {
return this;
} else {
return exportedBy.toList().firstWhere((l) => l.canonicalLibraryElement(libraryElements) != null);
}
}
}

/// Recursively builds the export graph, and also populates the index [map]. It takes
/// the library element from the arguments, adds it to the index, then goes through all
/// the libraries this library element exports, adds them to the index too and also connects
/// them to build a graph.
void _buildSubGraph(Map<String, _ExportGraphNode> map, LibraryElement libraryElement) {
if (!map.containsKey(libraryElement.source.fullName)) {
map[libraryElement.source.fullName] = new _ExportGraphNode(libraryElement);
}
final node = map[libraryElement.source.fullName];
libraryElement.exports.forEach((ExportElement export) {
final exportedLibraryElement = export.exportedLibrary;
if (!map.containsKey(exportedLibraryElement.source.fullName)) {
map[exportedLibraryElement.source.fullName] = new _ExportGraphNode(exportedLibraryElement);
}
final childNode = map[exportedLibraryElement.source.fullName];
childNode.exportedBy.add(node);
node.exports.add(childNode);
_buildSubGraph(map, exportedLibraryElement);
});
}

/// Directed graph, which allows to track what libs export and being exported by what libs
/// (where libs are the documentable package libraries).
///
/// Also, allows to find the "canonical" library element for the provided element, i.e. the
/// one, which should contain documentation of the provided element.
class ExportGraph {
final Map<String, _ExportGraphNode> map = {};
final Iterable<LibraryElement> packageLibraryElements;

ExportGraph(this.packageLibraryElements) {
packageLibraryElements.forEach((LibraryElement libraryElement) {
_buildSubGraph(map, libraryElement);
});
}

LibraryElement canonicalLibraryElement(Element element) {
if (map.containsKey(element.library.source.fullName)) {
final node = map[element.library.source.fullName];
return node.canonicalLibraryElement(packageLibraryElements)?.libraryElement;
} else {
return element.library;
}
}
}
2 changes: 1 addition & 1 deletion lib/src/html/html_generator_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HtmlGeneratorInstance implements HtmlOptions {

void _generateSearchIndex() {
File jsonFile = _createOutputFile(out, 'index.json');
String json = JSON.encode(documentedElements.map((ModelElement e) {
String json = JSON.encode(documentedElements.where((e) => e.isCanonical).map((ModelElement e) {
Map data = {
'name': e.name,
'qualifiedName': e.name,
Expand Down
61 changes: 48 additions & 13 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'markdown_processor.dart' show Documentation;
import 'model_utils.dart';
import 'package_meta.dart' show PackageMeta, FileContents;
import 'utils.dart';
import 'export_graph.dart';

Map<String, Map<String, List<Map<String, dynamic>>>> __crossdartJson;

Expand Down Expand Up @@ -98,7 +99,7 @@ class Accessor extends ModelElement

@override
String get href =>
'${library.dirName}/${_accessor.enclosingElement.name}/${name}.html';
'${canonicalLibrary.dirName}/${_accessor.enclosingElement.name}/${name}.html';

bool get isGetter => _accessor.isGetter;

Expand Down Expand Up @@ -310,7 +311,7 @@ class Class extends ModelElement implements EnclosedElement {
bool get hasSupertype => supertype != null;

@override
String get href => '${library.dirName}/$fileName';
String get href => '${canonicalLibrary.dirName}/$fileName';

/// Returns all the implementors of the class specified.
List<Class> get implementors =>
Expand Down Expand Up @@ -663,7 +664,7 @@ class Constructor extends ModelElement

@override
String get href =>
'${library.dirName}/${_constructor.enclosingElement.name}/$name.html';
'${canonicalLibrary.dirName}/${_constructor.enclosingElement.name}/$name.html';

@override
bool get isConst => _constructor.isConst;
Expand Down Expand Up @@ -790,7 +791,7 @@ class EnumField extends Field {

@override
String get href =>
'${library.dirName}/${(enclosingElement as Class).fileName}';
'${canonicalLibrary.dirName}/${(enclosingElement as Class).fileName}';

@override
String get linkedName => name;
Expand Down Expand Up @@ -844,12 +845,21 @@ class Field extends ModelElement
@override
bool get hasSetter => _field.setter != null;

@override
Library get canonicalLibrary {
if (isInherited) {
return enclosingElement.canonicalLibrary;
} else {
return super.canonicalLibrary;
}
}

@override
String get href {
if (enclosingElement is Class) {
return '${library.dirName}/${enclosingElement.name}/$_fileName';
return '${canonicalLibrary.dirName}/${enclosingElement.name}/$_fileName';
} else if (enclosingElement is Library) {
return '${library.dirName}/$_fileName';
return '${canonicalLibrary.dirName}/$_fileName';
} else {
throw new StateError(
'$name is not in a class or library, instead it is a ${enclosingElement.element}');
Expand Down Expand Up @@ -1284,6 +1294,15 @@ class Method extends ModelElement
}).toList();
}

@override
Library get canonicalLibrary {
if (isInherited) {
return enclosingElement.canonicalLibrary;
} else {
return super.canonicalLibrary;
}
}

@override
ModelElement get enclosingElement {
if (_enclosingClass == null) {
Expand All @@ -1301,7 +1320,7 @@ class Method extends ModelElement
}

@override
String get href => '${library.dirName}/${enclosingElement.name}/${fileName}';
String get href => '${canonicalLibrary.dirName}/${enclosingElement.name}/${fileName}';

bool get isInherited => _isInherited;

Expand Down Expand Up @@ -1456,6 +1475,17 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
return _rawDocs;
}

Library _canonicalLibrary;
Library get canonicalLibrary {
if (_canonicalLibrary == null) {
final libraryElement = package.exportGraph.canonicalLibraryElement(element) ?? library.element;
_canonicalLibrary = package.libraries.firstWhere((l) => l.element == libraryElement, orElse: () => library);
}
return _canonicalLibrary;
}

bool get isCanonical => library == canonicalLibrary;

@override
String get documentationAsHtml => _documentation.asHtml;

Expand Down Expand Up @@ -1897,7 +1927,7 @@ class ModelFunction extends ModelElement
String get fileName => "$name.html";

@override
String get href => '${library.dirName}/$fileName';
String get href => '${canonicalLibrary.dirName}/$fileName';

@override
bool get isStatic => _func.isStatic;
Expand Down Expand Up @@ -2137,6 +2167,11 @@ class Package implements Nameable, Documentable {
}
return _allModelElements;
}

ExportGraph _exportGraph;
ExportGraph get exportGraph {
return (_exportGraph ??= new ExportGraph(libraries.map((l) => l.element as LibraryElement)));
}
}

class PackageCategory implements Comparable<PackageCategory> {
Expand Down Expand Up @@ -2178,13 +2213,13 @@ class Parameter extends ModelElement implements EnclosedElement {
var p = _parameter.enclosingElement;

if (p is FunctionElement) {
return '${library.dirName}/${p.name}.html';
return '${canonicalLibrary.dirName}/${p.name}.html';
} else {
// TODO: why is this logic here?
var name = Operator.friendlyNames.containsKey(p.name)
? Operator.friendlyNames[p.name]
: p.name;
return '${library.dirName}/${p.enclosingElement.name}/' +
return '${canonicalLibrary.dirName}/${p.enclosingElement.name}/' +
'${name}.html#${htmlId}';
}
}
Expand Down Expand Up @@ -2353,7 +2388,7 @@ class TopLevelVariable extends ModelElement
bool get hasSetter => _variable.setter != null;

@override
String get href => '${library.dirName}/$_fileName';
String get href => '${canonicalLibrary.dirName}/$_fileName';

@override
bool get isConst => _variable.isConst;
Expand Down Expand Up @@ -2404,7 +2439,7 @@ class Typedef extends ModelElement
String get fileName => '$name.html';

@override
String get href => '${library.dirName}/$fileName';
String get href => '${canonicalLibrary.dirName}/$fileName';

@override
String get kind => 'typedef';
Expand Down Expand Up @@ -2435,7 +2470,7 @@ class TypeParameter extends ModelElement {

@override
String get href =>
'${library.dirName}/${_typeParameter.enclosingElement.name}/$name';
'${canonicalLibrary.dirName}/${_typeParameter.enclosingElement.name}/$name';

@override
String get kind => 'type parameter';
Expand Down
2 changes: 1 addition & 1 deletion test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ void main() {
.firstWhere((m) => m.name == 'returnCool', orElse: () => null);
expect(returnCool, isNotNull);
expect(returnCool.linkedReturnType,
equals('<a href="ex/Cool-class.html">Cool</a>'));
equals('<a href="fake/Cool-class.html">Cool</a>'));
});

test('F has a single instance method', () {
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/Animal-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/Apple-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/B-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/COLOR-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/COLOR_GREEN-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/COLOR_ORANGE-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/COMPLEX_COLOR-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/Cat-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/CatString-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/ex/ConstantCat-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>
<li><a href="ex/Cat-class.html">Cat</a></li>
<li><a href="ex/CatString-class.html">CatString</a></li>
<li><a href="ex/ConstantCat-class.html">ConstantCat</a></li>
<li><a href="ex/Cool-class.html">Cool</a></li>
<li><a href="fake/Cool-class.html">Cool</a></li>
<li><a href="ex/Deprecated-class.html">Deprecated</a></li>
<li><a href="ex/Dog-class.html">Dog</a></li>
<li><a href="ex/E-class.html">E</a></li>
Expand Down
Loading