Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8e043ab

Browse files
committedOct 24, 2023
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 b67985e commit 8e043ab

File tree

6 files changed

+557
-161
lines changed

6 files changed

+557
-161
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

+431-120
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-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
];

‎tests/rustdoc-js/generics-match-ambiguity.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@ const EXPECTED = [
88
{
99
'query': 'Wrap',
1010
'in_args': [
11-
{ 'path': 'generics_match_ambiguity', 'name': 'bar' },
12-
{ 'path': 'generics_match_ambiguity', 'name': 'foo' },
11+
{
12+
'path': 'generics_match_ambiguity',
13+
'name': 'bar',
14+
'displayTypeSignature': '*Wrap*, Wrap'
15+
},
16+
{
17+
'path': 'generics_match_ambiguity',
18+
'name': 'foo',
19+
'displayTypeSignature': '*Wrap*, Wrap'
20+
},
1321
],
1422
},
1523
{
1624
'query': 'Wrap<i32>',
1725
'in_args': [
18-
{ 'path': 'generics_match_ambiguity', 'name': 'bar' },
19-
{ 'path': 'generics_match_ambiguity', 'name': 'foo' },
26+
{
27+
'path': 'generics_match_ambiguity',
28+
'name': 'bar',
29+
'displayTypeSignature': '*Wrap*<*i32*, u32>, Wrap'
30+
},
31+
{
32+
'path': 'generics_match_ambiguity',
33+
'name': 'foo',
34+
'displayTypeSignature': '*Wrap*<*i32*>, Wrap'
35+
},
2036
],
2137
},
2238
{

‎tests/rustdoc-js/type-parameters.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -5,79 +5,79 @@ const EXPECTED = [
55
{
66
query: '-> trait:Some',
77
others: [
8-
{ path: 'foo', name: 'alef' },
9-
{ path: 'foo', name: 'alpha' },
8+
{ path: 'foo', name: 'alef', displayTypeSignature: '-> impl *Some*' },
9+
{ path: 'foo', name: 'alpha', displayTypeSignature: '-> impl *Some*' },
1010
],
1111
},
1212
{
1313
query: '-> generic:T',
1414
others: [
15-
{ path: 'foo', name: 'bet' },
16-
{ path: 'foo', name: 'alef' },
17-
{ path: 'foo', name: 'beta' },
15+
{ path: 'foo', name: 'bet', displayTypeSignature: '_ -> *T*' },
16+
{ path: 'foo', name: 'alef', displayTypeSignature: '-> *T*' },
17+
{ path: 'foo', name: 'beta', displayTypeSignature: 'T -> *T*' },
1818
],
1919
},
2020
{
2121
query: 'A -> B',
2222
others: [
23-
{ path: 'foo', name: 'bet' },
23+
{ path: 'foo', name: 'bet', displayTypeSignature: '*A* -> *B*' },
2424
],
2525
},
2626
{
2727
query: 'A -> A',
2828
others: [
29-
{ path: 'foo', name: 'beta' },
29+
{ path: 'foo', name: 'beta', displayTypeSignature: '*A* -> *A*' },
3030
],
3131
},
3232
{
3333
query: 'A, A',
3434
others: [
35-
{ path: 'foo', name: 'alternate' },
35+
{ path: 'foo', name: 'alternate', displayTypeSignature: '*A*, *A*' },
3636
],
3737
},
3838
{
3939
query: 'A, B',
4040
others: [
41-
{ path: 'foo', name: 'other' },
41+
{ path: 'foo', name: 'other', displayTypeSignature: '*A*, *B*' },
4242
],
4343
},
4444
{
4545
query: 'Other, Other',
4646
others: [
47-
{ path: 'foo', name: 'other' },
48-
{ path: 'foo', name: 'alternate' },
47+
{ path: 'foo', name: 'other', displayTypeSignature: 'impl *Other*, impl *Other*' },
48+
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
4949
],
5050
},
5151
{
5252
query: 'generic:T',
5353
in_args: [
54-
{ path: 'foo', name: 'bet' },
55-
{ path: 'foo', name: 'beta' },
56-
{ path: 'foo', name: 'other' },
57-
{ path: 'foo', name: 'alternate' },
54+
{ path: 'foo', name: 'bet', displayTypeSignature: '*T* -> _' },
55+
{ path: 'foo', name: 'beta', displayTypeSignature: '*T* -> T' },
56+
{ path: 'foo', name: 'other', displayTypeSignature: '*T*, _' },
57+
{ path: 'foo', name: 'alternate', displayTypeSignature: '*T*, T' },
5858
],
5959
},
6060
{
6161
query: 'generic:Other',
6262
in_args: [
63-
{ path: 'foo', name: 'bet' },
64-
{ path: 'foo', name: 'beta' },
65-
{ path: 'foo', name: 'other' },
66-
{ path: 'foo', name: 'alternate' },
63+
{ path: 'foo', name: 'bet', displayTypeSignature: '*Other* -> _' },
64+
{ path: 'foo', name: 'beta', displayTypeSignature: '*Other* -> Other' },
65+
{ path: 'foo', name: 'other', displayTypeSignature: '*Other*, _' },
66+
{ path: 'foo', name: 'alternate', displayTypeSignature: '*Other*, Other' },
6767
],
6868
},
6969
{
7070
query: 'trait:Other',
7171
in_args: [
72-
{ path: 'foo', name: 'other' },
73-
{ path: 'foo', name: 'alternate' },
72+
{ path: 'foo', name: 'other', displayTypeSignature: '_, impl *Other*' },
73+
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
7474
],
7575
},
7676
{
7777
query: 'Other',
7878
in_args: [
79-
{ path: 'foo', name: 'other' },
80-
{ path: 'foo', name: 'alternate' },
79+
{ path: 'foo', name: 'other', displayTypeSignature: '_, impl *Other*' },
80+
{ path: 'foo', name: 'alternate', displayTypeSignature: 'impl *Other*, impl *Other*' },
8181
],
8282
},
8383
{

0 commit comments

Comments
 (0)
Please sign in to comment.