Skip to content
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

rustdoc-search: use set ops for ranking and filtering #118402

Merged
merged 4 commits into from
Dec 13, 2023
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
3 changes: 2 additions & 1 deletion src/librustdoc/html/static/js/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function initSearch(searchIndex){}
* pathWithoutLast: Array<string>,
* pathLast: string,
* generics: Array<QueryElement>,
* bindings: Map<(string|integer), Array<QueryElement>>,
* bindings: Map<integer, Array<QueryElement>>,
* }}
*/
let QueryElement;
Expand Down Expand Up @@ -42,6 +42,7 @@ let ParserState;
* totalElems: number,
* literalSearch: boolean,
* corrections: Array<{from: string, to: integer}>,
* typeFingerprint: Uint32Array,
* }}
*/
let ParsedQuery;
Expand Down
312 changes: 215 additions & 97 deletions src/librustdoc/html/static/js/search.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/rustdoc-js/assoc-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const EXPECTED = [
'query': 'iterator<something> -> u32',
'correction': null,
'others': [
{ 'path': 'assoc_type', 'name': 'my_fn' },
{ 'path': 'assoc_type::my', 'name': 'other_fn' },
{ 'path': 'assoc_type', 'name': 'my_fn' },
],
},
{
'query': 'iterator<something>',
'correction': null,
'in_args': [
{ 'path': 'assoc_type', 'name': 'my_fn' },
{ 'path': 'assoc_type::my', 'name': 'other_fn' },
{ 'path': 'assoc_type', 'name': 'my_fn' },
],
},
{
Expand All @@ -26,8 +26,8 @@ const EXPECTED = [
{ 'path': 'assoc_type', 'name': 'Something' },
],
'in_args': [
{ 'path': 'assoc_type', 'name': 'my_fn' },
{ 'path': 'assoc_type::my', 'name': 'other_fn' },
{ 'path': 'assoc_type', 'name': 'my_fn' },
],
},
// if I write an explicit binding, only it shows up
Expand Down
39 changes: 39 additions & 0 deletions tests/rustdoc-js/big-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// exact-check

const EXPECTED = [
{
'query': 'First',
'in_args': (function() {
// Generate the list of 200 items that should match.
const results = [];
function generate(lx, ly) {
for (const x of lx) {
for (const y of ly) {
results.push({
'path': `big_result::${y}`,
'name': x,
});
}
}
}
// Fewest parameters that still match go on top.
generate(
['u', 'v', 'w', 'x', 'y'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
);
generate(
['p', 'q', 'r', 's', 't'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
);
generate(
['k', 'l', 'm', 'n', 'o'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
);
generate(
['f', 'g', 'h', 'i', 'j'],
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
);
return results;
})(),
},
];
61 changes: 61 additions & 0 deletions tests/rustdoc-js/big-result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![feature(concat_idents)]
#![allow(nonstandard_style)]
/// Generate 250 items that all match the query, starting with the longest.
/// Those long items should be dropped from the result set, and the short ones
/// should be shown instead.
macro_rules! generate {
([$($x:ident),+], $y:tt, $z:tt) => {
$(
generate!(@ $x, $y, $z);
)+
};
(@ $x:ident , [$($y:ident),+], $z:tt) => {
pub struct $x;
$(
generate!(@@ $x, $y, $z);
)+
};
(@@ $x:ident , $y:ident, [$($z:ident: $zt:ident),+]) => {
impl $y {
pub fn $x($($z: $zt,)+) {}
}
}
}

pub struct First;
pub struct Second;
pub struct Third;
pub struct Fourth;
pub struct Fifth;

generate!(
[a, b, c, d, e],
[a, b, c, d, e, f, g, h, i, j],
[a: First, b: Second, c: Third, d: Fourth, e: Fifth]
);

generate!(
[f, g, h, i, j],
[a, b, c, d, e, f, g, h, i, j],
[a: First, b: Second, c: Third, d: Fourth]
);

generate!(
[k, l, m, n, o],
[a, b, c, d, e, f, g, h, i, j],
[a: First, b: Second, c: Third]
);

generate!(
// reverse it, just to make sure they're alphabetized
// in the result set when all else is equal
[t, s, r, q, p],
[a, b, c, d, e, f, g, h, i, j],
[a: First, b: Second]
);

generate!(
[u, v, w, x, y],
[a, b, c, d, e, f, g, h, i, j],
[a: First]
);
4 changes: 2 additions & 2 deletions tests/rustdoc-js/full-path-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ const EXPECTED = [
{
'query': 'sac -> usize',
'others': [
{ 'path': 'full_path_function::b::Sac', 'name': 'bar' },
{ 'path': 'full_path_function::b::Sac', 'name': 'len' },
{ 'path': 'full_path_function::sac::Sac', 'name': 'len' },
{ 'path': 'full_path_function::b::Sac', 'name': 'bar' },
],
},
{
'query': 'b::sac -> usize',
'others': [
{ 'path': 'full_path_function::b::Sac', 'name': 'bar' },
{ 'path': 'full_path_function::b::Sac', 'name': 'len' },
{ 'path': 'full_path_function::b::Sac', 'name': 'bar' },
],
},
{
Expand Down
1 change: 1 addition & 0 deletions tests/rustdoc-js/generics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// exact-check
// ignore-order

const EXPECTED = [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-js/impl-trait.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const EXPECTED = [
{ 'path': 'impl_trait', 'name': 'Aaaaaaa' },
],
'in_args': [
{ 'path': 'impl_trait::Ccccccc', 'name': 'eeeeeee' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'eeeeeee' },
],
'returned': [
{ 'path': 'impl_trait', 'name': 'bbbbbbb' },
Expand Down
19 changes: 10 additions & 9 deletions tests/rustdoc-js/type-parameters.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// exact-check
// ignore-order

const EXPECTED = [
{
query: '-> trait:Some',
others: [
{ path: 'foo', name: 'alef' },
{ path: 'foo', name: 'alpha' },
{ path: 'foo', name: 'alef' },
],
},
{
query: '-> generic:T',
others: [
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'alef' },
{ path: 'foo', name: 'beta' },
],
},
{
Expand Down Expand Up @@ -44,38 +43,40 @@ const EXPECTED = [
{
query: 'Other, Other',
others: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
},
{
query: 'generic:T',
in_args: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
},
{
query: 'generic:Other',
in_args: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
},
{
query: 'trait:Other',
in_args: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
},
{
query: 'Other',
in_args: [
// because function is called "other", it's sorted first
// even though it has higher type distance
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
],
Expand Down
Loading