2021-05-14 13:56:15 +02:00
|
|
|
|
/* global addClass, getNakedUrl, getSettingValue, hasOwnPropertyRustdoc, initSearch, onEach */
|
2022-01-10 14:57:43 +01:00
|
|
|
|
/* global onEachLazy, removeClass, searchState, hasClass */
|
2021-05-14 13:56:15 +02:00
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
(function() {
|
|
|
|
|
// This mapping table should match the discriminants of
|
2022-03-29 19:30:54 +02:00
|
|
|
|
// `rustdoc::formats::item_type::ItemType` type in Rust.
|
2021-12-08 17:31:56 +01:00
|
|
|
|
var itemTypes = [
|
|
|
|
|
"mod",
|
|
|
|
|
"externcrate",
|
|
|
|
|
"import",
|
|
|
|
|
"struct",
|
|
|
|
|
"enum",
|
|
|
|
|
"fn",
|
|
|
|
|
"type",
|
|
|
|
|
"static",
|
|
|
|
|
"trait",
|
|
|
|
|
"impl",
|
|
|
|
|
"tymethod",
|
|
|
|
|
"method",
|
|
|
|
|
"structfield",
|
|
|
|
|
"variant",
|
|
|
|
|
"macro",
|
|
|
|
|
"primitive",
|
|
|
|
|
"associatedtype",
|
|
|
|
|
"constant",
|
|
|
|
|
"associatedconstant",
|
|
|
|
|
"union",
|
|
|
|
|
"foreigntype",
|
|
|
|
|
"keyword",
|
|
|
|
|
"existential",
|
|
|
|
|
"attr",
|
|
|
|
|
"derive",
|
|
|
|
|
"traitalias",
|
|
|
|
|
];
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// used for special search precedence
|
|
|
|
|
var TY_PRIMITIVE = itemTypes.indexOf("primitive");
|
|
|
|
|
var TY_KEYWORD = itemTypes.indexOf("keyword");
|
|
|
|
|
|
|
|
|
|
// In the search display, allows to switch between tabs.
|
|
|
|
|
function printTab(nb) {
|
|
|
|
|
if (nb === 0 || nb === 1 || nb === 2) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.currentTab = nb;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
var nb_copy = nb;
|
|
|
|
|
onEachLazy(document.getElementById("titles").childNodes, function(elem) {
|
|
|
|
|
if (nb_copy === 0) {
|
|
|
|
|
addClass(elem, "selected");
|
|
|
|
|
} else {
|
|
|
|
|
removeClass(elem, "selected");
|
|
|
|
|
}
|
|
|
|
|
nb_copy -= 1;
|
|
|
|
|
});
|
|
|
|
|
onEachLazy(document.getElementById("results").childNodes, function(elem) {
|
|
|
|
|
if (nb === 0) {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
addClass(elem, "active");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
removeClass(elem, "active");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
nb -= 1;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function to compute the Levenshtein distance between two strings
|
|
|
|
|
* Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
|
|
|
|
|
* Full License can be found at http://creativecommons.org/licenses/by-sa/3.0/legalcode
|
|
|
|
|
* This code is an unmodified version of the code written by Marco de Wit
|
2021-06-23 16:26:46 -04:00
|
|
|
|
* and was found at https://stackoverflow.com/a/18514751/745719
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
|
|
|
|
var levenshtein_row2 = [];
|
|
|
|
|
function levenshtein(s1, s2) {
|
|
|
|
|
if (s1 === s2) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
var s1_len = s1.length, s2_len = s2.length;
|
|
|
|
|
if (s1_len && s2_len) {
|
|
|
|
|
var i1 = 0, i2 = 0, a, b, c, c2, row = levenshtein_row2;
|
|
|
|
|
while (i1 < s1_len) {
|
|
|
|
|
row[i1] = ++i1;
|
|
|
|
|
}
|
|
|
|
|
while (i2 < s2_len) {
|
|
|
|
|
c2 = s2.charCodeAt(i2);
|
|
|
|
|
a = i2;
|
|
|
|
|
++i2;
|
|
|
|
|
b = i2;
|
|
|
|
|
for (i1 = 0; i1 < s1_len; ++i1) {
|
|
|
|
|
c = a + (s1.charCodeAt(i1) !== c2 ? 1 : 0);
|
|
|
|
|
a = row[i1];
|
|
|
|
|
b = b < a ? (b < c ? b + 1 : c) : (a < c ? a + 1 : c);
|
|
|
|
|
row[i1] = b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
return s1_len + s2_len;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 14:59:54 -07:00
|
|
|
|
window.initSearch = function(rawSearchIndex) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var MAX_LEV_DISTANCE = 3;
|
|
|
|
|
var MAX_RESULTS = 200;
|
2021-06-26 12:00:26 -07:00
|
|
|
|
var GENERICS_DATA = 2;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var NAME = 0;
|
|
|
|
|
var INPUTS_DATA = 0;
|
|
|
|
|
var OUTPUT_DATA = 1;
|
|
|
|
|
var NO_TYPE_FILTER = -1;
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* @type {Array<Row>}
|
|
|
|
|
*/
|
|
|
|
|
var searchIndex;
|
|
|
|
|
/**
|
|
|
|
|
* @type {Array<string>}
|
|
|
|
|
*/
|
|
|
|
|
var searchWords;
|
|
|
|
|
var currentResults;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var ALIASES = {};
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var params = searchState.getQueryStringParams();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// Populate search bar with query string search term when provided,
|
|
|
|
|
// but only if the input bar is empty. This avoid the obnoxious issue
|
|
|
|
|
// where you start trying to do a search, and the index loads, and
|
|
|
|
|
// suddenly your search is gone!
|
2021-04-12 23:50:18 -07:00
|
|
|
|
if (searchState.input.value === "") {
|
|
|
|
|
searchState.input.value = params.search || "";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 14:57:43 +01:00
|
|
|
|
/**
|
|
|
|
|
* Build an URL with search parameters.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} search - The current search being performed.
|
|
|
|
|
* @param {string|null} filterCrates - The current filtering crate (if any).
|
|
|
|
|
* @return {string}
|
|
|
|
|
*/
|
|
|
|
|
function buildUrl(search, filterCrates) {
|
|
|
|
|
var extra = "?search=" + encodeURIComponent(search);
|
|
|
|
|
|
|
|
|
|
if (filterCrates !== null) {
|
|
|
|
|
extra += "&filter-crate=" + encodeURIComponent(filterCrates);
|
|
|
|
|
}
|
|
|
|
|
return getNakedUrl() + extra + window.location.hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the filtering crate or `null` if there is none.
|
|
|
|
|
*
|
|
|
|
|
* @return {string|null}
|
|
|
|
|
*/
|
|
|
|
|
function getFilterCrates() {
|
|
|
|
|
var elem = document.getElementById("crate-search");
|
|
|
|
|
|
|
|
|
|
if (elem &&
|
|
|
|
|
elem.value !== "All crates" &&
|
|
|
|
|
hasOwnPropertyRustdoc(rawSearchIndex, elem.value))
|
|
|
|
|
{
|
|
|
|
|
return elem.value;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
/**
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* Executes the query and returns a list of results for each results tab.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
|
|
|
|
* @param {string} val - The user query
|
|
|
|
|
* @return {ParsedQuery} - The parsed query
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
2021-12-20 15:42:08 +01:00
|
|
|
|
function parseQuery(val) {
|
|
|
|
|
function isWhitespace(c) {
|
|
|
|
|
return " \t\n\r".indexOf(c) !== -1;
|
|
|
|
|
}
|
|
|
|
|
function isSpecialStartCharacter(c) {
|
|
|
|
|
return "(<\"".indexOf(c) !== -1;
|
|
|
|
|
}
|
|
|
|
|
function isStopCharacter(c) {
|
|
|
|
|
return isWhitespace(c) || "),>-=".indexOf(c) !== -1;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
function getStringElem(query, isInGenerics) {
|
|
|
|
|
if (isInGenerics) {
|
|
|
|
|
throw new Error("`\"` cannot be used in generics");
|
|
|
|
|
} else if (query.literalSearch) {
|
|
|
|
|
throw new Error("Cannot have more than one literal search element");
|
|
|
|
|
} else if (query.totalElems !== 0) {
|
|
|
|
|
throw new Error("Cannot use literal search when there is more than one element");
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
query.pos += 1;
|
|
|
|
|
while (query.pos < query.length && query.val[query.pos] !== "\"") {
|
|
|
|
|
if (query.val[query.pos] === "\\") {
|
|
|
|
|
// We ignore the next coming character.
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (query.pos >= query.length) {
|
|
|
|
|
throw new Error("Unclosed `\"`");
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
// To skip the quote at the end.
|
|
|
|
|
query.pos += 1;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
query.literalSearch = true;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
function skipWhitespaces(query) {
|
|
|
|
|
while (query.pos < query.length) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
var c = query.val[query.pos];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (!isWhitespace(c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function skipStopCharacters(query) {
|
|
|
|
|
while (query.pos < query.length) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
var c = query.val[query.pos];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (!isStopCharacter(c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function isPathStart(query) {
|
|
|
|
|
var pos = query.pos;
|
|
|
|
|
return pos + 1 < query.length && query.val[pos] === ':' && query.val[pos + 1] === ':';
|
|
|
|
|
}
|
|
|
|
|
function isReturnArrow(query) {
|
|
|
|
|
var pos = query.pos;
|
|
|
|
|
return pos + 1 < query.length && query.val[pos] === '-' && query.val[pos + 1] === '>';
|
|
|
|
|
}
|
|
|
|
|
function removeEmptyStringsFromArray(x) {
|
|
|
|
|
for (var i = 0, len = x.length; i < len; ++i) {
|
|
|
|
|
if (x[i] === "") {
|
|
|
|
|
x.splice(i, 1);
|
|
|
|
|
i -= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
function createQueryElement(query, elems, val, generics) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
removeEmptyStringsFromArray(generics);
|
|
|
|
|
if (val === '*' || (val.length === 0 && generics.length === 0)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var paths = val.split("::");
|
|
|
|
|
removeEmptyStringsFromArray(paths);
|
|
|
|
|
// In case we only have something like `<p>`, there is no name but it remains valid.
|
|
|
|
|
if (paths.length === 0) {
|
|
|
|
|
paths = [""];
|
|
|
|
|
}
|
|
|
|
|
elems.push({
|
|
|
|
|
name: val,
|
|
|
|
|
fullPath: paths,
|
|
|
|
|
pathWithoutLast: paths.slice(0, paths.length - 1),
|
|
|
|
|
pathLast: paths[paths.length - 1],
|
|
|
|
|
generics: generics,
|
|
|
|
|
});
|
2022-01-03 14:11:54 +01:00
|
|
|
|
query.totalElems += 1;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
function getNextElem(query, elems, isInGenerics) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var generics = [];
|
|
|
|
|
|
|
|
|
|
skipStopCharacters(query);
|
|
|
|
|
var start = query.pos;
|
|
|
|
|
var end = start;
|
|
|
|
|
// We handle the strings on their own mostly to make code easier to follow.
|
|
|
|
|
if (query.val[query.pos] === "\"") {
|
|
|
|
|
start += 1;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
getStringElem(query, isInGenerics);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
end = query.pos - 1;
|
|
|
|
|
skipWhitespaces(query);
|
|
|
|
|
} else {
|
|
|
|
|
while (query.pos < query.length) {
|
|
|
|
|
var c = query.val[query.pos];
|
|
|
|
|
if (isStopCharacter(c) || isSpecialStartCharacter(c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// If we allow paths ("str::string" for example).
|
|
|
|
|
else if (c === ":") {
|
|
|
|
|
if (!isPathStart(query)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Skip current ":".
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
end = query.pos;
|
|
|
|
|
skipWhitespaces(query);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (query.pos < query.length && query.val[query.pos] === "<") {
|
|
|
|
|
getItemsBefore(query, generics, ">");
|
|
|
|
|
}
|
|
|
|
|
if (start >= end && generics.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
createQueryElement(query, elems, query.val.slice(start, end), generics);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
function getItemsBefore(query, elems, limit) {
|
|
|
|
|
while (query.pos < query.length) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
var c = query.val[query.pos];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (c === limit) {
|
|
|
|
|
break;
|
|
|
|
|
} else if (isSpecialStartCharacter(c) || c === ":") {
|
|
|
|
|
// Something weird is going on in here. Ignoring it!
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
getNextElem(query, elems, limit === ">");
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
// We skip the "limit".
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
function parseInput(query) {
|
|
|
|
|
var c, before;
|
|
|
|
|
|
|
|
|
|
while (query.pos < query.length) {
|
|
|
|
|
c = query.val[query.pos];
|
|
|
|
|
if (isStopCharacter(c)) {
|
|
|
|
|
if (c === ",") {
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (c === "-" && isReturnArrow(query)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if (c == "(") {
|
|
|
|
|
break;
|
|
|
|
|
} else if (c === ":" && query.typeFilter === null && !isPathStart(query) &&
|
|
|
|
|
query.elems.length === 1)
|
|
|
|
|
{
|
|
|
|
|
// The type filter doesn't count as an element since it's a modifier.
|
|
|
|
|
query.typeFilter = query.elems.pop().name;
|
|
|
|
|
query.pos += 1;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
query.totalElems = 0;
|
|
|
|
|
query.literalSearch = false;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
before = query.elems.length;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
getNextElem(query, query.elems, false);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (query.elems.length === before) {
|
|
|
|
|
// Nothing was added, let's check it's not because of a solo ":"!
|
|
|
|
|
if (query.pos >= query.length || query.val[query.pos] !== ":") {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (query.pos < query.length) {
|
|
|
|
|
c = query.val[query.pos];
|
|
|
|
|
if (query.args.length === 0 && c === "(") {
|
|
|
|
|
if (query.elemName === null && query.elems.length === 1) {
|
|
|
|
|
query.elemName = query.elems.pop();
|
|
|
|
|
}
|
|
|
|
|
// Check for function/method arguments.
|
|
|
|
|
getItemsBefore(query, query.args, ")");
|
|
|
|
|
} else if (isReturnArrow(query)) {
|
|
|
|
|
// Get returned elements.
|
|
|
|
|
getItemsBefore(query, query.returned, "");
|
|
|
|
|
// Nothing can come afterward!
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
query.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function itemTypeFromName(typename) {
|
|
|
|
|
for (var i = 0, len = itemTypes.length; i < len; ++i) {
|
|
|
|
|
if (itemTypes[i] === typename) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NO_TYPE_FILTER;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
val = val.trim();
|
|
|
|
|
var query = {
|
|
|
|
|
original: val,
|
|
|
|
|
val: val.toLowerCase(),
|
|
|
|
|
length: val.length,
|
|
|
|
|
pos: 0,
|
|
|
|
|
typeFilter: null,
|
|
|
|
|
elems: [],
|
|
|
|
|
elemName: null,
|
|
|
|
|
args: [],
|
|
|
|
|
returned: [],
|
2022-01-03 14:11:54 +01:00
|
|
|
|
// Total number of elements (includes generics).
|
|
|
|
|
totalElems: 0,
|
|
|
|
|
// Total number of "top" elements (does not include generics).
|
2021-12-20 15:42:08 +01:00
|
|
|
|
foundElems: 0,
|
|
|
|
|
// This field is used to check if it's needed to re-run a search or not.
|
|
|
|
|
id: "",
|
|
|
|
|
// This field is used in `sortResults`.
|
|
|
|
|
nameSplit: null,
|
2022-01-03 14:11:54 +01:00
|
|
|
|
literalSearch: false,
|
|
|
|
|
error: null,
|
2021-12-20 15:42:08 +01:00
|
|
|
|
};
|
2022-01-03 14:11:54 +01:00
|
|
|
|
query.id = val;
|
|
|
|
|
try {
|
|
|
|
|
parseInput(query);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
query.error = err.message;
|
|
|
|
|
query.elems = [];
|
|
|
|
|
query.returned = [];
|
|
|
|
|
query.args = [];
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
query.foundElems = query.elems.length + query.args.length + query.returned.length;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (!query.literalSearch) {
|
|
|
|
|
// If there is more than one element in the query, we switch to literalSearch in any
|
|
|
|
|
// case.
|
|
|
|
|
query.literalSearch = query.foundElems > 1;
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (query.elemName !== null) {
|
|
|
|
|
query.foundElems += 1;
|
|
|
|
|
}
|
|
|
|
|
if (query.foundElems === 0 && val.length !== 0) {
|
|
|
|
|
// In this case, we'll simply keep whatever was entered by the user...
|
2022-01-03 14:11:54 +01:00
|
|
|
|
createQueryElement(query, query.elems, val, []);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
query.foundElems += 1;
|
|
|
|
|
}
|
|
|
|
|
if (query.typeFilter !== null) {
|
|
|
|
|
query.typeFilter = query.typeFilter.replace(/^const$/, "constant");
|
|
|
|
|
query.typeFilter = itemTypeFromName(query.typeFilter);
|
|
|
|
|
} else {
|
|
|
|
|
query.typeFilter = NO_TYPE_FILTER;
|
|
|
|
|
}
|
|
|
|
|
// In case we only have one argument, we move it back to `elems` to keep things simple.
|
|
|
|
|
if (query.foundElems === 1 && query.elemName !== null) {
|
|
|
|
|
query.elems.push(query.elemName);
|
|
|
|
|
query.elemName = null;
|
|
|
|
|
}
|
|
|
|
|
if (query.elemName !== null || query.elems.length === 1) {
|
|
|
|
|
val = query.elemName || query.elems[0];
|
|
|
|
|
query.nameSplit = typeof val.path === "undefined" ? null : val.path;
|
|
|
|
|
}
|
|
|
|
|
return query;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
/**
|
|
|
|
|
* Creates the query results.
|
|
|
|
|
*
|
|
|
|
|
* @param {Array<Object>} results_in_args
|
|
|
|
|
* @param {Array<Object>} results_returned
|
|
|
|
|
* @param {Array<Object>} results_in_args
|
|
|
|
|
* @param {ParsedQuery} queryInfo
|
|
|
|
|
* @return {Object} - A search index of results
|
|
|
|
|
*/
|
|
|
|
|
function createQueryResults(results_in_args, results_returned, results_others, queryInfo) {
|
|
|
|
|
return {
|
|
|
|
|
"in_args": results_in_args,
|
|
|
|
|
"returned": results_returned,
|
|
|
|
|
"others": results_others,
|
|
|
|
|
"query": queryInfo,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* Executes the query and builds an index of results
|
2022-01-03 14:11:54 +01:00
|
|
|
|
*
|
|
|
|
|
* @param {ParsedQuery} query - The user query
|
|
|
|
|
* @param {Object} searchWords - The list of search words to query against
|
|
|
|
|
* @param {Object} filterCrates - Crate to search in if defined
|
|
|
|
|
* @return {Object} - A search index of results
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
|
|
|
|
function execQuery(queryInfo, searchWords, filterCrates) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (queryInfo.error !== null) {
|
|
|
|
|
createQueryResults([], [], [], queryInfo);
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var results_others = {}, results_in_args = {}, results_returned = {};
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-05-09 13:49:22 -07:00
|
|
|
|
function transformResults(results) {
|
2021-12-20 18:01:04 +01:00
|
|
|
|
var duplicates = {};
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var out = [];
|
2021-12-20 18:01:04 +01:00
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
for (var i = 0, len = results.length; i < len; ++i) {
|
2021-12-20 18:01:04 +01:00
|
|
|
|
var result = results[i];
|
|
|
|
|
|
|
|
|
|
if (result.id > -1) {
|
|
|
|
|
var obj = searchIndex[result.id];
|
|
|
|
|
obj.lev = result.lev;
|
2021-05-09 13:49:22 -07:00
|
|
|
|
var res = buildHrefAndPath(obj);
|
|
|
|
|
obj.displayPath = pathSplitter(res[0]);
|
|
|
|
|
obj.fullPath = obj.displayPath + obj.name;
|
|
|
|
|
// To be sure than it some items aren't considered as duplicate.
|
|
|
|
|
obj.fullPath += "|" + obj.ty;
|
2021-12-20 18:01:04 +01:00
|
|
|
|
|
|
|
|
|
if (duplicates[obj.fullPath]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
duplicates[obj.fullPath] = true;
|
|
|
|
|
|
2021-05-09 13:49:22 -07:00
|
|
|
|
obj.href = res[1];
|
|
|
|
|
out.push(obj);
|
|
|
|
|
if (out.length >= MAX_RESULTS) {
|
|
|
|
|
break;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortResults(results, isType) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var nameSplit = queryInfo.nameSplit;
|
|
|
|
|
var query = queryInfo.val;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var ar = [];
|
|
|
|
|
for (var entry in results) {
|
2021-05-14 13:56:15 +02:00
|
|
|
|
if (hasOwnPropertyRustdoc(results, entry)) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
var result = results[entry];
|
|
|
|
|
result.word = searchWords[result.id];
|
|
|
|
|
result.item = searchIndex[result.id] || {};
|
|
|
|
|
ar.push(result);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
results = ar;
|
|
|
|
|
// if there are no results then return to default and fail
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.sort(function(aaa, bbb) {
|
|
|
|
|
var a, b;
|
|
|
|
|
|
|
|
|
|
// sort by exact match with regard to the last word (mismatch goes later)
|
2021-12-20 15:42:08 +01:00
|
|
|
|
a = (aaa.word !== query);
|
|
|
|
|
b = (bbb.word !== query);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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);
|
|
|
|
|
b = (bbb.lev);
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// sort by crate (non-current crate goes later)
|
|
|
|
|
a = (aaa.item.crate !== window.currentCrate);
|
|
|
|
|
b = (bbb.item.crate !== window.currentCrate);
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// sort by item name length (longer goes later)
|
|
|
|
|
a = aaa.word.length;
|
|
|
|
|
b = bbb.word.length;
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// sort by item name (lexicographically larger goes later)
|
|
|
|
|
a = aaa.word;
|
|
|
|
|
b = bbb.word;
|
|
|
|
|
if (a !== b) { return (a > b ? +1 : -1); }
|
|
|
|
|
|
|
|
|
|
// sort by index of keyword in item name (no literal occurrence goes later)
|
|
|
|
|
a = (aaa.index < 0);
|
|
|
|
|
b = (bbb.index < 0);
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
// (later literal occurrence, if any, goes later)
|
|
|
|
|
a = aaa.index;
|
|
|
|
|
b = bbb.index;
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// special precedence for primitive and keyword pages
|
|
|
|
|
if ((aaa.item.ty === TY_PRIMITIVE && bbb.item.ty !== TY_KEYWORD) ||
|
|
|
|
|
(aaa.item.ty === TY_KEYWORD && bbb.item.ty !== TY_PRIMITIVE)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if ((bbb.item.ty === TY_PRIMITIVE && aaa.item.ty !== TY_PRIMITIVE) ||
|
|
|
|
|
(bbb.item.ty === TY_KEYWORD && aaa.item.ty !== TY_KEYWORD)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sort by description (no description goes later)
|
|
|
|
|
a = (aaa.item.desc === "");
|
|
|
|
|
b = (bbb.item.desc === "");
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// sort by type (later occurrence in `itemTypes` goes later)
|
|
|
|
|
a = aaa.item.ty;
|
|
|
|
|
b = bbb.item.ty;
|
|
|
|
|
if (a !== b) { return a - b; }
|
|
|
|
|
|
|
|
|
|
// sort by path (lexicographically larger goes later)
|
|
|
|
|
a = aaa.item.path;
|
|
|
|
|
b = bbb.item.path;
|
|
|
|
|
if (a !== b) { return (a > b ? +1 : -1); }
|
|
|
|
|
|
|
|
|
|
// que sera, sera
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
for (var i = 0, len = results.length; i < len; ++i) {
|
2021-05-14 13:56:15 +02:00
|
|
|
|
result = results[i];
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// this validation does not make sense when searching by types
|
|
|
|
|
if (result.dontValidate) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var name = result.item.name.toLowerCase(),
|
|
|
|
|
path = result.item.path.toLowerCase(),
|
|
|
|
|
parent = result.item.parent;
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (!isType && !validateResult(name, path, nameSplit, parent)) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
result.id = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return transformResults(results);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function checks if the object (`obj`) generics match the given type (`val`)
|
|
|
|
|
* generics. If there are no generics on `obj`, `defaultLev` is returned.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} obj - The object to check.
|
|
|
|
|
* @param {integer} defaultLev - This is the value to return in case there are no generics.
|
|
|
|
|
*
|
|
|
|
|
* @return {integer} - Returns the best match (if any) or `MAX_LEV_DISTANCE + 1`.
|
|
|
|
|
*/
|
|
|
|
|
function checkGenerics(obj, val, defaultLev) {
|
|
|
|
|
if (obj.length <= GENERICS_DATA || obj[GENERICS_DATA].length === 0) {
|
|
|
|
|
return val.generics.length === 0 ? defaultLev : MAX_LEV_DISTANCE + 1;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
// The names match, but we need to be sure that all generics kinda
|
|
|
|
|
// match as well.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var elem_name;
|
|
|
|
|
if (val.generics.length > 0 && obj[GENERICS_DATA].length >= val.generics.length) {
|
|
|
|
|
var elems = {};
|
|
|
|
|
for (var x = 0, length = obj[GENERICS_DATA].length; x < length; ++x) {
|
|
|
|
|
elem_name = obj[GENERICS_DATA][x][NAME];
|
|
|
|
|
if (!elems[elem_name]) {
|
|
|
|
|
elems[elem_name] = 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
elems[elem_name] += 1;
|
|
|
|
|
}
|
|
|
|
|
// We need to find the type that matches the most to remove it in order
|
|
|
|
|
// to move forward.
|
|
|
|
|
for (x = 0, length = val.generics.length; x < length; ++x) {
|
|
|
|
|
var generic = val.generics[x];
|
|
|
|
|
var match = null;
|
|
|
|
|
if (elems[generic.name]) {
|
|
|
|
|
match = generic.name;
|
|
|
|
|
} else {
|
|
|
|
|
for (elem_name in elems) {
|
|
|
|
|
if (!hasOwnPropertyRustdoc(elems, elem_name)) {
|
|
|
|
|
continue;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (elem_name === generic) {
|
|
|
|
|
match = elem_name;
|
|
|
|
|
break;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (match === null) {
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
|
|
|
|
}
|
|
|
|
|
elems[match] -= 1;
|
|
|
|
|
if (elems[match] === 0) {
|
|
|
|
|
delete elems[match];
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function checks if the object (`obj`) matches the given type (`val`) and its
|
|
|
|
|
* generics (if any).
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} obj
|
2021-12-20 15:42:08 +01:00
|
|
|
|
* @param {Object} val
|
|
|
|
|
*
|
|
|
|
|
* @return {integer} - Returns a Levenshtein distance to the best match.
|
|
|
|
|
*/
|
|
|
|
|
function checkIfInGenerics(obj, val) {
|
|
|
|
|
var lev = MAX_LEV_DISTANCE + 1;
|
|
|
|
|
for (var x = 0, length = obj[GENERICS_DATA].length; x < length && lev !== 0; ++x) {
|
|
|
|
|
lev = Math.min(
|
2022-01-03 14:11:54 +01:00
|
|
|
|
checkType(obj[GENERICS_DATA][x], val, true),
|
2021-12-20 15:42:08 +01:00
|
|
|
|
lev
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return lev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function checks if the object (`obj`) matches the given type (`val`) and its
|
|
|
|
|
* generics (if any).
|
|
|
|
|
*
|
2022-01-03 14:11:54 +01:00
|
|
|
|
* @param {Row} obj
|
|
|
|
|
* @param {QueryElement} val - The element from the parsed query.
|
|
|
|
|
* @param {boolean} literalSearch
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*
|
|
|
|
|
* @return {integer} - Returns a Levenshtein distance to the best match. If there is
|
|
|
|
|
* no match, returns `MAX_LEV_DISTANCE + 1`.
|
|
|
|
|
*/
|
2022-01-03 14:11:54 +01:00
|
|
|
|
function checkType(obj, val, literalSearch) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (val.name.length === 0 || obj[NAME].length === 0) {
|
|
|
|
|
// This is a pure "generic" search, no need to run other checks.
|
|
|
|
|
if (obj.length > GENERICS_DATA) {
|
|
|
|
|
return checkIfInGenerics(obj, val);
|
|
|
|
|
}
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var lev = levenshtein(obj[NAME], val.name);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (literalSearch) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (lev !== 0) {
|
|
|
|
|
// The name didn't match, let's try to check if the generics do.
|
|
|
|
|
if (val.generics.length === 0) {
|
|
|
|
|
var checkGeneric = (obj.length > GENERICS_DATA &&
|
|
|
|
|
obj[GENERICS_DATA].length > 0);
|
|
|
|
|
if (checkGeneric && obj[GENERICS_DATA].findIndex(function(elem) {
|
|
|
|
|
return elem[NAME] === val.name;
|
|
|
|
|
}) !== -1) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
return 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
|
|
|
|
} else if (val.generics.length > 0) {
|
|
|
|
|
return checkGenerics(obj, val, MAX_LEV_DISTANCE + 1);
|
2021-08-23 22:19:43 +02:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return 0;
|
|
|
|
|
} else if (obj.length > GENERICS_DATA) {
|
|
|
|
|
if (val.generics.length === 0) {
|
|
|
|
|
if (lev === 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// The name didn't match so we now check if the type we're looking for is inside
|
|
|
|
|
// the generics!
|
|
|
|
|
lev = checkIfInGenerics(obj, val);
|
|
|
|
|
// Now whatever happens, the returned distance is "less good" so we should mark
|
|
|
|
|
// it as such, and so we add 0.5 to the distance to make it "less good".
|
|
|
|
|
return lev + 0.5;
|
|
|
|
|
} else if (lev > MAX_LEV_DISTANCE) {
|
|
|
|
|
// So our item's name doesn't match at all and has generics.
|
|
|
|
|
//
|
|
|
|
|
// Maybe it's present in a sub generic? For example "f<A<B<C>>>()", if we're
|
|
|
|
|
// looking for "B<C>", we'll need to go down.
|
|
|
|
|
return checkIfInGenerics(obj, val);
|
|
|
|
|
} else {
|
|
|
|
|
// At this point, the name kinda match and we have generics to check, so
|
|
|
|
|
// let's go!
|
|
|
|
|
var tmp_lev = checkGenerics(obj, val, lev);
|
|
|
|
|
if (tmp_lev > MAX_LEV_DISTANCE) {
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
2021-08-23 22:19:43 +02:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
// We compute the median value of both checks and return it.
|
|
|
|
|
return (tmp_lev + lev) / 2;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
} else if (val.generics.length > 0) {
|
|
|
|
|
// In this case, we were expecting generics but there isn't so we simply reject this
|
|
|
|
|
// one.
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
// No generics on our query or on the target type so we can return without doing
|
|
|
|
|
// anything else.
|
|
|
|
|
return lev;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function checks if the object (`obj`) has an argument with the given type (`val`).
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} obj
|
2021-12-20 15:42:08 +01:00
|
|
|
|
* @param {Object} val
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* @param {integer} typeFilter
|
|
|
|
|
*
|
|
|
|
|
* @return {integer} - Returns a Levenshtein distance to the best match. If there is no
|
|
|
|
|
* match, returns `MAX_LEV_DISTANCE + 1`.
|
|
|
|
|
*/
|
2021-12-20 15:42:08 +01:00
|
|
|
|
function findArg(obj, val, typeFilter) {
|
|
|
|
|
var lev = MAX_LEV_DISTANCE + 1;
|
|
|
|
|
var tmp;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
if (obj && obj.type && obj.type[INPUTS_DATA] && obj.type[INPUTS_DATA].length > 0) {
|
|
|
|
|
var length = obj.type[INPUTS_DATA].length;
|
|
|
|
|
for (var i = 0; i < length; i++) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
tmp = obj.type[INPUTS_DATA][i];
|
2021-05-09 13:49:22 -07:00
|
|
|
|
if (!typePassesFilter(typeFilter, tmp[1])) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
lev = Math.min(lev, checkType(tmp, val, queryInfo.literalSearch));
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (lev === 0) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
return 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
return queryInfo.literalSearch ? MAX_LEV_DISTANCE + 1 : lev;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param {Object} obj
|
|
|
|
|
* @param {Object} val
|
|
|
|
|
* @param {integer} typeFilter
|
|
|
|
|
*
|
|
|
|
|
* @return {integer} - Returns a Levenshtein distance to the best match. If there is no
|
|
|
|
|
* match, returns `MAX_LEV_DISTANCE + 1`.
|
|
|
|
|
*/
|
|
|
|
|
function checkReturned(obj, val, typeFilter) {
|
|
|
|
|
var lev = MAX_LEV_DISTANCE + 1;
|
|
|
|
|
var tmp;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
|
|
|
|
|
var ret = obj.type[OUTPUT_DATA];
|
|
|
|
|
if (typeof ret[0] === "string") {
|
|
|
|
|
ret = [ret];
|
|
|
|
|
}
|
|
|
|
|
for (var x = 0, len = ret.length; x < len; ++x) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
tmp = ret[x];
|
2021-05-09 13:49:22 -07:00
|
|
|
|
if (!typePassesFilter(typeFilter, tmp[1])) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
lev = Math.min(lev, checkType(tmp, val, queryInfo.literalSearch));
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (lev === 0) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
return 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
return queryInfo.literalSearch ? MAX_LEV_DISTANCE + 1 : lev;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkPath(contains, lastElem, ty) {
|
|
|
|
|
if (contains.length === 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
var ret_lev = MAX_LEV_DISTANCE + 1;
|
|
|
|
|
var path = ty.path.split("::");
|
|
|
|
|
|
|
|
|
|
if (ty.parent && ty.parent.name) {
|
|
|
|
|
path.push(ty.parent.name.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var length = path.length;
|
|
|
|
|
var clength = contains.length;
|
|
|
|
|
if (clength > length) {
|
|
|
|
|
return MAX_LEV_DISTANCE + 1;
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < length; ++i) {
|
|
|
|
|
if (i + clength > length) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
var lev_total = 0;
|
|
|
|
|
var aborted = false;
|
|
|
|
|
for (var x = 0; x < clength; ++x) {
|
|
|
|
|
var lev = levenshtein(path[i + x], contains[x]);
|
|
|
|
|
if (lev > MAX_LEV_DISTANCE) {
|
|
|
|
|
aborted = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
lev_total += lev;
|
|
|
|
|
}
|
2021-05-09 13:49:22 -07:00
|
|
|
|
if (!aborted) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
ret_lev = Math.min(ret_lev, Math.round(lev_total / clength));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret_lev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function typePassesFilter(filter, type) {
|
2022-02-04 14:59:22 -05:00
|
|
|
|
// No filter or Exact mach
|
|
|
|
|
if (filter <= NO_TYPE_FILTER || filter === type) return true;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// Match related items
|
|
|
|
|
var name = itemTypes[type];
|
|
|
|
|
switch (itemTypes[filter]) {
|
|
|
|
|
case "constant":
|
|
|
|
|
return name === "associatedconstant";
|
|
|
|
|
case "fn":
|
|
|
|
|
return name === "method" || name === "tymethod";
|
|
|
|
|
case "type":
|
|
|
|
|
return name === "primitive" || name === "associatedtype";
|
|
|
|
|
case "trait":
|
|
|
|
|
return name === "traitalias";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No match
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createAliasFromItem(item) {
|
|
|
|
|
return {
|
|
|
|
|
crate: item.crate,
|
|
|
|
|
name: item.name,
|
|
|
|
|
path: item.path,
|
|
|
|
|
desc: item.desc,
|
|
|
|
|
ty: item.ty,
|
|
|
|
|
parent: item.parent,
|
|
|
|
|
type: item.type,
|
|
|
|
|
is_alias: true,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleAliases(ret, query, filterCrates) {
|
|
|
|
|
// We separate aliases and crate aliases because we want to have current crate
|
|
|
|
|
// aliases to be before the others in the displayed results.
|
|
|
|
|
var aliases = [];
|
|
|
|
|
var crateAliases = [];
|
2022-01-10 14:57:43 +01:00
|
|
|
|
if (filterCrates !== null) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (ALIASES[filterCrates] && ALIASES[filterCrates][query]) {
|
|
|
|
|
var query_aliases = ALIASES[filterCrates][query];
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var len = query_aliases.length;
|
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
|
aliases.push(createAliasFromItem(searchIndex[query_aliases[i]]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Object.keys(ALIASES).forEach(function(crate) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (ALIASES[crate][query]) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var pushTo = crate === window.currentCrate ? crateAliases : aliases;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var query_aliases = ALIASES[crate][query];
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var len = query_aliases.length;
|
|
|
|
|
for (var i = 0; i < len; ++i) {
|
|
|
|
|
pushTo.push(createAliasFromItem(searchIndex[query_aliases[i]]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sortFunc = function(aaa, bbb) {
|
|
|
|
|
if (aaa.path < bbb.path) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else if (aaa.path === bbb.path) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
};
|
|
|
|
|
crateAliases.sort(sortFunc);
|
|
|
|
|
aliases.sort(sortFunc);
|
|
|
|
|
|
|
|
|
|
var pushFunc = function(alias) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
alias.alias = query;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var res = buildHrefAndPath(alias);
|
|
|
|
|
alias.displayPath = pathSplitter(res[0]);
|
|
|
|
|
alias.fullPath = alias.displayPath + alias.name;
|
|
|
|
|
alias.href = res[1];
|
|
|
|
|
|
|
|
|
|
ret.others.unshift(alias);
|
|
|
|
|
if (ret.others.length > MAX_RESULTS) {
|
|
|
|
|
ret.others.pop();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
onEach(aliases, pushFunc);
|
|
|
|
|
onEach(crateAliases, pushFunc);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function adds the given result into the provided `res` map if it matches the
|
|
|
|
|
* following condition:
|
|
|
|
|
*
|
2022-01-03 14:11:54 +01:00
|
|
|
|
* * If it is a "literal search" (`queryInfo.literalSearch`), then `lev` must be 0.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* * If it is not a "literal search", `lev` must be <= `MAX_LEV_DISTANCE`.
|
|
|
|
|
*
|
|
|
|
|
* The `res` map contains information which will be used to sort the search results:
|
|
|
|
|
*
|
|
|
|
|
* * `fullId` is a `string`` used as the key of the object we use for the `res` map.
|
|
|
|
|
* * `id` is the index in both `searchWords` and `searchIndex` arrays for this element.
|
|
|
|
|
* * `index` is an `integer`` used to sort by the position of the word in the item's name.
|
|
|
|
|
* * `lev` is the main metric used to sort the search results.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} res
|
|
|
|
|
* @param {string} fullId
|
|
|
|
|
* @param {integer} id
|
|
|
|
|
* @param {integer} index
|
|
|
|
|
* @param {integer} lev
|
|
|
|
|
*/
|
2022-01-03 14:11:54 +01:00
|
|
|
|
function addIntoResults(res, fullId, id, index, lev) {
|
|
|
|
|
if (lev === 0 || (!queryInfo.literalSearch && lev <= MAX_LEV_DISTANCE)) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
if (res[fullId] !== undefined) {
|
|
|
|
|
var result = res[fullId];
|
|
|
|
|
if (result.dontValidate || result.lev <= lev) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
res[fullId] = {
|
|
|
|
|
id: id,
|
|
|
|
|
index: index,
|
2022-01-03 14:11:54 +01:00
|
|
|
|
dontValidate: queryInfo.literalSearch,
|
2021-12-15 20:52:37 +01:00
|
|
|
|
lev: lev,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function is called in case the query is only one element (with or without generics).
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} ty
|
|
|
|
|
* @param {integer} pos - Position in the `searchIndex`.
|
|
|
|
|
* @param {Object} elem - The element from the parsed query.
|
|
|
|
|
*/
|
|
|
|
|
function handleSingleArg(ty, pos, elem) {
|
|
|
|
|
if (!ty || (filterCrates !== null && ty.crate !== filterCrates)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var lev, lev_add = 0, index = -1;
|
|
|
|
|
var fullId = ty.id;
|
|
|
|
|
|
|
|
|
|
var in_args = findArg(ty, elem, queryInfo.typeFilter);
|
|
|
|
|
var returned = checkReturned(ty, elem, queryInfo.typeFilter);
|
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_in_args, fullId, pos, index, in_args);
|
|
|
|
|
addIntoResults(results_returned, fullId, pos, index, returned);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
|
|
|
|
|
if (!typePassesFilter(queryInfo.typeFilter, ty.ty)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var searchWord = searchWords[pos];
|
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (queryInfo.literalSearch) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (searchWord === elem.name) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_others, fullId, pos, -1, 0);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No need to check anything else if it's a "pure" generics search.
|
|
|
|
|
if (elem.name.length === 0) {
|
|
|
|
|
if (ty.type !== null) {
|
|
|
|
|
lev = checkGenerics(ty.type, elem, MAX_LEV_DISTANCE + 1);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_others, fullId, pos, index, lev);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
|
|
|
|
|
if (elem.fullPath.length > 1) {
|
|
|
|
|
lev = checkPath(elem.pathWithoutLast, elem.pathLast, ty);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (lev > MAX_LEV_DISTANCE || (queryInfo.literalSearch && lev !== 0)) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
} else if (lev > 0) {
|
|
|
|
|
lev_add = lev / 10;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (searchWord.indexOf(elem.pathLast) > -1 ||
|
|
|
|
|
ty.normalizedName.indexOf(elem.pathLast) > -1)
|
|
|
|
|
{
|
|
|
|
|
// filter type: ... queries
|
|
|
|
|
if (!results_others[fullId] !== undefined) {
|
|
|
|
|
index = ty.normalizedName.indexOf(elem.pathLast);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
lev = levenshtein(searchWord, elem.pathLast);
|
|
|
|
|
lev += lev_add;
|
|
|
|
|
if (lev > 0 && elem.pathLast.length > 3 && searchWord.indexOf(elem.pathLast) > -1)
|
|
|
|
|
{
|
|
|
|
|
if (elem.pathLast.length < 6) {
|
|
|
|
|
lev = 1;
|
|
|
|
|
} else {
|
|
|
|
|
lev = 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
if (lev > MAX_LEV_DISTANCE) {
|
|
|
|
|
return;
|
|
|
|
|
} else if (index !== -1 && elem.fullPath.length < 2) {
|
|
|
|
|
lev -= 1;
|
|
|
|
|
}
|
|
|
|
|
if (lev < 0) {
|
|
|
|
|
lev = 0;
|
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_others, fullId, pos, index, lev);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function is called in case the query has more than one element.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} ty
|
|
|
|
|
* @param {integer} pos - Position in the `searchIndex`.
|
|
|
|
|
* @param {Object} elem - The element from the parsed query.
|
|
|
|
|
*/
|
|
|
|
|
function handleArgs(ty, pos, results) {
|
|
|
|
|
if (!ty || (filterCrates !== null && ty.crate !== filterCrates)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var totalLev = 0;
|
|
|
|
|
var nbLev = 0;
|
|
|
|
|
var lev;
|
|
|
|
|
var i, len;
|
|
|
|
|
var el;
|
|
|
|
|
|
|
|
|
|
// If the result is too "bad", we return false and it ends this search.
|
|
|
|
|
function checkArgs(args, callback) {
|
|
|
|
|
for (i = 0, len = args.length; i < len; ++i) {
|
|
|
|
|
el = args[i];
|
|
|
|
|
// There is more than one parameter to the query so all checks should be "exact"
|
|
|
|
|
lev = callback(ty, el, NO_TYPE_FILTER);
|
|
|
|
|
if (lev <= 1) {
|
|
|
|
|
nbLev += 1;
|
|
|
|
|
totalLev += lev;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return false;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!checkArgs(queryInfo.elems, findArg)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!checkArgs(queryInfo.args, findArg)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!checkArgs(queryInfo.returned, checkReturned)) {
|
|
|
|
|
return;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (nbLev === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lev = Math.round(totalLev / nbLev);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results, ty.id, pos, 0, lev);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
function innerRunQuery() {
|
|
|
|
|
var elem, i, nSearchWords, in_args, in_returned, ty;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (queryInfo.foundElems === 1) {
|
|
|
|
|
if (queryInfo.elems.length === 1) {
|
|
|
|
|
elem = queryInfo.elems[0];
|
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
|
|
|
|
// It means we want to check for this element everywhere (in names, args and
|
|
|
|
|
// returned).
|
|
|
|
|
handleSingleArg(searchIndex[i], i, elem);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
} else if (queryInfo.args.length === 1) {
|
|
|
|
|
// We received one argument to check, so looking into args.
|
|
|
|
|
elem = queryInfo.args[0];
|
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
|
|
|
|
ty = searchIndex[i];
|
|
|
|
|
in_args = findArg(ty, elem, queryInfo.typeFilter);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_in_args, ty.id, i, -1, in_args);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
} else if (queryInfo.returned.length === 1) {
|
|
|
|
|
// We received one returned argument to check, so looking into returned values.
|
|
|
|
|
elem = queryInfo.returned[0];
|
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
|
|
|
|
ty = searchIndex[i];
|
|
|
|
|
in_returned = checkReturned(ty, elem, queryInfo.typeFilter);
|
2022-01-03 14:11:54 +01:00
|
|
|
|
addIntoResults(results_returned, ty.id, i, -1, in_returned);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
} else if (queryInfo.foundElems > 0) {
|
|
|
|
|
var container = results_others;
|
|
|
|
|
// In the special case where only a "returned" information is available, we want to
|
|
|
|
|
// put the information into the "results_returned" dict.
|
|
|
|
|
if (queryInfo.returned.length !== 0 && queryInfo.elemName === null &&
|
|
|
|
|
queryInfo.args.length === 0 && queryInfo.elems.length === 0)
|
|
|
|
|
{
|
|
|
|
|
container = results_returned;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
|
|
|
|
handleArgs(searchIndex[i], i, container);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
innerRunQuery();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
var ret = createQueryResults(
|
|
|
|
|
sortResults(results_in_args, true),
|
|
|
|
|
sortResults(results_returned, true),
|
|
|
|
|
sortResults(results_others, false),
|
|
|
|
|
queryInfo);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
handleAliases(ret, queryInfo.original.replace(/"/g, "").toLowerCase(), filterCrates);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate performs the following boolean logic. For example:
|
|
|
|
|
* "File::open" will give IF A PARENT EXISTS => ("file" && "open")
|
|
|
|
|
* exists in (name || path || parent) OR => ("file" && "open") exists in
|
|
|
|
|
* (name || path )
|
|
|
|
|
*
|
|
|
|
|
* This could be written functionally, but I wanted to minimise
|
|
|
|
|
* functions on stack.
|
|
|
|
|
*
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* @param {string} name - The name of the result
|
|
|
|
|
* @param {string} path - The path of the result
|
|
|
|
|
* @param {string} keys - The keys to be used (["file", "open"])
|
|
|
|
|
* @param {Object} parent - The parent of the result
|
|
|
|
|
* @return {boolean} - Whether the result is valid or not
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
|
|
|
|
function validateResult(name, path, keys, parent) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (!keys || !keys.length) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
for (var i = 0, len = keys.length; i < len; ++i) {
|
|
|
|
|
// each check is for validation so we negate the conditions and invalidate
|
|
|
|
|
if (!(
|
|
|
|
|
// check for an exact name match
|
|
|
|
|
name.indexOf(keys[i]) > -1 ||
|
|
|
|
|
// then an exact path match
|
|
|
|
|
path.indexOf(keys[i]) > -1 ||
|
|
|
|
|
// next if there is a parent, check for exact parent match
|
|
|
|
|
(parent !== undefined && parent.name !== undefined &&
|
|
|
|
|
parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
|
|
|
|
|
// lastly check to see if the name was a levenshtein match
|
|
|
|
|
levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 12:56:21 -07:00
|
|
|
|
function nextTab(direction) {
|
|
|
|
|
var next = (searchState.currentTab + direction + 3) % searchState.focusedByTab.length;
|
|
|
|
|
searchState.focusedByTab[searchState.currentTab] = document.activeElement;
|
|
|
|
|
printTab(next);
|
|
|
|
|
focusSearchResult();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-05-20 12:45:25 +02:00
|
|
|
|
// Focus the first search result on the active tab, or the result that
|
2021-05-09 12:56:21 -07:00
|
|
|
|
// was focused last time this tab was active.
|
|
|
|
|
function focusSearchResult() {
|
|
|
|
|
var target = searchState.focusedByTab[searchState.currentTab] ||
|
2021-05-20 12:45:25 +02:00
|
|
|
|
document.querySelectorAll(".search-results.active a").item(0) ||
|
|
|
|
|
document.querySelectorAll("#titles > button").item(searchState.currentTab);
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (target) {
|
|
|
|
|
target.focus();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildHrefAndPath(item) {
|
|
|
|
|
var displayPath;
|
|
|
|
|
var href;
|
|
|
|
|
var type = itemTypes[item.ty];
|
|
|
|
|
var name = item.name;
|
|
|
|
|
var path = item.path;
|
|
|
|
|
|
|
|
|
|
if (type === "mod") {
|
|
|
|
|
displayPath = path + "::";
|
|
|
|
|
href = window.rootPath + path.replace(/::/g, "/") + "/" +
|
|
|
|
|
name + "/index.html";
|
|
|
|
|
} else if (type === "primitive" || type === "keyword") {
|
|
|
|
|
displayPath = "";
|
|
|
|
|
href = window.rootPath + path.replace(/::/g, "/") +
|
|
|
|
|
"/" + type + "." + name + ".html";
|
|
|
|
|
} else if (type === "externcrate") {
|
|
|
|
|
displayPath = "";
|
|
|
|
|
href = window.rootPath + name + "/index.html";
|
|
|
|
|
} else if (item.parent !== undefined) {
|
|
|
|
|
var myparent = item.parent;
|
|
|
|
|
var anchor = "#" + type + "." + name;
|
|
|
|
|
var parentType = itemTypes[myparent.ty];
|
|
|
|
|
var pageType = parentType;
|
|
|
|
|
var pageName = myparent.name;
|
|
|
|
|
|
|
|
|
|
if (parentType === "primitive") {
|
|
|
|
|
displayPath = myparent.name + "::";
|
|
|
|
|
} else if (type === "structfield" && parentType === "variant") {
|
|
|
|
|
// Structfields belonging to variants are special: the
|
|
|
|
|
// final path element is the enum name.
|
|
|
|
|
var enumNameIdx = item.path.lastIndexOf("::");
|
|
|
|
|
var enumName = item.path.substr(enumNameIdx + 2);
|
|
|
|
|
path = item.path.substr(0, enumNameIdx);
|
|
|
|
|
displayPath = path + "::" + enumName + "::" + myparent.name + "::";
|
|
|
|
|
anchor = "#variant." + myparent.name + ".field." + name;
|
|
|
|
|
pageType = "enum";
|
|
|
|
|
pageName = enumName;
|
|
|
|
|
} else {
|
|
|
|
|
displayPath = path + "::" + myparent.name + "::";
|
|
|
|
|
}
|
|
|
|
|
href = window.rootPath + path.replace(/::/g, "/") +
|
|
|
|
|
"/" + pageType +
|
|
|
|
|
"." + pageName +
|
|
|
|
|
".html" + anchor;
|
|
|
|
|
} else {
|
|
|
|
|
displayPath = item.path + "::";
|
|
|
|
|
href = window.rootPath + item.path.replace(/::/g, "/") +
|
|
|
|
|
"/" + type + "." + name + ".html";
|
|
|
|
|
}
|
|
|
|
|
return [displayPath, href];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function escape(content) {
|
|
|
|
|
var h1 = document.createElement("h1");
|
|
|
|
|
h1.textContent = content;
|
|
|
|
|
return h1.innerHTML;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pathSplitter(path) {
|
|
|
|
|
var tmp = "<span>" + path.replace(/::/g, "::</span><span>");
|
|
|
|
|
if (tmp.endsWith("<span>")) {
|
|
|
|
|
return tmp.slice(0, tmp.length - 6);
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* Render a set of search results for a single tab.
|
|
|
|
|
* @param {Array<?>} array - The search results for this tab
|
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {boolean} display - True if this is the active tab
|
|
|
|
|
*/
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function addTab(array, query, display) {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
var extraClass = "";
|
|
|
|
|
if (display === true) {
|
|
|
|
|
extraClass = " active";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 15:14:50 +02:00
|
|
|
|
var output = document.createElement("div");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var length = 0;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
if (array.length > 0 && query.error === null) {
|
2021-05-20 15:14:50 +02:00
|
|
|
|
output.className = "search-results " + extraClass;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
array.forEach(function(item) {
|
2021-05-24 14:24:34 +02:00
|
|
|
|
var name = item.name;
|
|
|
|
|
var type = itemTypes[item.ty];
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
length += 1;
|
|
|
|
|
|
2021-05-24 14:24:34 +02:00
|
|
|
|
var extra = "";
|
|
|
|
|
if (type === "primitive") {
|
|
|
|
|
extra = " <i>(primitive type)</i>";
|
|
|
|
|
} else if (type === "keyword") {
|
|
|
|
|
extra = " <i>(keyword)</i>";
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 15:14:50 +02:00
|
|
|
|
var link = document.createElement("a");
|
|
|
|
|
link.className = "result-" + type;
|
|
|
|
|
link.href = item.href;
|
|
|
|
|
|
|
|
|
|
var wrapper = document.createElement("div");
|
|
|
|
|
var resultName = document.createElement("div");
|
|
|
|
|
resultName.className = "result-name";
|
|
|
|
|
|
|
|
|
|
if (item.is_alias) {
|
|
|
|
|
var alias = document.createElement("span");
|
|
|
|
|
alias.className = "alias";
|
|
|
|
|
|
|
|
|
|
var bold = document.createElement("b");
|
|
|
|
|
bold.innerText = item.alias;
|
|
|
|
|
alias.appendChild(bold);
|
|
|
|
|
|
|
|
|
|
alias.insertAdjacentHTML(
|
|
|
|
|
"beforeend",
|
|
|
|
|
"<span class=\"grey\"><i> - see </i></span>");
|
|
|
|
|
|
|
|
|
|
resultName.appendChild(alias);
|
|
|
|
|
}
|
|
|
|
|
resultName.insertAdjacentHTML(
|
|
|
|
|
"beforeend",
|
|
|
|
|
item.displayPath + "<span class=\"" + type + "\">" + name + extra + "</span>");
|
|
|
|
|
wrapper.appendChild(resultName);
|
|
|
|
|
|
|
|
|
|
var description = document.createElement("div");
|
|
|
|
|
description.className = "desc";
|
|
|
|
|
var spanDesc = document.createElement("span");
|
2021-06-07 11:12:27 +02:00
|
|
|
|
spanDesc.insertAdjacentHTML("beforeend", item.desc);
|
2021-05-20 15:14:50 +02:00
|
|
|
|
|
|
|
|
|
description.appendChild(spanDesc);
|
|
|
|
|
wrapper.appendChild(description);
|
|
|
|
|
link.appendChild(wrapper);
|
|
|
|
|
output.appendChild(link);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
});
|
2022-01-03 14:11:54 +01:00
|
|
|
|
} else if (query.error !== null) {
|
|
|
|
|
output.className = "search-failed" + extraClass;
|
|
|
|
|
output.innerHTML = "Syntax error: " + query.error;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
2021-05-20 15:14:50 +02:00
|
|
|
|
output.className = "search-failed" + extraClass;
|
|
|
|
|
output.innerHTML = "No results :(<br/>" +
|
2021-04-11 22:19:29 -07:00
|
|
|
|
"Try on <a href=\"https://duckduckgo.com/?q=" +
|
2021-12-20 15:42:08 +01:00
|
|
|
|
encodeURIComponent("rust " + query.val) +
|
2021-04-11 22:19:29 -07:00
|
|
|
|
"\">DuckDuckGo</a>?<br/><br/>" +
|
|
|
|
|
"Or try looking in one of these:<ul><li>The <a " +
|
|
|
|
|
"href=\"https://doc.rust-lang.org/reference/index.html\">Rust Reference</a> " +
|
|
|
|
|
" for technical details about the language.</li><li><a " +
|
|
|
|
|
"href=\"https://doc.rust-lang.org/rust-by-example/index.html\">Rust By " +
|
|
|
|
|
"Example</a> for expository code examples.</a></li><li>The <a " +
|
|
|
|
|
"href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
|
|
|
|
|
"introductions to language features and the language itself.</li><li><a " +
|
|
|
|
|
"href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
|
2021-05-20 15:14:50 +02:00
|
|
|
|
" <a href=\"https://crates.io/\">crates.io</a>.</li></ul>";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
return [output, length];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeTabHeader(tabNb, text, nbElems) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
if (searchState.currentTab === tabNb) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
return "<button class=\"selected\">" + text +
|
|
|
|
|
" <div class=\"count\">(" + nbElems + ")</div></button>";
|
|
|
|
|
}
|
|
|
|
|
return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 23:48:34 -05:00
|
|
|
|
function showResults(results, go_to_first, filterCrates) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var search = searchState.outputElement();
|
2021-05-31 19:20:41 -07:00
|
|
|
|
if (go_to_first || (results.others.length === 1
|
2021-04-11 22:19:29 -07:00
|
|
|
|
&& getSettingValue("go-to-only-result") === "true"
|
|
|
|
|
// By default, the search DOM element is "empty" (meaning it has no children not
|
|
|
|
|
// text content). Once a search has been run, it won't be empty, even if you press
|
|
|
|
|
// ESC or empty the search input (which also "cancels" the search).
|
2021-05-31 19:20:41 -07:00
|
|
|
|
&& (!search.firstChild || search.firstChild.innerText !== searchState.loadingText)))
|
2021-04-11 22:19:29 -07:00
|
|
|
|
{
|
|
|
|
|
var elem = document.createElement("a");
|
|
|
|
|
elem.href = results.others[0].href;
|
2021-05-09 12:56:21 -07:00
|
|
|
|
removeClass(elem, "active");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// For firefox, we need the element to be in the DOM so it can be clicked.
|
|
|
|
|
document.body.appendChild(elem);
|
|
|
|
|
elem.click();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (results.query === undefined) {
|
|
|
|
|
results.query = parseQuery(searchState.input.value);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
currentResults = results.query.id;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var ret_others = addTab(results.others, results.query, true);
|
|
|
|
|
var ret_in_args = addTab(results.in_args, results.query, false);
|
|
|
|
|
var ret_returned = addTab(results.returned, results.query, false);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// Navigate to the relevant tab if the current tab is empty, like in case users search
|
|
|
|
|
// for "-> String". If they had selected another tab previously, they have to click on
|
|
|
|
|
// it again.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var currentTab = searchState.currentTab;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if ((currentTab === 0 && ret_others[1] === 0) ||
|
|
|
|
|
(currentTab === 1 && ret_in_args[1] === 0) ||
|
|
|
|
|
(currentTab === 2 && ret_returned[1] === 0)) {
|
|
|
|
|
if (ret_others[1] !== 0) {
|
|
|
|
|
currentTab = 0;
|
|
|
|
|
} else if (ret_in_args[1] !== 0) {
|
|
|
|
|
currentTab = 1;
|
|
|
|
|
} else if (ret_returned[1] !== 0) {
|
|
|
|
|
currentTab = 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 15:05:47 -08:00
|
|
|
|
let crates = "";
|
|
|
|
|
if (window.ALL_CRATES.length > 1) {
|
|
|
|
|
crates = ` in <select id="crate-search"><option value="All crates">All crates</option>`;
|
|
|
|
|
for (let c of window.ALL_CRATES) {
|
|
|
|
|
crates += `<option value="${c}" ${c == filterCrates && "selected"}>${c}</option>`;
|
|
|
|
|
}
|
|
|
|
|
crates += `</select>`;
|
2022-01-01 23:48:34 -05:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var typeFilter = "";
|
|
|
|
|
if (results.query.typeFilter !== NO_TYPE_FILTER) {
|
|
|
|
|
typeFilter = " (type: " + escape(results.query.typeFilter) + ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var output = `<div id="search-settings">` +
|
|
|
|
|
`<h1 class="search-results-title">Results for ${escape(results.query.val)}$` +
|
|
|
|
|
`${typeFilter}</h1> in ${crates} </div>` +
|
|
|
|
|
`<div id="titles">` +
|
2021-04-11 22:19:29 -07:00
|
|
|
|
makeTabHeader(0, "In Names", ret_others[1]) +
|
|
|
|
|
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
|
|
|
|
|
makeTabHeader(2, "In Return Types", ret_returned[1]) +
|
2021-05-20 15:14:50 +02:00
|
|
|
|
"</div>";
|
|
|
|
|
|
|
|
|
|
var resultsElem = document.createElement("div");
|
|
|
|
|
resultsElem.id = "results";
|
|
|
|
|
resultsElem.appendChild(ret_others[0]);
|
|
|
|
|
resultsElem.appendChild(ret_in_args[0]);
|
|
|
|
|
resultsElem.appendChild(ret_returned[0]);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
search.innerHTML = output;
|
2022-01-12 15:05:47 -08:00
|
|
|
|
let crateSearch = document.getElementById("crate-search");
|
|
|
|
|
if (crateSearch) {
|
|
|
|
|
crateSearch.addEventListener("input", updateCrate);
|
|
|
|
|
}
|
2021-05-20 15:14:50 +02:00
|
|
|
|
search.appendChild(resultsElem);
|
2021-05-20 12:45:25 +02:00
|
|
|
|
// Reset focused elements.
|
|
|
|
|
searchState.focusedByTab = [null, null, null];
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.showResults(search);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var elems = document.getElementById("titles").childNodes;
|
|
|
|
|
elems[0].onclick = function() { printTab(0); };
|
|
|
|
|
elems[1].onclick = function() { printTab(1); };
|
|
|
|
|
elems[2].onclick = function() { printTab(2); };
|
|
|
|
|
printTab(currentTab);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* Perform a search based on the current state of the search input element
|
|
|
|
|
* and display the results.
|
|
|
|
|
* @param {Event} [e] - The event that triggered this search, if any
|
|
|
|
|
* @param {boolean} [forced]
|
|
|
|
|
*/
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function search(e, forced) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var params = searchState.getQueryStringParams();
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var query = parseQuery(searchState.input.value.trim());
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 13:49:22 -07:00
|
|
|
|
if (!forced && query.id === currentResults) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (query.val.length > 0) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
putBackSearch();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 14:57:43 +01:00
|
|
|
|
var filterCrates = getFilterCrates();
|
|
|
|
|
|
|
|
|
|
// In case we have no information about the saved crate and there is a URL query parameter,
|
|
|
|
|
// we override it with the URL query parameter.
|
|
|
|
|
if (filterCrates === null && params["filter-crate"] !== undefined) {
|
|
|
|
|
filterCrates = params["filter-crate"];
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Update document title to maintain a meaningful browser history
|
2021-12-20 15:42:08 +01:00
|
|
|
|
searchState.title = "Results for " + query.original + " - Rust";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// Because searching is incremental by character, only the most
|
|
|
|
|
// recent search query is added to the browser history.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
if (searchState.browserSupportsHistoryApi()) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
var newURL = buildUrl(query.original, filterCrates);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (!history.state && !params.search) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
history.pushState(null, "", newURL);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
history.replaceState(null, "", newURL);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
showResults(
|
|
|
|
|
execQuery(query, searchWords, filterCrates),
|
|
|
|
|
params.go_to_first,
|
|
|
|
|
filterCrates);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildIndex(rawSearchIndex) {
|
|
|
|
|
searchIndex = [];
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* @type {Array<string>}
|
|
|
|
|
*/
|
2021-04-11 22:19:29 -07:00
|
|
|
|
var searchWords = [];
|
|
|
|
|
var i, word;
|
|
|
|
|
var currentIndex = 0;
|
|
|
|
|
var id = 0;
|
|
|
|
|
|
|
|
|
|
for (var crate in rawSearchIndex) {
|
2021-05-14 13:56:15 +02:00
|
|
|
|
if (!hasOwnPropertyRustdoc(rawSearchIndex, crate)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
var crateSize = 0;
|
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* The raw search data for a given crate. `n`, `t`, `d`, and `q`, `i`, and `f`
|
|
|
|
|
* are arrays with the same length. n[i] contains the name of an item.
|
|
|
|
|
* t[i] contains the type of that item (as a small integer that represents an
|
|
|
|
|
* offset in `itemTypes`). d[i] contains the description of that item.
|
|
|
|
|
*
|
|
|
|
|
* q[i] contains the full path of the item, or an empty string indicating
|
|
|
|
|
* "same as q[i-1]".
|
|
|
|
|
*
|
|
|
|
|
* i[i], f[i] are a mystery.
|
|
|
|
|
*
|
|
|
|
|
* `a` defines aliases with an Array of pairs: [name, offset], where `offset`
|
|
|
|
|
* points into the n/t/d/q/i/f arrays.
|
|
|
|
|
*
|
|
|
|
|
* `doc` contains the description of the crate.
|
|
|
|
|
*
|
|
|
|
|
* `p` is a mystery and isn't the same length as n/t/d/q/i/f.
|
|
|
|
|
*
|
|
|
|
|
* @type {{
|
|
|
|
|
* doc: string,
|
|
|
|
|
* a: Object,
|
|
|
|
|
* n: Array<string>,
|
|
|
|
|
* t: Array<Number>,
|
|
|
|
|
* d: Array<string>,
|
|
|
|
|
* q: Array<string>,
|
|
|
|
|
* i: Array<Number>,
|
|
|
|
|
* f: Array<Array<?>>,
|
|
|
|
|
* p: Array<Object>,
|
|
|
|
|
* }}
|
|
|
|
|
*/
|
|
|
|
|
var crateCorpus = rawSearchIndex[crate];
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
searchWords.push(crate);
|
|
|
|
|
// This object should have exactly the same set of fields as the "row"
|
|
|
|
|
// object defined below. Your JavaScript runtime will thank you.
|
|
|
|
|
// https://mathiasbynens.be/notes/shapes-ics
|
|
|
|
|
var crateRow = {
|
|
|
|
|
crate: crate,
|
|
|
|
|
ty: 1, // == ExternCrate
|
|
|
|
|
name: crate,
|
|
|
|
|
path: "",
|
2021-12-16 21:17:22 -08:00
|
|
|
|
desc: crateCorpus.doc,
|
2021-04-11 22:19:29 -07:00
|
|
|
|
parent: undefined,
|
|
|
|
|
type: null,
|
|
|
|
|
id: id,
|
2021-05-14 13:56:15 +02:00
|
|
|
|
normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
id += 1;
|
|
|
|
|
searchIndex.push(crateRow);
|
|
|
|
|
currentIndex += 1;
|
|
|
|
|
|
|
|
|
|
// an array of (Number) item types
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemTypes = crateCorpus.t;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (String) item names
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemNames = crateCorpus.n;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (String) full paths (or empty string for previous path)
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemPaths = crateCorpus.q;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (String) descriptions
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemDescs = crateCorpus.d;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemParentIdxs = crateCorpus.i;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (Object | null) the type of the function, if any
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var itemFunctionSearchTypes = crateCorpus.f;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of [(Number) item type,
|
|
|
|
|
// (String) name]
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var paths = crateCorpus.p;
|
2021-08-22 14:46:15 +02:00
|
|
|
|
// an array of [(String) alias name
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// [Number] index to items]
|
2021-12-16 21:17:22 -08:00
|
|
|
|
var aliases = crateCorpus.a;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// convert `rawPaths` entries into object form
|
|
|
|
|
var len = paths.length;
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
|
paths[i] = {ty: paths[i][0], name: paths[i][1]};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// convert `item*` into an object form, and construct word indices.
|
|
|
|
|
//
|
|
|
|
|
// before any analysis is performed lets gather the search terms to
|
|
|
|
|
// search against apart from the rest of the data. This is a quick
|
|
|
|
|
// operation that is cached for the life of the page state so that
|
|
|
|
|
// all other search operations have access to this cached data for
|
|
|
|
|
// faster analysis operations
|
|
|
|
|
len = itemTypes.length;
|
|
|
|
|
var lastPath = "";
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
|
// This object should have exactly the same set of fields as the "crateRow"
|
|
|
|
|
// object defined above.
|
|
|
|
|
if (typeof itemNames[i] === "string") {
|
|
|
|
|
word = itemNames[i].toLowerCase();
|
|
|
|
|
searchWords.push(word);
|
|
|
|
|
} else {
|
|
|
|
|
word = "";
|
|
|
|
|
searchWords.push("");
|
|
|
|
|
}
|
|
|
|
|
var row = {
|
|
|
|
|
crate: crate,
|
|
|
|
|
ty: itemTypes[i],
|
|
|
|
|
name: itemNames[i],
|
|
|
|
|
path: itemPaths[i] ? itemPaths[i] : lastPath,
|
|
|
|
|
desc: itemDescs[i],
|
|
|
|
|
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
|
|
|
|
|
type: itemFunctionSearchTypes[i],
|
|
|
|
|
id: id,
|
2021-05-14 13:56:15 +02:00
|
|
|
|
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
id += 1;
|
|
|
|
|
searchIndex.push(row);
|
|
|
|
|
lastPath = row.path;
|
|
|
|
|
crateSize += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aliases) {
|
|
|
|
|
ALIASES[crate] = {};
|
|
|
|
|
var j, local_aliases;
|
|
|
|
|
for (var alias_name in aliases) {
|
2021-05-14 13:56:15 +02:00
|
|
|
|
if (!hasOwnPropertyRustdoc(aliases, alias_name)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-05-14 13:56:15 +02:00
|
|
|
|
if (!hasOwnPropertyRustdoc(ALIASES[crate], alias_name)) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
ALIASES[crate][alias_name] = [];
|
|
|
|
|
}
|
|
|
|
|
local_aliases = aliases[alias_name];
|
|
|
|
|
for (j = 0, len = local_aliases.length; j < len; ++j) {
|
|
|
|
|
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
currentIndex += crateSize;
|
|
|
|
|
}
|
|
|
|
|
return searchWords;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* Callback for when the search form is submitted.
|
|
|
|
|
* @param {Event} [e] - The event that triggered this call, if any
|
|
|
|
|
*/
|
|
|
|
|
function onSearchSubmit(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
searchState.clearInputTimeout();
|
|
|
|
|
search();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 14:57:43 +01:00
|
|
|
|
function putBackSearch() {
|
|
|
|
|
var search_input = searchState.input;
|
|
|
|
|
if (!searchState.input) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var search = searchState.outputElement();
|
|
|
|
|
if (search_input.value !== "" && hasClass(search, "hidden")) {
|
|
|
|
|
searchState.showResults(search);
|
|
|
|
|
if (searchState.browserSupportsHistoryApi()) {
|
|
|
|
|
history.replaceState(null, "",
|
|
|
|
|
buildUrl(search_input.value, getFilterCrates()));
|
|
|
|
|
}
|
|
|
|
|
document.title = searchState.title;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function registerSearchEvents() {
|
|
|
|
|
var searchAfter500ms = function() {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.clearInputTimeout();
|
|
|
|
|
if (searchState.input.value.length === 0) {
|
|
|
|
|
if (searchState.browserSupportsHistoryApi()) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
history.replaceState(null, window.currentCrate + " - Rust",
|
2021-04-11 22:19:29 -07:00
|
|
|
|
getNakedUrl() + window.location.hash);
|
|
|
|
|
}
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.hideResults();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.timeout = setTimeout(search, 500);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.input.onkeyup = searchAfter500ms;
|
|
|
|
|
searchState.input.oninput = searchAfter500ms;
|
2021-12-16 21:17:22 -08:00
|
|
|
|
document.getElementsByClassName("search-form")[0].onsubmit = onSearchSubmit;
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.input.onchange = function(e) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (e.target !== document.activeElement) {
|
|
|
|
|
// To prevent doing anything when it's from a blur event.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Do NOT e.preventDefault() here. It will prevent pasting.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.clearInputTimeout();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// zero-timeout necessary here because at the time of event handler execution the
|
|
|
|
|
// pasted content is not in the input field yet. Shouldn’t make any difference for
|
|
|
|
|
// change, though.
|
|
|
|
|
setTimeout(search, 0);
|
|
|
|
|
};
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.input.onpaste = searchState.input.onchange;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-05-09 12:56:21 -07:00
|
|
|
|
searchState.outputElement().addEventListener("keydown", function(e) {
|
|
|
|
|
// We only handle unmodified keystrokes here. We don't want to interfere with,
|
|
|
|
|
// for instance, alt-left and alt-right for history navigation.
|
|
|
|
|
if (e.altKey || e.ctrlKey || e.shiftKey || e.metaKey) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// up and down arrow select next/previous search result, or the
|
|
|
|
|
// search box if we're already at the top.
|
|
|
|
|
if (e.which === 38) { // up
|
|
|
|
|
var previous = document.activeElement.previousElementSibling;
|
|
|
|
|
if (previous) {
|
|
|
|
|
previous.focus();
|
|
|
|
|
} else {
|
|
|
|
|
searchState.focus();
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
} else if (e.which === 40) { // down
|
|
|
|
|
var next = document.activeElement.nextElementSibling;
|
|
|
|
|
if (next) {
|
|
|
|
|
next.focus();
|
|
|
|
|
}
|
|
|
|
|
var rect = document.activeElement.getBoundingClientRect();
|
|
|
|
|
if (window.innerHeight - rect.bottom < rect.height) {
|
|
|
|
|
window.scrollBy(0, rect.height);
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
} else if (e.which === 37) { // left
|
|
|
|
|
nextTab(-1);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
} else if (e.which === 39) { // right
|
|
|
|
|
nextTab(1);
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
searchState.input.addEventListener("keydown", function(e) {
|
|
|
|
|
if (e.which === 40) { // down
|
|
|
|
|
focusSearchResult();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-10 14:57:43 +01:00
|
|
|
|
searchState.input.addEventListener("focus", function() {
|
|
|
|
|
putBackSearch();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
searchState.input.addEventListener("blur", function() {
|
|
|
|
|
searchState.input.placeholder = searchState.input.origPlaceholder;
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Push and pop states are used to add search results to the browser
|
|
|
|
|
// history.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
if (searchState.browserSupportsHistoryApi()) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Store the previous <title> so we can revert back to it later.
|
|
|
|
|
var previousTitle = document.title;
|
|
|
|
|
|
|
|
|
|
window.addEventListener("popstate", function(e) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var params = searchState.getQueryStringParams();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Revert to the previous title manually since the History
|
|
|
|
|
// API ignores the title parameter.
|
|
|
|
|
document.title = previousTitle;
|
|
|
|
|
// When browsing forward to search results the previous
|
|
|
|
|
// search will be repeated, so the currentResults are
|
|
|
|
|
// cleared to ensure the search is successful.
|
|
|
|
|
currentResults = null;
|
|
|
|
|
// Synchronize search bar with query string state and
|
|
|
|
|
// perform the search. This will empty the bar if there's
|
|
|
|
|
// nothing there, which lets you really go back to a
|
|
|
|
|
// previous state with nothing in the bar.
|
|
|
|
|
if (params.search && params.search.length > 0) {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.input.value = params.search;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Some browsers fire "onpopstate" for every page load
|
|
|
|
|
// (Chrome), while others fire the event only when actually
|
|
|
|
|
// popping a state (Firefox), which is why search() is
|
|
|
|
|
// called both here and at the end of the startSearch()
|
|
|
|
|
// function.
|
|
|
|
|
search(e);
|
|
|
|
|
} else {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.input.value = "";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// When browsing back from search results the main page
|
|
|
|
|
// visibility must be reset.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.hideResults();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This is required in firefox to avoid this problem: Navigating to a search result
|
|
|
|
|
// with the keyboard, hitting enter, and then hitting back would take you back to
|
|
|
|
|
// the doc page, rather than the search that should overlay it.
|
|
|
|
|
// This was an interaction between the back-forward cache and our handlers
|
|
|
|
|
// that try to sync state between the URL and the search input. To work around it,
|
|
|
|
|
// do a small amount of re-init on page show.
|
|
|
|
|
window.onpageshow = function(){
|
2021-04-12 23:50:18 -07:00
|
|
|
|
var qSearch = searchState.getQueryStringParams().search;
|
|
|
|
|
if (searchState.input.value === "" && qSearch) {
|
|
|
|
|
searchState.input.value = qSearch;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
search();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-01 23:48:34 -05:00
|
|
|
|
function updateCrate(ev) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
if (ev.target.value === "All crates") {
|
|
|
|
|
// If we don't remove it from the URL, it'll be picked up again by the search.
|
|
|
|
|
var params = searchState.getQueryStringParams();
|
|
|
|
|
var query = searchState.input.value.trim();
|
|
|
|
|
if (!history.state && !params.search) {
|
|
|
|
|
history.pushState(null, "", buildUrl(query, null));
|
|
|
|
|
} else {
|
|
|
|
|
history.replaceState(null, "", buildUrl(query, null));
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-01 23:48:34 -05:00
|
|
|
|
// In case you "cut" the entry from the search input, then change the crate filter
|
|
|
|
|
// before paste back the previous search, you get the old search results without
|
|
|
|
|
// the filter. To prevent this, we need to remove the previous results.
|
|
|
|
|
currentResults = null;
|
|
|
|
|
search(undefined, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
|
searchWords = buildIndex(rawSearchIndex);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
registerSearchEvents();
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
|
|
|
|
function runSearchIfNeeded() {
|
|
|
|
|
// If there's a search term in the URL, execute the search now.
|
|
|
|
|
if (searchState.getQueryStringParams().search) {
|
|
|
|
|
search();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
|
|
|
|
runSearchIfNeeded();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
|
2021-04-13 14:59:54 -07:00
|
|
|
|
if (window.searchIndex !== undefined) {
|
2021-05-11 11:47:39 +02:00
|
|
|
|
initSearch(window.searchIndex);
|
2021-04-13 14:59:54 -07:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
})();
|