Skip to content
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

Move crate drop-down to search results page #92490

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/ci/docker/host-x86_64/mingw-check/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
pkg-config \
mingw-w64

RUN curl -sL https://nodejs.org/dist/v14.4.0/node-v14.4.0-linux-x64.tar.xz | tar -xJ
ENV PATH="/node-v14.4.0-linux-x64/bin:${PATH}"
RUN curl -sL https://nodejs.org/dist/v16.9.0/node-v16.9.0-linux-x64.tar.xz | tar -xJ
ENV PATH="/node-v16.9.0-linux-x64/bin:${PATH}"
# Install es-check
# Pin its version to prevent unrelated CI failures due to future es-check versions.
RUN npm install es-check@5.2.3 -g
RUN npm install eslint@7.20.0 -g
RUN npm install es-check@6.1.1 -g
RUN npm install eslint@8.6.0 -g

COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh
Expand All @@ -40,5 +40,5 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \
/scripts/validate-toolstate.sh && \
/scripts/validate-error-codes.sh && \
# Runs checks to ensure that there are no ES5 issues in our JS code.
es-check es5 ../src/librustdoc/html/static/js/*.js && \
es-check es6 ../src/librustdoc/html/static/js/*.js && \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to make this change in bigger PR but I guess it's fine... Don't forget to update the comment just above please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think a giant PR adding ES6 features wherever possible, and updating the linter at the same time would be a bad idea. The way we should do it is like I'm doing here:

  • update the linter to accept es6 as a standalone commit
  • start using es6 features where they are useful
  • do a series of standalone commits that do one of two things:
    • adopt an ES6 feature where it is useful (e.g. const everything that can be consted), or
    • migrate a discrete chunk of code to using various useful ES6 features.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said, it's fine. 😉

eslint ../src/librustdoc/html/static/js/*.js
18 changes: 12 additions & 6 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -862,18 +862,24 @@ h2.small-section-header > .anchor {
display: inline-flex;
width: calc(100% - 63px);
}
.search-results-title {
display: inline;
}
#search-settings {
font-size: 1.5rem;
font-weight: 500;
margin-bottom: 20px;
}
#crate-search {
min-width: 115px;
margin-top: 5px;
padding: 6px;
padding-right: 19px;
flex: none;
margin-left: 0.2em;
padding-left: 0.3em;
padding-right: 23px;
border: 0;
border-right: 0;
border-radius: 4px 0 0 4px;
border-radius: 4px;
outline: none;
cursor: pointer;
border-right: 1px solid;
-moz-appearance: none;
-webkit-appearance: none;
/* Removes default arrow from firefox */
Expand Down
39 changes: 22 additions & 17 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ window.initSearch = function(rawSearchIndex) {
return "<button>" + text + " <div class=\"count\">(" + nbElems + ")</div></button>";
}

