Skip to content

Commit 6a94e87

Browse files
committed
Auto merge of #110688 - GuillaumeGomez:result-search-type, r=notriddle,jsha
rustdoc: Add search result item types after their name Here what it looks like: ![Screenshot from 2023-04-22 15-16-58](https://user-images.githubusercontent.com/3050060/233789566-b5f3f625-3b78-4c56-a7ee-0a4f2d62e667.png) The idea is to improve accessibility by providing this information directly in the text and not only in the text color. Currently we already use it for doc aliases and for primitive types, so I extended it to all types. r? `@notriddle`
2 parents 2304917 + de85f7f commit 6a94e87

File tree

6 files changed

+42
-40
lines changed

6 files changed

+42
-40
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ a.anchor,
213213
h1 a,
214214
.search-results a,
215215
.stab,
216-
.result-name .primitive > i, .result-name .keyword > i {
216+
.result-name i {
217217
color: var(--main-color);
218218
}
219219

@@ -887,7 +887,7 @@ so that we can apply CSS-filters to change the arrow color in themes */
887887
.search-results .result-name span.alias {
888888
color: var(--search-results-alias-color);
889889
}
890-
.search-results .result-name span.grey {
890+
.search-results .result-name .grey {
891891
color: var(--search-results-grey-color);
892892
}
893893

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

+34-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ const itemTypes = [
3535
"traitalias",
3636
];
3737

38+
const longItemTypes = [
39+
"module",
40+
"extern crate",
41+
"re-export",
42+
"struct",
43+
"enum",
44+
"function",
45+
"type alias",
46+
"static",
47+
"trait",
48+
"",
49+
"trait method",
50+
"method",
51+
"struct field",
52+
"enum variant",
53+
"macro",
54+
"primitive type",
55+
"associated type",
56+
"constant",
57+
"associated constant",
58+
"union",
59+
"foreign type",
60+
"keyword",
61+
"existential type",
62+
"attribute macro",
63+
"derive macro",
64+
"trait alias",
65+
];
66+
3867
// used for special search precedence
3968
const TY_PRIMITIVE = itemTypes.indexOf("primitive");
4069
const TY_KEYWORD = itemTypes.indexOf("keyword");
@@ -1966,16 +1995,11 @@ function initSearch(rawSearchIndex) {
19661995
array.forEach(item => {
19671996
const name = item.name;
19681997
const type = itemTypes[item.ty];
1998+
const longType = longItemTypes[item.ty];
1999+
const typeName = longType.length !== 0 ? `${longType}` : "?";
19692000

19702001
length += 1;
19712002

1972-
let extra = "";
1973-
if (type === "primitive") {
1974-
extra = " <i>(primitive type)</i>";
1975-
} else if (type === "keyword") {
1976-
extra = " <i>(keyword)</i>";
1977-
}
1978-
19792003
const link = document.createElement("a");
19802004
link.className = "result-" + type;
19812005
link.href = item.href;
@@ -1993,13 +2017,14 @@ function initSearch(rawSearchIndex) {
19932017

19942018
alias.insertAdjacentHTML(
19952019
"beforeend",
1996-
"<span class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>");
2020+
"<i class=\"grey\">&nbsp;- see&nbsp;</i>");
19972021

19982022
resultName.appendChild(alias);
19992023
}
2024+
20002025
resultName.insertAdjacentHTML(
20012026
"beforeend",
2002-
item.displayPath + "<span class=\"" + type + "\">" + name + extra + "</span>");
2027+
`${typeName} ${item.displayPath}<span class="${type}">${name}</span>`);
20032028
link.appendChild(resultName);
20042029

20052030
const description = document.createElement("div");

tests/rustdoc-gui/search-reexport.goml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ assert-attribute: (
1414
"//a[@class='result-import']",
1515
{"href": "../test_docs/index.html#reexport.TheStdReexport"},
1616
)
17-
assert-text: ("//a[@class='result-import']", "test_docs::TheStdReexport")
17+
assert-text: ("a.result-import .result-name", "re-export test_docs::TheStdReexport")
1818
click: "//a[@class='result-import']"
1919
// We check that it has the background modified thanks to the focus.
2020
wait-for-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "#494a3d"})
@@ -25,8 +25,8 @@ press-key: 'Enter'
2525
write: (".search-input", "AliasForTheStdReexport")
2626
wait-for: "//a[@class='result-import']"
2727
assert-text: (
28-
"//a[@class='result-import']",
29-
"AliasForTheStdReexport - see test_docs::TheStdReexport",
28+
"a.result-import .result-name",
29+
"AliasForTheStdReexport - see re-export test_docs::TheStdReexport",
3030
)
3131
// Same thing again, we click on it to ensure the background is once again set as expected.
3232
click: "//a[@class='result-import']"

tests/rustdoc-gui/search-result-color.goml

-18
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ assert-css: (
6565
{"border-bottom-color": "#aaa3"}
6666
)
6767

68-
// Checking the color of "keyword" text.
69-
assert-css: (
70-
"//*[@class='result-name']//*[text()='(keyword)']",
71-
{"color": "#788797"},
72-
)
73-
7468
store-value: (entry_color, "#0096cf") // color of the search entry
7569
store-value: (hover_entry_color, "#fff") // color of the hovered/focused search entry
7670
store-value: (background_color, "transparent") // background color
@@ -182,12 +176,6 @@ assert-css: (
182176
{"border-bottom-color": "#aaa3"}
183177
)
184178

185-
// Checking the color for "keyword" text.
186-
assert-css: (
187-
"//*[@class='result-name']//*[text()='(keyword)']",
188-
{"color": "#ddd"},
189-
)
190-
191179
store-value: (entry_color, "#ddd") // color of the search entry
192180
store-value: (hover_entry_color, "#ddd") // color of the hovered/focused search entry
193181
store-value: (background_color, "transparent") // background color
@@ -284,12 +272,6 @@ assert-css: (
284272
{"border-bottom-color": "#aaa3"}
285273
)
286274

287-
// Checking the color for "keyword" text.
288-
assert-css: (
289-
"//*[@class='result-name']//*[text()='(keyword)']",
290-
{"color": "#000"},
291-
)
292-
293275
store-value: (entry_color, "#000") // color of the search entry
294276
store-value: (hover_entry_color, "#000") // color of the hovered/focused search entry
295277
store-value: (background_color, "transparent") // background color

tests/rustdoc-gui/search-result-display.goml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ press-key: 'Enter'
77
wait-for: "#crate-search"
88
// The width is returned by "getComputedStyle" which returns the exact number instead of the
99
// CSS rule which is "50%"...
10-
assert-css: (".search-results div.desc", {"width": "310px"})
10+
assert-size: (".search-results div.desc", {"width": 310})
1111
set-window-size: (600, 100)
1212
// As counter-intuitive as it may seem, in this width, the width is "100%", which is why
1313
// when computed it's larger.
14-
assert-css: (".search-results div.desc", {"width": "566px"})
14+
assert-size: (".search-results div.desc", {"width": 566})
1515

1616
// The result set is all on one line.
1717
assert-css: (".search-results .result-name > span", {"display": "inline"})

tests/rustdoc-gui/search-result-keyword.goml

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,4 @@ write: (".search-input", "CookieMonster")
55
press-key: 'Enter'
66
// Waiting for the search results to appear...
77
wait-for: "#search-tabs"
8-
// Note: The two next assert commands could be merged as one but readability would be
9-
// less good.
10-
//
11-
// Checking that the CSS is displaying " (keyword)" in italic.
12-
assert-text: (".result-name span.keyword > i", "(keyword)")
13-
assert-text: (".result-name span.keyword", "CookieMonster (keyword)")
8+
assert-text: (".result-keyword .result-name", "keyword CookieMonster")

0 commit comments

Comments
 (0)