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

Add crate filter parameter in URL #92735

Merged
merged 4 commits into from
Feb 4, 2022

Conversation

GuillaumeGomez
Copy link
Member

Fixes #92621.

r? @jsha

@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-search Area: Rustdoc's search feature A-rustdoc-js Area: Rustdoc's front-end labels Jan 10, 2022
@rust-highfive
Copy link
Collaborator

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez,@Folyd

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 10, 2022
@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@jsha jsha left a comment

Choose a reason for hiding this comment

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

Can you add a description of 0bd7b06fdf5c3cfa9e69728542bbc751b690a4e0 in the PR description?

src/librustdoc/html/static/js/search.js Outdated Show resolved Hide resolved
src/librustdoc/html/static/js/search.js Outdated Show resolved Hide resolved
if (!history.state && !params.search) {
history.pushState(query, "", newURL);
history.pushState(query.raw, "", newURL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the change to query.raw here? Do we actually use this field of the history state?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was invalid before for some reason...

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, we don't use as it seems. So I guess it only makes it smaller?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll clarify: We should just pass null here (and in the replaceState call below) because we never use the resulting state in our popstate event handler.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I see! Let's do this then!

src/librustdoc/html/static/js/search.js Outdated Show resolved Hide resolved
Comment on lines 1637 to 1678
if (ev.target.value === "All crates") {
removeLocalStorageValue("saved-filter-crate");
// If we don't remove it from the URL, it'll be picked up again by the search.
var params = searchState.getQueryStringParams();
var query = searchState.input.value.trim();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is the "update URL" code path only activated when selecting "All crates"? It seems like we should always update the URL after the user makes a selection from the crates dropdown.

Copy link
Member Author

@GuillaumeGomez GuillaumeGomez Feb 2, 2022

Choose a reason for hiding this comment

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

Because it'll be done in the search for the other cases (ie, when we have a filter crate). This needs to be changed the current URL before we make the search so that the filter crate won't be picked since there isn't one anymore.

src/librustdoc/html/static/js/search.js Outdated Show resolved Hide resolved
@GuillaumeGomez GuillaumeGomez force-pushed the crate-filter-url-param branch 3 times, most recently from 339a242 to 4b216f5 Compare February 2, 2022 22:17
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the crate-filter-url-param branch 2 times, most recently from eeeb9d5 to 075a97c Compare February 2, 2022 22:43
@GuillaumeGomez
Copy link
Member Author

Updated!

@rust-log-analyzer

This comment has been minimized.

@@ -134,6 +134,18 @@ function hideThemeButtonState() {
themePicker.style.borderBottomLeftRadius = "3px";
}

function buildUrl(search, filterCrates) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add some JSDoc comments to this function, indicating that filterCrates is optional - or better yet, make it non-optional, and specify that it should be "null" for "no filter". Then we can remove the !== "undefined" check below.

Also, let's move this function to search.js. It's search-specific functionality, after all. And we shouldn't call it in hideResults; we should keep the code currently there, which doesn't care about search URL parameters.

In general, many of the things in searchState can and should be moved to search.js eventually. What's in there is the stuff that had relationships with other things in main.js that I didn't want to untangle in my initial refactoring to split search.js.

Comment on lines 260 to 261
// This function will be replaced once the search has been loaded.
getFilterCrates: function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not do this. Instead, let's move putBackSearch into search.js.

Since putBackSearch is referenced from the event listener created in setup, we'll also have to move the registration of that event listener into search.js.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually it's not possible because we load the search.js file thanks to this event, making its functions unavailable until they're loaded (so after this event is triggered). I'll try to find another way.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nevermind, it's totally possible. I misunderstood what putBackSearch was doing.

Copy link
Contributor

Choose a reason for hiding this comment

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

In main.js we have:

        setup: function() {
            ...
            search_input.addEventListener("focus", function() {
                searchState.putBackSearch(this);
                search_input.origPlaceholder = searchState.input.placeholder;
                search_input.placeholder = "Type your search here.";
                loadSearch();
            });
            search_input.addEventListener("blur", function() {
                search_input.placeholder = searchState.input.origPlaceholder;
            });

Those addEventListener calls should be moved into search.js, in registerSearchEvents. I think that should work. At the time that function runs, putBackSearch will be available, even if it's defined inside search.js.

Copy link
Member Author

Choose a reason for hiding this comment

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

I still need to keep focus in here because it loads the script. And to make it smoother, I'll keep the placeholder update before the search scripts are loaded.

Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, I've been thinking: maybe we can do the placeholder with CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder

#search-input::placeholder {
  content: "Click or press 'S' for search...";
}

#search-input:focus::placeholder {
  content: "Type your search here.";
}

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought you didn't want to put text in the CSS? I remember something like that. Well, if so, I'll send a small PR for that. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Just tried it, you can't set content for ::placeholder unfortunately...

src/librustdoc/html/static/js/search.js Outdated Show resolved Hide resolved
if (!history.state && !params.search) {
history.pushState(query, "", newURL);
history.pushState(query.raw, "", newURL);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll clarify: We should just pass null here (and in the replaceState call below) because we never use the resulting state in our popstate event handler.

@jsha
Copy link
Contributor

jsha commented Feb 3, 2022

I had another thought about this: We could make the crate dropdown edit the textual search. For instance, if you are on https://doc.rust-lang.org/nightly/std/index.html?search=panic and select "core" from the drop-down, the URL would change to https://doc.rust-lang.org/nightly/std/index.html?search=panic+crate:core. And we would also parse crate:core in the search box to mean "filter to just the core crate."

@GuillaumeGomez
Copy link
Member Author

That would mean strongly changing the search syntax (which I have another PR for :p). Better to be done as part of the other PR. Or in a follow-up, as you prefer. But changing the search input syntax isn't a good idea before the parser is merged.

@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez
Copy link
Member Author

I think I applied all your comments as much as I could. I moved parts of the event subscriptions on the search input in the search.js file but I couldn't move all of them (the focus one cannot for example).

@jsha
Copy link
Contributor

jsha commented Feb 4, 2022

@bors r+ rollup

Looks great, thanks!

@bors
Copy link
Contributor

bors commented Feb 4, 2022

📌 Commit 829a047 has been approved by jsha

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 4, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 4, 2022
…askrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#92735 (Add crate filter parameter in URL)
 - rust-lang#93402 (Windows: Disable LLVM crash dialog boxes.)
 - rust-lang#93508 (Add rustdoc info to jsondocck output)
 - rust-lang#93551 (Add package.json in gitignore)
 - rust-lang#93555 (Link `try_exists` docs to `Path::exists`)
 - rust-lang#93585 (Missing tests for rust-lang#92630)
 - rust-lang#93593 (Fix ret > 1 bound if shadowed by const)
 - rust-lang#93630 (clippy::perf fixes)
 - rust-lang#93631 (rustc_mir_dataflow: use iter::once instead of Some().into_iter)
 - rust-lang#93632 (rustdoc: clippy::complexity fixes)
 - rust-lang#93638 (rustdoc: remove unused Hash impl)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f2721fa into rust-lang:master Feb 4, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 4, 2022
@GuillaumeGomez GuillaumeGomez deleted the crate-filter-url-param branch February 4, 2022 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-js Area: Rustdoc's front-end A-rustdoc-search Area: Rustdoc's search feature S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add rustdoc search "crate filter" into the URL when it's being used
6 participants