From 56dbb3e32d929ae67ca76d85a5351035b3eae592 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Oct 2017 13:21:18 +0100 Subject: [PATCH 1/5] Keep displaying good information when query is made --- src/librustdoc/html/static/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 04bf466a7804d..d6c89ef6ff00a 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -884,6 +884,7 @@ elems[0].onclick = function() { printTab(0); }; elems[1].onclick = function() { printTab(1); }; elems[2].onclick = function() { printTab(2); }; + printTab(currentTab); } function search(e) { From 6f21008aba0242cf622a98cca52fb4a3a6dfca5b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Oct 2017 13:29:07 +0100 Subject: [PATCH 2/5] Better check for returned value --- src/librustdoc/html/static/main.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index d6c89ef6ff00a..55ebfcc37078c 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -371,6 +371,11 @@ return false; } + function checkReturned(obj, val) { + return obj && obj.type && obj.type.output && + obj.type.output.name.toLowerCase() === val; + } + function typePassesFilter(filter, type) { // No filter if (filter < 0) return true; @@ -503,6 +508,15 @@ lev: lev_distance, }); } + } else if (checkReturned(searchIndex[j], val)) { + if (typePassesFilter(typeFilter, searchIndex[j].ty)) { + results.push({ + id: j, + index: 0, + // we want lev results to go lower than others + lev: lev_distance, + }); + } } if (results.length === max) { break; From f6a546e14d1e318eb6e38343eb98d20ac788437a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Oct 2017 13:39:11 +0100 Subject: [PATCH 3/5] Be more flexible when looking for something by using levenshtein method --- src/librustdoc/html/static/main.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 55ebfcc37078c..5bef81c9662ca 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -361,19 +361,30 @@ } 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) { @@ -489,8 +500,7 @@ }); } } 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, @@ -499,7 +509,8 @@ 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, @@ -508,7 +519,9 @@ 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, From e8db5adcce3034b7cde6c62f7bcf9ff52970105c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Oct 2017 13:42:34 +0100 Subject: [PATCH 4/5] fix function not appearing in first tab when appearing in another one. Thanks to @Seeker14491 for this one! --- src/librustdoc/html/static/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 5bef81c9662ca..0323c083b9b83 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -979,7 +979,8 @@ } } if (results['others'].length < maxResults && - ((query.search && obj.name.indexOf(query.search)) || added === false)) { + ((query.search && obj.name.indexOf(query.search) !== -1) || + added === false)) { results['others'].push(obj); } } From ee7e372bbf2d6ce184e50aaf2679873eb961dc49 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Oct 2017 18:22:13 +0100 Subject: [PATCH 5/5] Remove duplicated results in the search --- src/librustdoc/html/static/main.js | 135 +++++++++++++++++++---------- 1 file changed, 88 insertions(+), 47 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 0323c083b9b83..b1120e0af3894 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -349,7 +349,7 @@ var valLower = query.query.toLowerCase(), val = valLower, typeFilter = itemTypeFromName(query.type), - results = [], + results = {}, split = valLower.split("::"); // remove empty keywords @@ -360,6 +360,23 @@ } } + function min(a, b) { + if (a < b) { + return a; + } + return b; + } + + function nbElements(obj) { + var size = 0, key; + for (key in obj) { + if (obj.hasOwnProperty(key)) { + size += 1; + } + } + return size; + } + function findArg(obj, val) { var lev_distance = MAX_LEV_DISTANCE + 1; if (obj && obj.type && obj.type.inputs.length > 0) { @@ -368,9 +385,9 @@ // 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; + lev_distance = min(levenshtein(obj.type.inputs[i].name, val), lev_distance); + if (lev_distance === 0) { + return 0; } } } @@ -378,13 +395,17 @@ } function checkReturned(obj, val) { + var lev_distance = MAX_LEV_DISTANCE + 1; if (obj && obj.type && obj.type.output) { if (obj.type.output.name.toLowerCase() === val) { return 0; } - return levenshtein(obj.type.output.name.toLowerCase(), val); + lev_distance = min(levenshtein(obj.type.output.name, val)); + if (lev_distance === 0) { + return 0; + } } - return MAX_LEV_DISTANCE + 1; + return lev_distance; } function typePassesFilter(filter, type) { @@ -414,22 +435,27 @@ if ((val.charAt(0) === "\"" || val.charAt(0) === "'") && val.charAt(val.length - 1) === val.charAt(0)) { - val = val.substr(1, val.length - 2); + val = val.substr(1, val.length - 2).toLowerCase(); for (var i = 0; i < nSearchWords; ++i) { + var ty = searchIndex[i]; if (searchWords[i] === val) { // filter type: ... queries if (typePassesFilter(typeFilter, searchIndex[i].ty)) { - results.push({id: i, index: -1}); + results[ty.path + ty.name] = {id: i, index: -1}; } - } else if (findArg(searchIndex[i], val.toLowerCase()) || - (searchIndex[i].type && - searchIndex[i].type.output && - searchIndex[i].type.output.name === val.toLowerCase())) { + } else if (findArg(searchIndex[i], val) || + (ty.type && + ty.type.output && + ty.type.output.name === val)) { if (typePassesFilter(typeFilter, searchIndex[i].ty)) { - results.push({id: i, index: -1, dontValidate: true}); + results[ty.path + ty.name] = { + id: i, + index: -1, + dontValidate: true, + }; } } - if (results.length === max) { + if (nbElements(results) === max) { break; } } @@ -447,6 +473,7 @@ for (var i = 0; i < nSearchWords; ++i) { var type = searchIndex[i].type; + var ty = searchIndex[i]; if (!type) { continue; } @@ -460,7 +487,7 @@ var typeOutput = type.output ? type.output.name : ""; if (output === "*" || output == typeOutput) { if (input === "*") { - results.push({id: i, index: -1, dontValidate: true}); + results[ty.path + ty.name] = {id: i, index: -1, dontValidate: true}; } else { var allFound = true; for (var it = 0; allFound === true && it < inputs.length; it++) { @@ -471,7 +498,11 @@ allFound = found; } if (allFound === true) { - results.push({id: i, index: -1, dontValidate: true}); + results[ty.path + ty.name] = { + id: i, + index: -1, + dontValidate: true, + }; } } } @@ -487,57 +518,77 @@ for (var i = 0; i < split.length; ++i) { for (var j = 0; j < nSearchWords; ++j) { var lev_distance; + var ty = searchIndex[j]; + if (!ty) { + continue; + } if (searchWords[j].indexOf(split[i]) > -1 || searchWords[j].indexOf(val) > -1 || searchWords[j].replace(/_/g, "").indexOf(val) > -1) { // filter type: ... queries if (typePassesFilter(typeFilter, searchIndex[j].ty)) { - results.push({ + results[ty.path + ty.name] = { id: j, index: searchWords[j].replace(/_/g, "").indexOf(val), lev: 0, - }); + }; } } else if ( (lev_distance = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) { if (typePassesFilter(typeFilter, searchIndex[j].ty)) { - results.push({ - id: j, - index: 0, - // we want lev results to go lower than others - lev: lev_distance, - }); + if (results[ty.path + ty.name] === undefined || + results[ty.path + ty.name].lev > lev_distance) { + results[ty.path + ty.name] = { + id: j, + index: 0, + // we want lev results to go lower than others + lev: lev_distance, + }; + } } } else if ( (lev_distance = findArg(searchIndex[j], val)) <= MAX_LEV_DISTANCE) { if (typePassesFilter(typeFilter, searchIndex[j].ty)) { - results.push({ - id: j, - index: 0, - // we want lev results to go lower than others - lev: lev_distance, - }); + if (results[ty.path + ty.name] === undefined || + results[ty.path + ty.name].lev > lev_distance) { + results[ty.path + ty.name] = { + id: j, + index: 0, + // we want lev results to go lower than others + lev: lev_distance, + }; + } } } else if ( (lev_distance = checkReturned(searchIndex[j], val)) <= MAX_LEV_DISTANCE) { if (typePassesFilter(typeFilter, searchIndex[j].ty)) { - results.push({ - id: j, - index: 0, - // we want lev results to go lower than others - lev: lev_distance, - }); + if (results[ty.path + ty.name] === undefined || + results[ty.path + ty.name].lev > lev_distance) { + results[ty.path + ty.name] = { + id: j, + index: 0, + // we want lev results to go lower than others + lev: lev_distance, + }; + } } } - if (results.length === max) { + if (nbElements(results) === max) { break; } } } } + var ar = []; + for (var entry in results) { + if (results.hasOwnProperty(entry)) { + ar.push(results[entry]); + } + } + results = ar; var nresults = results.length; for (var i = 0; i < nresults; ++i) { results[i].word = searchWords[results[i].id]; @@ -613,16 +664,6 @@ return 0; }); - // remove duplicates, according to the data provided - for (var i = results.length - 1; i > 0; i -= 1) { - if (results[i].word === results[i - 1].word && - results[i].item.ty === results[i - 1].item.ty && - results[i].item.path === results[i - 1].item.path && - (results[i].item.parent || {}).name === (results[i - 1].item.parent || {}).name) - { - results[i].id = -1; - } - } for (var i = 0; i < results.length; ++i) { var result = results[i], name = result.item.name.toLowerCase(),