From 9af8712270ee7cc4aefe957249cfbb1f5c4c77ec Mon Sep 17 00:00:00 2001 From: binarycat Date: Sun, 3 Nov 2024 13:17:39 -0600 Subject: [PATCH] rustdoc search: allow queries to end in an empty path segment fixes https://github.com/rust-lang/rust/issues/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 --- src/librustdoc/html/static/js/search.js | 16 ++++++++++------ tests/rustdoc-js-std/parser-errors.js | 9 --------- tests/rustdoc-js-std/path-end-empty.js | 6 ++++++ tests/rustdoc-js/generics.js | 7 +++++++ 4 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 tests/rustdoc-js-std/path-end-empty.js diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index eed64d024c046..65c3287661666 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -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]]; } @@ -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); diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index 5ce35bf511d75..c01b7cc1f09c7 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -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: [], diff --git a/tests/rustdoc-js-std/path-end-empty.js b/tests/rustdoc-js-std/path-end-empty.js new file mode 100644 index 0000000000000..6e853c61b4d95 --- /dev/null +++ b/tests/rustdoc-js-std/path-end-empty.js @@ -0,0 +1,6 @@ +const EXPECTED = { + 'query': 'Option::', + 'others': [ + { 'path': 'std::option::Option', 'name': 'get_or_insert_default' }, + ], +} diff --git a/tests/rustdoc-js/generics.js b/tests/rustdoc-js/generics.js index b3ca0af3056a5..89505e6d44093 100644 --- a/tests/rustdoc-js/generics.js +++ b/tests/rustdoc-js/generics.js @@ -75,4 +75,11 @@ const EXPECTED = [ { 'path': 'generics', 'name': 'super_soup' }, ], }, + { + 'query': 'generics::', + 'others': [ + { 'path': 'generics', 'name': 'extracreditlabhomework' }, + { 'path': 'generics', 'name': 'redherringmatchforextracredit' }, + ], + } ];