function showResults(results, go_to_first) {
function showResults(results, go_to_first, filterCrates) {
var search = searchState.outputElement();
if (go_to_first || (results.others.length === 1
&& getSettingValue("go-to-only-result") === "true"
Expand Down Expand Up @@ -1126,9 +1126,16 @@ window.initSearch = function(rawSearchIndex) {
}
}

var output = "<h1>Results for " + escape(query.query) +
let crates = `<select id="crate-search"><option value="All crates">All crates</option>`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't eslint complaining when you use backticks without ${} content?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope! I wouldn't expect it to. Backticks do a bunch of things besides template expansion. They also allow multiline strings, and serve as a different delimiter so you don't have to backslash double quotes (\")

for (let c of window.ALL_CRATES) {
crates += `<option value="${c}" ${c == filterCrates && "selected"}>${c}</option>`;
}
crates += `</select>`;
var output = `<div id="search-settings">
<h1 class="search-results-title">Results for ${escape(query.query)} ` +
(query.type ? " (type: " + escape(query.type) + ")" : "") + "</h1>" +
"<div id=\"titles\">" +
` in ${crates} ` +
`</div><div id="titles">` +
makeTabHeader(0, "In Names", ret_others[1]) +
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
makeTabHeader(2, "In Return Types", ret_returned[1]) +
Expand All @@ -1141,6 +1148,7 @@ window.initSearch = function(rawSearchIndex) {
resultsElem.appendChild(ret_returned[0]);

search.innerHTML = output;
document.getElementById("crate-search").addEventListener("input", updateCrate);
search.appendChild(resultsElem);
// Reset focused elements.
searchState.focusedByTab = [null, null, null];
Expand Down Expand Up @@ -1316,7 +1324,8 @@ window.initSearch = function(rawSearchIndex) {
}

var filterCrates = getFilterCrates();
showResults(execSearch(query, searchWords, filterCrates), params["go_to_first"]);
showResults(execSearch(query, searchWords, filterCrates),
params["go_to_first"], filterCrates);
}

function buildIndex(rawSearchIndex) {
Expand Down Expand Up @@ -1552,19 +1561,6 @@ window.initSearch = function(rawSearchIndex) {
}
});


var selectCrate = document.getElementById("crate-search");
if (selectCrate) {
selectCrate.onchange = function() {
updateLocalStorage("rustdoc-saved-filter-crate", selectCrate.value);
// In case you "cut" the entry from the search input, then change the crate filter
// before paste back the previous search, you get the old search results without
// the filter. To prevent this, we need to remove the previous results.
currentResults = null;
search(undefined, true);
};
}

// Push and pop states are used to add search results to the browser
// history.
if (searchState.browserSupportsHistoryApi()) {
Expand Down Expand Up @@ -1616,6 +1612,15 @@ window.initSearch = function(rawSearchIndex) {
};
}

function updateCrate(ev) {
updateLocalStorage("rustdoc-saved-filter-crate", ev.target.value);
// In case you "cut" the entry from the search input, then change the crate filter
// before paste back the previous search, you get the old search results without
// the filter. To prevent this, we need to remove the previous results.
currentResults = null;
search(undefined, true);
}

searchWords = buildIndex(rawSearchIndex);
registerSearchEvents();
// If there's a search term in the URL, execute the search now.
Expand Down
6 changes: 1 addition & 5 deletions src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@
</div> {#- -#}
<form class="search-form"> {#- -#}
<div class="search-container"> {#- -#}
<div>{%- if layout.generate_search_filter -%}
<select id="crate-search"> {#- -#}
<option value="All crates">All crates</option> {#- -#}
</select> {#- -#}
{%- endif -%}
<div>
<input {# -#}
class="search-input" {# -#}
name="search" {# -#}
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc-gui/escape-key.goml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
goto: file://|DOC_PATH|/test_docs/index.html
// First, we check that the search results are hidden when the Escape key is pressed.
write: (".search-input", "test")
wait-for: "#search > h1" // The search element is empty before the first search
wait-for: "#search h1" // The search element is empty before the first search
assert-attribute: ("#search", {"class": "content"})
assert-attribute: ("#main-content", {"class": "content hidden"})
press-key: "Escape"
Expand Down
4 changes: 1 addition & 3 deletions src/test/rustdoc-gui/search-filter.goml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ write: (".search-input", "test")
wait-for: "#titles"
assert-text: ("#results .externcrate", "test_docs")

goto: file://|DOC_PATH|/test_docs/index.html
wait-for: "#crate-search"
// We now want to change the crate filter.
click: "#crate-search"
// We select "lib2" option then press enter to change the filter.
press-key: "ArrowDown"
press-key: "Enter"
// We now make the search again.
write: (".search-input", "test")
// Waiting for the search results to appear...
wait-for: "#titles"
// We check that there is no more "test_docs" appearing.
Expand Down
6 changes: 3 additions & 3 deletions src/test/rustdoc-gui/toggle-docs-mobile.goml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
size: (433, 600)
assert-attribute: (".top-doc", {"open": ""})
click: (4, 280) // This is the position of the top doc comment toggle
click: (4, 240) // This is the position of the top doc comment toggle
assert-attribute-false: (".top-doc", {"open": ""})
click: (4, 280)
click: (4, 240)
assert-attribute: (".top-doc", {"open": ""})
// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked.
click: (3, 280)
click: (3, 240)
assert-attribute: (".top-doc", {"open": ""})

// Assert the position of the toggle on the top doc block.
Expand Down