Skip to content

Commit

Permalink
rustdoc: fold path to lower case, but preserve original name
Browse files Browse the repository at this point in the history
This fixes the "About This Result" page so that it shows the case
of type parameters in the query.
  • Loading branch information
notriddle committed Jun 8, 2024
1 parent 5522634 commit bccb9f7
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 359 deletions.
32 changes: 19 additions & 13 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ function initSearch(rawSearchIndex) {
} else if (quadcolon !== null) {
throw ["Unexpected ", quadcolon[0]];
}
const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/);
const pathSegments = path.split(/(?:::\s*)|(?:\s+(?:::\s*)?)/).map(x => x.toLowerCase());
// In case we only have something like `<p>`, there is no name.
if (pathSegments.length === 0 || (pathSegments.length === 1 && pathSegments[0] === "")) {
if (generics.length > 0 || prevIs(parserState, ">")) {
Expand Down Expand Up @@ -624,7 +624,10 @@ function initSearch(rawSearchIndex) {
if (gen.name !== null) {
gen.bindingName.generics.unshift(gen);
}
bindings.set(gen.bindingName.name, gen.bindingName.generics);
bindings.set(
gen.bindingName.name.toLowerCase().replace(/_/g, ""),
gen.bindingName.generics,
);
return false;
}
return true;
Expand Down Expand Up @@ -1204,8 +1207,7 @@ function initSearch(rawSearchIndex) {
*/
function newParsedQuery(userQuery) {
return {
original: userQuery,
userQuery: userQuery.toLowerCase(),
userQuery: userQuery,
elems: [],
returned: [],
// Total number of "top" elements (does not include generics).
Expand Down Expand Up @@ -1297,7 +1299,7 @@ function initSearch(rawSearchIndex) {
genericsElems: 0,
typeFilter: null,
isInBinding: null,
userQuery: userQuery.toLowerCase(),
userQuery,
};
let query = newParsedQuery(userQuery);

Expand Down Expand Up @@ -2781,21 +2783,25 @@ function initSearch(rawSearchIndex) {
if ((elem.id === null && parsedQuery.totalElems > 1 && elem.typeFilter === -1
&& elem.generics.length === 0 && elem.bindings.size === 0)
|| elem.typeFilter === TY_GENERIC) {
if (genericSymbols.has(elem.name)) {
elem.id = genericSymbols.get(elem.name);
if (genericSymbols.has(elem.normalizedPathLast)) {
elem.id = genericSymbols.get(elem.normalizedPathLast);
} else {
elem.id = -(genericSymbols.size + 1);
genericSymbols.set(elem.name, elem.id);
genericSymbols.set(elem.normalizedPathLast, elem.id);
}
if (elem.typeFilter === -1 && elem.name.length >= 3) {
if (elem.typeFilter === -1 && elem.normalizedPathLast.length >= 3) {
// Silly heuristic to catch if the user probably meant
// to not write a generic parameter. We don't use it,
// just bring it up.
const maxPartDistance = Math.floor(elem.name.length / 3);
const maxPartDistance = Math.floor(elem.normalizedPathLast.length / 3);
let matchDist = maxPartDistance + 1;
let matchName = "";
for (const name of typeNameIdMap.keys()) {
const dist = editDistance(name, elem.name, maxPartDistance);
const dist = editDistance(
name,
elem.normalizedPathLast,
maxPartDistance,
);
if (dist <= matchDist && dist <= maxPartDistance) {
if (dist === matchDist && matchName > name) {
continue;
Expand Down Expand Up @@ -3356,11 +3362,11 @@ ${item.displayPath}<span class="${type}">${name}</span>\
}

// Update document title to maintain a meaningful browser history
searchState.title = "Results for " + query.original + " - Rust";
searchState.title = "Results for " + query.userQuery + " - Rust";

// Because searching is incremental by character, only the most
// recent search query is added to the browser history.
updateSearchHistory(buildUrl(query.original, filterCrates));
updateSearchHistory(buildUrl(query.userQuery, filterCrates));

await showResults(
await execQuery(query, filterCrates, window.currentCrate),
Expand Down
1 change: 0 additions & 1 deletion src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function checkNeededFields(fullPath, expected, error_text, queryName, position)
if (fullPath.length === 0) {
fieldsToCheck = [
"foundElems",
"original",
"returned",
"userQuery",
"error",
Expand Down
12 changes: 6 additions & 6 deletions tests/rustdoc-gui/search-corrections.goml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ assert-css: (".search-corrections", {
})
assert-text: (
".search-corrections",
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
)

// Corrections do get shown on the "In Return Type" tab.
Expand All @@ -35,7 +35,7 @@ assert-css: (".search-corrections", {
})
assert-text: (
".search-corrections",
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
)

// Now, explicit return values
Expand All @@ -52,7 +52,7 @@ assert-css: (".search-corrections", {
})
assert-text: (
".search-corrections",
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
)

// Now, generic correction
Expand All @@ -69,7 +69,7 @@ assert-css: (".search-corrections", {
})
assert-text: (
".search-corrections",
"Type \"notablestructwithlongnamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
"Type \"NotableStructWithLongNamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
)

// Now, generic correction plus error
Expand All @@ -86,7 +86,7 @@ assert-css: (".search-corrections", {
})
assert-text: (
".search-corrections",
"Type \"notablestructwithlongnamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
"Type \"NotableStructWithLongNamr\" not found and used as generic parameter. Consider searching for \"notablestructwithlongname\" instead."
)

go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
Expand All @@ -102,5 +102,5 @@ assert-css: (".error", {
})
assert-text: (
".error",
"Query parser error: \"Generic type parameter notablestructwithlongnamr does not accept generic parameters\"."
"Query parser error: \"Generic type parameter NotableStructWithLongNamr does not accept generic parameters\"."
)
50 changes: 20 additions & 30 deletions tests/rustdoc-js-std/parser-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const PARSED = [
query: 'A<B=C>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand All @@ -13,7 +13,7 @@ const PARSED = [
'b',
[
{
name: "c",
name: "C",
fullPath: ["c"],
pathWithoutLast: [],
pathLast: "c",
Expand All @@ -27,16 +27,15 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B=C>',
userQuery: 'A<B=C>',
returned: [],
userQuery: 'a<b=c>',
error: null,
},
{
query: 'A<B = C>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand All @@ -45,7 +44,7 @@ const PARSED = [
[
'b',
[{
name: "c",
name: "C",
fullPath: ["c"],
pathWithoutLast: [],
pathLast: "c",
Expand All @@ -58,16 +57,15 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B = C>',
userQuery: 'A<B = C>',
returned: [],
userQuery: 'a<b = c>',
error: null,
},
{
query: 'A<B=!>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand All @@ -89,16 +87,15 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B=!>',
userQuery: 'A<B=!>',
returned: [],
userQuery: 'a<b=!>',
error: null,
},
{
query: 'A<B=[]>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand All @@ -120,16 +117,15 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B=[]>',
userQuery: 'A<B=[]>',
returned: [],
userQuery: 'a<b=[]>',
error: null,
},
{
query: 'A<B=[!]>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand Down Expand Up @@ -160,52 +156,47 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B=[!]>',
userQuery: 'A<B=[!]>',
returned: [],
userQuery: 'a<b=[!]>',
error: null,
},
{
query: 'A<B=C=>',
elems: [],
foundElems: 0,
original: 'A<B=C=>',
userQuery: 'A<B=C=>',
returned: [],
userQuery: 'a<b=c=>',
error: "Cannot write `=` twice in a binding",
},
{
query: 'A<B=>',
elems: [],
foundElems: 0,
original: 'A<B=>',
userQuery: 'A<B=>',
returned: [],
userQuery: 'a<b=>',
error: "Unexpected `>` after `=`",
},
{
query: 'B=C',
elems: [],
foundElems: 0,
original: 'B=C',
userQuery: 'B=C',
returned: [],
userQuery: 'b=c',
error: "Type parameter `=` must be within generics list",
},
{
query: '[B=C]',
elems: [],
foundElems: 0,
original: '[B=C]',
userQuery: '[B=C]',
returned: [],
userQuery: '[b=c]',
error: "Type parameter `=` cannot be within slice `[]`",
},
{
query: 'A<B<X>=C>',
elems: [
{
name: "a",
name: "A",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
Expand All @@ -215,15 +206,15 @@ const PARSED = [
'b',
[
{
name: "c",
name: "C",
fullPath: ["c"],
pathWithoutLast: [],
pathLast: "c",
generics: [],
typeFilter: -1,
},
{
name: "x",
name: "X",
fullPath: ["x"],
pathWithoutLast: [],
pathLast: "x",
Expand All @@ -237,9 +228,8 @@ const PARSED = [
},
],
foundElems: 1,
original: 'A<B<X>=C>',
userQuery: 'A<B<X>=C>',
returned: [],
userQuery: 'a<b<x>=c>',
error: null,
},
];
Loading

0 comments on commit bccb9f7

Please sign in to comment.