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

rustdoc: add line breaks to where clauses a la rustfmt #37190

Merged
merged 6 commits into from
Nov 12, 2016

Conversation

QuietMisdreavus
Copy link
Member

Much like my last PR for rustdoc (#36679), this adds line breaks to certain statements based on their line length. Here the focus was on where clauses.

Some examples:

The libstd links above are the whole docs rendered with this, and the "bare function" link above is in another set that pulls some notable crates together. petgraph was the one that brought this request up, and that collection also includes itertools which provided an easy sample to test with.

r? @GuillaumeGomez

@@ -50,7 +50,7 @@ pub struct MutableSpace(pub clean::Mutability);
#[derive(Copy, Clone)]
pub struct RawMutableSpace(pub clean::Mutability);
/// Wrapper struct for emitting a where clause from Generics.
pub struct WhereClause<'a>(pub &'a clean::Generics);
pub struct WhereClause<'a>(pub &'a clean::Generics, pub String);
Copy link
Member

Choose a reason for hiding this comment

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

I'd be in favor of making this:

pub struct WhereClause<'a> {
    pub generics: &'a clean::Generics,
    pub pad: String,
};

So we're explicit about what the fields are instead of a collection of unnamed data structures.

Copy link
Member Author

Choose a reason for hiding this comment

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

No big deal to me either way. Looking at it again now, I kinda want to change that String (and Method's str, above) to integers since all they're conveying is the number of spaces to prepend onto new lines. They're passed differently, too, but handled the same.

clause.push_str(", ");
} else {
clause.push_str(",<br>");
}
Copy link
Member

Choose a reason for hiding this comment

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

Subjective, but these sort of patterns can be written:

clause.push_str(if f.alternate() {
    ", "
} else {
    ",<br>"
})

Copy link
Member Author

Choose a reason for hiding this comment

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

Personal preference, I guess. It it part of a broader style guideline somewhere? At that point it's a ternary expression, too, and can be collapsed onto one line.

Copy link
Member

Choose a reason for hiding this comment

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

It it part of a broader style guideline somewhere?

I don't think so; what you have is fine.

if f.alternate() {
write!(f, "impl{:#} ", i.generics)?;
} else {
write!(f, "impl{} ", i.generics)?;
}
plain.push_str(&format!("impl{:#} ", i.generics));
Copy link
Member

Choose a reason for hiding this comment

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

Could this just be:

let mut plain = format!("impl{:#} ", i.generics);

...and then getting rid of the String::new() above?

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 don't see why not. I guess my intent was to have the declaration separate since I was going to be pushing more things onto it later.

@bluss
Copy link
Member

bluss commented Oct 16, 2016

Great work! It's an improvement for sure.

