Skip to content

Commit 8206a6c

Browse files
committed
rustdoc-search: show type signature on type-driven SERP
This displays the function signature, as rustdoc understands it, on the In Parameters, In Return Types, and In Function Signature pages, but not in the In Names page, since it's not used there. It also highlights the matching parts, to clarify why a function is considered a good match.
1 parent 6fb981d commit 8206a6c

File tree

12 files changed

+585
-122
lines changed

12 files changed

+585
-122
lines changed

src/librustdoc/html/static/css/rustdoc.css

+5-3
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,11 @@ details:not(.toggle) summary {
328328
margin-bottom: .6em;
329329
}
330330

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

683684
.docblock code, .docblock-short code,
684-
pre, .rustdoc.src .example-wrap {
685+
pre, .rustdoc.src .example-wrap,
686+
.search-results .type-signature strong {
685687
background-color: var(--code-block-background-color);
686688
}
687689

src/librustdoc/html/static/js/search.js

+427-62
Large diffs are not rendered by default.

src/tools/rustdoc-js/tester.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,35 @@ function loadSearchJS(doc_folder, resource_suffix) {
373373

374374
return {
375375
doSearch: function(queryStr, filterCrate, currentCrate) {
376-
return searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
376+
const results = searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
377377
filterCrate, currentCrate);
378+
for (const key in results) {
379+
if (results[key]) {
380+
for (const resultKey in results[key]) {
381+
if (!Object.prototype.hasOwnProperty.call(results[key], resultKey)) {
382+
continue;
383+
}
384+
const entry = results[key][resultKey];
385+
if (!entry) {
386+
continue;
387+
}
388+
if (Object.prototype.hasOwnProperty.call(entry, "displayTypeSignature") &&
389+
entry.displayTypeSignature !== null &&
390+
entry.displayTypeSignature instanceof Array
391+
) {
392+
entry.displayTypeSignature.forEach((value, index) => {
393+
if (index % 2 === 1) {
394+
entry.displayTypeSignature[index] = "*" + value + "*";
395+
} else {
396+
entry.displayTypeSignature[index] = value;
397+
}
398+
});
399+
entry.displayTypeSignature = entry.displayTypeSignature.join("");
400+
}
401+
}
402+
}
403+
}
404+
return results;
378405
},
379406
getCorrections: function(queryStr, filterCrate, currentCrate) {
380407
const parsedQuery = searchModule.parseQuery(queryStr);

tests/rustdoc-gui/search-corrections.goml

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ assert-css: (".search-corrections", {
2424
})
2525
assert-text: (
2626
".search-corrections",
27-
"Type \"notablestructwithlongnamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
27+
"Type \"NotableStructWithLongNamr\" not found. Showing results for closest type name \"notablestructwithlongname\" instead."
2828
)
2929

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

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

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

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

9292
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
@@ -102,5 +102,5 @@ assert-css: (".error", {
102102
})
103103
assert-text: (
104104
".error",
105-
"Query parser error: \"Generic type parameter notablestructwithlongnamr does not accept generic parameters\"."
105+
"Query parser error: \"Generic type parameter NotableStructWithLongNamr does not accept generic parameters\"."
106106
)

tests/rustdoc-js-std/parser-ident.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const PARSED = [
22
{
33
query: "R<!>",
44
elems: [{
5-
name: "r",
5+
name: "R",
6+
normalizedName: "r",
67
fullPath: ["r"],
78
pathWithoutLast: [],
89
pathLast: "r",
@@ -28,6 +29,7 @@ const PARSED = [
2829
query: "!",
2930
elems: [{
3031
name: "never",
32+
normalizedName: "never",
3133
fullPath: ["never"],
3234
pathWithoutLast: [],
3335
pathLast: "never",
@@ -44,6 +46,7 @@ const PARSED = [
4446
query: "a!",
4547
elems: [{
4648
name: "a",
49+
normalizedName: "a",
4750
fullPath: ["a"],
4851
pathWithoutLast: [],
4952
pathLast: "a",
@@ -78,6 +81,7 @@ const PARSED = [
7881
query: "!::b",
7982
elems: [{
8083
name: "!::b",
84+
normalizedName: "!::b",
8185
fullPath: ["never", "b"],
8286
pathWithoutLast: ["never"],
8387
pathLast: "b",
@@ -126,7 +130,8 @@ const PARSED = [
126130
pathLast: "b",
127131
generics: [
128132
{
129-
name: "t",
133+
name: "T",
134+
normalizedName: "t",
130135
fullPath: ["t"],
131136
pathWithoutLast: [],
132137
pathLast: "t",

tests/rustdoc-js-std/parser-literal.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const PARSED = [
22
{
33
query: 'R<P>',
44
elems: [{
5-
name: "r",
5+
name: "R",
6+
normalizedName: "r",
67
fullPath: ["r"],
78
pathWithoutLast: [],
89
pathLast: "r",
910
generics: [
1011
{
11-
name: "p",
12+
name: "P",
13+
normalizedName: "p",
1214
fullPath: ["p"],
1315
pathWithoutLast: [],
1416
pathLast: "p",

tests/rustdoc-js-std/parser-paths.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const PARSED = [
22
{
33
query: 'A::B',
44
elems: [{
5-
name: "a::b",
5+
name: "A::B",
6+
normalizedName: "a::b",
67
fullPath: ["a", "b"],
78
pathWithoutLast: ["a"],
89
pathLast: "b",
@@ -19,15 +20,17 @@ const PARSED = [
1920
query: 'A::B,C',
2021
elems: [
2122
{
22-
name: "a::b",
23+
name: "A::B",
24+
normalizedName: "a::b",
2325
fullPath: ["a", "b"],
2426
pathWithoutLast: ["a"],
2527
pathLast: "b",
2628
generics: [],
2729
typeFilter: -1,
2830
},
2931
{
30-
name: "c",
32+
name: "C",
33+
normalizedName: "c",
3134
fullPath: ["c"],
3235
pathWithoutLast: [],
3336
pathLast: "c",
@@ -45,13 +48,15 @@ const PARSED = [
4548
query: 'A::B<f>,C',
4649
elems: [
4750
{
48-
name: "a::b",
51+
name: "A::B",
52+
normalizedName: "a::b",
4953
fullPath: ["a", "b"],
5054
pathWithoutLast: ["a"],
5155
pathLast: "b",
5256
generics: [
5357
{
5458
name: "f",
59+
normalizedName: "f",
5560
fullPath: ["f"],
5661
pathWithoutLast: [],
5762
pathLast: "f",
@@ -61,7 +66,8 @@ const PARSED = [
6166
typeFilter: -1,
6267
},
6368
{
64-
name: "c",
69+
name: "C",
70+
normalizedName: "c",
6571
fullPath: ["c"],
6672
pathWithoutLast: [],
6773
pathLast: "c",

tests/rustdoc-js-std/parser-returned.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const PARSED = [
55
foundElems: 1,
66
original: "-> F<P>",
77
returned: [{
8-
name: "f",
8+
name: "F",
99
fullPath: ["f"],
1010
pathWithoutLast: [],
1111
pathLast: "f",
1212
generics: [
1313
{
14-
name: "p",
14+
name: "P",
1515
fullPath: ["p"],
1616
pathWithoutLast: [],
1717
pathLast: "p",
@@ -29,7 +29,7 @@ const PARSED = [
2929
foundElems: 1,
3030
original: "-> P",
3131
returned: [{
32-
name: "p",
32+
name: "P",
3333
fullPath: ["p"],
3434
pathWithoutLast: [],
3535
pathLast: "p",

tests/rustdoc-js-std/parser-slice-array.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const PARSED = [
3030
pathLast: "[]",
3131
generics: [
3232
{
33-
name: "d",
33+
name: "D",
3434
fullPath: ["d"],
3535
pathWithoutLast: [],
3636
pathLast: "d",

tests/rustdoc-js/generics-impl.js

+50-10
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,61 @@ const EXPECTED = [
44
{
55
'query': 'Aaaaaaa -> u32',
66
'others': [
7-
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'bbbbbbb' },
7+
{
8+
'path': 'generics_impl::Aaaaaaa',
9+
'name': 'bbbbbbb',
10+
'displayTypeSignature': '*Aaaaaaa* -> *u32*'
11+
},
812
],
913
},
1014
{
1115
'query': 'Aaaaaaa -> bool',
1216
'others': [
13-
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'ccccccc' },
17+
{
18+
'path': 'generics_impl::Aaaaaaa',
19+
'name': 'ccccccc',
20+
'displayTypeSignature': '*Aaaaaaa* -> *bool*'
21+
},
1422
],
1523
},
1624
{
1725
'query': 'Aaaaaaa -> usize',
1826
'others': [
19-
{ 'path': 'generics_impl::Aaaaaaa', 'name': 'read' },
27+
{
28+
'path': 'generics_impl::Aaaaaaa',
29+
'name': 'read',
30+
'displayTypeSignature': '*Aaaaaaa*, [] -> Result<*usize*>'
31+
},
2032
],
2133
},
2234
{
2335
'query': 'Read -> u64',
2436
'others': [
25-
{ 'path': 'generics_impl::Ddddddd', 'name': 'eeeeeee' },
26-
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
37+
{
38+
'path': 'generics_impl::Ddddddd',
39+
'name': 'eeeeeee',
40+
'displayTypeSignature': 'impl *Read* -> *u64*'
41+
},
42+
{
43+
'path': 'generics_impl::Ddddddd',
44+
'name': 'ggggggg',
45+
'displayTypeSignature': 'Ddddddd<impl *Read*> -> *u64*'
46+
},
2747
],
2848
},
2949
{
3050
'query': 'trait:Read -> u64',
3151
'others': [
32-
{ 'path': 'generics_impl::Ddddddd', 'name': 'eeeeeee' },
33-
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
52+
{
53+
'path': 'generics_impl::Ddddddd',
54+
'name': 'eeeeeee',
55+
'displayTypeSignature': 'impl *Read* -> *u64*'
56+
},
57+
{
58+
'path': 'generics_impl::Ddddddd',
59+
'name': 'ggggggg',
60+
'displayTypeSignature': 'Ddddddd<impl *Read*> -> *u64*'
61+
},
3462
],
3563
},
3664
{
@@ -40,19 +68,31 @@ const EXPECTED = [
4068
{
4169
'query': 'bool -> u64',
4270
'others': [
43-
{ 'path': 'generics_impl::Ddddddd', 'name': 'fffffff' },
71+
{
72+
'path': 'generics_impl::Ddddddd',
73+
'name': 'fffffff',
74+
'displayTypeSignature': '*bool* -> *u64*'
75+
},
4476
],
4577
},
4678
{
4779
'query': 'Ddddddd -> u64',
4880
'others': [
49-
{ 'path': 'generics_impl::Ddddddd', 'name': 'ggggggg' },
81+
{
82+
'path': 'generics_impl::Ddddddd',
83+
'name': 'ggggggg',
84+
'displayTypeSignature': '*Ddddddd* -> *u64*'
85+
},
5086
],
5187
},
5288
{
5389
'query': '-> Ddddddd',
5490
'others': [
55-
{ 'path': 'generics_impl::Ddddddd', 'name': 'hhhhhhh' },
91+
{
92+
'path': 'generics_impl::Ddddddd',
93+
'name': 'hhhhhhh',
94+
'displayTypeSignature': '-> *Ddddddd*'
95+
},
5696
],
5797
},
5898
];

0 commit comments

Comments
 (0)