Skip to content

rustdoc-search: show type signature on type-driven SERP #117112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
@@ -328,10 +328,11 @@ details:not(.toggle) summary {
margin-bottom: .6em;
}

code, pre, a.test-arrow, .code-header {
code, pre, a.test-arrow, .code-header, .search-results .type-signature {
font-family: "Source Code Pro", monospace;
}
.docblock code, .docblock-short code {
.docblock code, .docblock-short code,
.search-results .type-signature strong {
border-radius: 3px;
padding: 0 0.125em;
}
@@ -681,7 +682,8 @@ ul.block, .block li {
}

.docblock code, .docblock-short code,
pre, .rustdoc.src .example-wrap {
pre, .rustdoc.src .example-wrap,
.search-results .type-signature strong {
background-color: var(--code-block-background-color);
}

553 changes: 434 additions & 119 deletions src/librustdoc/html/static/js/search.js

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
@@ -373,8 +373,35 @@ function loadSearchJS(doc_folder, resource_suffix) {

return {
doSearch: function(queryStr, filterCrate, currentCrate) {
return searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
const results = searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
filterCrate, currentCrate);
for (const key in results) {
if (results[key]) {
for (const resultKey in results[key]) {
if (!Object.prototype.hasOwnProperty.call(results[key], resultKey)) {
continue;
}
const entry = results[key][resultKey];
if (!entry) {
continue;
}
if (Object.prototype.hasOwnProperty.call(entry, "displayTypeSignature") &&
entry.displayTypeSignature !== null &&
entry.displayTypeSignature instanceof Array
) {
entry.displayTypeSignature.forEach((value, index) => {
if (index % 2 === 1) {
entry.displayTypeSignature[index] = "*" + value + "*";
} else {
entry.displayTypeSignature[index] = value;
}
});
entry.displayTypeSignature = entry.displayTypeSignature.join("");
}
}
}
}
return results;
},
getCorrections: function(queryStr, filterCrate, currentCrate) {
const parsedQuery = searchModule.parseQuery(queryStr);
12 changes: 6 additions & 6 deletions tests/rustdoc-gui/search-corrections.goml
Original file line number Diff line number Diff line change
@@ -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.
@@ -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
@@ -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
@@ -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
@@ -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"
@@ -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\"."
)
9 changes: 7 additions & 2 deletions tests/rustdoc-js-std/parser-ident.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ const PARSED = [
{
query: "R<!>",
elems: [{
name: "r",
name: "R",
normalizedName: "r",
fullPath: ["r"],
pathWithoutLast: [],
pathLast: "r",
@@ -28,6 +29,7 @@ const PARSED = [
query: "!",
elems: [{
name: "never",
normalizedName: "never",
fullPath: ["never"],
pathWithoutLast: [],
pathLast: "never",
@@ -44,6 +46,7 @@ const PARSED = [
query: "a!",
elems: [{
name: "a",
normalizedName: "a",
fullPath: ["a"],
pathWithoutLast: [],
pathLast: "a",
@@ -78,6 +81,7 @@ const PARSED = [
query: "!::b",
elems: [{
name: "!::b",
normalizedName: "!::b",
fullPath: ["never", "b"],
pathWithoutLast: ["never"],
pathLast: "b",
@@ -126,7 +130,8 @@ const PARSED = [
pathLast: "b",
generics: [
{
name: "t",
name: "T",
normalizedName: "t",
fullPath: ["t"],
pathWithoutLast: [],
pathLast: "t",
6 changes: 4 additions & 2 deletions tests/rustdoc-js-std/parser-literal.js
Original file line number Diff line number Diff line change
@@ -2,13 +2,15 @@ const PARSED = [
{
query: 'R<P>',
elems: [{
name: "r",
name: "R",
normalizedName: "r",
fullPath: ["r"],
pathWithoutLast: [],
pathLast: "r",
generics: [
{
name: "p",
name: "P",
normalizedName: "p",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
16 changes: 11 additions & 5 deletions tests/rustdoc-js-std/parser-paths.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ const PARSED = [
{
query: 'A::B',
elems: [{
name: "a::b",
name: "A::B",
normalizedName: "a::b",
fullPath: ["a", "b"],
pathWithoutLast: ["a"],
pathLast: "b",
@@ -19,15 +20,17 @@ const PARSED = [
query: 'A::B,C',
elems: [
{
name: "a::b",
name: "A::B",
normalizedName: "a::b",
fullPath: ["a", "b"],
pathWithoutLast: ["a"],
pathLast: "b",
generics: [],
typeFilter: -1,
},
{
name: "c",
name: "C",
normalizedName: "c",
fullPath: ["c"],
pathWithoutLast: [],
pathLast: "c",
@@ -45,13 +48,15 @@ const PARSED = [
query: 'A::B<f>,C',
elems: [
{
name: "a::b",
name: "A::B",
normalizedName: "a::b",
fullPath: ["a", "b"],
pathWithoutLast: ["a"],
pathLast: "b",
generics: [
{
name: "f",
normalizedName: "f",
fullPath: ["f"],
pathWithoutLast: [],
pathLast: "f",
@@ -61,7 +66,8 @@ const PARSED = [
typeFilter: -1,
},
{
name: "c",
name: "C",
normalizedName: "c",
fullPath: ["c"],
pathWithoutLast: [],
pathLast: "c",
6 changes: 3 additions & 3 deletions tests/rustdoc-js-std/parser-returned.js
Original file line number Diff line number Diff line change
@@ -5,13 +5,13 @@ const PARSED = [
foundElems: 1,
original: "-> F<P>",
returned: [{
name: "f",
name: "F",
fullPath: ["f"],
pathWithoutLast: [],
pathLast: "f",
generics: [
{
name: "p",
name: "P",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
@@ -29,7 +29,7 @@ const PARSED = [
foundElems: 1,
original: "-> P",
returned: [{
name: "p",
name: "P",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
2 changes: 1 addition & 1 deletion tests/rustdoc-js-std/parser-slice-array.js
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ const PARSED = [
pathLast: "[]",
generics: [
{
name: "d",
name: "D",
fullPath: ["d"],
pathWithoutLast: [],
pathLast: "d",
60 changes: 50 additions & 10 deletions tests/rustdoc-js/generics-impl.js
Original file line number Diff line number Diff line change
@@ -4,33 +4,61 @@ const EXPECTED = [
{
'query': 'Aaaaaaa -> u32',
'others': [
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'bbbbbbb' },
{
'path': 'generics_impl::Aaaaaaa',
'name': 'bbbbbbb',
'displayTypeSignature': '*Aaaaaaa* -> *u32*'
},
],
},
{
'query': 'Aaaaaaa -> bool',
'others': [
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'ccccccc' },
{
'path': 'generics_impl::Aaaaaaa',
'name': 'ccccccc',
'displayTypeSignature': '*Aaaaaaa* -> *bool*'
},
],
},
{
'query': 'Aaaaaaa -> usize',
'others': [
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'read' },
{
'path': 'generics_impl::Aaaaaaa',
'name': 'read',
'displayTypeSignature': '*Aaaaaaa*, [] -> Result<*usize*>'
},
],
},
{
'query': 'Read -> u64',
'others': [
{ 'path': 'generics_impl::Ddddddd', 'name': 'eeeeeee' },
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
{
'path': 'generics_impl::Ddddddd',
'name': 'eeeeeee',
'displayTypeSignature': 'impl *Read* -> *u64*'
},
{
'path': 'generics_impl::Ddddddd',
'name': 'ggggggg',
'displayTypeSignature': 'Ddddddd<impl *Read*> -> *u64*'
},
],
},
{
'query': 'trait:Read -> u64',
'others': [
{ 'path': 'generics_impl::Ddddddd', 'name': 'eeeeeee' },
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
{
'path': 'generics_impl::Ddddddd',
'name': 'eeeeeee',
'displayTypeSignature': 'impl *Read* -> *u64*'
},
{
'path': 'generics_impl::Ddddddd',
'name': 'ggggggg',
'displayTypeSignature': 'Ddddddd<impl *Read*> -> *u64*'
},
],
},
{
@@ -40,19 +68,31 @@ const EXPECTED = [
{
'query': 'bool -> u64',
'others': [
{ 'path': 'generics_impl::Ddddddd', 'name': 'fffffff' },
{
'path': 'generics_impl::Ddddddd',
'name': 'fffffff',
'displayTypeSignature': '*bool* -> *u64*'
},
],
},
{
'query': 'Ddddddd -> u64',
'others': [
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
{
'path': 'generics_impl::Ddddddd',
'name': 'ggggggg',
'displayTypeSignature': '*Ddddddd* -> *u64*'
},
],
},
{
'query': '-> Ddddddd',
'others': [
{ 'path': 'generics_impl::Ddddddd', 'name': 'hhhhhhh' },
{
'path': 'generics_impl::Ddddddd',
'name': 'hhhhhhh',
'displayTypeSignature': '-> *Ddddddd*'
},
],
},
];
24 changes: 20 additions & 4 deletions tests/rustdoc-js/generics-match-ambiguity.js
Original file line number Diff line number Diff line change
@@ -8,15 +8,31 @@ const EXPECTED = [
{
'query': 'Wrap',
'in_args': [
{ 'path': 'generics_match_ambiguity', 'name': 'bar' },
{ 'path': 'generics_match_ambiguity', 'name': 'foo' },
{
'path': 'generics_match_ambiguity',
'name': 'bar',
'displayTypeSignature': '*Wrap*, Wrap'
},
{
'path': 'generics_match_ambiguity',
'name': 'foo',
'displayTypeSignature': '*Wrap*, Wrap'
},
],
},
{
'query': 'Wrap<i32>',
'in_args': [
{ 'path': 'generics_match_ambiguity', 'name': 'bar' },
{ 'path': 'generics_match_ambiguity', 'name': 'foo' },
{
'path': 'generics_match_ambiguity',
'name': 'bar',
'displayTypeSignature': '*Wrap*<*i32*, u32>, Wrap'
},
{
'path': 'generics_match_ambiguity',
'name': 'foo',
'displayTypeSignature': '*Wrap*<*i32*>, Wrap'
},
],
},
{
6 changes: 4 additions & 2 deletions tests/rustdoc-js/search-method-disambiguate.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ const EXPECTED = [
{
'path': 'search_method_disambiguate::MyTy',
'name': 'my_method',
'href': '../search_method_disambiguate/struct.MyTy.html#impl-X-for-MyTy%3Cbool%3E/method.my_method'
'href': '../search_method_disambiguate/struct.MyTy.html#impl-X-for-MyTy%3Cbool%3E/method.my_method',
'displayTypeSignature': '*MyTy*<bool> -> *bool*',
},
],
},
@@ -21,7 +22,8 @@ const EXPECTED = [
{
'path': 'search_method_disambiguate::MyTy',
'name': 'my_method',
'href': '../search_method_disambiguate/struct.MyTy.html#impl-X-for-MyTy%3Cu8%3E/method.my_method'
'href': '../search_method_disambiguate/struct.MyTy.html#impl-X-for-MyTy%3Cu8%3E/method.my_method',
'displayTypeSignature': '*MyTy*<u8> -> *u8*',
},
],
}
46 changes: 23 additions & 23 deletions tests/rustdoc-js/type-parameters.js
Original file line number Diff line number Diff line change
@@ -5,79 +5,79 @@ const EXPECTED = [
{
query: '-> trait:Some',
others: [
{ path: 'foo', name: 'alef' },
{ path: 'foo', name: 'alpha' },
{ path: 'foo', name: 'alef', displayTypeSignature: '-> impl *Some*' },
{ path: 'foo', name: 'alpha', displayTypeSignature: '-> impl *Some*' },
],
},
{
query: '-> generic:T',
others: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'alef' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'bet', displayTypeSignature: '_ -> *T*' },
{ path: 'foo', name: 'alef', displayTypeSignature: '-> *T*' },
{ path: 'foo', name: 'beta', displayTypeSignature: 'T -> *T*' },
],
},
{
query: 'A -> B',
others: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'bet', displayTypeSignature: '*A* -> *B*' },
],
},
{
query: 'A -> A',
others: [
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'beta', displayTypeSignature: '*A* -> *A*' },
],
},
{
query: 'A, A',
others: [
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'alternate', displayTypeSignature: '*A*, *A*' },
],
},
{
query: 'A, B',
others: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'other', displayTypeSignature: '*A*, *B*' },
],
},
{
query: 'Other, Other',
others: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other', displayTypeSignature: 'impl *Other*, impl *Other*' },
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
],
},
{
query: 'generic:T',
in_args: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'bet', displayTypeSignature: '*T* -> _' },
{ path: 'foo', name: 'beta', displayTypeSignature: '*T* -> T' },
{ path: 'foo', name: 'other', displayTypeSignature: '*T*, _' },
{ path: 'foo', name: 'alternate', displayTypeSignature: '*T*, T' },
],
},
{
query: 'generic:Other',
in_args: [
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'bet', displayTypeSignature: '*Other* -> _' },
{ path: 'foo', name: 'beta', displayTypeSignature: '*Other* -> Other' },
{ path: 'foo', name: 'other', displayTypeSignature: '*Other*, _' },
{ path: 'foo', name: 'alternate', displayTypeSignature: '*Other*, Other' },
],
},
{
query: 'trait:Other',
in_args: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other', displayTypeSignature: '_, impl *Other*' },
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
],
},
{
query: 'Other',
in_args: [
{ path: 'foo', name: 'other' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other', displayTypeSignature: '_, impl *Other*' },
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
],
},
{