rustdoc-search: allow trailing Foo -> arg search

This commit is contained in:
Michael Howell 2024-09-05 17:58:05 -07:00
parent 9c01301c52
commit 3699e939e8
5 changed files with 81 additions and 14 deletions

View File

@ -41,6 +41,7 @@ let ParserState;
* foundElems: number, * foundElems: number,
* totalElems: number, * totalElems: number,
* literalSearch: boolean, * literalSearch: boolean,
* hasReturnArrow: boolean,
* corrections: Array<{from: string, to: integer}> | null, * corrections: Array<{from: string, to: integer}> | null,
* typeFingerprint: Uint32Array, * typeFingerprint: Uint32Array,
* error: Array<string> | null, * error: Array<string> | null,

View File

@ -657,7 +657,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
} }
const typeFilter = parserState.typeFilter; const typeFilter = parserState.typeFilter;
parserState.typeFilter = null; parserState.typeFilter = null;
if (name === "!") { if (name.trim() === "!") {
if (typeFilter !== null && typeFilter !== "primitive") { if (typeFilter !== null && typeFilter !== "primitive") {
throw [ throw [
"Invalid search type: primitive never type ", "Invalid search type: primitive never type ",
@ -1795,6 +1795,7 @@ class DocSearch {
// Total number of elements (includes generics). // Total number of elements (includes generics).
totalElems: 0, totalElems: 0,
literalSearch: false, literalSearch: false,
hasReturnArrow: false,
error: null, error: null,
correction: null, correction: null,
proposeCorrectionFrom: null, proposeCorrectionFrom: null,
@ -1823,6 +1824,7 @@ class DocSearch {
continue; continue;
} else if (c === "-" || c === ">") { } else if (c === "-" || c === ">") {
if (isReturnArrow(parserState)) { if (isReturnArrow(parserState)) {
query.hasReturnArrow = true;
break; break;
} }
throw ["Unexpected ", c, " (did you mean ", "->", "?)"]; throw ["Unexpected ", c, " (did you mean ", "->", "?)"];
@ -1889,9 +1891,7 @@ class DocSearch {
// Get returned elements. // Get returned elements.
getItemsBefore(query, parserState, query.returned, ""); getItemsBefore(query, parserState, query.returned, "");
// Nothing can come afterward! // Nothing can come afterward!
if (query.returned.length === 0) { query.hasReturnArrow = true;
throw ["Expected at least one item after ", "->"];
}
break; break;
} else { } else {
parserState.pos += 1; parserState.pos += 1;
@ -3249,7 +3249,7 @@ class DocSearch {
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps); this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps);
} }
if (parsedQuery.foundElems === 1 && parsedQuery.returned.length === 0) { if (parsedQuery.foundElems === 1 && !parsedQuery.hasReturnArrow) {
if (parsedQuery.elems.length === 1) { if (parsedQuery.elems.length === 1) {
const elem = parsedQuery.elems[0]; const elem = parsedQuery.elems[0];
const length = this.searchIndex.length; const length = this.searchIndex.length;

View File

@ -251,15 +251,6 @@ const PARSED = [
userQuery: "ab'", userQuery: "ab'",
error: "Unexpected `'` after `b` (not a valid identifier)", error: "Unexpected `'` after `b` (not a valid identifier)",
}, },
{
query: "a->",
elems: [],
foundElems: 0,
original: "a->",
returned: [],
userQuery: "a->",
error: "Expected at least one item after `->`",
},
{ {
query: '"p" <a>', query: '"p" <a>',
elems: [], elems: [],

View File

@ -94,4 +94,72 @@ const PARSED = [
userQuery: "-> !", userQuery: "-> !",
error: null, error: null,
}, },
{
query: "a->",
elems: [{
name: "a",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
generics: [],
typeFilter: -1,
}],
foundElems: 1,
original: "a->",
returned: [],
userQuery: "a->",
hasReturnArrow: true,
error: null,
},
{
query: "!->",
elems: [{
name: "never",
fullPath: ["never"],
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 1,
}],
foundElems: 1,
original: "!->",
returned: [],
userQuery: "!->",
hasReturnArrow: true,
error: null,
},
{
query: "! ->",
elems: [{
name: "never",
fullPath: ["never"],
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 1,
}],
foundElems: 1,
original: "! ->",
returned: [],
userQuery: "! ->",
hasReturnArrow: true,
error: null,
},
{
query: "primitive:!->",
elems: [{
name: "never",
fullPath: ["never"],
pathWithoutLast: [],
pathLast: "never",
generics: [],
typeFilter: 1,
}],
foundElems: 1,
original: "primitive:!->",
returned: [],
userQuery: "primitive:!->",
hasReturnArrow: true,
error: null,
},
]; ];

View File

@ -1,6 +1,13 @@
// exact-check // exact-check
const EXPECTED = [ const EXPECTED = [
{
'query': '! ->',
'others': [
{ 'path': 'never_search', 'name': 'impossible' },
{ 'path': 'never_search', 'name': 'box_impossible' },
],
},
{ {
'query': '-> !', 'query': '-> !',
'others': [ 'others': [