From ff0c10c37511ce86379c0e6223c224cc2f0b9ba0 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 8 Jun 2024 15:18:23 -0700 Subject: [PATCH] rustdoc-search: use lowercase, non-normalized name for type search The type name ID map has underscores in its names, so the query element should have them, too. --- src/librustdoc/html/static/js/search.js | 12 +++-- tests/rustdoc-js-std/pidt.js | 64 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tests/rustdoc-js-std/pidt.js diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 8ac4b53673f4c..a0ab262bf0b02 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2399,15 +2399,19 @@ function initSearch(rawSearchIndex) { * @param {boolean} isAssocType */ function convertNameToId(elem, isAssocType) { - if (typeNameIdMap.has(elem.normalizedPathLast) && - (isAssocType || !typeNameIdMap.get(elem.normalizedPathLast).assocOnly)) { - elem.id = typeNameIdMap.get(elem.normalizedPathLast).id; + const loweredName = elem.pathLast.toLowerCase(); + if (typeNameIdMap.has(loweredName) && + (isAssocType || !typeNameIdMap.get(loweredName).assocOnly)) { + elem.id = typeNameIdMap.get(loweredName).id; } else if (!parsedQuery.literalSearch) { let match = null; let matchDist = maxEditDistance + 1; let matchName = ""; for (const [name, {id, assocOnly}] of typeNameIdMap) { - const dist = editDistance(name, elem.normalizedPathLast, maxEditDistance); + const dist = Math.min( + editDistance(name, loweredName, maxEditDistance), + editDistance(name, elem.normalizedPathLast, maxEditDistance), + ); if (dist <= matchDist && dist <= maxEditDistance && (isAssocType || !assocOnly)) { if (dist === matchDist && matchName > name) { diff --git a/tests/rustdoc-js-std/pidt.js b/tests/rustdoc-js-std/pidt.js new file mode 100644 index 0000000000000..c4f3d00fc5d4e --- /dev/null +++ b/tests/rustdoc-js-std/pidt.js @@ -0,0 +1,64 @@ +const FILTER_CRATE = 'std'; + +const EXPECTED = [ + { + 'query': 'pid_t', + 'correction': null, + 'proposeCorrectionFrom': null, + 'proposeCorrectionTo': null, + 'others': [ + { 'path': 'std::os::unix::raw', 'name': 'pid_t' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'set_pid' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'get_pid' }, + ], + }, + { + 'query': 'pidt', + 'correction': 'pid_t', + 'proposeCorrectionFrom': null, + 'proposeCorrectionTo': null, + 'others': [ + { 'path': 'std::os::unix::raw', 'name': 'pid_t' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'set_pid' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'get_pid' }, + ], + }, + { + 'query': 'unix::pid_t', + 'correction': null, + 'proposeCorrectionFrom': null, + 'proposeCorrectionTo': null, + 'others': [ + { 'path': 'std::os::unix::raw', 'name': 'pid_t' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'set_pid' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'get_pid' }, + ], + }, + { + 'query': 'unix::pidt', + 'correction': 'pid_t', + 'proposeCorrectionFrom': null, + 'proposeCorrectionTo': null, + 'others': [ + { 'path': 'std::os::unix::raw', 'name': 'pid_t' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'set_pid' }, + ], + 'returned': [ + { 'path': 'std::os::unix::net::SocketCred', 'name': 'get_pid' }, + ], + }, +];