Skip to content

Commit e87b4b3

Browse files
Search a substring instead of start of string in rustdoc search
1 parent bf1e461 commit e87b4b3

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/librustdoc/html/static/main.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@
741741
return literalSearch === true ? false : lev_distance;
742742
}
743743

744-
function checkPath(startsWith, lastElem, ty) {
745-
if (startsWith.length === 0) {
744+
function checkPath(contains, lastElem, ty) {
745+
if (contains.length === 0) {
746746
return 0;
747747
}
748748
var ret_lev = MAX_LEV_DISTANCE + 1;
@@ -752,25 +752,25 @@
752752
path.push(ty.parent.name.toLowerCase());
753753
}
754754

755-
if (startsWith.length > path.length) {
755+
if (contains.length > path.length) {
756756
return MAX_LEV_DISTANCE + 1;
757757
}
758758
for (var i = 0; i < path.length; ++i) {
759-
if (i + startsWith.length > path.length) {
759+
if (i + contains.length > path.length) {
760760
break;
761761
}
762762
var lev_total = 0;
763763
var aborted = false;
764-
for (var x = 0; x < startsWith.length; ++x) {
765-
var lev = levenshtein(path[i + x], startsWith[x]);
764+
for (var x = 0; x < contains.length; ++x) {
765+
var lev = levenshtein(path[i + x], contains[x]);
766766
if (lev > MAX_LEV_DISTANCE) {
767767
aborted = true;
768768
break;
769769
}
770770
lev_total += lev;
771771
}
772772
if (aborted === false) {
773-
ret_lev = Math.min(ret_lev, Math.round(lev_total / startsWith.length));
773+
ret_lev = Math.min(ret_lev, Math.round(lev_total / contains.length));
774774
}
775775
}
776776
return ret_lev;
@@ -934,7 +934,7 @@
934934
}
935935
}
936936
val = paths[paths.length - 1];
937-
var startsWith = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
937+
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
938938

939939
for (j = 0; j < nSearchWords; ++j) {
940940
var lev_distance;
@@ -944,7 +944,7 @@
944944
}
945945
var lev_add = 0;
946946
if (paths.length > 1) {
947-
var lev = checkPath(startsWith, paths[paths.length - 1], ty);
947+
var lev = checkPath(contains, paths[paths.length - 1], ty);
948948
if (lev > MAX_LEV_DISTANCE) {
949949
continue;
950950
} else if (lev > 0) {
@@ -987,7 +987,7 @@
987987
}
988988

989989
lev += lev_add;
990-
if (lev > 0 && val.length > 3 && searchWords[j].startsWith(val)) {
990+
if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
991991
if (val.length < 6) {
992992
lev -= 1;
993993
} else {

src/test/rustdoc-js/substring.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// exact-check
12+
13+
const QUERY = 'waker_from';
14+
15+
const EXPECTED = {
16+
'others': [
17+
{ 'path': 'std::task', 'name': 'local_waker_from_nonlocal' },
18+
{ 'path': 'alloc::task', 'name': 'local_waker_from_nonlocal' },
19+
],
20+
};

0 commit comments

Comments
 (0)