Skip to content

Commit

Permalink
rustdoc search: allow queries to end in an empty path segment
Browse files Browse the repository at this point in the history
fixes rust-lang#129707

this can be used to show all items in a module,
or all associated items for a type.
currently sufferes slightly due to case insensitivity,
so `Option::` will also show items in the `option::` module.

it disables the checking of the last path element,
otherwise only items with short names will be shown
  • Loading branch information
lolbinarycat committed Nov 4, 2024
1 parent 7028d93 commit 9af8712
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,6 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
const quadcolon = /::\s*::/.exec(path);
if (path.startsWith("::")) {
throw ["Paths cannot start with ", "::"];
} else if (path.endsWith("::")) {
throw ["Paths cannot end with ", "::"];
} else if (quadcolon !== null) {
throw ["Unexpected ", quadcolon[0]];
}
Expand Down Expand Up @@ -3029,10 +3027,16 @@ class DocSearch {
return;
}

const dist = editDistance(row.normalizedName, elem.normalizedPathLast, maxEditDistance);

if (index === -1 && dist > maxEditDistance) {
return;
let dist = 0;
if (elem.pathLast === "") {
if (path_dist > 0) {
return;
}
} else {
dist = editDistance(row.normalizedName, elem.normalizedPathLast, maxEditDistance);
if (index === -1 && dist > maxEditDistance) {
return;
}
}

addIntoResults(results_others, fullId, pos, index, dist, path_dist, maxEditDistance);
Expand Down
9 changes: 0 additions & 9 deletions tests/rustdoc-js-std/parser-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ const PARSED = [
userQuery: "a:: ::b",
error: "Unexpected `:: ::`",
},
{
query: "a::b::",
elems: [],
foundElems: 0,
original: "a::b::",
returned: [],
userQuery: "a::b::",
error: "Paths cannot end with `::`",
},
{
query: ":a",
elems: [],
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc-js-std/path-end-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const EXPECTED = {
'query': 'Option::',
'others': [
{ 'path': 'std::option::Option', 'name': 'get_or_insert_default' },
],
}
7 changes: 7 additions & 0 deletions tests/rustdoc-js/generics.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,11 @@ const EXPECTED = [
{ 'path': 'generics', 'name': 'super_soup' },
],
},
{
'query': 'generics::',
'others': [
{ 'path': 'generics', 'name': 'extracreditlabhomework' },
{ 'path': 'generics', 'name': 'redherringmatchforextracredit' },
],
}
];

0 comments on commit 9af8712

Please sign in to comment.