Maybe where clauses that are line broken should always be hanging on impls? Here's an image to illustrate http://i.imgur.com/UjP8aK9.png (2) looks good and (1) is not as good.

}
match pred {
&clean::WherePredicate::BoundPredicate { ref ty, ref bounds } => {
let bounds = bounds;
if f.alternate() {
write!(f, "{:#}: {:#}", ty, TyParamBounds(bounds))?;
clause.push_str(&format!("{:#}: {:#}", ty, TyParamBounds(bounds)));
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to use write! instead of .push_str(&format!( to avoid an extra string allocation. For example, by importing std::fmt::Write this can be write!(clause, "{:#}: {:#}", ty, TyParamBounds(bounds)).unwrap();.

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 about that adjustment as I was going through - I even asked in IRC whether it was possible to use write!() on a String when I was doing it. I decided against writing it in right then because there were other places (not just in my additions here or the previous PR) that used this pattern, so my sense of purism wanted to separate that change to its own logical unit, after this one got dealt with.

@QuietMisdreavus
Copy link
Member Author

@bluss To be honest, minutes after I posted this PR I thought about whether that was the way to go. Doing that change would be pretty easy - I'll go ahead and work that in.

@@ -2899,10 +2945,12 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi

fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
t: &clean::Typedef) -> fmt::Result {
let indent = format!("type {}{:#} ", it.name.as_ref().unwrap(), t.generics);
let indent = repeat(" ").take(indent.len()).collect::<String>();
Copy link
Member

@bluss bluss Oct 16, 2016

Choose a reason for hiding this comment

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

There's a new method for this actually, " ".repeat(indent.len()) (-> String). It has the advantage of sizing the allocation correctly upfront.

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 yeah, I remember you talking about that on IRC. This is a pattern used more broadly than my changes, so I'd be more comfortable taking that to a separate PR? I dunno what the general consensus on that would be, though.

@bluss
Copy link
Member

bluss commented Oct 16, 2016

Here's a way to count the length of a formatting without making the string. https://is.gd/Yks7tx

@QuietMisdreavus
Copy link
Member Author

@bluss On that counting code: Looking at it again, it seems to be just a byte count? That might backfire if non-ascii idents are allowed. On the other hand, I've already been falling into the same trap by using len() myself, so it's not a substantial change from what I've already been doing. I'd be willing to work it in.

@bluss
Copy link
Member

bluss commented Oct 17, 2016

Yes that's true. It's the same count as String::len's, though.

@bluss
Copy link
Member

bluss commented Oct 23, 2016

The result looks great, and I'd really like this. I think it would be a lot easier to accept if we had a cleaner way to do the plain/vs regular formatting, without the duplicate code for each piece.

@alexcrichton
Copy link
Member

@rust-lang/docs thoughts about this PR? It seems to be getting a little old, but it's still mergeable! Would nice to get a resolution!

@alexcrichton alexcrichton added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Nov 10, 2016
@GuillaumeGomez
Copy link
Member

@alexcrichton: I totally forgot about this one. Since people are globally in favor for it, let's merge it. It's still possible in the future to remove it anyway.

@QuietMisdreavus: Thanks for your work and sorry for the delay!

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Nov 11, 2016

📌 Commit 61cc870 has been approved by GuillaumeGomez

eddyb added a commit to eddyb/rust that referenced this pull request Nov 11, 2016
…ne, r=GuillaumeGomez

rustdoc: add line breaks to where clauses a la rustfmt

Much like my last PR for rustdoc (rust-lang#36679), this adds line breaks to certain statements based on their line length. Here the focus was on where clauses.

Some examples:
- [Where clause in a trait function](https://shiva.icesoldier.me/custom-std/std/iter/trait.Iterator.html?search=#method.unzip) (also in the trait header block at the top of the page)
- [Where clause on a bare function](https://shiva.icesoldier.me/doc-custom2/petgraph/visit/fn.depth_first_search.html)
- [Where clauses in trait impls on a struct](https://shiva.icesoldier.me/custom-std/std/collections/struct.HashMap.html) (scroll to the bottom) These are regularly not on their own line, but will be given their own line now if their "prefix text" doesn't give them enough room to sensibly print their constraints. HashMap's trait impls provide some examples of both behaviors.

The libstd links above are the whole docs rendered with this, and the "bare function" link above is in another set that pulls some notable crates together. `petgraph` was the one that brought this request up, and that collection also includes [itertools](https://shiva.icesoldier.me/doc-custom2/itertools/trait.Itertools.html) which provided an easy sample to test with.

r? @GuillaumeGomez
eddyb added a commit to eddyb/rust that referenced this pull request Nov 12, 2016
…ne, r=GuillaumeGomez

rustdoc: add line breaks to where clauses a la rustfmt

Much like my last PR for rustdoc (rust-lang#36679), this adds line breaks to certain statements based on their line length. Here the focus was on where clauses.

Some examples:
- [Where clause in a trait function](https://shiva.icesoldier.me/custom-std/std/iter/trait.Iterator.html?search=#method.unzip) (also in the trait header block at the top of the page)
- [Where clause on a bare function](https://shiva.icesoldier.me/doc-custom2/petgraph/visit/fn.depth_first_search.html)
- [Where clauses in trait impls on a struct](https://shiva.icesoldier.me/custom-std/std/collections/struct.HashMap.html) (scroll to the bottom) These are regularly not on their own line, but will be given their own line now if their "prefix text" doesn't give them enough room to sensibly print their constraints. HashMap's trait impls provide some examples of both behaviors.

The libstd links above are the whole docs rendered with this, and the "bare function" link above is in another set that pulls some notable crates together. `petgraph` was the one that brought this request up, and that collection also includes [itertools](https://shiva.icesoldier.me/doc-custom2/itertools/trait.Itertools.html) which provided an easy sample to test with.

r? @GuillaumeGomez
bors added a commit that referenced this pull request Nov 12, 2016
@bors bors merged commit 61cc870 into rust-lang:master Nov 12, 2016
@QuietMisdreavus QuietMisdreavus deleted the rustdoc-where-newline branch November 14, 2016 14:27
steveklabnik added a commit to steveklabnik/rust that referenced this pull request Dec 24, 2016
…, r=steveklabnik

rustdoc: properly calculate line length for where clauses

Apparently, while I was cleaning up rust-lang#37190, I regressed the formatting for long where clauses, where it wouldn't take the "prefix" length into account when deciding whether to break the line up. This patch fixes that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants