Skip to content

Search a substring instead of start of string in rustdoc search #53577

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 1 commit into from
Aug 25, 2018
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
20 changes: 10 additions & 10 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,8 @@
return literalSearch === true ? false : lev_distance;
}

function checkPath(startsWith, lastElem, ty) {
if (startsWith.length === 0) {
function checkPath(contains, lastElem, ty) {
if (contains.length === 0) {
return 0;
}
var ret_lev = MAX_LEV_DISTANCE + 1;
Expand All @@ -752,25 +752,25 @@
path.push(ty.parent.name.toLowerCase());
}

if (startsWith.length > path.length) {
if (contains.length > path.length) {
return MAX_LEV_DISTANCE + 1;
}
for (var i = 0; i < path.length; ++i) {
if (i + startsWith.length > path.length) {
if (i + contains.length > path.length) {
break;
}
var lev_total = 0;
var aborted = false;
for (var x = 0; x < startsWith.length; ++x) {
var lev = levenshtein(path[i + x], startsWith[x]);
for (var x = 0; x < contains.length; ++x) {
var lev = levenshtein(path[i + x], contains[x]);
if (lev > MAX_LEV_DISTANCE) {
aborted = true;
break;
}
lev_total += lev;
}
if (aborted === false) {
ret_lev = Math.min(ret_lev, Math.round(lev_total / startsWith.length));
ret_lev = Math.min(ret_lev, Math.round(lev_total / contains.length));
}
}
return ret_lev;
Expand Down Expand Up @@ -934,7 +934,7 @@
}
}
val = paths[paths.length - 1];
var startsWith = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);
var contains = paths.slice(0, paths.length > 1 ? paths.length - 1 : 1);

for (j = 0; j < nSearchWords; ++j) {
var lev_distance;
Expand All @@ -944,7 +944,7 @@
}
var lev_add = 0;
if (paths.length > 1) {
var lev = checkPath(startsWith, paths[paths.length - 1], ty);
var lev = checkPath(contains, paths[paths.length - 1], ty);
if (lev > MAX_LEV_DISTANCE) {
continue;
} else if (lev > 0) {
Expand Down Expand Up @@ -987,7 +987,7 @@
}

lev += lev_add;
if (lev > 0 && val.length > 3 && searchWords[j].startsWith(val)) {
if (lev > 0 && val.length > 3 && searchWords[j].indexOf(val) > -1) {
if (val.length < 6) {
lev -= 1;
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/test/rustdoc-js/substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// exact-check

const QUERY = 'waker_from';

const EXPECTED = {
'others': [
{ 'path': 'std::task', 'name': 'local_waker_from_nonlocal' },
{ 'path': 'alloc::task', 'name': 'local_waker_from_nonlocal' },
],
};