Skip to content

Commit

Permalink
rustdoc: allow searches to match against both type and name
Browse files Browse the repository at this point in the history
repurposes existing syntax that previously had a nonsese meaning.

now `fn:add, u8 -> u8` searches for fn items with "add" in the name,
that take a `u8` argument and return a `u8`.

the kind is included in anticipation that type based searches will
soon work on items other than functions and methods.

fixes #131130
  • Loading branch information
lolbinarycat committed Oct 17, 2024
1 parent bed75e7 commit c4e90b8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/js/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ let ParserState;
* userQuery: string,
* typeFilter: number,
* elems: Array<QueryElement>,
* args: Array<QueryElement>,
* returned: Array<QueryElement>,
* extraNameElem: QueryElement | null,
* foundElems: number,
* totalElems: number,
* literalSearch: boolean,
Expand Down
56 changes: 52 additions & 4 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const itemTypes = [
// used for special search precedence
const TY_GENERIC = itemTypes.indexOf("generic");
const TY_IMPORT = itemTypes.indexOf("import");
// used for isType
const TY_STRUCT = itemTypes.indexOf("struct");
const TY_ENUM = itemTypes.indexOf("enum");
const TY_UNION = itemTypes.indexOf("union");
const TY_PRIMATIVE = itemTypes.indexOf("primative");
const TY_FOREIGN_TYPE = itemTypes.indexOf("foreigntype");
const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../";

// Hard limit on how deep to recurse into generics when doing type-driven search.
Expand Down Expand Up @@ -239,6 +245,18 @@ function prevIs(parserState, lookingFor) {
return false;
}

function isType(ty) {
let r = ty === TY_STRUCT || ty === TY_ENUM || ty === TY_UNION || ty === TY_PRIMATIVE || ty === TY_FOREIGN_TYPE || ty === -1;

Check failure on line 249 in src/librustdoc/html/static/js/search.js

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
return r;
}

/**
* This function removes any queryElem that cannot be a function argument.
*/
function filterOnlyTypes(elems) {
return elems.filter((elem) => isType(elem.typeFilter));
}

/**
* Returns `true` if the last element in the `elems` argument has generics.
*
Expand Down Expand Up @@ -1800,6 +1818,8 @@ class DocSearch {
correction: null,
proposeCorrectionFrom: null,
proposeCorrectionTo: null,
// used for type-and-name searches
extraNameElem: null,
// bloom filter build from type ids
typeFingerprint: new Uint32Array(4),
};
Expand Down Expand Up @@ -1932,6 +1952,17 @@ class DocSearch {
query.literalSearch = parserState.totalElems > 1;
}
query.foundElems = query.elems.length + query.returned.length;
if (query.returned.length > 0 || query.elems.length > 1) {
for (const elem of query.elems) {
if (!isType(elem.typeFilter)) {
query.extraNameElem = elem;
query.elems = filterOnlyTypes(query.elems);
query.hasReturnArrow = true;
console.log(query.elems);
break;
}
}
}
query.totalElems = parserState.totalElems;
return query;
}
Expand All @@ -1946,6 +1977,7 @@ class DocSearch {
* @return {ResultsTable}
*/
async execQuery(parsedQuery, filterCrates, currentCrate) {
console.log(parsedQuery);

Check failure on line 1980 in src/librustdoc/html/static/js/search.js

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

tab character
const results_others = new Map(), results_in_args = new Map(),
results_returned = new Map();

Expand Down Expand Up @@ -3047,7 +3079,7 @@ class DocSearch {
* @param {integer} pos - Position in the `searchIndex`.
* @param {Object} results
*/
function handleArgs(row, pos, results) {
function handleArgs(row, pos, results, maxEditDistance) {
if (!row || (filterCrates !== null && row.crate !== filterCrates) || !row.type) {
return;
}
Expand Down Expand Up @@ -3083,9 +3115,25 @@ class DocSearch {
)) {
return;
}


Check failure on line 3118 in src/librustdoc/html/static/js/search.js

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
let name_dist = 0;
let name_elem = parsedQuery.extraNameElem;
let path_dist = tfpDist;
if (name_elem !== null) {
if (!typePassesFilter(name_elem.typeFilter, row.ty)) {
return;
}
name_dist = editDistance(row.normalizedName, name_elem.normalizedPathLast, maxEditDistance);

Check failure on line 3126 in src/librustdoc/html/static/js/search.js

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
if (row.normalizedName.includes(name_elem.normalizedPathLast)) {
name_dist = name_dist / 3;
}
if (name_dist > maxEditDistance) {
return;
}
path_dist = checkPath(name_elem.fullPath, row);
}
results.max_dist = Math.max(results.max_dist || 0, tfpDist);
addIntoResults(results, row.id, pos, 0, tfpDist, 0, Number.MAX_VALUE);
addIntoResults(results, row.id, pos, name_dist, (path_dist + tfpDist)/2, 0, Number.MAX_VALUE);

Check failure on line 3136 in src/librustdoc/html/static/js/search.js

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

line longer than 100 chars
}

/**
Expand Down Expand Up @@ -3285,7 +3333,7 @@ class DocSearch {
parsedQuery.elems.sort(sortQ);
parsedQuery.returned.sort(sortQ);
for (let i = 0, nSearchIndex = this.searchIndex.length; i < nSearchIndex; ++i) {
handleArgs(this.searchIndex[i], i, results_others);
handleArgs(this.searchIndex[i], i, results_others, maxEditDistance);
}
}
};
Expand Down
7 changes: 7 additions & 0 deletions tests/rustdoc-js-std/type-and-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const EXPECTED = {
'query': 'method:ascii, char -> bool',
'others': [
{ 'path': 'char', 'name': 'is_ascii_digit' },
{ 'path': 'char', 'name': 'eq_ignore_ascii_case' },
],
}

0 comments on commit c4e90b8

Please sign in to comment.