Skip to content

Commit 3fcb6ab

Browse files
Rollup merge of rust-lang#96590 - notriddle:notriddle/tab-bar-fn-search, r=GuillaumeGomez,jsha
rustdoc: when running a function-signature search, tweak the tab bar # Before ![In Names (7) / In Parameters (0) / In Return types (0)](https://user-images.githubusercontent.com/1593513/166122875-ffdeafe6-8d4d-4e61-84a6-f5986b50ac35.png) # After ![In Function Signature (7)](https://user-images.githubusercontent.com/1593513/166122883-9a3d7515-3235-4ee3-8c4b-5401d109e099.png)
2 parents 81824cc + 4c183cd commit 3fcb6ab

File tree

4 files changed

+113
-48
lines changed

4 files changed

+113
-48
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,11 @@ pre.rust {
13331333
border-top: 2px solid;
13341334
}
13351335

1336+
#titles > button:first-child:last-child {
1337+
margin-right: 1px;
1338+
width: calc(100% - 1px);
1339+
}
1340+
13361341
#titles > button:not(:last-child) {
13371342
margin-right: 1px;
13381343
width: calc(33.3% - 1px);

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

+44-25
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,33 @@ const TY_KEYWORD = itemTypes.indexOf("keyword");
4545

4646
// In the search display, allows to switch between tabs.
4747
function printTab(nb) {
48-
if (nb === 0 || nb === 1 || nb === 2) {
49-
searchState.currentTab = nb;
50-
}
51-
let nb_copy = nb;
48+
let iter = 0;
49+
let foundCurrentTab = false;
50+
let foundCurrentResultSet = false;
5251
onEachLazy(document.getElementById("titles").childNodes, elem => {
53-
if (nb_copy === 0) {
52+
if (nb === iter) {
5453
addClass(elem, "selected");
54+
foundCurrentTab = true;
5555
} else {
5656
removeClass(elem, "selected");
5757
}
58-
nb_copy -= 1;
58+
iter += 1;
5959
});
60+
iter = 0;
6061
onEachLazy(document.getElementById("results").childNodes, elem => {
61-
if (nb === 0) {
62+
if (nb === iter) {
6263
addClass(elem, "active");
64+
foundCurrentResultSet = true;
6365
} else {
6466
removeClass(elem, "active");
6567
}
66-
nb -= 1;
68+
iter += 1;
6769
});
70+
if (foundCurrentTab && foundCurrentResultSet) {
71+
searchState.currentTab = nb;
72+
} else if (nb != 0) {
73+
printTab(0);
74+
}
6875
}
6976

7077
/**
@@ -1409,18 +1416,12 @@ window.initSearch = rawSearchIndex => {
14091416
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
14101417
row = searchIndex[i];
14111418
in_returned = checkReturned(row, elem, parsedQuery.typeFilter);
1412-
addIntoResults(results_returned, row.id, i, -1, in_returned);
1419+
addIntoResults(results_others, row.id, i, -1, in_returned);
14131420
}
14141421
}
14151422
} else if (parsedQuery.foundElems > 0) {
1416-
let container = results_others;
1417-
// In the special case where only a "returned" information is available, we want to
1418-
// put the information into the "results_returned" dict.
1419-
if (parsedQuery.returned.length !== 0 && parsedQuery.elems.length === 0) {
1420-
container = results_returned;
1421-
}
14221423
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
1423-
handleArgs(searchIndex[i], i, container);
1424+
handleArgs(searchIndex[i], i, results_others);
14241425
}
14251426
}
14261427
}
@@ -1725,12 +1726,26 @@ window.initSearch = rawSearchIndex => {
17251726
`${typeFilter}</h1> in ${crates} </div>`;
17261727
if (results.query.error !== null) {
17271728
output += `<h3>Query parser error: "${results.query.error}".</h3>`;
1729+
output += '<div id="titles">' +
1730+
makeTabHeader(0, "In Names", ret_others[1]) +
1731+
"</div>";
1732+
currentTab = 0;
1733+
} else if (results.query.foundElems <= 1 && results.query.returned.length === 0) {
1734+
output += `<div id="titles">` +
1735+
makeTabHeader(0, "In Names", ret_others[1]) +
1736+
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1737+
makeTabHeader(2, "In Return Types", ret_returned[1]) +
1738+
"</div>";
1739+
} else {
1740+
const signatureTabTitle =
1741+
results.query.elems.length === 0 ? "In Function Return Types" :
1742+
results.query.returned.length === 0 ? "In Function Parameters" :
1743+
"In Function Signatures";
1744+
output += '<div id="titles">' +
1745+
makeTabHeader(0, signatureTabTitle, ret_others[1]) +
1746+
"</div>";
1747+
currentTab = 0;
17281748
}
1729-
output += `<div id="titles">` +
1730-
makeTabHeader(0, "In Names", ret_others[1]) +
1731-
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
1732-
makeTabHeader(2, "In Return Types", ret_returned[1]) +
1733-
"</div>";
17341749

17351750
const resultsElem = document.createElement("div");
17361751
resultsElem.id = "results";
@@ -1745,12 +1760,16 @@ window.initSearch = rawSearchIndex => {
17451760
}
17461761
search.appendChild(resultsElem);
17471762
// Reset focused elements.
1748-
searchState.focusedByTab = [null, null, null];
17491763
searchState.showResults(search);
17501764
const elems = document.getElementById("titles").childNodes;
1751-
elems[0].onclick = () => { printTab(0); };
1752-
elems[1].onclick = () => { printTab(1); };
1753-
elems[2].onclick = () => { printTab(2); };
1765+
searchState.focusedByTab = [];
1766+
let i = 0;
1767+
for (const elem of elems) {
1768+
const j = i;
1769+
elem.onclick = () => { printTab(j); };
1770+
searchState.focusedByTab.push(null);
1771+
i += 1;
1772+
}
17541773
printTab(currentTab);
17551774
}
17561775

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Checks that the search tab results work correctly with function signature syntax
2+
// First, try a search-by-name
3+
goto: file://|DOC_PATH|/test_docs/index.html
4+
write: (".search-input", "Foo")
5+
// Waiting for the search results to appear...
6+
wait-for: "#titles"
7+
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
8+
assert-text: ("#titles > button:nth-of-type(1)", "In Names", STARTS_WITH)
9+
assert: "input.search-input:focus"
10+
// Use left-right keys
11+
press-key: "ArrowDown"
12+
assert: "#results > .search-results.active > a:nth-of-type(1):focus"
13+
press-key: "ArrowRight"
14+
wait-for-attribute: ("#titles > button:nth-of-type(2)", {"class": "selected"})
15+
press-key: "ArrowRight"
16+
wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
17+
press-key: "ArrowRight"
18+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
19+
press-key: "ArrowLeft"
20+
wait-for-attribute: ("#titles > button:nth-of-type(3)", {"class": "selected"})
21+
22+
// Now try search-by-return
23+
goto: file://|DOC_PATH|/test_docs/index.html
24+
write: (".search-input", "-> String")
25+
// Waiting for the search results to appear...
26+
wait-for: "#titles"
27+
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
28+
assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH)
29+
assert: "input.search-input:focus"
30+
// Use left-right keys
31+
press-key: "ArrowDown"
32+
assert: "#results > .search-results.active > a:nth-of-type(1):focus"
33+
press-key: "ArrowRight"
34+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
35+
press-key: "ArrowRight"
36+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
37+
press-key: "ArrowRight"
38+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
39+
press-key: "ArrowLeft"
40+
wait-for-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
41+
42+
// Try with a search-by-return with no results
43+
goto: file://|DOC_PATH|/test_docs/index.html
44+
write: (".search-input", "-> Something")
45+
// Waiting for the search results to appear...
46+
wait-for: "#titles"
47+
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
48+
assert-text: ("#titles > button:nth-of-type(1)", "In Function Return Types", STARTS_WITH)
49+
50+
// Try with a search-by-parameter
51+
goto: file://|DOC_PATH|/test_docs/index.html
52+
write: (".search-input", "usize pattern")
53+
// Waiting for the search results to appear...
54+
wait-for: "#titles"
55+
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
56+
assert-text: ("#titles > button:nth-of-type(1)", "In Function Parameters", STARTS_WITH)
57+
58+
// Try with a search-by-parameter-and-return
59+
goto: file://|DOC_PATH|/test_docs/index.html
60+
write: (".search-input", "pattern -> str")
61+
// Waiting for the search results to appear...
62+
wait-for: "#titles"
63+
assert-attribute: ("#titles > button:nth-of-type(1)", {"class": "selected"})
64+
assert-text: ("#titles > button:nth-of-type(1)", "In Function Signatures", STARTS_WITH)

src/test/rustdoc-gui/search-tab-selection-if-current-is-empty.goml

-23
This file was deleted.

0 commit comments

Comments
 (0)