rustdoc-search: show types signatures in results

This commit is contained in:
Michael Howell
2024-09-24 14:31:44 -07:00
parent 5973005d93
commit 20a4b4fea1
10 changed files with 995 additions and 126 deletions

View File

@@ -2,6 +2,14 @@
const fs = require("fs");
const path = require("path");
function arrayToCode(array) {
return array.map((value, index) => {
value = value.split(" ").join(" ");
return (index % 2 === 1) ? ("`" + value + "`") : value;
}).join("");
}
function loadContent(content) {
const Module = module.constructor;
const m = new Module();
@@ -180,15 +188,7 @@ function valueCheck(fullPath, expected, result, error_text, queryName) {
if (!result_v.forEach) {
throw result_v;
}
result_v.forEach((value, index) => {
value = value.split(" ").join(" ");
if (index % 2 === 1) {
result_v[index] = "`" + value + "`";
} else {
result_v[index] = value;
}
});
result_v = result_v.join("");
result_v = arrayToCode(result_v);
}
const obj_path = fullPath + (fullPath.length > 0 ? "." : "") + key;
valueCheck(obj_path, expected[key], result_v, error_text, queryName);
@@ -436,9 +436,41 @@ function loadSearchJS(doc_folder, resource_suffix) {
searchModule.initSearch(searchIndex.searchIndex);
const docSearch = searchModule.docSearch;
return {
doSearch: function(queryStr, filterCrate, currentCrate) {
return docSearch.execQuery(searchModule.parseQuery(queryStr),
doSearch: async function(queryStr, filterCrate, currentCrate) {
const result = await docSearch.execQuery(searchModule.parseQuery(queryStr),
filterCrate, currentCrate);
for (const tab in result) {
if (!Object.prototype.hasOwnProperty.call(result, tab)) {
continue;
}
if (!(result[tab] instanceof Array)) {
continue;
}
for (const entry of result[tab]) {
for (const key in entry) {
if (!Object.prototype.hasOwnProperty.call(entry, key)) {
continue;
}
if (key === "displayTypeSignature") {
const {type, mappedNames, whereClause} =
await entry.displayTypeSignature;
entry.displayType = arrayToCode(type);
entry.displayMappedNames = [...mappedNames.entries()]
.map(([name, qname]) => {
return `${name} = ${qname}`;
}).join(", ");
entry.displayWhereClause = [...whereClause.entries()]
.flatMap(([name, value]) => {
if (value.length === 0) {
return [];
}
return [`${name}: ${arrayToCode(value)}`];
}).join(", ");
}
}
}
}
return result;
},
getCorrections: function(queryStr, filterCrate, currentCrate) {
const parsedQuery = searchModule.parseQuery(queryStr);