]> git.lizzy.rs Git - rust.git/commitdiff
Improve searching in rustdoc and add tests
authorShiqing <shiqing-thu18@yandex.com>
Mon, 2 Sep 2019 13:03:35 +0000 (21:03 +0800)
committerShiqing <shiqing-thu18@yandex.com>
Wed, 4 Sep 2019 13:27:13 +0000 (21:27 +0800)
src/librustdoc/html/static/main.js
src/test/rustdoc-js-std/vec-new.js
src/test/rustdoc-js/exact-match.js [new file with mode: 0644]
src/test/rustdoc-js/exact-match.rs [new file with mode: 0644]
src/test/rustdoc-js/module-substring.js [new file with mode: 0644]
src/test/rustdoc-js/module-substring.rs [new file with mode: 0644]
src/test/rustdoc-js/search-short-types.js
src/test/rustdoc-js/search-short-types.rs

index 637c6ef8e8e0bb893daf117e7d5621687c7e1195..5cb2456fefdb27609533ec46522743b455db1e9b 100644 (file)
@@ -547,6 +547,11 @@ if (!DOMTokenList.prototype.remove) {
                 results.sort(function(aaa, bbb) {
                     var a, b;
 
+                    // sort by exact match with regard to the last word (mismatch goes later)
+                    a = (aaa.word !== val);
+                    b = (bbb.word !== val);
+                    if (a !== b) { return a - b; }
+
                     // Sort by non levenshtein results and then levenshtein results by the distance
                     // (less changes required to match means higher rankings)
                     a = (aaa.lev);
@@ -558,11 +563,6 @@ if (!DOMTokenList.prototype.remove) {
                     b = (bbb.item.crate !== window.currentCrate);
                     if (a !== b) { return a - b; }
 
-                    // sort by exact match (mismatch goes later)
-                    a = (aaa.word !== valLower);
-                    b = (bbb.word !== valLower);
-                    if (a !== b) { return a - b; }
-
                     // sort by item name length (longer goes later)
                     a = aaa.word.length;
                     b = bbb.word.length;
@@ -1028,7 +1028,7 @@ if (!DOMTokenList.prototype.remove) {
                         if (lev > MAX_LEV_DISTANCE) {
                             continue;
                         } else if (lev > 0) {
-                            lev_add = 1;
+                            lev_add = lev / 10;
                         }
                     }
 
@@ -1099,10 +1099,6 @@ if (!DOMTokenList.prototype.remove) {
                     if (index !== -1 || lev <= MAX_LEV_DISTANCE) {
                         if (index !== -1 && paths.length < 2) {
                             lev = 0;
-                        } else if (searchWords[j] === val) {
-                            // Small trick to fix when you're looking for a one letter type
-                            // and there are other short named types.
-                            lev = -1;
                         }
                         if (results[fullId] === undefined) {
                             results[fullId] = {
index e4daa5065d2330cf302eb1c7ae2c0cf37b83a1de..e1a3256876bde15af94274c65d1ab29a71c6ce62 100644 (file)
@@ -4,5 +4,6 @@ const EXPECTED = {
     'others': [
         { 'path': 'std::vec::Vec', 'name': 'new' },
         { 'path': 'std::vec::Vec', 'name': 'ne' },
+        { 'path': 'std::rc::Rc', 'name': 'ne' },
     ],
 };
diff --git a/src/test/rustdoc-js/exact-match.js b/src/test/rustdoc-js/exact-match.js
new file mode 100644 (file)
index 0000000..b0a411b
--- /dev/null
@@ -0,0 +1,9 @@
+const QUERY = 'si::pc';
+
+const EXPECTED = {
+    'others': [
+        { 'path': 'exact_match::Si', 'name': 'pc' },
+        { 'path': 'exact_match::Psi', 'name': 'pc' },
+        { 'path': 'exact_match::Si', 'name': 'pa' },
+    ],
+};
diff --git a/src/test/rustdoc-js/exact-match.rs b/src/test/rustdoc-js/exact-match.rs
new file mode 100644 (file)
index 0000000..2eacc0a
--- /dev/null
@@ -0,0 +1,68 @@
+macro_rules! imp {
+    ($name:ident) => {
+        pub struct $name {
+            pub op: usize,
+        }
+        impl $name {
+            pub fn op() {}
+            pub fn cmp() {}
+            pub fn map() {}
+            pub fn pop() {}
+            pub fn ptr() {}
+            pub fn rpo() {}
+            pub fn drop() {}
+            pub fn copy() {}
+            pub fn zip() {}
+            pub fn sup() {}
+            pub fn pa() {}
+            pub fn pb() {}
+            pub fn pc() {}
+            pub fn pd() {}
+            pub fn pe() {}
+            pub fn pf() {}
+            pub fn pg() {}
+            pub fn ph() {}
+            pub fn pi() {}
+            pub fn pj() {}
+            pub fn pk() {}
+            pub fn pl() {}
+            pub fn pm() {}
+            pub fn pn() {}
+            pub fn po() {}
+        }
+    };
+    ($name:ident, $($names:ident),*) => {
+        imp!($name);
+        imp!($($names),*);
+    };
+}
+macro_rules! en {
+    ($name:ident) => {
+        pub enum $name {
+            Ptr,
+            Rp,
+            Rpo,
+            Pt,
+            Drop,
+            Dr,
+            Dro,
+            Sup,
+            Op,
+            Cmp,
+            Map,
+            Mp,
+        }
+    };
+    ($name:ident, $($names:ident),*) => {
+        en!($name);
+        en!($($names),*);
+    };
+}
+
+imp!(Ot, Foo, Cmp, Map, Loc, Lac, Toc, Si, Sig, Sip, Psy, Psi, Py, Pi, Pa, Pb, Pc, Pd);
+imp!(Pe, Pf, Pg, Ph, Pj, Pk, Pl, Pm, Pn, Po, Pq, Pr, Ps, Pt, Pu, Pv, Pw, Px, Pz, Ap, Bp, Cp);
+imp!(Dp, Ep, Fp, Gp, Hp, Ip, Jp, Kp, Lp, Mp, Np, Op, Pp, Qp, Rp, Sp, Tp, Up, Vp, Wp, Xp, Yp, Zp);
+
+en!(Place, Plac, Plae, Plce, Pace, Scalar, Scalr, Scaar, Sclar, Salar);
+
+pub struct P;
diff --git a/src/test/rustdoc-js/module-substring.js b/src/test/rustdoc-js/module-substring.js
new file mode 100644 (file)
index 0000000..a446c39
--- /dev/null
@@ -0,0 +1,9 @@
+const QUERY = 'ig::pc';
+
+const EXPECTED = {
+    'others': [
+        { 'path': 'module_substring::Sig', 'name': 'pc' },
+        { 'path': 'module_substring::Si', 'name': 'pc' },
+        { 'path': 'module_substring::Si', 'name': 'pa' },
+    ],
+};
diff --git a/src/test/rustdoc-js/module-substring.rs b/src/test/rustdoc-js/module-substring.rs
new file mode 100644 (file)
index 0000000..2eacc0a
--- /dev/null
@@ -0,0 +1,68 @@
+macro_rules! imp {
+    ($name:ident) => {
+        pub struct $name {
+            pub op: usize,
+        }
+        impl $name {
+            pub fn op() {}
+            pub fn cmp() {}
+            pub fn map() {}
+            pub fn pop() {}
+            pub fn ptr() {}
+            pub fn rpo() {}
+            pub fn drop() {}
+            pub fn copy() {}
+            pub fn zip() {}
+            pub fn sup() {}
+            pub fn pa() {}
+            pub fn pb() {}
+            pub fn pc() {}
+            pub fn pd() {}
+            pub fn pe() {}
+            pub fn pf() {}
+            pub fn pg() {}
+            pub fn ph() {}
+            pub fn pi() {}
+            pub fn pj() {}
+            pub fn pk() {}
+            pub fn pl() {}
+            pub fn pm() {}
+            pub fn pn() {}
+            pub fn po() {}
+        }
+    };
+    ($name:ident, $($names:ident),*) => {
+        imp!($name);
+        imp!($($names),*);
+    };
+}
+macro_rules! en {
+    ($name:ident) => {
+        pub enum $name {
+            Ptr,
+            Rp,
+            Rpo,
+            Pt,
+            Drop,
+            Dr,
+            Dro,
+            Sup,
+            Op,
+            Cmp,
+            Map,
+            Mp,
+        }
+    };
+    ($name:ident, $($names:ident),*) => {
+        en!($name);
+        en!($($names),*);
+    };
+}
+
+imp!(Ot, Foo, Cmp, Map, Loc, Lac, Toc, Si, Sig, Sip, Psy, Psi, Py, Pi, Pa, Pb, Pc, Pd);
+imp!(Pe, Pf, Pg, Ph, Pj, Pk, Pl, Pm, Pn, Po, Pq, Pr, Ps, Pt, Pu, Pv, Pw, Px, Pz, Ap, Bp, Cp);
+imp!(Dp, Ep, Fp, Gp, Hp, Ip, Jp, Kp, Lp, Mp, Np, Op, Pp, Qp, Rp, Sp, Tp, Up, Vp, Wp, Xp, Yp, Zp);
+
+en!(Place, Plac, Plae, Plce, Pace, Scalar, Scalr, Scaar, Sclar, Salar);
+
+pub struct P;
index 0ebf4860cfa583ad08a64c9b6c83148726bc71d7..d14672af71fd6b714d3107b68c04ee16bd130d71 100644 (file)
@@ -3,6 +3,8 @@ const QUERY = 'P';
 const EXPECTED = {
     'others': [
         { 'path': 'search_short_types', 'name': 'P' },
+        { 'path': 'search_short_types::VeryLongTypeName', 'name': 'p' },
         { 'path': 'search_short_types', 'name': 'Ap' },
+        { 'path': 'search_short_types::VeryLongTypeName', 'name': 'ap' },
     ],
 };
index 2eacc0a358284f07a4dd63f0ddb9be58f6a8bbbe..a4083f9a76401897190efbe3c0086954d5b9622d 100644 (file)
@@ -66,3 +66,9 @@ pub enum $name {
 en!(Place, Plac, Plae, Plce, Pace, Scalar, Scalr, Scaar, Sclar, Salar);
 
 pub struct P;
+
+pub struct VeryLongTypeName;
+impl VeryLongTypeName {
+    pub fn p() {}
+    pub fn ap() {}
+}