Skip to content

Commit 5ed224d

Browse files
committed
Update latest search core
1 parent b9480e7 commit 5ed224d

File tree

3 files changed

+63
-54
lines changed

3 files changed

+63
-54
lines changed

extension/search/docs/base.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import DocSearchV2 from "./base-v2.js";
1+
import LibrustDocSearch from "./core.js";
22

3-
export default class DocSearch extends DocSearchV2 {
3+
export default class DocSearch extends LibrustDocSearch {
44
// The searchIndex should be Map([[crate, shards],) format
55
constructor(name, searchIndex, rootPath, descShards) {
66
super(searchIndex, rootPath, descShards);
@@ -25,7 +25,7 @@ export default class DocSearch extends DocSearchV2 {
2525

2626
async search(query) {
2727
if (!query) return [];
28-
let result = await this.execQuery(DocSearchV2.parseQuery(query), null, this.name);
28+
let result = await this.execQuery(LibrustDocSearch.parseQuery(query), null, this.name);
2929
return result.others || [];
3030
}
3131
}
Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
648648
}
649649
const typeFilter = parserState.typeFilter;
650650
parserState.typeFilter = null;
651-
if (name === "!") {
651+
if (name.trim() === "!") {
652652
if (typeFilter !== null && typeFilter !== "primitive") {
653653
throw [
654654
"Invalid search type: primitive never type ",
@@ -916,10 +916,9 @@ class VlqHexDecoder {
916916
}
917917
// call after consuming `{`
918918
decodeList() {
919-
const cb = "}".charCodeAt(0);
920919
let c = this.string.charCodeAt(this.offset);
921920
const ret = [];
922-
while (c !== cb) {
921+
while (c !== 125) { // 125 = "}"
923922
ret.push(this.decode());
924923
c = this.string.charCodeAt(this.offset);
925924
}
@@ -928,14 +927,13 @@ class VlqHexDecoder {
928927
}
929928
// consumes and returns a list or integer
930929
decode() {
931-
const [ob, la] = ["{", "`"].map(c => c.charCodeAt(0));
932930
let n = 0;
933931
let c = this.string.charCodeAt(this.offset);
934-
if (c === ob) {
932+
if (c === 123) { // 123 = "{"
935933
this.offset += 1;
936934
return this.decodeList();
937935
}
938-
while (c < la) {
936+
while (c < 96) { // 96 = "`"
939937
n = (n << 4) | (c & 0xF);
940938
this.offset += 1;
941939
c = this.string.charCodeAt(this.offset);
@@ -948,15 +946,14 @@ class VlqHexDecoder {
948946
}
949947
next() {
950948
const c = this.string.charCodeAt(this.offset);
951-
const [zero, ua, la] = ["0", "@", "`"].map(c => c.charCodeAt(0));
952949
// sixteen characters after "0" are backref
953-
if (c >= zero && c < ua) {
950+
if (c >= 48 && c < 64) { // 48 = "0", 64 = "@"
954951
this.offset += 1;
955-
return this.backrefQueue[c - zero];
952+
return this.backrefQueue[c - 48];
956953
}
957954
// special exception: 0 doesn't use backref encoding
958955
// it's already one character, and it's always nullish
959-
if (c === la) {
956+
if (c === 96) { // 96 = "`"
960957
this.offset += 1;
961958
return this.cons(0);
962959
}
@@ -1087,7 +1084,7 @@ class RoaringBitmapBits {
10871084
}
10881085

10891086

1090-
export default class DocSearchV2 {
1087+
export default class LibrustDocSearch {
10911088
constructor(rawSearchIndex, rootPath, searchState) {
10921089
/**
10931090
* @type {Map<String, RoaringBitmap>}
@@ -1510,7 +1507,6 @@ export default class DocSearchV2 {
15101507
};
15111508

15121509
const searchIndex = [];
1513-
const charA = "A".charCodeAt(0);
15141510
let currentIndex = 0;
15151511
let id = 0;
15161512

@@ -1586,8 +1582,10 @@ export default class DocSearchV2 {
15861582
// An array of [(Number) item index, (Number) path index]
15871583
// Used to de-duplicate inlined and re-exported stuff
15881584
const itemReexports = new Map(crateCorpus.r);
1585+
// librustdoc changed crateCorpus.i from array to string, we need some compatible code.
1586+
const newCorpusFormat = typeof crateCorpus.i === "string";
15891587
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
1590-
const itemParentIdxs = crateCorpus.i;
1588+
const itemParentIdxDecoder = new VlqHexDecoder(crateCorpus.i, noop => noop);
15911589
// a map Number, string for impl disambiguators
15921590
const implDisambiguator = new Map(crateCorpus.b);
15931591
// an array of [(Number) item type,
@@ -1634,6 +1632,8 @@ export default class DocSearchV2 {
16341632
// faster analysis operations
16351633
lastPath = "";
16361634
len = itemTypes.length;
1635+
let lastName = "";
1636+
let lastWord = "";
16371637
for (let i = 0; i < len; ++i) {
16381638
const bitIndex = i + 1;
16391639
if (descIndex >= descShard.len &&
@@ -1649,10 +1649,8 @@ export default class DocSearchV2 {
16491649
descIndex = 0;
16501650
descShardList.push(descShard);
16511651
}
1652-
let word = "";
1653-
if (typeof itemNames[i] === "string") {
1654-
word = itemNames[i].toLowerCase();
1655-
}
1652+
const name = itemNames[i] === "" ? lastName : itemNames[i];
1653+
const word = itemNames[i] === "" ? lastWord : itemNames[i].toLowerCase();
16561654
const path = itemPaths.has(i) ? itemPaths.get(i) : lastPath;
16571655
const type = itemFunctionDecoder.next();
16581656
if (type !== null) {
@@ -1674,16 +1672,17 @@ export default class DocSearchV2 {
16741672
}
16751673
// This object should have exactly the same set of fields as the "crateRow"
16761674
// object defined above.
1675+
const itemParentIdx = newCorpusFormat ? itemParentIdxDecoder.next(): crateCorpus.i[i];
16771676
const row = {
16781677
crate,
1679-
ty: itemTypes.charCodeAt(i) - charA,
1680-
name: itemNames[i],
1678+
ty: itemTypes.charCodeAt(i) - 65, // 65 = "A"
1679+
name: newCorpusFormat? name : itemNames[i],
16811680
path,
16821681
descShard,
16831682
descIndex,
16841683
exactPath: itemReexports.has(i) ?
16851684
itemPaths.get(itemReexports.get(i)) : path,
1686-
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
1685+
parent: itemParentIdx > 0 ? paths[itemParentIdx - 1] : undefined,
16871686
type,
16881687
id,
16891688
word,
@@ -1698,6 +1697,8 @@ export default class DocSearchV2 {
16981697
if (!this.searchIndexEmptyDesc.get(crate).contains(bitIndex)) {
16991698
descIndex += 1;
17001699
}
1700+
lastName = name;
1701+
lastWord = word;
17011702
}
17021703

17031704
if (aliases) {
@@ -1787,6 +1788,7 @@ export default class DocSearchV2 {
17871788
// Total number of elements (includes generics).
17881789
totalElems: 0,
17891790
literalSearch: false,
1791+
hasReturnArrow: false,
17901792
error: null,
17911793
correction: null,
17921794
proposeCorrectionFrom: null,
@@ -1815,6 +1817,7 @@ export default class DocSearchV2 {
18151817
continue;
18161818
} else if (c === "-" || c === ">") {
18171819
if (isReturnArrow(parserState)) {
1820+
query.hasReturnArrow = true;
18181821
break;
18191822
}
18201823
throw ["Unexpected ", c, " (did you mean ", "->", "?)"];
@@ -1881,9 +1884,7 @@ export default class DocSearchV2 {
18811884
// Get returned elements.
18821885
getItemsBefore(query, parserState, query.returned, "");
18831886
// Nothing can come afterward!
1884-
if (query.returned.length === 0) {
1885-
throw ["Expected at least one item after ", "->"];
1886-
}
1887+
query.hasReturnArrow = true;
18871888
break;
18881889
} else {
18891890
parserState.pos += 1;
@@ -2087,8 +2088,9 @@ export default class DocSearchV2 {
20872088
* @param {string} preferredCrate
20882089
* @returns {Promise<[ResultObject]>}
20892090
*/
2090-
const sortResults = async (results, isType, preferredCrate) => {
2091+
const sortResults = async(results, isType, preferredCrate) => {
20912092
const userQuery = parsedQuery.userQuery;
2093+
const casedUserQuery = parsedQuery.original;
20922094
const result_list = [];
20932095
for (const result of results.values()) {
20942096
result.item = this.searchIndex[result.id];
@@ -2099,6 +2101,13 @@ export default class DocSearchV2 {
20992101
result_list.sort((aaa, bbb) => {
21002102
let a, b;
21012103

2104+
// sort by exact case-sensitive match
2105+
a = (aaa.item.name !== casedUserQuery);
2106+
b = (bbb.item.name !== casedUserQuery);
2107+
if (a !== b) {
2108+
return a - b;
2109+
}
2110+
21022111
// sort by exact match with regard to the last word (mismatch goes later)
21032112
a = (aaa.word !== userQuery);
21042113
b = (bbb.word !== userQuery);
@@ -2822,7 +2831,7 @@ export default class DocSearchV2 {
28222831
};
28232832
}
28242833

2825-
const handleAliases = async (ret, query, filterCrates, currentCrate) => {
2834+
const handleAliases = async(ret, query, filterCrates, currentCrate) => {
28262835
const lowerQuery = query.toLowerCase();
28272836
// We separate aliases and crate aliases because we want to have current crate
28282837
// aliases to be before the others in the displayed results.
@@ -2964,30 +2973,30 @@ export default class DocSearchV2 {
29642973

29652974
// fpDist is a minimum possible type distance, where "type distance" is the number of
29662975
// atoms in the function not present in the query
2967-
const tfpDist = compareTypeFingerprints(
2968-
fullId,
2969-
parsedQuery.typeFingerprint,
2970-
);
2971-
if (tfpDist !== null) {
2972-
const in_args = row.type && row.type.inputs
2973-
&& checkIfInList(row.type.inputs, elem, row.type.where_clause, null, 0);
2974-
const returned = row.type && row.type.output
2975-
&& checkIfInList(row.type.output, elem, row.type.where_clause, null, 0);
2976-
if (in_args) {
2977-
results_in_args.max_dist = Math.max(results_in_args.max_dist || 0, tfpDist);
2978-
const maxDist = results_in_args.size < MAX_RESULTS ?
2979-
(tfpDist + 1) :
2980-
results_in_args.max_dist;
2981-
addIntoResults(results_in_args, fullId, pos, -1, tfpDist, 0, maxDist);
2982-
}
2983-
if (returned) {
2984-
results_returned.max_dist = Math.max(results_returned.max_dist || 0, tfpDist);
2985-
const maxDist = results_returned.size < MAX_RESULTS ?
2986-
(tfpDist + 1) :
2987-
results_returned.max_dist;
2988-
addIntoResults(results_returned, fullId, pos, -1, tfpDist, 0, maxDist);
2989-
}
2990-
}
2976+
// const tfpDist = compareTypeFingerprints(
2977+
// fullId,
2978+
// parsedQuery.typeFingerprint,
2979+
// );
2980+
// if (tfpDist !== null) {
2981+
// const in_args = row.type && row.type.inputs
2982+
// && checkIfInList(row.type.inputs, elem, row.type.where_clause, null, 0);
2983+
// const returned = row.type && row.type.output
2984+
// && checkIfInList(row.type.output, elem, row.type.where_clause, null, 0);
2985+
// if (in_args) {
2986+
// results_in_args.max_dist = Math.max(results_in_args.max_dist || 0, tfpDist);
2987+
// const maxDist = results_in_args.size < MAX_RESULTS ?
2988+
// (tfpDist + 1) :
2989+
// results_in_args.max_dist;
2990+
// addIntoResults(results_in_args, fullId, pos, -1, tfpDist, 0, maxDist);
2991+
// }
2992+
// if (returned) {
2993+
// results_returned.max_dist = Math.max(results_returned.max_dist || 0, tfpDist);
2994+
// const maxDist = results_returned.size < MAX_RESULTS ?
2995+
// (tfpDist + 1) :
2996+
// results_returned.max_dist;
2997+
// addIntoResults(results_returned, fullId, pos, -1, tfpDist, 0, maxDist);
2998+
// }
2999+
// }
29913000

29923001
if (!typePassesFilter(elem.typeFilter, row.ty)) {
29933002
return;
@@ -3233,7 +3242,7 @@ export default class DocSearchV2 {
32333242
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps);
32343243
}
32353244

3236-
if (parsedQuery.foundElems === 1 && parsedQuery.returned.length === 0) {
3245+
if (parsedQuery.foundElems === 1 && !parsedQuery.hasReturnArrow) {
32373246
if (parsedQuery.elems.length === 1) {
32383247
const elem = parsedQuery.elems[0];
32393248
const length = this.searchIndex.length;

0 commit comments

Comments
 (0)