Skip to content

Commit 61797bc

Browse files
astashovdevoncarew
authored andcommitted
Rank parent class method higher than its overrides in search suggestions (#1308)
We do that by saving `overriddenDepth` to index.json (which is just how many overridden elements in the inheritance chain that element has), and then reducing weight of elements in the suggestions according to `overriddenDepth`. Also, fixed `grind update-test-package-docs`, it didn't know where to find examples for `@examples` directives. Fixes #896
1 parent c9bb363 commit 61797bc

File tree

7 files changed

+25
-3
lines changed

7 files changed

+25
-3
lines changed

lib/resources/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function initSearch() {
7676
var allMatches = []; // list of matches
7777

7878
function score(element, num) {
79+
num -= element.overriddenDepth * 10;
7980
var weightFactor = weights[element.type] || 4;
8081
return {e: element, score: (num / weightFactor) >> 0};
8182
}

lib/src/html/html_generator_instance.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class HtmlGeneratorInstance implements HtmlOptions {
5555
'name': e.name,
5656
'qualifiedName': e.name,
5757
'href': e.href,
58-
'type': e.kind
58+
'type': e.kind,
59+
'overriddenDepth': e.overriddenDepth,
5960
};
6061
if (e is EnclosedElement) {
6162
EnclosedElement ee = e as EnclosedElement;

lib/src/model.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,19 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
15291529

15301530
ModelElement get overriddenElement => null;
15311531

1532+
int _overriddenDepth;
1533+
int get overriddenDepth {
1534+
if (_overriddenDepth == null) {
1535+
_overriddenDepth = 0;
1536+
ModelElement e = this;
1537+
while (e.overriddenElement != null) {
1538+
_overriddenDepth += 1;
1539+
e = e.overriddenElement;
1540+
}
1541+
}
1542+
return _overriddenDepth;
1543+
}
1544+
15321545
Package get package =>
15331546
(this is Library) ? (this as Library).package : this.library.package;
15341547

test/model_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,12 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
911911
expect(abstractMethod.fullkind, 'abstract method');
912912
});
913913

914+
test("returns correct overriddenDepth", () {
915+
final bAbstractMethod = classB.allInstanceMethods.firstWhere((m) => m.name == "abstractMethod");
916+
expect(abstractMethod.overriddenDepth, equals(0));
917+
expect(bAbstractMethod.overriddenDepth, equals(1));
918+
});
919+
914920
test(
915921
'an inherited method has class as the enclosing class, when superclass not in package',
916922
() {

testing/test_package_docs/index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

testing/test_package_docs/static-assets/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function initSearch() {
7676
var allMatches = []; // list of matches
7777

7878
function score(element, num) {
79+
num -= element.overriddenDepth * 10;
7980
var weightFactor = weights[element.type] || 4;
8081
return {e: element, score: (num / weightFactor) >> 0};
8182
}

tool/grind.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ updateTestPackageDocs() {
202202
var options = new RunOptions(workingDirectory: 'testing/test_package');
203203
delete(getDir('test_package_docs'));
204204
Dart.run('../../bin/dartdoc.dart',
205-
arguments: ['--no-include-source', '--output', '../test_package_docs'],
205+
arguments: ['--no-include-source', '--output', '../test_package_docs', '--example-path-prefix', 'examples'],
206206
runOptions: options);
207207
}
208208

0 commit comments

Comments
 (0)