2022-05-15 21:09:55 -07:00
|
|
|
|
/* global addClass, getNakedUrl, getSettingValue */
|
|
|
|
|
/* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi, exports */
|
2021-05-14 13:56:15 +02:00
|
|
|
|
|
2022-05-02 14:32:56 +02:00
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
(function() {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// This mapping table should match the discriminants of
|
2022-03-29 19:30:54 +02:00
|
|
|
|
// `rustdoc::formats::item_type::ItemType` type in Rust.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const itemTypes = [
|
2021-12-08 17:31:56 +01:00
|
|
|
|
"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
|
|
|
|
|
2023-04-22 16:00:20 +02:00
|
|
|
|
const longItemTypes = [
|
|
|
|
|
"module",
|
|
|
|
|
"extern crate",
|
|
|
|
|
"re-export",
|
|
|
|
|
"struct",
|
|
|
|
|
"enum",
|
|
|
|
|
"function",
|
|
|
|
|
"type alias",
|
|
|
|
|
"static",
|
|
|
|
|
"trait",
|
|
|
|
|
"",
|
|
|
|
|
"trait method",
|
|
|
|
|
"method",
|
|
|
|
|
"struct field",
|
|
|
|
|
"enum variant",
|
|
|
|
|
"macro",
|
|
|
|
|
"primitive type",
|
2023-06-23 09:51:53 -07:00
|
|
|
|
"assoc type",
|
2023-04-22 16:00:20 +02:00
|
|
|
|
"constant",
|
2023-06-23 09:51:53 -07:00
|
|
|
|
"assoc const",
|
2023-04-22 16:00:20 +02:00
|
|
|
|
"union",
|
|
|
|
|
"foreign type",
|
|
|
|
|
"keyword",
|
|
|
|
|
"existential type",
|
|
|
|
|
"attribute macro",
|
|
|
|
|
"derive macro",
|
|
|
|
|
"trait alias",
|
|
|
|
|
];
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// used for special search precedence
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const TY_PRIMITIVE = itemTypes.indexOf("primitive");
|
|
|
|
|
const TY_KEYWORD = itemTypes.indexOf("keyword");
|
2022-05-15 21:09:55 -07:00
|
|
|
|
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";
|
|
|
|
|
|
|
|
|
|
function hasOwnPropertyRustdoc(obj, property) {
|
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, property);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// In the search display, allows to switch between tabs.
|
|
|
|
|
function printTab(nb) {
|
2022-05-02 15:50:01 -07:00
|
|
|
|
let iter = 0;
|
|
|
|
|
let foundCurrentTab = false;
|
|
|
|
|
let foundCurrentResultSet = false;
|
2022-12-22 15:52:34 -07:00
|
|
|
|
onEachLazy(document.getElementById("search-tabs").childNodes, elem => {
|
2022-05-02 15:50:01 -07:00
|
|
|
|
if (nb === iter) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
addClass(elem, "selected");
|
2022-05-02 15:50:01 -07:00
|
|
|
|
foundCurrentTab = true;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
|
|
|
|
removeClass(elem, "selected");
|
|
|
|
|
}
|
2022-05-02 15:50:01 -07:00
|
|
|
|
iter += 1;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
});
|
2023-04-15 11:53:50 -07:00
|
|
|
|
const isTypeSearch = (nb > 0 || iter === 1);
|
2022-05-02 15:50:01 -07:00
|
|
|
|
iter = 0;
|
2022-05-03 12:03:17 +08:00
|
|
|
|
onEachLazy(document.getElementById("results").childNodes, elem => {
|
2022-05-02 15:50:01 -07:00
|
|
|
|
if (nb === iter) {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
addClass(elem, "active");
|
2022-05-02 15:50:01 -07:00
|
|
|
|
foundCurrentResultSet = true;
|
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
|
|
|
|
}
|
2022-05-02 15:50:01 -07:00
|
|
|
|
iter += 1;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
});
|
2022-05-02 15:50:01 -07:00
|
|
|
|
if (foundCurrentTab && foundCurrentResultSet) {
|
|
|
|
|
searchState.currentTab = nb;
|
2023-04-15 11:53:50 -07:00
|
|
|
|
// Corrections only kick in on type-based searches.
|
|
|
|
|
const correctionsElem = document.getElementsByClassName("search-corrections");
|
|
|
|
|
if (isTypeSearch) {
|
|
|
|
|
removeClass(correctionsElem[0], "hidden");
|
|
|
|
|
} else {
|
|
|
|
|
addClass(correctionsElem[0], "hidden");
|
|
|
|
|
}
|
2022-05-27 22:30:19 +02:00
|
|
|
|
} else if (nb !== 0) {
|
2022-05-02 15:50:01 -07:00
|
|
|
|
printTab(0);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-03-10 18:18:43 -07:00
|
|
|
|
* The [edit distance] is a metric for measuring the difference between two strings.
|
|
|
|
|
*
|
|
|
|
|
* [edit distance]: https://en.wikipedia.org/wiki/Edit_distance
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This function was translated, mostly line-for-line, from
|
|
|
|
|
* https://github.com/rust-lang/rust/blob/ff4b772f805ec1e/compiler/rustc_span/src/edit_distance.rs
|
|
|
|
|
*
|
|
|
|
|
* The current implementation is the restricted Damerau-Levenshtein algorithm. It is restricted
|
|
|
|
|
* because it does not permit modifying characters that have already been transposed. The specific
|
|
|
|
|
* algorithm should not matter to the caller of the methods, which is why it is not noted in the
|
|
|
|
|
* documentation.
|
|
|
|
|
*/
|
2023-03-11 20:36:43 -07:00
|
|
|
|
const editDistanceState = {
|
|
|
|
|
current: [],
|
|
|
|
|
prev: [],
|
|
|
|
|
prevPrev: [],
|
|
|
|
|
calculate: function calculate(a, b, limit) {
|
|
|
|
|
// Ensure that `b` is the shorter string, minimizing memory use.
|
|
|
|
|
if (a.length < b.length) {
|
|
|
|
|
const aTmp = a;
|
|
|
|
|
a = b;
|
|
|
|
|
b = aTmp;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
const minDist = a.length - b.length;
|
|
|
|
|
// If we know the limit will be exceeded, we can return early.
|
|
|
|
|
if (minDist > limit) {
|
|
|
|
|
return limit + 1;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
// Strip common prefix.
|
|
|
|
|
// We know that `b` is the shorter string, so we don't need to check
|
|
|
|
|
// `a.length`.
|
|
|
|
|
while (b.length > 0 && b[0] === a[0]) {
|
|
|
|
|
a = a.substring(1);
|
|
|
|
|
b = b.substring(1);
|
|
|
|
|
}
|
|
|
|
|
// Strip common suffix.
|
|
|
|
|
while (b.length > 0 && b[b.length - 1] === a[a.length - 1]) {
|
|
|
|
|
a = a.substring(0, a.length - 1);
|
|
|
|
|
b = b.substring(0, b.length - 1);
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
// If either string is empty, the distance is the length of the other.
|
|
|
|
|
// We know that `b` is the shorter string, so we don't need to check `a`.
|
|
|
|
|
if (b.length === 0) {
|
|
|
|
|
return minDist;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
const aLength = a.length;
|
|
|
|
|
const bLength = b.length;
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
for (let i = 0; i <= bLength; ++i) {
|
|
|
|
|
this.current[i] = 0;
|
|
|
|
|
this.prev[i] = i;
|
|
|
|
|
this.prevPrev[i] = Number.MAX_VALUE;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
// row by row
|
|
|
|
|
for (let i = 1; i <= aLength; ++i) {
|
|
|
|
|
this.current[0] = i;
|
|
|
|
|
const aIdx = i - 1;
|
|
|
|
|
|
|
|
|
|
// column by column
|
|
|
|
|
for (let j = 1; j <= bLength; ++j) {
|
|
|
|
|
const bIdx = j - 1;
|
|
|
|
|
|
|
|
|
|
// There is no cost to substitute a character with itself.
|
|
|
|
|
const substitutionCost = a[aIdx] === b[bIdx] ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
this.current[j] = Math.min(
|
|
|
|
|
// deletion
|
|
|
|
|
this.prev[j] + 1,
|
|
|
|
|
// insertion
|
|
|
|
|
this.current[j - 1] + 1,
|
|
|
|
|
// substitution
|
|
|
|
|
this.prev[j - 1] + substitutionCost
|
2023-03-10 18:18:43 -07:00
|
|
|
|
);
|
2023-03-11 20:36:43 -07:00
|
|
|
|
|
|
|
|
|
if ((i > 1) && (j > 1) && (a[aIdx] === b[bIdx - 1]) && (a[aIdx - 1] === b[bIdx])) {
|
|
|
|
|
// transposition
|
|
|
|
|
this.current[j] = Math.min(
|
|
|
|
|
this.current[j],
|
|
|
|
|
this.prevPrev[j - 2] + 1
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-03-11 20:36:43 -07:00
|
|
|
|
|
|
|
|
|
// Rotate the buffers, reusing the memory
|
|
|
|
|
const prevPrevTmp = this.prevPrev;
|
|
|
|
|
this.prevPrev = this.prev;
|
|
|
|
|
this.prev = this.current;
|
|
|
|
|
this.current = prevPrevTmp;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
// `prev` because we already rotated the buffers.
|
|
|
|
|
const distance = this.prev[bLength];
|
|
|
|
|
return distance <= limit ? distance : (limit + 1);
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-03-10 18:18:43 -07:00
|
|
|
|
|
2023-03-11 20:36:43 -07:00
|
|
|
|
function editDistance(a, b, limit) {
|
|
|
|
|
return editDistanceState.calculate(a, b, limit);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
function initSearch(rawSearchIndex) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const MAX_RESULTS = 200;
|
|
|
|
|
const NO_TYPE_FILTER = -1;
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
|
|
|
|
* @type {Array<Row>}
|
|
|
|
|
*/
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let searchIndex;
|
|
|
|
|
let currentResults;
|
2023-04-15 11:53:50 -07:00
|
|
|
|
/**
|
|
|
|
|
* Map from normalized type names to integers. Used to make type search
|
|
|
|
|
* more efficient.
|
|
|
|
|
*
|
|
|
|
|
* @type {Map<string, integer>}
|
|
|
|
|
*/
|
|
|
|
|
let typeNameIdMap;
|
2023-04-13 16:30:02 -07:00
|
|
|
|
const ALIASES = new Map();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2023-05-25 12:58:36 -07:00
|
|
|
|
/**
|
|
|
|
|
* Special type name IDs for searching by array.
|
|
|
|
|
*/
|
|
|
|
|
let typeNameIdOfArray;
|
|
|
|
|
/**
|
|
|
|
|
* Special type name IDs for searching by slice.
|
|
|
|
|
*/
|
|
|
|
|
let typeNameIdOfSlice;
|
|
|
|
|
/**
|
|
|
|
|
* Special type name IDs for searching by both array and slice (`[]` syntax).
|
|
|
|
|
*/
|
|
|
|
|
let typeNameIdOfArrayOrSlice;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add an item to the type Name->ID map, or, if one already exists, use it.
|
|
|
|
|
* Returns the number. If name is "" or null, return -1 (pure generic).
|
|
|
|
|
*
|
|
|
|
|
* This is effectively string interning, so that function matching can be
|
|
|
|
|
* done more quickly. Two types with the same name but different item kinds
|
|
|
|
|
* get the same ID.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} name
|
|
|
|
|
*
|
|
|
|
|
* @returns {integer}
|
|
|
|
|
*/
|
|
|
|
|
function buildTypeMapIndex(name) {
|
|
|
|
|
if (name === "" || name === null) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeNameIdMap.has(name)) {
|
|
|
|
|
return typeNameIdMap.get(name);
|
|
|
|
|
} else {
|
|
|
|
|
const id = typeNameIdMap.size;
|
|
|
|
|
typeNameIdMap.set(name, id);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
function isWhitespace(c) {
|
|
|
|
|
return " \t\n\r".indexOf(c) !== -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isSpecialStartCharacter(c) {
|
2022-02-09 20:56:37 +01:00
|
|
|
|
return "<\"".indexOf(c) !== -1;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 15:12:46 +01:00
|
|
|
|
function isEndCharacter(c) {
|
2023-05-25 12:58:36 -07:00
|
|
|
|
return ",>-]".indexOf(c) !== -1;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 15:12:46 +01:00
|
|
|
|
function isStopCharacter(c) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
return isEndCharacter(c);
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 20:56:37 +01:00
|
|
|
|
function isErrorCharacter(c) {
|
|
|
|
|
return "()".indexOf(c) !== -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
function itemTypeFromName(typename) {
|
2023-02-11 18:03:06 +01:00
|
|
|
|
const index = itemTypes.findIndex(i => i === typename);
|
|
|
|
|
if (index < 0) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unknown type filter ", typename];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2023-02-11 18:03:06 +01:00
|
|
|
|
return index;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If we encounter a `"`, then we try to extract the string from it until we find another `"`.
|
|
|
|
|
*
|
|
|
|
|
* This function will throw an error in the following cases:
|
|
|
|
|
* * There is already another string element.
|
|
|
|
|
* * We are parsing a generic argument.
|
|
|
|
|
* * There is more than one element.
|
|
|
|
|
* * There is no closing `"`.
|
|
|
|
|
*
|
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
* @param {boolean} isInGenerics
|
|
|
|
|
*/
|
|
|
|
|
function getStringElem(query, parserState, isInGenerics) {
|
|
|
|
|
if (isInGenerics) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", "\"", " in generics"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
} else if (query.literalSearch) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Cannot have more than one literal search element"];
|
2022-03-28 16:14:00 +02:00
|
|
|
|
} else if (parserState.totalElems - parserState.genericsElems > 0) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Cannot use literal search when there is more than one element"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
parserState.pos += 1;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const start = parserState.pos;
|
|
|
|
|
const end = getIdentEndPosition(parserState);
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (parserState.pos >= parserState.length) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unclosed ", "\""];
|
2022-04-16 16:33:42 +02:00
|
|
|
|
} else if (parserState.userQuery[end] !== "\"") {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", parserState.userQuery[end], " in a string element"];
|
2022-04-17 01:43:26 +02:00
|
|
|
|
} else if (start === end) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Cannot have empty string element"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
// To skip the quote at the end.
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
query.literalSearch = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the current parser position is starting with "::".
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isPathStart(parserState) {
|
2022-05-27 22:30:19 +02:00
|
|
|
|
return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) === "::";
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the current parser position is starting with "->".
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isReturnArrow(parserState) {
|
2022-05-27 22:30:19 +02:00
|
|
|
|
return parserState.userQuery.slice(parserState.pos, parserState.pos + 2) === "->";
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 17:26:32 +01:00
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the given `c` character is valid for an ident.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} c
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isIdentCharacter(c) {
|
|
|
|
|
return (
|
2022-05-07 20:18:23 +02:00
|
|
|
|
c === "_" ||
|
|
|
|
|
(c >= "0" && c <= "9") ||
|
|
|
|
|
(c >= "a" && c <= "z") ||
|
|
|
|
|
(c >= "A" && c <= "Z"));
|
2022-03-23 17:26:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 11:32:03 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the given `c` character is a separator.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} c
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isSeparatorCharacter(c) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
return c === ",";
|
2022-04-17 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 13:23:38 +02:00
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the given `c` character is a path separator. For example
|
|
|
|
|
* `:` in `a::b` or a whitespace in `a b`.
|
2022-04-17 12:05:31 +02:00
|
|
|
|
*
|
|
|
|
|
* @param {string} c
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
2023-06-12 13:23:38 +02:00
|
|
|
|
function isPathSeparator(c) {
|
|
|
|
|
return c === ":" || isWhitespace(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the previous character is `lookingFor`.
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
* @param {String} lookingFor
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function prevIs(parserState, lookingFor) {
|
|
|
|
|
let pos = parserState.pos;
|
|
|
|
|
while (pos > 0) {
|
|
|
|
|
const c = parserState.userQuery[pos - 1];
|
|
|
|
|
if (c === lookingFor) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (!isWhitespace(c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pos -= 1;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns `true` if the last element in the `elems` argument has generics.
|
|
|
|
|
*
|
|
|
|
|
* @param {Array<QueryElement>} elems
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isLastElemGeneric(elems, parserState) {
|
|
|
|
|
return (elems.length > 0 && elems[elems.length - 1].generics.length > 0) ||
|
|
|
|
|
prevIs(parserState, ">");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Increase current parser position until it doesn't find a whitespace anymore.
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
*/
|
|
|
|
|
function skipWhitespace(parserState) {
|
|
|
|
|
while (parserState.pos < parserState.userQuery.length) {
|
|
|
|
|
const c = parserState.userQuery[parserState.pos];
|
|
|
|
|
if (!isWhitespace(c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
}
|
2022-03-30 11:32:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
* @param {string} name - Name of the query element.
|
|
|
|
|
* @param {Array<QueryElement>} generics - List of generics of this query element.
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
|
|
|
|
* @return {QueryElement} - The newly created `QueryElement`.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
*/
|
2022-03-28 16:14:00 +02:00
|
|
|
|
function createQueryElement(query, parserState, name, generics, isInGenerics) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
const path = name.trim();
|
|
|
|
|
if (path.length === 0 && generics.length === 0) {
|
|
|
|
|
throw ["Unexpected ", parserState.userQuery[parserState.pos]];
|
|
|
|
|
} else if (path === "*") {
|
|
|
|
|
throw ["Unexpected ", "*"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (query.literalSearch && parserState.totalElems - parserState.genericsElems > 0) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
throw ["Cannot have more than one element if you use quotes"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2023-06-12 14:56:54 -07:00
|
|
|
|
const typeFilter = parserState.typeFilter;
|
|
|
|
|
parserState.typeFilter = null;
|
|
|
|
|
if (name === "!") {
|
|
|
|
|
if (typeFilter !== null && typeFilter !== "primitive") {
|
|
|
|
|
throw [
|
|
|
|
|
"Invalid search type: primitive never type ",
|
|
|
|
|
"!",
|
|
|
|
|
" and ",
|
|
|
|
|
typeFilter,
|
|
|
|
|
" both specified",
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
if (generics.length !== 0) {
|
|
|
|
|
throw [
|
|
|
|
|
"Never type ",
|
|
|
|
|
"!",
|
|
|
|
|
" does not accept generic parameters",
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
name: "never",
|
|
|
|
|
id: -1,
|
|
|
|
|
fullPath: ["never"],
|
|
|
|
|
pathWithoutLast: [],
|
|
|
|
|
pathLast: "never",
|
|
|
|
|
generics: [],
|
|
|
|
|
typeFilter: "primitive",
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-06-12 13:23:38 +02:00
|
|
|
|
if (path.startsWith("::")) {
|
|
|
|
|
throw ["Paths cannot start with ", "::"];
|
|
|
|
|
} else if (path.endsWith("::")) {
|
|
|
|
|
throw ["Paths cannot end with ", "::"];
|
|
|
|
|
} else if (path.includes("::::")) {
|
|
|
|
|
throw ["Unexpected ", "::::"];
|
|
|
|
|
} else if (path.includes(" ::")) {
|
|
|
|
|
throw ["Unexpected ", " ::"];
|
|
|
|
|
} else if (path.includes(":: ")) {
|
|
|
|
|
throw ["Unexpected ", ":: "];
|
|
|
|
|
}
|
|
|
|
|
const pathSegments = path.split(/::|\s+/);
|
2022-03-20 15:03:17 +01:00
|
|
|
|
// In case we only have something like `<p>`, there is no name.
|
|
|
|
|
if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
if (generics.length > 0 || prevIs(parserState, ">")) {
|
|
|
|
|
throw ["Found generics without a path"];
|
|
|
|
|
} else {
|
|
|
|
|
throw ["Unexpected ", parserState.userQuery[parserState.pos]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const [i, pathSegment] of pathSegments.entries()) {
|
|
|
|
|
if (pathSegment === "!") {
|
|
|
|
|
if (i !== 0) {
|
|
|
|
|
throw ["Never type ", "!", " is not associated item"];
|
|
|
|
|
}
|
|
|
|
|
pathSegments[i] = "never";
|
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2022-03-20 15:03:17 +01:00
|
|
|
|
parserState.totalElems += 1;
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (isInGenerics) {
|
|
|
|
|
parserState.genericsElems += 1;
|
|
|
|
|
}
|
2022-03-20 15:03:17 +01:00
|
|
|
|
return {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
name: name.trim(),
|
2023-04-15 11:53:50 -07:00
|
|
|
|
id: -1,
|
2022-01-04 15:44:00 +01:00
|
|
|
|
fullPath: pathSegments,
|
|
|
|
|
pathWithoutLast: pathSegments.slice(0, pathSegments.length - 1),
|
|
|
|
|
pathLast: pathSegments[pathSegments.length - 1],
|
|
|
|
|
generics: generics,
|
2023-02-28 18:17:59 -07:00
|
|
|
|
typeFilter,
|
2022-03-20 15:03:17 +01:00
|
|
|
|
};
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 16:33:42 +02:00
|
|
|
|
/**
|
|
|
|
|
* This function goes through all characters until it reaches an invalid ident character or the
|
|
|
|
|
* end of the query. It returns the position of the last character of the ident.
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
*
|
|
|
|
|
* @return {integer}
|
|
|
|
|
*/
|
|
|
|
|
function getIdentEndPosition(parserState) {
|
2023-02-16 15:32:38 -07:00
|
|
|
|
const start = parserState.pos;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let end = parserState.pos;
|
2023-02-16 15:32:38 -07:00
|
|
|
|
let foundExclamation = -1;
|
2022-04-16 16:33:42 +02:00
|
|
|
|
while (parserState.pos < parserState.length) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const c = parserState.userQuery[parserState.pos];
|
2022-04-16 16:33:42 +02:00
|
|
|
|
if (!isIdentCharacter(c)) {
|
2022-04-26 13:58:23 +02:00
|
|
|
|
if (c === "!") {
|
2023-02-16 15:32:38 -07:00
|
|
|
|
if (foundExclamation !== -1) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Cannot have more than one ", "!", " in an ident"];
|
2022-04-26 13:58:23 +02:00
|
|
|
|
} else if (parserState.pos + 1 < parserState.length &&
|
2022-05-14 13:50:52 +02:00
|
|
|
|
isIdentCharacter(parserState.userQuery[parserState.pos + 1])
|
|
|
|
|
) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", "!", ": it can only be at the end of an ident"];
|
2022-04-26 13:58:23 +02:00
|
|
|
|
}
|
2023-02-16 15:32:38 -07:00
|
|
|
|
foundExclamation = parserState.pos;
|
2022-04-26 13:58:23 +02:00
|
|
|
|
} else if (isErrorCharacter(c)) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", c];
|
2023-06-12 13:23:38 +02:00
|
|
|
|
} else if (isPathSeparator(c)) {
|
|
|
|
|
if (c === ":") {
|
|
|
|
|
if (!isPathStart(parserState)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
// Skip current ":".
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
} else {
|
|
|
|
|
while (parserState.pos + 1 < parserState.length) {
|
|
|
|
|
const next_c = parserState.userQuery[parserState.pos + 1];
|
|
|
|
|
if (!isWhitespace(next_c)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
}
|
2022-04-16 16:33:42 +02:00
|
|
|
|
}
|
2023-02-16 15:32:38 -07:00
|
|
|
|
if (foundExclamation !== -1) {
|
2023-06-12 14:56:54 -07:00
|
|
|
|
if (foundExclamation !== start &&
|
|
|
|
|
isIdentCharacter(parserState.userQuery[foundExclamation - 1])
|
|
|
|
|
) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Cannot have associated items in macros"];
|
2023-02-16 15:32:38 -07:00
|
|
|
|
} else {
|
|
|
|
|
// while the never type has no associated macros, we still
|
|
|
|
|
// can parse a path like that
|
|
|
|
|
foundExclamation = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-12 13:23:38 +02:00
|
|
|
|
} else if (
|
|
|
|
|
c === "[" ||
|
|
|
|
|
isStopCharacter(c) ||
|
|
|
|
|
isSpecialStartCharacter(c) ||
|
|
|
|
|
isSeparatorCharacter(c)
|
|
|
|
|
) {
|
|
|
|
|
break;
|
2022-04-16 16:33:42 +02:00
|
|
|
|
} else {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", c];
|
2022-04-16 16:33:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
end = parserState.pos;
|
|
|
|
|
}
|
2023-02-16 15:32:38 -07:00
|
|
|
|
// if start == end - 1, we got the never type
|
2023-06-12 14:56:54 -07:00
|
|
|
|
if (foundExclamation !== -1 &&
|
|
|
|
|
foundExclamation !== start &&
|
|
|
|
|
isIdentCharacter(parserState.userQuery[foundExclamation - 1])
|
|
|
|
|
) {
|
2023-02-16 15:32:38 -07:00
|
|
|
|
if (parserState.typeFilter === null) {
|
|
|
|
|
parserState.typeFilter = "macro";
|
|
|
|
|
} else if (parserState.typeFilter !== "macro") {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw [
|
|
|
|
|
"Invalid search type: macro ",
|
|
|
|
|
"!",
|
|
|
|
|
" and ",
|
|
|
|
|
parserState.typeFilter,
|
|
|
|
|
" both specified",
|
|
|
|
|
];
|
2023-02-16 15:32:38 -07:00
|
|
|
|
}
|
|
|
|
|
end = foundExclamation;
|
|
|
|
|
}
|
2022-04-16 16:33:42 +02:00
|
|
|
|
return end;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
* @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
|
|
|
|
|
* @param {boolean} isInGenerics
|
|
|
|
|
*/
|
|
|
|
|
function getNextElem(query, parserState, elems, isInGenerics) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const generics = [];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
|
2023-06-12 13:23:38 +02:00
|
|
|
|
skipWhitespace(parserState);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let start = parserState.pos;
|
|
|
|
|
let end;
|
2023-05-25 12:58:36 -07:00
|
|
|
|
if (parserState.userQuery[parserState.pos] === "[") {
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
getItemsBefore(query, parserState, generics, "]");
|
|
|
|
|
const typeFilter = parserState.typeFilter;
|
|
|
|
|
if (typeFilter !== null && typeFilter !== "primitive") {
|
|
|
|
|
throw [
|
|
|
|
|
"Invalid search type: primitive ",
|
|
|
|
|
"[]",
|
|
|
|
|
" and ",
|
|
|
|
|
typeFilter,
|
|
|
|
|
" both specified",
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
parserState.typeFilter = null;
|
|
|
|
|
parserState.totalElems += 1;
|
|
|
|
|
if (isInGenerics) {
|
|
|
|
|
parserState.genericsElems += 1;
|
|
|
|
|
}
|
|
|
|
|
elems.push({
|
|
|
|
|
name: "[]",
|
|
|
|
|
id: -1,
|
|
|
|
|
fullPath: ["[]"],
|
|
|
|
|
pathWithoutLast: [],
|
|
|
|
|
pathLast: "[]",
|
|
|
|
|
generics,
|
|
|
|
|
typeFilter: "primitive",
|
|
|
|
|
});
|
2022-01-04 15:44:00 +01:00
|
|
|
|
} else {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
const isStringElem = parserState.userQuery[start] === "\"";
|
2023-05-25 12:58:36 -07:00
|
|
|
|
// We handle the strings on their own mostly to make code easier to follow.
|
2023-06-12 13:23:38 +02:00
|
|
|
|
if (isStringElem) {
|
2023-05-25 12:58:36 -07:00
|
|
|
|
start += 1;
|
|
|
|
|
getStringElem(query, parserState, isInGenerics);
|
|
|
|
|
end = parserState.pos - 1;
|
|
|
|
|
} else {
|
|
|
|
|
end = getIdentEndPosition(parserState);
|
2022-02-09 20:56:37 +01:00
|
|
|
|
}
|
2023-05-25 12:58:36 -07:00
|
|
|
|
if (parserState.pos < parserState.length &&
|
|
|
|
|
parserState.userQuery[parserState.pos] === "<"
|
|
|
|
|
) {
|
|
|
|
|
if (start >= end) {
|
|
|
|
|
throw ["Found generics without a path"];
|
|
|
|
|
}
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
getItemsBefore(query, parserState, generics, ">");
|
|
|
|
|
}
|
2023-06-12 13:23:38 +02:00
|
|
|
|
if (isStringElem) {
|
|
|
|
|
skipWhitespace(parserState);
|
|
|
|
|
}
|
2023-05-25 12:58:36 -07:00
|
|
|
|
if (start >= end && generics.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
elems.push(
|
|
|
|
|
createQueryElement(
|
|
|
|
|
query,
|
|
|
|
|
parserState,
|
|
|
|
|
parserState.userQuery.slice(start, end),
|
|
|
|
|
generics,
|
|
|
|
|
isInGenerics
|
|
|
|
|
)
|
|
|
|
|
);
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-02-11 15:48:57 +01:00
|
|
|
|
* This function parses the next query element until it finds `endChar`, calling `getNextElem`
|
|
|
|
|
* to collect each element.
|
|
|
|
|
*
|
|
|
|
|
* If there is no `endChar`, this function will implicitly stop at the end without raising an
|
|
|
|
|
* error.
|
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
* @param {Array<QueryElement>} elems - This is where the new {QueryElement} will be added.
|
2022-02-11 15:48:57 +01:00
|
|
|
|
* @param {string} endChar - This function will stop when it'll encounter this
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* character.
|
|
|
|
|
*/
|
2022-02-11 15:48:57 +01:00
|
|
|
|
function getItemsBefore(query, parserState, elems, endChar) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let foundStopChar = true;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
let start = parserState.pos;
|
|
|
|
|
|
|
|
|
|
// If this is a generic, keep the outer item's type filter around.
|
|
|
|
|
const oldTypeFilter = parserState.typeFilter;
|
|
|
|
|
parserState.typeFilter = null;
|
2022-03-28 16:14:00 +02:00
|
|
|
|
|
2023-05-25 12:58:36 -07:00
|
|
|
|
let extra = "";
|
|
|
|
|
if (endChar === ">") {
|
|
|
|
|
extra = "<";
|
|
|
|
|
} else if (endChar === "]") {
|
|
|
|
|
extra = "[";
|
|
|
|
|
} else if (endChar === "") {
|
|
|
|
|
extra = "->";
|
|
|
|
|
} else {
|
|
|
|
|
extra = endChar;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
while (parserState.pos < parserState.length) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const c = parserState.userQuery[parserState.pos];
|
2022-02-11 15:48:57 +01:00
|
|
|
|
if (c === endChar) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
break;
|
2022-03-30 11:32:03 +02:00
|
|
|
|
} else if (isSeparatorCharacter(c)) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 1;
|
2022-03-28 16:14:00 +02:00
|
|
|
|
foundStopChar = true;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
continue;
|
2022-01-09 15:12:46 +01:00
|
|
|
|
} else if (c === ":" && isPathStart(parserState)) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", "::", ": paths cannot start with ", "::"];
|
2023-02-28 18:17:59 -07:00
|
|
|
|
} else if (c === ":") {
|
|
|
|
|
if (parserState.typeFilter !== null) {
|
|
|
|
|
throw ["Unexpected ", ":"];
|
|
|
|
|
}
|
|
|
|
|
if (elems.length === 0) {
|
|
|
|
|
throw ["Expected type filter before ", ":"];
|
|
|
|
|
} else if (query.literalSearch) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
throw ["Cannot use quotes on type filter"];
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
|
|
|
|
// The type filter doesn't count as an element since it's a modifier.
|
|
|
|
|
const typeFilterElem = elems.pop();
|
|
|
|
|
checkExtraTypeFilterCharacters(start, parserState);
|
|
|
|
|
parserState.typeFilter = typeFilterElem.name;
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
parserState.totalElems -= 1;
|
|
|
|
|
query.literalSearch = false;
|
|
|
|
|
foundStopChar = true;
|
|
|
|
|
continue;
|
|
|
|
|
} else if (isEndCharacter(c)) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", c, " after ", extra];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (!foundStopChar) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
let extra = [];
|
|
|
|
|
if (isLastElemGeneric(query.elems, parserState)) {
|
|
|
|
|
extra = [" after ", ">"];
|
|
|
|
|
} else if (prevIs(parserState, "\"")) {
|
|
|
|
|
throw ["Cannot have more than one element if you use quotes"];
|
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (endChar !== "") {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw [
|
|
|
|
|
"Expected ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
",",
|
2023-02-28 22:37:52 +01:00
|
|
|
|
" or ",
|
|
|
|
|
endChar,
|
2023-06-12 13:23:38 +02:00
|
|
|
|
...extra,
|
2023-02-28 22:37:52 +01:00
|
|
|
|
", found ",
|
|
|
|
|
c,
|
|
|
|
|
];
|
2022-03-28 16:14:00 +02:00
|
|
|
|
}
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw [
|
|
|
|
|
"Expected ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
",",
|
|
|
|
|
...extra,
|
2023-02-28 22:37:52 +01:00
|
|
|
|
", found ",
|
|
|
|
|
c,
|
|
|
|
|
];
|
2022-03-28 16:14:00 +02:00
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const posBefore = parserState.pos;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
start = parserState.pos;
|
2023-05-25 12:58:36 -07:00
|
|
|
|
getNextElem(query, parserState, elems, endChar !== "");
|
2023-02-28 18:17:59 -07:00
|
|
|
|
if (endChar !== "" && parserState.pos >= parserState.length) {
|
2023-05-25 12:58:36 -07:00
|
|
|
|
throw ["Unclosed ", extra];
|
2023-03-02 14:28:06 +01:00
|
|
|
|
}
|
2022-07-03 20:10:19 +02:00
|
|
|
|
// This case can be encountered if `getNextElem` encountered a "stop character" right
|
|
|
|
|
// from the start. For example if you have `,,` or `<>`. In this case, we simply move up
|
|
|
|
|
// the current position to continue the parsing.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (posBefore === parserState.pos) {
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
foundStopChar = false;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2023-03-02 14:28:06 +01:00
|
|
|
|
if (parserState.pos >= parserState.length && endChar !== "") {
|
2023-05-25 12:58:36 -07:00
|
|
|
|
throw ["Unclosed ", extra];
|
2023-03-02 14:28:06 +01:00
|
|
|
|
}
|
|
|
|
|
// We are either at the end of the string or on the `endChar` character, let's move forward
|
2022-01-09 15:12:46 +01:00
|
|
|
|
// in any case.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 1;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
|
|
|
|
|
parserState.typeFilter = oldTypeFilter;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 12:05:31 +02:00
|
|
|
|
/**
|
|
|
|
|
* Checks that the type filter doesn't have unwanted characters like `<>` (which are ignored
|
|
|
|
|
* if empty).
|
|
|
|
|
*
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
*/
|
2023-02-28 18:17:59 -07:00
|
|
|
|
function checkExtraTypeFilterCharacters(start, parserState) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
const query = parserState.userQuery.slice(start, parserState.pos).trim();
|
2022-04-17 12:05:31 +02:00
|
|
|
|
|
2023-06-12 13:23:38 +02:00
|
|
|
|
for (const c in query) {
|
|
|
|
|
if (!isIdentCharacter(query[c])) {
|
|
|
|
|
throw [
|
|
|
|
|
"Unexpected ",
|
|
|
|
|
query[c],
|
|
|
|
|
" in type filter (before ",
|
|
|
|
|
":",
|
|
|
|
|
")",
|
|
|
|
|
];
|
2022-04-17 12:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
/**
|
2022-02-11 15:48:57 +01:00
|
|
|
|
* Parses the provided `query` input to fill `parserState`. If it encounters an error while
|
|
|
|
|
* parsing `query`, it'll throw an error.
|
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {ParsedQuery} query
|
|
|
|
|
* @param {ParserState} parserState
|
|
|
|
|
*/
|
|
|
|
|
function parseInput(query, parserState) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let foundStopChar = true;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
let start = parserState.pos;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
|
|
|
|
|
while (parserState.pos < parserState.length) {
|
2023-02-26 16:26:59 +01:00
|
|
|
|
const c = parserState.userQuery[parserState.pos];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (isStopCharacter(c)) {
|
2022-03-28 16:14:00 +02:00
|
|
|
|
foundStopChar = true;
|
2022-03-30 11:32:03 +02:00
|
|
|
|
if (isSeparatorCharacter(c)) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
continue;
|
2022-03-20 15:03:17 +01:00
|
|
|
|
} else if (c === "-" || c === ">") {
|
|
|
|
|
if (isReturnArrow(parserState)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", c, " (did you mean ", "->", "?)"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Unexpected ", c];
|
2022-04-18 23:11:42 +02:00
|
|
|
|
} else if (c === ":" && !isPathStart(parserState)) {
|
|
|
|
|
if (parserState.typeFilter !== null) {
|
2023-06-13 14:01:01 +02:00
|
|
|
|
throw [
|
|
|
|
|
"Unexpected ",
|
|
|
|
|
":",
|
|
|
|
|
" (expected path after type filter ",
|
|
|
|
|
parserState.typeFilter + ":",
|
|
|
|
|
")",
|
|
|
|
|
];
|
2023-06-12 13:23:38 +02:00
|
|
|
|
} else if (query.elems.length === 0) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Expected type filter before ", ":"];
|
2022-04-17 12:05:31 +02:00
|
|
|
|
} else if (query.literalSearch) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
throw ["Cannot use quotes on type filter"];
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
// The type filter doesn't count as an element since it's a modifier.
|
2023-02-28 18:17:59 -07:00
|
|
|
|
const typeFilterElem = query.elems.pop();
|
|
|
|
|
checkExtraTypeFilterCharacters(start, parserState);
|
|
|
|
|
parserState.typeFilter = typeFilterElem.name;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 1;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
parserState.totalElems -= 1;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
query.literalSearch = false;
|
2022-03-28 16:14:00 +02:00
|
|
|
|
foundStopChar = true;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
continue;
|
2023-06-13 14:01:01 +02:00
|
|
|
|
} else if (isWhitespace(c)) {
|
|
|
|
|
skipWhitespace(parserState);
|
|
|
|
|
continue;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (!foundStopChar) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
let extra = "";
|
|
|
|
|
if (isLastElemGeneric(query.elems, parserState)) {
|
|
|
|
|
extra = [" after ", ">"];
|
|
|
|
|
} else if (prevIs(parserState, "\"")) {
|
|
|
|
|
throw ["Cannot have more than one element if you use quotes"];
|
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
if (parserState.typeFilter !== null) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw [
|
|
|
|
|
"Expected ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
",",
|
2023-02-28 22:37:52 +01:00
|
|
|
|
" or ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
"->",
|
|
|
|
|
...extra,
|
2023-02-28 22:37:52 +01:00
|
|
|
|
", found ",
|
|
|
|
|
c,
|
|
|
|
|
];
|
2022-03-28 16:14:00 +02:00
|
|
|
|
}
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw [
|
|
|
|
|
"Expected ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
",",
|
2023-02-28 22:37:52 +01:00
|
|
|
|
", ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
":",
|
2023-02-28 22:37:52 +01:00
|
|
|
|
" or ",
|
2023-06-12 13:23:38 +02:00
|
|
|
|
"->",
|
|
|
|
|
...extra,
|
2023-02-28 22:37:52 +01:00
|
|
|
|
", found ",
|
|
|
|
|
c,
|
|
|
|
|
];
|
2022-03-28 16:14:00 +02:00
|
|
|
|
}
|
2023-02-26 16:26:59 +01:00
|
|
|
|
const before = query.elems.length;
|
2023-02-28 18:17:59 -07:00
|
|
|
|
start = parserState.pos;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
getNextElem(query, parserState, query.elems, false);
|
|
|
|
|
if (query.elems.length === before) {
|
2022-04-18 23:11:42 +02:00
|
|
|
|
// Nothing was added, weird... Let's increase the position to not remain stuck.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
}
|
2022-03-28 16:14:00 +02:00
|
|
|
|
foundStopChar = false;
|
2022-01-04 15:44:00 +01:00
|
|
|
|
}
|
2023-02-28 18:17:59 -07:00
|
|
|
|
if (parserState.typeFilter !== null) {
|
2023-06-12 13:23:38 +02:00
|
|
|
|
throw [
|
|
|
|
|
"Unexpected ",
|
|
|
|
|
":",
|
|
|
|
|
" (expected path after type filter ",
|
|
|
|
|
parserState.typeFilter + ":",
|
|
|
|
|
")",
|
|
|
|
|
];
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
while (parserState.pos < parserState.length) {
|
2022-02-09 20:56:37 +01:00
|
|
|
|
if (isReturnArrow(parserState)) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parserState.pos += 2;
|
2023-06-12 13:23:38 +02:00
|
|
|
|
skipWhitespace(parserState);
|
2022-01-04 15:44:00 +01:00
|
|
|
|
// Get returned elements.
|
|
|
|
|
getItemsBefore(query, parserState, query.returned, "");
|
|
|
|
|
// Nothing can come afterward!
|
2022-03-28 15:08:47 +02:00
|
|
|
|
if (query.returned.length === 0) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
throw ["Expected at least one item after ", "->"];
|
2022-03-28 15:08:47 +02:00
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
parserState.pos += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes the user search input and returns an empty `ParsedQuery`.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} userQuery
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @return {ParsedQuery}
|
|
|
|
|
*/
|
|
|
|
|
function newParsedQuery(userQuery) {
|
|
|
|
|
return {
|
|
|
|
|
original: userQuery,
|
|
|
|
|
userQuery: userQuery.toLowerCase(),
|
|
|
|
|
elems: [],
|
|
|
|
|
returned: [],
|
|
|
|
|
// Total number of "top" elements (does not include generics).
|
|
|
|
|
foundElems: 0,
|
|
|
|
|
literalSearch: false,
|
|
|
|
|
error: null,
|
2023-04-15 11:53:50 -07:00
|
|
|
|
correction: null,
|
2022-01-04 15:44:00 +01: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).
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-10 14:57:43 +01:00
|
|
|
|
* @return {string}
|
|
|
|
|
*/
|
|
|
|
|
function buildUrl(search, filterCrates) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let extra = "?search=" + encodeURIComponent(search);
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
|
|
|
|
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() {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const elem = document.getElementById("crate-search");
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
|
|
|
|
if (elem &&
|
2022-07-03 20:10:19 +02:00
|
|
|
|
elem.value !== "all crates" &&
|
2022-05-14 13:50:52 +02:00
|
|
|
|
hasOwnPropertyRustdoc(rawSearchIndex, elem.value)
|
|
|
|
|
) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
return elem.value;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
/**
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* Parses the query.
|
2022-01-02 19:12:28 +01:00
|
|
|
|
*
|
|
|
|
|
* The supported syntax by this parser is as follow:
|
|
|
|
|
*
|
2023-02-25 08:20:53 -07:00
|
|
|
|
* ident = *(ALPHA / DIGIT / "_")
|
2023-06-12 13:23:38 +02:00
|
|
|
|
* path = ident *(DOUBLE-COLON/{WS} ident) [!]
|
2023-05-25 12:58:36 -07:00
|
|
|
|
* slice = OPEN-SQUARE-BRACKET [ nonempty-arg-list ] CLOSE-SQUARE-BRACKET
|
|
|
|
|
* arg = [type-filter *WS COLON *WS] (path [generics] / slice)
|
2023-06-13 14:01:01 +02:00
|
|
|
|
* type-sep = *WS COMMA *(COMMA)
|
2022-03-28 16:14:00 +02:00
|
|
|
|
* nonempty-arg-list = *(type-sep) arg *(type-sep arg) *(type-sep)
|
2023-03-30 21:53:11 -07:00
|
|
|
|
* generics = OPEN-ANGLE-BRACKET [ nonempty-arg-list ] *(type-sep)
|
|
|
|
|
* CLOSE-ANGLE-BRACKET
|
2022-03-28 16:14:00 +02:00
|
|
|
|
* return-args = RETURN-ARROW *(type-sep) nonempty-arg-list
|
2022-01-02 19:12:28 +01:00
|
|
|
|
*
|
2022-03-28 16:14:00 +02:00
|
|
|
|
* exact-search = [type-filter *WS COLON] [ RETURN-ARROW ] *WS QUOTE ident QUOTE [ generics ]
|
2023-02-28 18:17:59 -07:00
|
|
|
|
* type-search = [ nonempty-arg-list ] [ return-args ]
|
2022-01-02 19:12:28 +01:00
|
|
|
|
*
|
2022-03-28 16:14:00 +02:00
|
|
|
|
* query = *WS (exact-search / type-search) *WS
|
2022-01-02 19:12:28 +01:00
|
|
|
|
*
|
|
|
|
|
* type-filter = (
|
|
|
|
|
* "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")
|
|
|
|
|
*
|
|
|
|
|
* OPEN-ANGLE-BRACKET = "<"
|
|
|
|
|
* CLOSE-ANGLE-BRACKET = ">"
|
2023-05-25 12:58:36 -07:00
|
|
|
|
* OPEN-SQUARE-BRACKET = "["
|
|
|
|
|
* CLOSE-SQUARE-BRACKET = "]"
|
2022-01-02 19:12:28 +01:00
|
|
|
|
* COLON = ":"
|
|
|
|
|
* DOUBLE-COLON = "::"
|
|
|
|
|
* QUOTE = %x22
|
|
|
|
|
* COMMA = ","
|
|
|
|
|
* RETURN-ARROW = "->"
|
|
|
|
|
*
|
|
|
|
|
* ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
|
|
|
|
|
* DIGIT = %x30-39
|
|
|
|
|
* WS = %x09 / " "
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
|
|
|
|
* @param {string} val - The user query
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2021-12-20 15:42:08 +01:00
|
|
|
|
* @return {ParsedQuery} - The parsed query
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
2022-01-03 16:43:30 +01:00
|
|
|
|
function parseQuery(userQuery) {
|
2023-02-28 18:17:59 -07:00
|
|
|
|
function convertTypeFilterOnElem(elem) {
|
|
|
|
|
if (elem.typeFilter !== null) {
|
|
|
|
|
let typeFilter = elem.typeFilter;
|
|
|
|
|
if (typeFilter === "const") {
|
|
|
|
|
typeFilter = "constant";
|
|
|
|
|
}
|
|
|
|
|
elem.typeFilter = itemTypeFromName(typeFilter);
|
|
|
|
|
} else {
|
|
|
|
|
elem.typeFilter = NO_TYPE_FILTER;
|
|
|
|
|
}
|
|
|
|
|
for (const elem2 of elem.generics) {
|
|
|
|
|
convertTypeFilterOnElem(elem2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-03 16:43:30 +01:00
|
|
|
|
userQuery = userQuery.trim();
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const parserState = {
|
2022-01-03 16:43:30 +01:00
|
|
|
|
length: userQuery.length,
|
2021-12-20 15:42:08 +01:00
|
|
|
|
pos: 0,
|
2022-01-03 14:11:54 +01:00
|
|
|
|
// Total number of elements (includes generics).
|
|
|
|
|
totalElems: 0,
|
2022-03-28 16:14:00 +02:00
|
|
|
|
genericsElems: 0,
|
2022-01-04 15:44:00 +01:00
|
|
|
|
typeFilter: null,
|
|
|
|
|
userQuery: userQuery.toLowerCase(),
|
2021-12-20 15:42:08 +01:00
|
|
|
|
};
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let query = newParsedQuery(userQuery);
|
2022-01-04 15:44:00 +01:00
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
try {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parseInput(query, parserState);
|
2023-02-28 18:17:59 -07:00
|
|
|
|
for (const elem of query.elems) {
|
|
|
|
|
convertTypeFilterOnElem(elem);
|
|
|
|
|
}
|
|
|
|
|
for (const elem of query.returned) {
|
|
|
|
|
convertTypeFilterOnElem(elem);
|
2022-01-09 15:12:46 +01:00
|
|
|
|
}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
} catch (err) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
query = newParsedQuery(userQuery);
|
2023-02-28 22:37:52 +01:00
|
|
|
|
query.error = err;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
|
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.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
query.literalSearch = parserState.totalElems > 1;
|
2022-01-03 14:11:54 +01:00
|
|
|
|
}
|
2022-02-09 20:56:37 +01:00
|
|
|
|
query.foundElems = query.elems.length + query.returned.length;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-01-03 14:11:54 +01:00
|
|
|
|
/**
|
|
|
|
|
* Creates the query results.
|
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {Array<Result>} results_in_args
|
|
|
|
|
* @param {Array<Result>} results_returned
|
2023-04-14 13:32:44 -07:00
|
|
|
|
* @param {Array<Result>} results_others
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {ParsedQuery} parsedQuery
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @return {ResultsTable}
|
2022-01-03 14:11:54 +01:00
|
|
|
|
*/
|
2022-01-04 15:44:00 +01:00
|
|
|
|
function createQueryResults(results_in_args, results_returned, results_others, parsedQuery) {
|
2022-01-03 14:11:54 +01:00
|
|
|
|
return {
|
|
|
|
|
"in_args": results_in_args,
|
|
|
|
|
"returned": results_returned,
|
|
|
|
|
"others": results_others,
|
2022-01-04 15:44:00 +01:00
|
|
|
|
"query": parsedQuery,
|
2022-01-03 14:11:54 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* Executes the parsed query and builds a {ResultsTable}.
|
2022-01-03 14:11:54 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {ParsedQuery} parsedQuery - The parsed user query
|
|
|
|
|
* @param {Object} searchWords - The list of search words to query against
|
|
|
|
|
* @param {Object} [filterCrates] - Crate to search in if defined
|
2022-05-15 21:09:55 -07:00
|
|
|
|
* @param {Object} [currentCrate] - Current crate, to rank results from this crate higher
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @return {ResultsTable}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
2022-05-15 21:09:55 -07:00
|
|
|
|
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
|
2023-04-13 17:05:12 -07:00
|
|
|
|
const results_others = new Map(), results_in_args = new Map(),
|
|
|
|
|
results_returned = new Map();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2023-04-13 17:05:12 -07:00
|
|
|
|
/**
|
|
|
|
|
* Add extra data to result objects, and filter items that have been
|
|
|
|
|
* marked for removal.
|
|
|
|
|
*
|
|
|
|
|
* @param {[ResultObject]} results
|
|
|
|
|
* @returns {[ResultObject]}
|
|
|
|
|
*/
|
2021-05-09 13:49:22 -07:00
|
|
|
|
function transformResults(results) {
|
2023-04-13 16:51:01 -07:00
|
|
|
|
const duplicates = new Set();
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const out = [];
|
2021-12-20 18:01:04 +01:00
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const result of results) {
|
2021-12-20 18:01:04 +01:00
|
|
|
|
if (result.id > -1) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const obj = searchIndex[result.id];
|
2023-03-10 18:18:43 -07:00
|
|
|
|
obj.dist = result.dist;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const res = buildHrefAndPath(obj);
|
2021-05-09 13:49:22 -07:00
|
|
|
|
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
|
|
|
|
|
2023-04-13 16:51:01 -07:00
|
|
|
|
if (duplicates.has(obj.fullPath)) {
|
2021-12-20 18:01:04 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-13 16:51:01 -07:00
|
|
|
|
duplicates.add(obj.fullPath);
|
2021-12-20 18:01:04 +01:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 17:05:12 -07:00
|
|
|
|
/**
|
|
|
|
|
* This function takes a result map, and sorts it by various criteria, including edit
|
|
|
|
|
* distance, substring match, and the crate it comes from.
|
|
|
|
|
*
|
|
|
|
|
* @param {Results} results
|
|
|
|
|
* @param {boolean} isType
|
|
|
|
|
* @param {string} preferredCrate
|
|
|
|
|
* @returns {[ResultObject]}
|
|
|
|
|
*/
|
2022-05-15 21:09:55 -07:00
|
|
|
|
function sortResults(results, isType, preferredCrate) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// if there are no results then return to default and fail
|
2023-04-13 17:05:12 -07:00
|
|
|
|
if (results.size === 0) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 17:05:12 -07:00
|
|
|
|
const userQuery = parsedQuery.userQuery;
|
|
|
|
|
const result_list = [];
|
|
|
|
|
for (const result of results.values()) {
|
|
|
|
|
result.word = searchWords[result.id];
|
|
|
|
|
result.item = searchIndex[result.id] || {};
|
|
|
|
|
result_list.push(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result_list.sort((aaa, bbb) => {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let a, b;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// sort by exact match with regard to the last word (mismatch goes later)
|
2022-03-20 15:03:17 +01:00
|
|
|
|
a = (aaa.word !== userQuery);
|
|
|
|
|
b = (bbb.word !== userQuery);
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-12-16 11:12:59 -07:00
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by distance in the path part, if specified
|
|
|
|
|
// (less changes required to match means higher rankings)
|
2023-03-10 18:18:43 -07:00
|
|
|
|
a = aaa.path_dist;
|
|
|
|
|
b = bbb.path_dist;
|
2022-12-16 11:12:59 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by distance in the name part, the last part of the path
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// (less changes required to match means higher rankings)
|
2023-03-10 18:18:43 -07:00
|
|
|
|
a = (aaa.dist);
|
|
|
|
|
b = (bbb.dist);
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2023-02-03 00:08:57 -07:00
|
|
|
|
// sort deprecated items later
|
|
|
|
|
a = aaa.item.deprecated;
|
|
|
|
|
b = bbb.item.deprecated;
|
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
// sort by crate (current crate comes first)
|
|
|
|
|
a = (aaa.item.crate !== preferredCrate);
|
|
|
|
|
b = (bbb.item.crate !== preferredCrate);
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// sort by item name length (longer goes later)
|
|
|
|
|
a = aaa.word.length;
|
|
|
|
|
b = bbb.word.length;
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// sort by item name (lexicographically larger goes later)
|
|
|
|
|
a = aaa.word;
|
|
|
|
|
b = bbb.word;
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return (a > b ? +1 : -1);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// 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 === "");
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// sort by type (later occurrence in `itemTypes` goes later)
|
|
|
|
|
a = aaa.item.ty;
|
|
|
|
|
b = bbb.item.ty;
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return a - b;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// sort by path (lexicographically larger goes later)
|
|
|
|
|
a = aaa.item.path;
|
|
|
|
|
b = bbb.item.path;
|
2022-05-14 13:50:52 +02:00
|
|
|
|
if (a !== b) {
|
|
|
|
|
return (a > b ? +1 : -1);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
// que sera, sera
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let nameSplit = null;
|
2022-03-20 15:03:17 +01:00
|
|
|
|
if (parsedQuery.elems.length === 1) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const hasPath = typeof parsedQuery.elems[0].path === "undefined";
|
2022-03-20 15:03:17 +01:00
|
|
|
|
nameSplit = hasPath ? null : parsedQuery.elems[0].path;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 17:05:12 -07:00
|
|
|
|
for (const result of result_list) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// this validation does not make sense when searching by types
|
|
|
|
|
if (result.dontValidate) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const name = result.item.name.toLowerCase(),
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-13 17:05:12 -07:00
|
|
|
|
return transformResults(result_list);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
2023-06-02 19:58:44 -07:00
|
|
|
|
* This function checks generics in search query `queryElem` can all be found in the
|
|
|
|
|
* search index (`fnType`),
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
2023-06-02 19:58:44 -07:00
|
|
|
|
* @param {FunctionType} fnType - The object to check.
|
|
|
|
|
* @param {QueryElement} queryElem - The element from the parsed query.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
2023-06-02 19:58:44 -07:00
|
|
|
|
* @return {boolean} - Returns true if a match, false otherwise.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
2023-06-02 19:58:44 -07:00
|
|
|
|
function checkGenerics(fnType, queryElem) {
|
|
|
|
|
return unifyFunctionTypes(fnType.generics, queryElem.generics);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* This function checks if a list of search query `queryElems` can all be found in the
|
|
|
|
|
* search index (`fnTypes`).
|
|
|
|
|
*
|
|
|
|
|
* @param {Array<FunctionType>} fnTypes - The objects to check.
|
|
|
|
|
* @param {Array<QueryElement>} queryElems - The elements from the parsed query.
|
|
|
|
|
*
|
|
|
|
|
* @return {boolean} - Returns true if a match, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
function unifyFunctionTypes(fnTypes, queryElems) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
// This search engine implements order-agnostic unification. There
|
|
|
|
|
// should be no missing duplicates (generics have "bag semantics"),
|
|
|
|
|
// and the row is allowed to have extras.
|
2023-06-02 19:58:44 -07:00
|
|
|
|
if (queryElems.length === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!fnTypes || fnTypes.length === 0) {
|
2023-06-02 18:31:51 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
/**
|
|
|
|
|
* @type Map<integer, QueryElement[]>
|
|
|
|
|
*/
|
|
|
|
|
const queryElemSet = new Map();
|
2023-08-31 18:51:18 +02:00
|
|
|
|
const addQueryElemToQueryElemSet = queryElem => {
|
2023-06-08 23:12:36 -07:00
|
|
|
|
let currentQueryElemList;
|
|
|
|
|
if (queryElemSet.has(queryElem.id)) {
|
|
|
|
|
currentQueryElemList = queryElemSet.get(queryElem.id);
|
|
|
|
|
} else {
|
|
|
|
|
currentQueryElemList = [];
|
|
|
|
|
queryElemSet.set(queryElem.id, currentQueryElemList);
|
|
|
|
|
}
|
|
|
|
|
currentQueryElemList.push(queryElem);
|
|
|
|
|
};
|
|
|
|
|
for (const queryElem of queryElems) {
|
|
|
|
|
addQueryElemToQueryElemSet(queryElem);
|
|
|
|
|
}
|
2023-06-02 19:58:44 -07:00
|
|
|
|
/**
|
|
|
|
|
* @type Map<integer, FunctionType[]>
|
|
|
|
|
*/
|
|
|
|
|
const fnTypeSet = new Map();
|
2023-08-31 18:51:18 +02:00
|
|
|
|
const addFnTypeToFnTypeSet = fnType => {
|
2023-06-08 23:12:36 -07:00
|
|
|
|
// Pure generic, or an item that's not matched by any query elems.
|
|
|
|
|
// Try [unboxing] it.
|
|
|
|
|
//
|
|
|
|
|
// [unboxing]:
|
|
|
|
|
// http://ndmitchell.com/downloads/slides-hoogle_fast_type_searching-09_aug_2008.pdf
|
|
|
|
|
const queryContainsArrayOrSliceElem = queryElemSet.has(typeNameIdOfArrayOrSlice);
|
|
|
|
|
if (fnType.id === -1 || !(
|
|
|
|
|
queryElemSet.has(fnType.id) ||
|
|
|
|
|
(fnType.id === typeNameIdOfSlice && queryContainsArrayOrSliceElem) ||
|
|
|
|
|
(fnType.id === typeNameIdOfArray && queryContainsArrayOrSliceElem)
|
|
|
|
|
)) {
|
|
|
|
|
for (const innerFnType of fnType.generics) {
|
|
|
|
|
addFnTypeToFnTypeSet(innerFnType);
|
2022-02-09 20:56:37 +01:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let currentQueryElemList = queryElemSet.get(fnType.id) || [];
|
|
|
|
|
let matchIdx = currentQueryElemList.findIndex(queryElem => {
|
|
|
|
|
return typePassesFilter(queryElem.typeFilter, fnType.ty) &&
|
|
|
|
|
checkGenerics(fnType, queryElem);
|
|
|
|
|
});
|
|
|
|
|
if (matchIdx === -1 &&
|
|
|
|
|
(fnType.id === typeNameIdOfSlice || fnType.id === typeNameIdOfArray) &&
|
|
|
|
|
queryContainsArrayOrSliceElem
|
|
|
|
|
) {
|
|
|
|
|
currentQueryElemList = queryElemSet.get(typeNameIdOfArrayOrSlice) || [];
|
|
|
|
|
matchIdx = currentQueryElemList.findIndex(queryElem => {
|
|
|
|
|
return typePassesFilter(queryElem.typeFilter, fnType.ty) &&
|
|
|
|
|
checkGenerics(fnType, queryElem);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// None of the query elems match the function type.
|
|
|
|
|
// Try [unboxing] it.
|
|
|
|
|
if (matchIdx === -1) {
|
2023-06-02 19:58:44 -07:00
|
|
|
|
for (const innerFnType of fnType.generics) {
|
|
|
|
|
addFnTypeToFnTypeSet(innerFnType);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-06-02 18:31:51 -07:00
|
|
|
|
return;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2023-06-02 19:58:44 -07:00
|
|
|
|
let currentFnTypeList;
|
|
|
|
|
if (fnTypeSet.has(fnType.id)) {
|
|
|
|
|
currentFnTypeList = fnTypeSet.get(fnType.id);
|
2023-06-02 18:31:51 -07:00
|
|
|
|
} else {
|
2023-06-02 19:58:44 -07:00
|
|
|
|
currentFnTypeList = [];
|
|
|
|
|
fnTypeSet.set(fnType.id, currentFnTypeList);
|
2023-06-02 18:31:51 -07:00
|
|
|
|
}
|
2023-06-02 19:58:44 -07:00
|
|
|
|
currentFnTypeList.push(fnType);
|
2023-06-02 18:31:51 -07:00
|
|
|
|
};
|
2023-06-02 19:58:44 -07:00
|
|
|
|
for (const fnType of fnTypes) {
|
|
|
|
|
addFnTypeToFnTypeSet(fnType);
|
2023-06-02 18:31:51 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
const doHandleQueryElemList = (currentFnTypeList, queryElemList) => {
|
|
|
|
|
if (queryElemList.length === 0) {
|
|
|
|
|
return true;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
// Multiple items in one list might match multiple items in another.
|
|
|
|
|
// Since an item with fewer generics can match an item with more, we
|
|
|
|
|
// need to check all combinations for a potential match.
|
|
|
|
|
const queryElem = queryElemList.pop();
|
|
|
|
|
const l = currentFnTypeList.length;
|
|
|
|
|
for (let i = 0; i < l; i += 1) {
|
|
|
|
|
const fnType = currentFnTypeList[i];
|
2023-06-02 19:58:44 -07:00
|
|
|
|
if (!typePassesFilter(queryElem.typeFilter, fnType.ty)) {
|
2023-06-08 23:12:36 -07:00
|
|
|
|
continue;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
if (queryElem.generics.length === 0 || checkGenerics(fnType, queryElem)) {
|
|
|
|
|
currentFnTypeList.splice(i, 1);
|
|
|
|
|
const result = doHandleQueryElemList(currentFnTypeList, queryElemList);
|
|
|
|
|
if (result) {
|
|
|
|
|
return true;
|
2023-03-30 21:53:11 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
currentFnTypeList.splice(i, 0, fnType);
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
2023-06-02 18:31:51 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
return false;
|
2023-06-02 18:31:51 -07:00
|
|
|
|
};
|
2023-06-08 23:12:36 -07:00
|
|
|
|
const handleQueryElemList = (id, queryElemList) => {
|
|
|
|
|
if (!fnTypeSet.has(id)) {
|
|
|
|
|
if (id === typeNameIdOfArrayOrSlice) {
|
|
|
|
|
return handleQueryElemList(typeNameIdOfSlice, queryElemList) ||
|
|
|
|
|
handleQueryElemList(typeNameIdOfArray, queryElemList);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
return false;
|
2023-06-02 18:31:51 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
const currentFnTypeList = fnTypeSet.get(id);
|
|
|
|
|
if (currentFnTypeList.length < queryElemList.length) {
|
|
|
|
|
// It's not possible for all the query elems to find a match.
|
|
|
|
|
return false;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
const result = doHandleQueryElemList(currentFnTypeList, queryElemList);
|
|
|
|
|
if (result) {
|
|
|
|
|
// Found a solution.
|
|
|
|
|
// Any items that weren't used for it can be unboxed, and might form
|
|
|
|
|
// part of the solution for another item.
|
|
|
|
|
for (const innerFnType of currentFnTypeList) {
|
|
|
|
|
addFnTypeToFnTypeSet(innerFnType);
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
fnTypeSet.delete(id);
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
let queryElemSetSize = -1;
|
|
|
|
|
while (queryElemSetSize !== queryElemSet.size) {
|
|
|
|
|
queryElemSetSize = queryElemSet.size;
|
|
|
|
|
for (const [id, queryElemList] of queryElemSet) {
|
|
|
|
|
if (handleQueryElemList(id, queryElemList)) {
|
|
|
|
|
queryElemSet.delete(id);
|
2023-02-28 18:17:59 -07:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-08 23:12:36 -07:00
|
|
|
|
return queryElemSetSize === 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
/**
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* This function checks if the object (`row`) matches the given type (`elem`) and its
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* generics (if any).
|
|
|
|
|
*
|
2023-06-02 19:58:44 -07:00
|
|
|
|
* @param {Array<FunctionType>} list
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {QueryElement} elem - The element from the parsed query.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
2023-04-15 11:53:50 -07:00
|
|
|
|
* @return {boolean} - Returns true if found, false otherwise.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
2023-06-02 19:58:44 -07:00
|
|
|
|
function checkIfInList(list, elem) {
|
|
|
|
|
for (const entry of list) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
if (checkType(entry, elem)) {
|
|
|
|
|
return true;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2023-04-15 11:53:50 -07:00
|
|
|
|
return false;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* This function checks if the object (`row`) matches the given type (`elem`) and its
|
2021-12-20 15:42:08 +01:00
|
|
|
|
* generics (if any).
|
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {Row} row
|
|
|
|
|
* @param {QueryElement} elem - The element from the parsed query.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*
|
2023-04-15 11:53:50 -07:00
|
|
|
|
* @return {boolean} - Returns true if the type matches, false otherwise.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*/
|
2023-04-15 11:53:50 -07:00
|
|
|
|
function checkType(row, elem) {
|
|
|
|
|
if (row.id === -1) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
// This is a pure "generic" search, no need to run other checks.
|
2023-06-02 19:58:44 -07:00
|
|
|
|
return row.generics.length > 0 ? checkIfInList(row.generics, elem) : false;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2023-05-25 12:58:36 -07:00
|
|
|
|
const matchesExact = row.id === elem.id;
|
|
|
|
|
const matchesArrayOrSlice = elem.id === typeNameIdOfArrayOrSlice &&
|
|
|
|
|
(row.id === typeNameIdOfSlice || row.id === typeNameIdOfArray);
|
|
|
|
|
|
|
|
|
|
if ((matchesExact || matchesArrayOrSlice) &&
|
|
|
|
|
typePassesFilter(elem.typeFilter, row.ty)) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
if (elem.generics.length > 0) {
|
|
|
|
|
return checkGenerics(row, elem);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-04-15 11:53:50 -07:00
|
|
|
|
return true;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2023-04-15 11:53:50 -07:00
|
|
|
|
|
|
|
|
|
// If the current item does not match, try [unboxing] the generic.
|
|
|
|
|
// [unboxing]:
|
|
|
|
|
// https://ndmitchell.com/downloads/slides-hoogle_fast_type_searching-09_aug_2008.pdf
|
2023-06-02 19:58:44 -07:00
|
|
|
|
return checkIfInList(row.generics, elem);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 18:18:43 -07:00
|
|
|
|
function checkPath(contains, ty, maxEditDistance) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (contains.length === 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
let ret_dist = maxEditDistance + 1;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const path = ty.path.split("::");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
if (ty.parent && ty.parent.name) {
|
|
|
|
|
path.push(ty.parent.name.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const length = path.length;
|
|
|
|
|
const clength = contains.length;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (clength > length) {
|
2023-03-10 18:18:43 -07:00
|
|
|
|
return maxEditDistance + 1;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (let i = 0; i < length; ++i) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (i + clength > length) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
let dist_total = 0;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let aborted = false;
|
|
|
|
|
for (let x = 0; x < clength; ++x) {
|
2023-03-10 18:18:43 -07:00
|
|
|
|
const dist = editDistance(path[i + x], contains[x], maxEditDistance);
|
|
|
|
|
if (dist > maxEditDistance) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
aborted = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
dist_total += dist;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-05-09 13:49:22 -07:00
|
|
|
|
if (!aborted) {
|
2023-03-10 18:18:43 -07:00
|
|
|
|
ret_dist = Math.min(ret_dist, Math.round(dist_total / clength));
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
return ret_dist;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const name = itemTypes[type];
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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,
|
2023-02-03 00:08:57 -07:00
|
|
|
|
deprecated: item.deprecated,
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
function handleAliases(ret, query, filterCrates, currentCrate) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const lowerQuery = query.toLowerCase();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// We separate aliases and crate aliases because we want to have current crate
|
|
|
|
|
// aliases to be before the others in the displayed results.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const aliases = [];
|
|
|
|
|
const crateAliases = [];
|
2022-01-10 14:57:43 +01:00
|
|
|
|
if (filterCrates !== null) {
|
2023-04-13 16:30:02 -07:00
|
|
|
|
if (ALIASES.has(filterCrates) && ALIASES.get(filterCrates).has(lowerQuery)) {
|
|
|
|
|
const query_aliases = ALIASES.get(filterCrates).get(lowerQuery);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const alias of query_aliases) {
|
|
|
|
|
aliases.push(createAliasFromItem(searchIndex[alias]));
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-04-13 16:30:02 -07:00
|
|
|
|
for (const [crate, crateAliasesIndex] of ALIASES) {
|
|
|
|
|
if (crateAliasesIndex.has(lowerQuery)) {
|
2022-05-15 21:09:55 -07:00
|
|
|
|
const pushTo = crate === currentCrate ? crateAliases : aliases;
|
2023-04-13 16:30:02 -07:00
|
|
|
|
const query_aliases = crateAliasesIndex.get(lowerQuery);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const alias of query_aliases) {
|
|
|
|
|
pushTo.push(createAliasFromItem(searchIndex[alias]));
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-13 16:30:02 -07:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
const sortFunc = (aaa, bbb) => {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
if (aaa.path < bbb.path) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else if (aaa.path === bbb.path) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
};
|
|
|
|
|
crateAliases.sort(sortFunc);
|
|
|
|
|
aliases.sort(sortFunc);
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
const pushFunc = alias => {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
alias.alias = query;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const res = buildHrefAndPath(alias);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-05-15 21:09:55 -07:00
|
|
|
|
|
|
|
|
|
aliases.forEach(pushFunc);
|
|
|
|
|
crateAliases.forEach(pushFunc);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 20:52:37 +01:00
|
|
|
|
/**
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* This function adds the given result into the provided `results` map if it matches the
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* following condition:
|
|
|
|
|
*
|
2023-03-10 18:18:43 -07:00
|
|
|
|
* * If it is a "literal search" (`parsedQuery.literalSearch`), then `dist` must be 0.
|
|
|
|
|
* * If it is not a "literal search", `dist` must be <= `maxEditDistance`.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* The `results` map contains information which will be used to sort the search results:
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* * `fullId` is a `string`` used as the key of the object we use for the `results` map.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* * `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.
|
2023-03-10 18:18:43 -07:00
|
|
|
|
* * `dist` is the main metric used to sort the search results.
|
|
|
|
|
* * `path_dist` is zero if a single-component search query is used, otherwise it's the
|
2022-12-16 11:12:59 -07:00
|
|
|
|
* distance computed for everything other than the last path component.
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*
|
2022-03-20 15:03:17 +01:00
|
|
|
|
* @param {Results} results
|
2021-12-15 20:52:37 +01:00
|
|
|
|
* @param {string} fullId
|
|
|
|
|
* @param {integer} id
|
|
|
|
|
* @param {integer} index
|
2023-03-10 18:18:43 -07:00
|
|
|
|
* @param {integer} dist
|
|
|
|
|
* @param {integer} path_dist
|
2021-12-15 20:52:37 +01:00
|
|
|
|
*/
|
2023-03-10 18:18:43 -07:00
|
|
|
|
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
|
|
|
|
|
const inBounds = dist <= maxEditDistance || index !== -1;
|
|
|
|
|
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
|
2023-04-13 17:05:12 -07:00
|
|
|
|
if (results.has(fullId)) {
|
|
|
|
|
const result = results.get(fullId);
|
2023-03-10 18:18:43 -07:00
|
|
|
|
if (result.dontValidate || result.dist <= dist) {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-13 17:05:12 -07:00
|
|
|
|
results.set(fullId, {
|
2021-12-15 20:52:37 +01:00
|
|
|
|
id: id,
|
|
|
|
|
index: index,
|
2022-01-04 15:44:00 +01:00
|
|
|
|
dontValidate: parsedQuery.literalSearch,
|
2023-03-10 18:18:43 -07:00
|
|
|
|
dist: dist,
|
|
|
|
|
path_dist: path_dist,
|
2023-04-13 17:05:12 -07:00
|
|
|
|
});
|
2021-12-15 20:52:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* This function is called in case the query is only one element (with or without generics).
|
2022-04-20 21:11:39 +02:00
|
|
|
|
* This element will be compared to arguments' and returned values' items and also to items.
|
|
|
|
|
*
|
2023-03-10 18:18:43 -07:00
|
|
|
|
* Other important thing to note: since there is only one element, we use edit
|
2022-04-20 21:11:39 +02:00
|
|
|
|
* distance for name comparisons.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {Row} row
|
2022-03-20 15:03:17 +01:00
|
|
|
|
* @param {integer} pos - Position in the `searchIndex`.
|
|
|
|
|
* @param {QueryElement} elem - The element from the parsed query.
|
|
|
|
|
* @param {Results} results_others - Unqualified results (not in arguments nor in
|
|
|
|
|
* returned values).
|
|
|
|
|
* @param {Results} results_in_args - Matching arguments results.
|
|
|
|
|
* @param {Results} results_returned - Matching returned arguments results.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
2022-03-20 15:03:17 +01:00
|
|
|
|
function handleSingleArg(
|
|
|
|
|
row,
|
|
|
|
|
pos,
|
|
|
|
|
elem,
|
|
|
|
|
results_others,
|
|
|
|
|
results_in_args,
|
2022-10-28 21:54:09 -07:00
|
|
|
|
results_returned,
|
2023-03-10 18:18:43 -07:00
|
|
|
|
maxEditDistance
|
2022-03-20 15:03:17 +01:00
|
|
|
|
) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (!row || (filterCrates !== null && row.crate !== filterCrates)) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-15 11:53:50 -07:00
|
|
|
|
let index = -1, path_dist = 0;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const fullId = row.id;
|
2022-12-16 11:12:59 -07:00
|
|
|
|
const searchWord = searchWords[pos];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
|
2023-06-02 19:58:44 -07:00
|
|
|
|
const in_args = row.type && row.type.inputs && checkIfInList(row.type.inputs, elem);
|
|
|
|
|
if (in_args) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
// path_dist is 0 because no parent path information is currently stored
|
|
|
|
|
// in the search index
|
|
|
|
|
addIntoResults(results_in_args, fullId, pos, -1, 0, 0, maxEditDistance);
|
|
|
|
|
}
|
2023-06-02 19:58:44 -07:00
|
|
|
|
const returned = row.type && row.type.output && checkIfInList(row.type.output, elem);
|
|
|
|
|
if (returned) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
addIntoResults(results_returned, fullId, pos, -1, 0, 0, maxEditDistance);
|
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
|
2023-02-28 18:17:59 -07:00
|
|
|
|
if (!typePassesFilter(elem.typeFilter, row.ty)) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 11:12:59 -07:00
|
|
|
|
const row_index = row.normalizedName.indexOf(elem.pathLast);
|
|
|
|
|
const word_index = searchWord.indexOf(elem.pathLast);
|
|
|
|
|
|
|
|
|
|
// lower indexes are "better" matches
|
|
|
|
|
// rank based on the "best" match
|
|
|
|
|
if (row_index === -1) {
|
|
|
|
|
index = word_index;
|
|
|
|
|
} else if (word_index === -1) {
|
|
|
|
|
index = row_index;
|
|
|
|
|
} else if (word_index < row_index) {
|
|
|
|
|
index = word_index;
|
|
|
|
|
} else {
|
|
|
|
|
index = row_index;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (elem.fullPath.length > 1) {
|
2023-03-10 18:18:43 -07:00
|
|
|
|
path_dist = checkPath(elem.pathWithoutLast, row, maxEditDistance);
|
|
|
|
|
if (path_dist > maxEditDistance) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 11:12:59 -07:00
|
|
|
|
if (parsedQuery.literalSearch) {
|
|
|
|
|
if (searchWord === elem.name) {
|
2023-03-10 18:18:43 -07:00
|
|
|
|
addIntoResults(results_others, fullId, pos, index, 0, path_dist);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-16 11:12:59 -07:00
|
|
|
|
|
2023-04-15 11:53:50 -07:00
|
|
|
|
const dist = editDistance(searchWord, elem.pathLast, maxEditDistance);
|
2022-12-16 11:12:59 -07:00
|
|
|
|
|
2023-03-10 18:18:43 -07:00
|
|
|
|
if (index === -1 && dist + path_dist > maxEditDistance) {
|
2022-12-16 11:12:59 -07:00
|
|
|
|
return;
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2022-12-16 11:12:59 -07:00
|
|
|
|
|
2023-03-10 18:18:43 -07:00
|
|
|
|
addIntoResults(results_others, fullId, pos, index, dist, path_dist, maxEditDistance);
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
/**
|
2022-02-11 15:48:57 +01:00
|
|
|
|
* This function is called in case the query has more than one element. In this case, it'll
|
|
|
|
|
* try to match the items which validates all the elements. For `aa -> bb` will look for
|
|
|
|
|
* functions which have a parameter `aa` and has `bb` in its returned values.
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*
|
2022-01-04 15:44:00 +01:00
|
|
|
|
* @param {Row} row
|
|
|
|
|
* @param {integer} pos - Position in the `searchIndex`.
|
|
|
|
|
* @param {Object} results
|
2021-12-20 15:42:08 +01:00
|
|
|
|
*/
|
2023-04-15 11:53:50 -07:00
|
|
|
|
function handleArgs(row, pos, results) {
|
2023-06-02 19:58:44 -07:00
|
|
|
|
if (!row || (filterCrates !== null && row.crate !== filterCrates) || !row.type) {
|
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 the result is too "bad", we return false and it ends this search.
|
2023-06-02 19:58:44 -07:00
|
|
|
|
if (!unifyFunctionTypes(row.type.inputs, parsedQuery.elems)) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-02 19:58:44 -07:00
|
|
|
|
if (!unifyFunctionTypes(row.type.output, parsedQuery.returned)) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
return;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-15 11:53:50 -07:00
|
|
|
|
addIntoResults(results, row.id, pos, 0, 0, 0, Number.MAX_VALUE);
|
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() {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let elem, i, nSearchWords, in_returned, row;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-10-28 21:54:09 -07:00
|
|
|
|
let queryLen = 0;
|
|
|
|
|
for (const elem of parsedQuery.elems) {
|
|
|
|
|
queryLen += elem.name.length;
|
|
|
|
|
}
|
|
|
|
|
for (const elem of parsedQuery.returned) {
|
|
|
|
|
queryLen += elem.name.length;
|
|
|
|
|
}
|
2023-03-10 18:18:43 -07:00
|
|
|
|
const maxEditDistance = Math.floor(queryLen / 3);
|
2022-10-28 21:54:09 -07:00
|
|
|
|
|
2023-04-15 11:53:50 -07:00
|
|
|
|
/**
|
|
|
|
|
* Convert names to ids in parsed query elements.
|
|
|
|
|
* This is not used for the "In Names" tab, but is used for the
|
|
|
|
|
* "In Params", "In Returns", and "In Function Signature" tabs.
|
|
|
|
|
*
|
|
|
|
|
* If there is no matching item, but a close-enough match, this
|
|
|
|
|
* function also that correction.
|
|
|
|
|
*
|
|
|
|
|
* See `buildTypeMapIndex` for more information.
|
|
|
|
|
*
|
|
|
|
|
* @param {QueryElement} elem
|
|
|
|
|
*/
|
|
|
|
|
function convertNameToId(elem) {
|
|
|
|
|
if (typeNameIdMap.has(elem.name)) {
|
|
|
|
|
elem.id = typeNameIdMap.get(elem.name);
|
|
|
|
|
} else if (!parsedQuery.literalSearch) {
|
|
|
|
|
let match = -1;
|
|
|
|
|
let matchDist = maxEditDistance + 1;
|
|
|
|
|
let matchName = "";
|
|
|
|
|
for (const [name, id] of typeNameIdMap) {
|
|
|
|
|
const dist = editDistance(name, elem.name, maxEditDistance);
|
|
|
|
|
if (dist <= matchDist && dist <= maxEditDistance) {
|
2023-04-20 12:34:17 -07:00
|
|
|
|
if (dist === matchDist && matchName > name) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-04-15 11:53:50 -07:00
|
|
|
|
match = id;
|
|
|
|
|
matchDist = dist;
|
|
|
|
|
matchName = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (match !== -1) {
|
|
|
|
|
parsedQuery.correction = matchName;
|
|
|
|
|
}
|
|
|
|
|
elem.id = match;
|
|
|
|
|
}
|
|
|
|
|
for (const elem2 of elem.generics) {
|
|
|
|
|
convertNameToId(elem2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const elem of parsedQuery.elems) {
|
|
|
|
|
convertNameToId(elem);
|
|
|
|
|
}
|
|
|
|
|
for (const elem of parsedQuery.returned) {
|
|
|
|
|
convertNameToId(elem);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (parsedQuery.foundElems === 1) {
|
|
|
|
|
if (parsedQuery.elems.length === 1) {
|
|
|
|
|
elem = parsedQuery.elems[0];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
|
|
|
|
// It means we want to check for this element everywhere (in names, args and
|
|
|
|
|
// returned).
|
2022-03-20 15:03:17 +01:00
|
|
|
|
handleSingleArg(
|
|
|
|
|
searchIndex[i],
|
|
|
|
|
i,
|
|
|
|
|
elem,
|
|
|
|
|
results_others,
|
|
|
|
|
results_in_args,
|
2022-10-28 21:54:09 -07:00
|
|
|
|
results_returned,
|
2023-03-10 18:18:43 -07:00
|
|
|
|
maxEditDistance
|
2022-03-20 15:03:17 +01:00
|
|
|
|
);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
} else if (parsedQuery.returned.length === 1) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
// We received one returned argument to check, so looking into returned values.
|
2022-01-04 15:44:00 +01:00
|
|
|
|
elem = parsedQuery.returned[0];
|
2021-12-20 15:42:08 +01:00
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
2022-01-04 15:44:00 +01:00
|
|
|
|
row = searchIndex[i];
|
2023-06-02 19:58:44 -07:00
|
|
|
|
in_returned = row.type &&
|
|
|
|
|
unifyFunctionTypes(row.type.output, parsedQuery.returned);
|
|
|
|
|
if (in_returned) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
addIntoResults(
|
|
|
|
|
results_others,
|
|
|
|
|
row.id,
|
|
|
|
|
i,
|
|
|
|
|
-1,
|
|
|
|
|
0,
|
|
|
|
|
Number.MAX_VALUE
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-04 15:44:00 +01:00
|
|
|
|
} else if (parsedQuery.foundElems > 0) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
handleArgs(searchIndex[i], i, results_others);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-09 15:12:46 +01:00
|
|
|
|
|
|
|
|
|
if (parsedQuery.error === null) {
|
|
|
|
|
innerRunQuery();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const ret = createQueryResults(
|
2022-05-15 21:09:55 -07:00
|
|
|
|
sortResults(results_in_args, true, currentCrate),
|
|
|
|
|
sortResults(results_returned, true, currentCrate),
|
|
|
|
|
sortResults(results_others, false, currentCrate),
|
2022-01-04 15:44:00 +01:00
|
|
|
|
parsedQuery);
|
2022-05-15 21:09:55 -07:00
|
|
|
|
handleAliases(ret, parsedQuery.original.replace(/"/g, ""), filterCrates, currentCrate);
|
2022-01-09 15:12:46 +01:00
|
|
|
|
if (parsedQuery.error !== null && ret.others.length !== 0) {
|
|
|
|
|
// It means some doc aliases were found so let's "remove" the error!
|
|
|
|
|
ret.query.error = null;
|
|
|
|
|
}
|
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
|
2022-03-20 15:03:17 +01:00
|
|
|
|
*
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* @return {boolean} - Whether the result is valid or not
|
2021-04-11 22:19:29 -07:00
|
|
|
|
*/
|
2023-03-10 18:18:43 -07:00
|
|
|
|
function validateResult(name, path, keys, parent, maxEditDistance) {
|
2021-12-20 15:42:08 +01:00
|
|
|
|
if (!keys || !keys.length) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const key of keys) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// each check is for validation so we negate the conditions and invalidate
|
|
|
|
|
if (!(
|
|
|
|
|
// check for an exact name match
|
2022-04-24 15:36:02 +02:00
|
|
|
|
name.indexOf(key) > -1 ||
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// then an exact path match
|
2022-04-24 15:36:02 +02:00
|
|
|
|
path.indexOf(key) > -1 ||
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// next if there is a parent, check for exact parent match
|
|
|
|
|
(parent !== undefined && parent.name !== undefined &&
|
2022-04-24 15:36:02 +02:00
|
|
|
|
parent.name.toLowerCase().indexOf(key) > -1) ||
|
2023-03-10 18:18:43 -07:00
|
|
|
|
// lastly check to see if the name was an editDistance match
|
|
|
|
|
editDistance(name, key, maxEditDistance) <= maxEditDistance)) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 12:56:21 -07:00
|
|
|
|
function nextTab(direction) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const next = (searchState.currentTab + direction + 3) % searchState.focusedByTab.length;
|
2021-05-09 12:56:21 -07:00
|
|
|
|
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() {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const target = searchState.focusedByTab[searchState.currentTab] ||
|
2021-05-20 12:45:25 +02:00
|
|
|
|
document.querySelectorAll(".search-results.active a").item(0) ||
|
2022-12-22 15:52:34 -07:00
|
|
|
|
document.querySelectorAll("#search-tabs button").item(searchState.currentTab);
|
2022-11-14 10:41:07 -07:00
|
|
|
|
searchState.focusedByTab[searchState.currentTab] = null;
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (target) {
|
|
|
|
|
target.focus();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildHrefAndPath(item) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let displayPath;
|
|
|
|
|
let href;
|
|
|
|
|
const type = itemTypes[item.ty];
|
|
|
|
|
const name = item.name;
|
|
|
|
|
let path = item.path;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
if (type === "mod") {
|
|
|
|
|
displayPath = path + "::";
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + path.replace(/::/g, "/") + "/" +
|
|
|
|
|
name + "/index.html";
|
2022-05-05 21:56:03 +02:00
|
|
|
|
} else if (type === "import") {
|
|
|
|
|
displayPath = item.path + "::";
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + item.path.replace(/::/g, "/") + "/index.html#reexport." + name;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else if (type === "primitive" || type === "keyword") {
|
|
|
|
|
displayPath = "";
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + path.replace(/::/g, "/") +
|
|
|
|
|
"/" + type + "." + name + ".html";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else if (type === "externcrate") {
|
|
|
|
|
displayPath = "";
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + name + "/index.html";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else if (item.parent !== undefined) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const myparent = item.parent;
|
|
|
|
|
let anchor = "#" + type + "." + name;
|
|
|
|
|
const parentType = itemTypes[myparent.ty];
|
|
|
|
|
let pageType = parentType;
|
|
|
|
|
let pageName = myparent.name;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
|
|
|
|
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.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const enumNameIdx = item.path.lastIndexOf("::");
|
|
|
|
|
const enumName = item.path.substr(enumNameIdx + 2);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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 + "::";
|
|
|
|
|
}
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + path.replace(/::/g, "/") +
|
|
|
|
|
"/" + pageType +
|
|
|
|
|
"." + pageName +
|
|
|
|
|
".html" + anchor;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
} else {
|
|
|
|
|
displayPath = item.path + "::";
|
2022-05-15 21:09:55 -07:00
|
|
|
|
href = ROOT_PATH + item.path.replace(/::/g, "/") +
|
|
|
|
|
"/" + type + "." + name + ".html";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
return [displayPath, href];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pathSplitter(path) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const tmp = "<span>" + path.replace(/::/g, "::</span><span>");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let extraClass = "";
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (display === true) {
|
|
|
|
|
extraClass = " active";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const output = document.createElement("div");
|
|
|
|
|
let length = 0;
|
2022-01-09 15:12:46 +01:00
|
|
|
|
if (array.length > 0) {
|
2021-05-20 15:14:50 +02:00
|
|
|
|
output.className = "search-results " + extraClass;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
array.forEach(item => {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const name = item.name;
|
|
|
|
|
const type = itemTypes[item.ty];
|
2023-04-22 16:00:20 +02:00
|
|
|
|
const longType = longItemTypes[item.ty];
|
2023-05-18 16:16:13 +02:00
|
|
|
|
const typeName = longType.length !== 0 ? `${longType}` : "?";
|
2021-05-24 14:24:34 +02:00
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
length += 1;
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const link = document.createElement("a");
|
2021-05-20 15:14:50 +02:00
|
|
|
|
link.className = "result-" + type;
|
|
|
|
|
link.href = item.href;
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const resultName = document.createElement("div");
|
2021-05-20 15:14:50 +02:00
|
|
|
|
resultName.className = "result-name";
|
|
|
|
|
|
2023-07-18 14:22:27 +02:00
|
|
|
|
resultName.insertAdjacentHTML(
|
|
|
|
|
"beforeend",
|
|
|
|
|
`<span class="typename">${typeName}</span>`);
|
|
|
|
|
link.appendChild(resultName);
|
2021-05-20 15:14:50 +02:00
|
|
|
|
|
2023-07-18 14:22:27 +02:00
|
|
|
|
let alias = " ";
|
|
|
|
|
if (item.is_alias) {
|
|
|
|
|
alias = ` <div class="alias">\
|
|
|
|
|
<b>${item.alias}</b><i class="grey"> - see </i>\
|
|
|
|
|
</div>`;
|
2021-05-20 15:14:50 +02:00
|
|
|
|
}
|
|
|
|
|
resultName.insertAdjacentHTML(
|
|
|
|
|
"beforeend",
|
2023-07-18 14:22:27 +02:00
|
|
|
|
`<div class="path">${alias}\
|
|
|
|
|
${item.displayPath}<span class="${type}">${name}</span>\
|
2023-06-27 15:05:43 +02:00
|
|
|
|
</div>`);
|
2021-05-20 15:14:50 +02:00
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const description = document.createElement("div");
|
2021-05-20 15:14:50 +02:00
|
|
|
|
description.className = "desc";
|
2022-11-04 13:19:33 -07:00
|
|
|
|
description.insertAdjacentHTML("beforeend", item.desc);
|
2021-05-20 15:14:50 +02:00
|
|
|
|
|
2022-11-04 12:34:24 -07:00
|
|
|
|
link.appendChild(description);
|
2021-05-20 15:14:50 +02:00
|
|
|
|
output.appendChild(link);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
});
|
2022-01-09 15:12:46 +01:00
|
|
|
|
} else if (query.error === null) {
|
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=" +
|
2022-01-03 16:43:30 +01:00
|
|
|
|
encodeURIComponent("rust " + query.userQuery) +
|
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 +
|
2022-12-22 15:52:34 -07:00
|
|
|
|
" <span class=\"count\">(" + nbElems + ")</span></button>";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-12-22 15:52:34 -07:00
|
|
|
|
return "<button>" + text + " <span class=\"count\">(" + nbElems + ")</span></button>";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
/**
|
|
|
|
|
* @param {ResultsTable} results
|
|
|
|
|
* @param {boolean} go_to_first
|
|
|
|
|
* @param {string} filterCrates
|
|
|
|
|
*/
|
2022-01-01 23:48:34 -05:00
|
|
|
|
function showResults(results, go_to_first, filterCrates) {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const search = searchState.outputElement();
|
2021-05-31 19:20:41 -07:00
|
|
|
|
if (go_to_first || (results.others.length === 1
|
2023-03-26 17:50:02 +02:00
|
|
|
|
&& getSettingValue("go-to-only-result") === "true")
|
2022-05-14 13:50:52 +02:00
|
|
|
|
) {
|
2023-06-16 16:56:13 +02:00
|
|
|
|
// Needed to force re-execution of JS when coming back to a page. Let's take this
|
|
|
|
|
// scenario as example:
|
|
|
|
|
//
|
|
|
|
|
// 1. You have the "Directly go to item in search if there is only one result" option
|
|
|
|
|
// enabled.
|
|
|
|
|
// 2. You make a search which results only one result, leading you automatically to
|
|
|
|
|
// this result.
|
|
|
|
|
// 3. You go back to previous page.
|
|
|
|
|
//
|
|
|
|
|
// Now, without the call below, the JS will not be re-executed and the previous state
|
|
|
|
|
// will be used, starting search again since the search input is not empty, leading you
|
|
|
|
|
// back to the previous page again.
|
|
|
|
|
window.onunload = () => {};
|
|
|
|
|
searchState.removeQueryParameters();
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const elem = document.createElement("a");
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
currentResults = results.query.userQuery;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const ret_others = addTab(results.others, results.query, true);
|
|
|
|
|
const ret_in_args = addTab(results.in_args, results.query, false);
|
|
|
|
|
const 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.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let 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 = "";
|
2022-06-14 23:25:51 -07:00
|
|
|
|
const crates_list = Object.keys(rawSearchIndex);
|
|
|
|
|
if (crates_list.length > 1) {
|
2022-07-03 20:10:19 +02:00
|
|
|
|
crates = " in <div id=\"crate-search-div\"><select id=\"crate-search\">" +
|
|
|
|
|
"<option value=\"all crates\">all crates</option>";
|
2022-06-14 23:25:51 -07:00
|
|
|
|
for (const c of crates_list) {
|
2022-05-27 22:30:19 +02:00
|
|
|
|
crates += `<option value="${c}" ${c === filterCrates && "selected"}>${c}</option>`;
|
2022-01-12 15:05:47 -08:00
|
|
|
|
}
|
2022-07-03 20:10:19 +02:00
|
|
|
|
crates += "</select></div>";
|
2021-12-20 15:42:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-03 20:10:19 +02:00
|
|
|
|
let output = `<h1 class="search-results-title">Results${crates}</h1>`;
|
2022-01-09 15:12:46 +01:00
|
|
|
|
if (results.query.error !== null) {
|
2023-02-28 22:37:52 +01:00
|
|
|
|
const error = results.query.error;
|
|
|
|
|
error.forEach((value, index) => {
|
|
|
|
|
value = value.split("<").join("<").split(">").join(">");
|
|
|
|
|
if (index % 2 !== 0) {
|
2023-06-13 10:50:16 +02:00
|
|
|
|
error[index] = `<code>${value.replaceAll(" ", " ")}</code>`;
|
2023-02-28 22:37:52 +01:00
|
|
|
|
} else {
|
|
|
|
|
error[index] = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
output += `<h3 class="error">Query parser error: "${error.join("")}".</h3>`;
|
2022-12-22 15:52:34 -07:00
|
|
|
|
output += "<div id=\"search-tabs\">" +
|
2022-05-03 09:20:45 -07:00
|
|
|
|
makeTabHeader(0, "In Names", ret_others[1]) +
|
|
|
|
|
"</div>";
|
|
|
|
|
currentTab = 0;
|
2022-04-30 13:05:40 -07:00
|
|
|
|
} else if (results.query.foundElems <= 1 && results.query.returned.length === 0) {
|
2022-12-22 15:52:34 -07:00
|
|
|
|
output += "<div id=\"search-tabs\">" +
|
2022-04-30 13:05:40 -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]) +
|
|
|
|
|
"</div>";
|
|
|
|
|
} else {
|
2022-05-02 09:45:32 -07:00
|
|
|
|
const signatureTabTitle =
|
|
|
|
|
results.query.elems.length === 0 ? "In Function Return Types" :
|
|
|
|
|
results.query.returned.length === 0 ? "In Function Parameters" :
|
|
|
|
|
"In Function Signatures";
|
2022-12-22 15:52:34 -07:00
|
|
|
|
output += "<div id=\"search-tabs\">" +
|
2022-05-02 09:45:32 -07:00
|
|
|
|
makeTabHeader(0, signatureTabTitle, ret_others[1]) +
|
2022-04-30 13:05:40 -07:00
|
|
|
|
"</div>";
|
2022-05-02 15:50:01 -07:00
|
|
|
|
currentTab = 0;
|
2022-01-09 15:12:46 +01:00
|
|
|
|
}
|
2021-05-20 15:14:50 +02:00
|
|
|
|
|
2023-04-15 11:53:50 -07:00
|
|
|
|
if (results.query.correction !== null) {
|
2023-04-19 10:16:14 -07:00
|
|
|
|
const orig = results.query.returned.length > 0
|
|
|
|
|
? results.query.returned[0].name
|
|
|
|
|
: results.query.elems[0].name;
|
|
|
|
|
output += "<h3 class=\"search-corrections\">" +
|
|
|
|
|
`Type "${orig}" not found. ` +
|
2023-04-20 14:32:02 -07:00
|
|
|
|
"Showing results for closest type name " +
|
2023-04-19 10:16:14 -07:00
|
|
|
|
`"${results.query.correction}" instead.</h3>`;
|
2023-04-15 11:53:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const resultsElem = document.createElement("div");
|
2021-05-20 15:14:50 +02:00
|
|
|
|
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-04-24 15:36:02 +02:00
|
|
|
|
const crateSearch = document.getElementById("crate-search");
|
2022-01-12 15:05:47 -08:00
|
|
|
|
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.
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.showResults(search);
|
2022-12-22 15:52:34 -07:00
|
|
|
|
const elems = document.getElementById("search-tabs").childNodes;
|
2022-05-02 15:50:01 -07:00
|
|
|
|
searchState.focusedByTab = [];
|
|
|
|
|
let i = 0;
|
|
|
|
|
for (const elem of elems) {
|
|
|
|
|
const j = i;
|
2022-05-14 13:50:52 +02:00
|
|
|
|
elem.onclick = () => printTab(j);
|
2022-05-02 15:50:01 -07:00
|
|
|
|
searchState.focusedByTab.push(null);
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
printTab(currentTab);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 13:43:55 +02:00
|
|
|
|
function updateSearchHistory(url) {
|
|
|
|
|
if (!browserSupportsHistoryApi()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const params = searchState.getQueryStringParams();
|
|
|
|
|
if (!history.state && !params.search) {
|
|
|
|
|
history.pushState(null, "", url);
|
|
|
|
|
} else {
|
|
|
|
|
history.replaceState(null, "", url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2022-11-28 17:23:50 +01:00
|
|
|
|
const query = parseQuery(searchState.input.value.trim());
|
|
|
|
|
let filterCrates = getFilterCrates();
|
|
|
|
|
|
2022-01-04 15:44:00 +01:00
|
|
|
|
if (!forced && query.userQuery === currentResults) {
|
2022-01-02 19:12:28 +01:00
|
|
|
|
if (query.userQuery.length > 0) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
putBackSearch();
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 17:23:50 +01:00
|
|
|
|
searchState.setLoadingSearch();
|
|
|
|
|
|
|
|
|
|
const params = searchState.getQueryStringParams();
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
|
|
|
|
// 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.
|
2023-06-16 13:43:55 +02:00
|
|
|
|
updateSearchHistory(buildUrl(query.original, filterCrates));
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2021-12-20 15:42:08 +01:00
|
|
|
|
showResults(
|
2022-05-15 21:09:55 -07:00
|
|
|
|
execQuery(query, searchWords, filterCrates, window.currentCrate),
|
2021-12-20 15:42:08 +01:00
|
|
|
|
params.go_to_first,
|
|
|
|
|
filterCrates);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 11:07:16 -07:00
|
|
|
|
/**
|
|
|
|
|
* Convert a list of RawFunctionType / ID to object-based FunctionType.
|
|
|
|
|
*
|
|
|
|
|
* Crates often have lots of functions in them, and it's common to have a large number of
|
|
|
|
|
* functions that operate on a small set of data types, so the search index compresses them
|
|
|
|
|
* by encoding function parameter and return types as indexes into an array of names.
|
|
|
|
|
*
|
|
|
|
|
* Even when a general-purpose compression algorithm is used, this is still a win. I checked.
|
|
|
|
|
* https://github.com/rust-lang/rust/pull/98475#issue-1284395985
|
|
|
|
|
*
|
|
|
|
|
* The format for individual function types is encoded in
|
|
|
|
|
* librustdoc/html/render/mod.rs: impl Serialize for RenderType
|
|
|
|
|
*
|
|
|
|
|
* @param {null|Array<RawFunctionType>} types
|
|
|
|
|
* @param {Array<{name: string, ty: number}>} lowercasePaths
|
|
|
|
|
*
|
|
|
|
|
* @return {Array<FunctionSearchType>}
|
|
|
|
|
*/
|
2023-05-25 12:58:36 -07:00
|
|
|
|
function buildItemSearchTypeAll(types, lowercasePaths) {
|
2022-06-24 17:12:58 -07:00
|
|
|
|
const PATH_INDEX_DATA = 0;
|
|
|
|
|
const GENERICS_DATA = 1;
|
|
|
|
|
return types.map(type => {
|
|
|
|
|
let pathIndex, generics;
|
|
|
|
|
if (typeof type === "number") {
|
|
|
|
|
pathIndex = type;
|
|
|
|
|
generics = [];
|
|
|
|
|
} else {
|
|
|
|
|
pathIndex = type[PATH_INDEX_DATA];
|
2023-04-15 11:53:50 -07:00
|
|
|
|
generics = buildItemSearchTypeAll(
|
|
|
|
|
type[GENERICS_DATA],
|
2023-05-25 12:58:36 -07:00
|
|
|
|
lowercasePaths
|
2023-04-15 11:53:50 -07:00
|
|
|
|
);
|
2022-06-24 17:12:58 -07:00
|
|
|
|
}
|
2023-08-31 18:51:18 +02:00
|
|
|
|
// `0` is used as a sentinel because it's fewer bytes than `null`
|
|
|
|
|
const item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1];
|
2022-06-24 17:12:58 -07:00
|
|
|
|
return {
|
2023-08-31 18:51:18 +02:00
|
|
|
|
id: item === null ? -1 : buildTypeMapIndex(item.name),
|
|
|
|
|
ty: item === null ? null : item.ty,
|
2022-06-24 17:12:58 -07:00
|
|
|
|
generics: generics,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 11:07:16 -07:00
|
|
|
|
/**
|
|
|
|
|
* Convert from RawFunctionSearchType to FunctionSearchType.
|
|
|
|
|
*
|
|
|
|
|
* Crates often have lots of functions in them, and function signatures are sometimes complex,
|
|
|
|
|
* so rustdoc uses a pretty tight encoding for them. This function converts it to a simpler,
|
|
|
|
|
* object-based encoding so that the actual search code is more readable and easier to debug.
|
|
|
|
|
*
|
|
|
|
|
* The raw function search type format is generated using serde in
|
|
|
|
|
* librustdoc/html/render/mod.rs: impl Serialize for IndexItemFunctionType
|
|
|
|
|
*
|
|
|
|
|
* @param {RawFunctionSearchType} functionSearchType
|
|
|
|
|
* @param {Array<{name: string, ty: number}>} lowercasePaths
|
2023-04-15 11:53:50 -07:00
|
|
|
|
* @param {Map<string, integer>}
|
2022-06-27 11:07:16 -07:00
|
|
|
|
*
|
|
|
|
|
* @return {null|FunctionSearchType}
|
|
|
|
|
*/
|
2023-05-25 12:58:36 -07:00
|
|
|
|
function buildFunctionSearchType(functionSearchType, lowercasePaths) {
|
2022-06-24 17:12:58 -07:00
|
|
|
|
const INPUTS_DATA = 0;
|
|
|
|
|
const OUTPUT_DATA = 1;
|
2022-06-27 14:13:13 -07:00
|
|
|
|
// `0` is used as a sentinel because it's fewer bytes than `null`
|
|
|
|
|
if (functionSearchType === 0) {
|
2022-06-24 17:12:58 -07:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-08-31 18:51:18 +02:00
|
|
|
|
let inputs, output, item;
|
2022-06-24 17:12:58 -07:00
|
|
|
|
if (typeof functionSearchType[INPUTS_DATA] === "number") {
|
|
|
|
|
const pathIndex = functionSearchType[INPUTS_DATA];
|
2023-08-31 18:51:18 +02:00
|
|
|
|
item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1];
|
2022-06-24 17:12:58 -07:00
|
|
|
|
inputs = [{
|
2023-08-31 18:51:18 +02:00
|
|
|
|
id: item === null ? -1 : buildTypeMapIndex(item.name),
|
|
|
|
|
ty: item === null ? null : item.ty,
|
2022-06-24 17:12:58 -07:00
|
|
|
|
generics: [],
|
|
|
|
|
}];
|
|
|
|
|
} else {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
inputs = buildItemSearchTypeAll(
|
|
|
|
|
functionSearchType[INPUTS_DATA],
|
2023-05-25 12:58:36 -07:00
|
|
|
|
lowercasePaths
|
2023-04-15 11:53:50 -07:00
|
|
|
|
);
|
2022-06-24 17:12:58 -07:00
|
|
|
|
}
|
|
|
|
|
if (functionSearchType.length > 1) {
|
|
|
|
|
if (typeof functionSearchType[OUTPUT_DATA] === "number") {
|
|
|
|
|
const pathIndex = functionSearchType[OUTPUT_DATA];
|
2023-08-31 18:51:18 +02:00
|
|
|
|
item = pathIndex === 0 ? null : lowercasePaths[pathIndex - 1];
|
2022-06-24 17:12:58 -07:00
|
|
|
|
output = [{
|
2023-08-31 18:51:18 +02:00
|
|
|
|
id: item === null ? -1 : buildTypeMapIndex(item.name),
|
|
|
|
|
ty: item === null ? null : item.ty,
|
2022-06-24 17:12:58 -07:00
|
|
|
|
generics: [],
|
|
|
|
|
}];
|
|
|
|
|
} else {
|
2023-04-15 11:53:50 -07:00
|
|
|
|
output = buildItemSearchTypeAll(
|
|
|
|
|
functionSearchType[OUTPUT_DATA],
|
2023-05-25 12:58:36 -07:00
|
|
|
|
lowercasePaths
|
2023-04-15 11:53:50 -07:00
|
|
|
|
);
|
2022-06-24 17:12:58 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-06-27 11:07:45 -07:00
|
|
|
|
output = [];
|
2022-06-24 17:12:58 -07:00
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
inputs, output,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function buildIndex(rawSearchIndex) {
|
|
|
|
|
searchIndex = [];
|
2021-12-16 21:17:22 -08:00
|
|
|
|
/**
|
2023-04-15 11:53:50 -07:00
|
|
|
|
* List of normalized search words (ASCII lowercased, and undescores removed).
|
|
|
|
|
*
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* @type {Array<string>}
|
|
|
|
|
*/
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const searchWords = [];
|
2023-04-15 11:53:50 -07:00
|
|
|
|
typeNameIdMap = new Map();
|
2023-02-13 13:39:01 -07:00
|
|
|
|
const charA = "A".charCodeAt(0);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let currentIndex = 0;
|
|
|
|
|
let id = 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2023-05-25 12:58:36 -07:00
|
|
|
|
// Initialize type map indexes for primitive list types
|
|
|
|
|
// that can be searched using `[]` syntax.
|
|
|
|
|
typeNameIdOfArray = buildTypeMapIndex("array");
|
|
|
|
|
typeNameIdOfSlice = buildTypeMapIndex("slice");
|
|
|
|
|
typeNameIdOfArrayOrSlice = buildTypeMapIndex("[]");
|
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const crate in rawSearchIndex) {
|
2021-05-14 13:56:15 +02:00
|
|
|
|
if (!hasOwnPropertyRustdoc(rawSearchIndex, crate)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let crateSize = 0;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
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.
|
2023-02-13 13:39:01 -07:00
|
|
|
|
* t[i] contains the type of that item (as a string of characters that represent an
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* 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]".
|
|
|
|
|
*
|
2022-06-24 17:12:58 -07:00
|
|
|
|
* i[i] contains an item's parent, usually a module. For compactness,
|
|
|
|
|
* it is a set of indexes into the `p` array.
|
|
|
|
|
*
|
|
|
|
|
* f[i] contains function signatures, or `0` if the item isn't a function.
|
|
|
|
|
* Functions are themselves encoded as arrays. The first item is a list of
|
|
|
|
|
* types representing the function's inputs, and the second list item is a list
|
|
|
|
|
* of types representing the function's output. Tuples are flattened.
|
|
|
|
|
* Types are also represented as arrays; the first item is an index into the `p`
|
|
|
|
|
* array, while the second is a list of types representing any generic parameters.
|
2021-12-16 21:17:22 -08:00
|
|
|
|
*
|
|
|
|
|
* `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.
|
|
|
|
|
*
|
2022-06-24 17:12:58 -07:00
|
|
|
|
* `p` is a list of path/type pairs. It is used for parents and function parameters.
|
2021-12-16 21:17:22 -08:00
|
|
|
|
*
|
|
|
|
|
* @type {{
|
|
|
|
|
* doc: string,
|
|
|
|
|
* a: Object,
|
|
|
|
|
* n: Array<string>,
|
2023-02-13 13:39:01 -07:00
|
|
|
|
* t: String,
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* d: Array<string>,
|
2023-02-03 00:08:57 -07:00
|
|
|
|
* q: Array<[Number, string]>,
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* i: Array<Number>,
|
2022-06-27 11:07:16 -07:00
|
|
|
|
* f: Array<RawFunctionSearchType>,
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* p: Array<Object>,
|
2023-02-03 00:08:57 -07:00
|
|
|
|
* c: Array<Number>
|
2021-12-16 21:17:22 -08:00
|
|
|
|
* }}
|
|
|
|
|
*/
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const crateCorpus = rawSearchIndex[crate];
|
2021-12-16 21:17:22 -08:00
|
|
|
|
|
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
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const crateRow = {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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, ""),
|
2023-02-03 00:08:57 -07:00
|
|
|
|
deprecated: null,
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
id += 1;
|
|
|
|
|
searchIndex.push(crateRow);
|
|
|
|
|
currentIndex += 1;
|
|
|
|
|
|
2023-02-13 13:39:01 -07:00
|
|
|
|
// a String of one character item type codes
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const itemTypes = crateCorpus.t;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (String) item names
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const itemNames = crateCorpus.n;
|
2023-02-03 00:08:57 -07:00
|
|
|
|
// an array of [(Number) item index,
|
|
|
|
|
// (String) full path]
|
|
|
|
|
// an item whose index is not present will fall back to the previous present path
|
|
|
|
|
// i.e. if indices 4 and 11 are present, but 5-10 and 12-13 are not present,
|
|
|
|
|
// 5-10 will fall back to the path for 4 and 12-13 will fall back to the path for 11
|
|
|
|
|
const itemPaths = new Map(crateCorpus.q);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (String) descriptions
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const 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
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const itemParentIdxs = crateCorpus.i;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of (Object | null) the type of the function, if any
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const itemFunctionSearchTypes = crateCorpus.f;
|
2023-02-03 00:08:57 -07:00
|
|
|
|
// an array of (Number) indices for the deprecated items
|
|
|
|
|
const deprecatedItems = new Set(crateCorpus.c);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// an array of [(Number) item type,
|
|
|
|
|
// (String) name]
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const 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]
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const aliases = crateCorpus.a;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-06-24 17:12:58 -07:00
|
|
|
|
// an array of [{name: String, ty: Number}]
|
|
|
|
|
const lowercasePaths = [];
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// convert `rawPaths` entries into object form
|
2022-06-24 17:12:58 -07:00
|
|
|
|
// generate normalizedPaths for function search mode
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let len = paths.length;
|
2023-02-26 16:26:59 +01:00
|
|
|
|
for (let i = 0; i < len; ++i) {
|
2022-06-24 17:12:58 -07:00
|
|
|
|
lowercasePaths.push({ty: paths[i][0], name: paths[i][1].toLowerCase()});
|
2021-04-11 22:19:29 -07:00
|
|
|
|
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;
|
2022-04-24 15:36:02 +02:00
|
|
|
|
let lastPath = "";
|
2023-02-26 16:26:59 +01:00
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
|
|
|
let word = "";
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// 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();
|
|
|
|
|
}
|
2023-02-26 16:26:59 +01:00
|
|
|
|
searchWords.push(word);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const row = {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
crate: crate,
|
2023-02-13 13:39:01 -07:00
|
|
|
|
ty: itemTypes.charCodeAt(i) - charA,
|
2021-04-11 22:19:29 -07:00
|
|
|
|
name: itemNames[i],
|
2023-02-03 00:08:57 -07:00
|
|
|
|
path: itemPaths.has(i) ? itemPaths.get(i) : lastPath,
|
2021-04-11 22:19:29 -07:00
|
|
|
|
desc: itemDescs[i],
|
|
|
|
|
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
|
2023-04-15 11:53:50 -07:00
|
|
|
|
type: buildFunctionSearchType(
|
|
|
|
|
itemFunctionSearchTypes[i],
|
2023-05-25 12:58:36 -07:00
|
|
|
|
lowercasePaths
|
2023-04-15 11:53:50 -07:00
|
|
|
|
),
|
2021-04-11 22:19:29 -07:00
|
|
|
|
id: id,
|
2021-05-14 13:56:15 +02:00
|
|
|
|
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
|
2023-02-03 00:08:57 -07:00
|
|
|
|
deprecated: deprecatedItems.has(i),
|
2021-04-11 22:19:29 -07:00
|
|
|
|
};
|
|
|
|
|
id += 1;
|
|
|
|
|
searchIndex.push(row);
|
|
|
|
|
lastPath = row.path;
|
|
|
|
|
crateSize += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aliases) {
|
2023-04-13 16:30:02 -07:00
|
|
|
|
const currentCrateAliases = new Map();
|
|
|
|
|
ALIASES.set(crate, currentCrateAliases);
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const 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
|
|
|
|
|
2023-04-13 16:30:02 -07:00
|
|
|
|
let currentNameAliases;
|
|
|
|
|
if (currentCrateAliases.has(alias_name)) {
|
|
|
|
|
currentNameAliases = currentCrateAliases.get(alias_name);
|
|
|
|
|
} else {
|
|
|
|
|
currentNameAliases = [];
|
|
|
|
|
currentCrateAliases.set(alias_name, currentNameAliases);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
for (const local_alias of aliases[alias_name]) {
|
2023-04-13 16:30:02 -07:00
|
|
|
|
currentNameAliases.push(local_alias + currentIndex);
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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() {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const search_input = searchState.input;
|
2022-01-10 14:57:43 +01:00
|
|
|
|
if (!searchState.input) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-19 22:36:15 +01:00
|
|
|
|
if (search_input.value !== "" && !searchState.isDisplayed()) {
|
|
|
|
|
searchState.showResults();
|
|
|
|
|
if (browserSupportsHistoryApi()) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
history.replaceState(null, "",
|
|
|
|
|
buildUrl(search_input.value, getFilterCrates()));
|
|
|
|
|
}
|
|
|
|
|
document.title = searchState.title;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
function registerSearchEvents() {
|
2022-05-15 21:09:55 -07:00
|
|
|
|
const params = searchState.getQueryStringParams();
|
|
|
|
|
|
|
|
|
|
// 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!
|
|
|
|
|
if (searchState.input.value === "") {
|
|
|
|
|
searchState.input.value = params.search || "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
const searchAfter500ms = () => {
|
2021-04-12 23:50:18 -07:00
|
|
|
|
searchState.clearInputTimeout();
|
|
|
|
|
if (searchState.input.value.length === 0) {
|
|
|
|
|
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;
|
2022-05-03 12:03:17 +08:00
|
|
|
|
searchState.input.onchange = 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
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
searchState.outputElement().addEventListener("keydown", e => {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
// 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
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const previous = document.activeElement.previousElementSibling;
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (previous) {
|
|
|
|
|
previous.focus();
|
|
|
|
|
} else {
|
|
|
|
|
searchState.focus();
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
} else if (e.which === 40) { // down
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const next = document.activeElement.nextElementSibling;
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (next) {
|
|
|
|
|
next.focus();
|
|
|
|
|
}
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const rect = document.activeElement.getBoundingClientRect();
|
2021-05-09 12:56:21 -07:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
searchState.input.addEventListener("keydown", e => {
|
2021-05-09 12:56:21 -07:00
|
|
|
|
if (e.which === 40) { // down
|
|
|
|
|
focusSearchResult();
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
searchState.input.addEventListener("focus", () => {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
putBackSearch();
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
searchState.input.addEventListener("blur", () => {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
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.
|
2022-01-19 22:36:15 +01:00
|
|
|
|
if (browserSupportsHistoryApi()) {
|
2021-04-11 22:19:29 -07:00
|
|
|
|
// Store the previous <title> so we can revert back to it later.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const previousTitle = document.title;
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
|
window.addEventListener("popstate", e => {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const 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.
|
2022-05-03 12:03:17 +08:00
|
|
|
|
window.onpageshow = () => {
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const qSearch = searchState.getQueryStringParams().search;
|
2021-04-12 23:50:18 -07:00
|
|
|
|
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-07-03 20:10:19 +02:00
|
|
|
|
if (ev.target.value === "all crates") {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
// If we don't remove it from the URL, it'll be picked up again by the search.
|
2022-04-24 15:36:02 +02:00
|
|
|
|
const query = searchState.input.value.trim();
|
2023-06-16 13:43:55 +02:00
|
|
|
|
updateSearchHistory(buildUrl(query, null));
|
2022-01-10 14:57:43 +01:00
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 14:12:12 +02:00
|
|
|
|
/**
|
|
|
|
|
* @type {Array<string>}
|
|
|
|
|
*/
|
|
|
|
|
const searchWords = buildIndex(rawSearchIndex);
|
2022-05-15 21:09:55 -07:00
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
registerSearchEvents();
|
2022-01-10 14:57:43 +01:00
|
|
|
|
// If there's a search term in the URL, execute the search now.
|
2022-05-15 21:09:55 -07:00
|
|
|
|
if (window.searchState.getQueryStringParams().search) {
|
2022-01-10 14:57:43 +01:00
|
|
|
|
search();
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
}
|
2022-01-10 14:57:43 +01:00
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
if (typeof exports !== "undefined") {
|
|
|
|
|
exports.initSearch = initSearch;
|
|
|
|
|
exports.execQuery = execQuery;
|
|
|
|
|
exports.parseQuery = parseQuery;
|
|
|
|
|
}
|
|
|
|
|
return searchWords;
|
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
|
window.initSearch = initSearch;
|
|
|
|
|
if (window.searchIndex !== undefined) {
|
|
|
|
|
initSearch(window.searchIndex);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Running in Node, not a browser. Run initSearch just to produce the
|
|
|
|
|
// exports.
|
|
|
|
|
initSearch({});
|
2021-04-13 14:59:54 -07:00
|
|
|
|
}
|
2021-04-11 22:19:29 -07:00
|
|
|
|
|
2022-05-15 21:09:55 -07:00
|
|
|
|
|
2021-04-11 22:19:29 -07:00
|
|
|
|
})();
|