]> git.lizzy.rs Git - rust.git/commitdiff
Be more flexible when looking for something by using levenshtein method
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 29 Oct 2017 12:39:11 +0000 (13:39 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 1 Nov 2017 12:41:43 +0000 (13:41 +0100)
src/librustdoc/html/static/main.js

index 55ebfcc37078c488c11a2cd18a1975c475f0ff36..5bef81c9662cae8e4fa9421b0e7703d1388e0339 100644 (file)
             }
 
             function findArg(obj, val) {
+                var lev_distance = MAX_LEV_DISTANCE + 1;
                 if (obj && obj.type && obj.type.inputs.length > 0) {
                     for (var i = 0; i < obj.type.inputs.length; i++) {
                         if (obj.type.inputs[i].name === val) {
-                            return true;
+                            // No need to check anything else: we found it. Let's just move on.
+                            return 0;
+                        }
+                        var tmp = levenshtein(obj.type.inputs[i].name, val);
+                        if (tmp < lev_distance) {
+                            lev_distance = tmp;
                         }
                     }
                 }
-                return false;
+                return lev_distance;
             }
 
             function checkReturned(obj, val) {
-                return obj && obj.type && obj.type.output &&
-                       obj.type.output.name.toLowerCase() === val;
+                if (obj && obj.type && obj.type.output) {
+                    if (obj.type.output.name.toLowerCase() === val) {
+                        return 0;
+                    }
+                    return levenshtein(obj.type.output.name.toLowerCase(), val);
+                }
+                return MAX_LEV_DISTANCE + 1;
             }
 
             function typePassesFilter(filter, type) {
                                 });
                             }
                         } else if (
-                            (lev_distance = levenshtein(searchWords[j], val)) <=
-                                MAX_LEV_DISTANCE) {
+                            (lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
                                 results.push({
                                     id: j,
                                     lev: lev_distance,
                                 });
                             }
-                        } else if (findArg(searchIndex[j], val)) {
+                        } else if (
+                            (lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) {
                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
                                 results.push({
                                     id: j,
                                     lev: lev_distance,
                                 });
                             }
-                        } else if (checkReturned(searchIndex[j], val)) {
+                        } else if (
+                            (lev_distance = checkReturned(searchIndex[j], val)) <=
+                            MAX_LEV_DISTANCE) {
                             if (typePassesFilter(typeFilter, searchIndex[j].ty)) {
                                 results.push({
                                     id: j,