From 360d6e4b7ddcfbb203dced9883532bc4f39137e0 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 9 May 2022 11:46:24 -0700 Subject: [PATCH 1/2] rustdoc: search result ranking fix --- src/librustdoc/html/static/js/search.js | 2 +- src/test/rustdoc-js/path-ordering.js | 14 ++++++++++++++ src/test/rustdoc-js/path-ordering.rs | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc-js/path-ordering.js create mode 100644 src/test/rustdoc-js/path-ordering.rs diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 1e3894c1fcd27..5a2ca145ac798 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1323,7 +1323,6 @@ window.initSearch = rawSearchIndex => { } } lev = levenshtein(searchWord, elem.pathLast); - lev += lev_add; if (lev > 0 && elem.pathLast.length > 2 && searchWord.indexOf(elem.pathLast) > -1) { if (elem.pathLast.length < 6) { @@ -1332,6 +1331,7 @@ window.initSearch = rawSearchIndex => { lev = 0; } } + lev += lev_add; if (lev > MAX_LEV_DISTANCE) { return; } else if (index !== -1 && elem.fullPath.length < 2) { diff --git a/src/test/rustdoc-js/path-ordering.js b/src/test/rustdoc-js/path-ordering.js new file mode 100644 index 0000000000000..4aee569b0f481 --- /dev/null +++ b/src/test/rustdoc-js/path-ordering.js @@ -0,0 +1,14 @@ +// exact-check + +const QUERY = 'b::ccccccc'; + +const EXPECTED = { + 'others': [ + // `ccccccc` is an exact match for all three of these. + // However `b` is a closer match for `bb` than for any + // of the others, so it ought to go first. + { 'path': 'path_ordering::bb', 'name': 'Ccccccc' }, + { 'path': 'path_ordering::aa', 'name': 'Ccccccc' }, + { 'path': 'path_ordering::dd', 'name': 'Ccccccc' }, + ], +}; diff --git a/src/test/rustdoc-js/path-ordering.rs b/src/test/rustdoc-js/path-ordering.rs new file mode 100644 index 0000000000000..7843cf7f9dc48 --- /dev/null +++ b/src/test/rustdoc-js/path-ordering.rs @@ -0,0 +1,9 @@ +pub mod dd { + pub struct Ccccccc; +} +pub mod aa { + pub struct Ccccccc; +} +pub mod bb { + pub struct Ccccccc; +} From a9a90d450d11a06dff8bc49a89352e733e904fcf Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 9 May 2022 12:17:47 -0700 Subject: [PATCH 2/2] Add test case for `hashset::insert` ranking --- src/test/rustdoc-js-std/path-ordering.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/test/rustdoc-js-std/path-ordering.js diff --git a/src/test/rustdoc-js-std/path-ordering.js b/src/test/rustdoc-js-std/path-ordering.js new file mode 100644 index 0000000000000..7dcdd40231248 --- /dev/null +++ b/src/test/rustdoc-js-std/path-ordering.js @@ -0,0 +1,12 @@ +const QUERY = 'hashset::insert'; + +const EXPECTED = { + 'others': [ + // ensure hashset::insert comes first + { 'path': 'std::collections::hash_set::HashSet', 'name': 'insert' }, + { 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert' }, + { 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert_with' }, + { 'path': 'std::collections::hash_set::HashSet', 'name': 'get_or_insert_owned' }, + { 'path': 'std::collections::hash_map::HashMap', 'name': 'insert' }, + ], +};