Skip to content

[rustdoc] Feature Request: Allow code wrapping #127334

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

Closed
aramrw opened this issue Jul 4, 2024 · 21 comments · Fixed by #136991
Closed

[rustdoc] Feature Request: Allow code wrapping #127334

aramrw opened this issue Jul 4, 2024 · 21 comments · Fixed by #136991
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Comments

@aramrw
Copy link

aramrw commented Jul 4, 2024

Would be nice to have a setting (or just a button) that could have the code wrap, gets tiring if you are reading lots of code on your phone to scroll back and forth.

My bad if this has been requested before, I didn't see it in any issues yet

@syphar
Copy link
Member

syphar commented Jul 4, 2024

Hi,

thank you for the idea.

About which code views are you talking? can you provide example URLs?

I'm asking because there are actually two separate ones:

  • the rustdoc code view, where this issue would have to be moved
  • the docs.rs internal code view

@aramrw
Copy link
Author

aramrw commented Jul 4, 2024

Hi,

thank you for the idea.

About which code views are you talking? can you provide example URLs?

I'm asking because there are actually two separate ones:

  • the rustdoc code view, where this issue would have to be moved
  • the docs.rs internal code view

Like it says Run in the top right, could also have a wrap button there maybe if possible?
https://doc.rust-lang.org/std/rc/index.html#examples

@syphar
Copy link
Member

syphar commented Jul 4, 2024

Ah, that's even option 3),

not the code views for the crate itself, but code blocks in the docs.

I'm not sure if this is a rustdoc issue (where we don't have the "run" button yet), or an issue for the stdlib docs.

@GuillaumeGomez ?

@GuillaumeGomez
Copy link
Member

Sorry I still don't understand what's being asked. ^^'

@aramrw
Copy link
Author

aramrw commented Jul 4, 2024

Sorry I still don't understand what's being asked. ^^'

Are you talking to me or...? I mean wrapping the code blocks on the rust docs website. As in not having any overflow-x..? Having the text wrap to the next line, so you don't need to scroll left and right, to read the code block

@GuillaumeGomez
Copy link
Member

Oh I see. So a change in rustdoc. Moving the issue to the right repository.

@GuillaumeGomez GuillaumeGomez transferred this issue from rust-lang/docs.rs Jul 4, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 4, 2024
@GuillaumeGomez GuillaumeGomez changed the title Feature Request: Allow code wrapping [rustdoc] Feature Request: Allow code wrapping Jul 4, 2024
@GuillaumeGomez GuillaumeGomez added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-rustdoc-ui Area: Rustdoc UI (generated HTML) T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 4, 2024
@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Jul 4, 2024

In case it's not clear for everyone, let me expand a bit (please correct me if I'm wrong @aramrw):

Currently, the code examples in Rust HTML documentation are scrollable if they are too wide. The idea here would be to add a setting allowing for the code examples to instead wrap to the next line.

Now for the implementation: we need to be careful how this new setting would interact with the line numbers on the left (if the setting is enabled).

One thing I'm not clear about: @aramrw, would you want this setting to be applied to both code examples and source code pages or only to code examples?

I think it's fine to add such a setting. If @rust-lang/rustdoc-frontend agrees, I'll add it.

@aramrw
Copy link
Author

aramrw commented Jul 4, 2024

One thing I'm not clear about: @aramrw, would you want this setting to be applied to both code examples and source code pages or only to code examples?

If it's possible to add it for both the code examples & the source code it would be much appreciated 👍🏼

@notriddle
Copy link
Contributor

It's fine. Correct line numbers with soft wrapping is essentially a CSS table, which means the DOM needs redone. It's not just a one-line change, but it is feasible, and I'd approve it. It makes sense that you'd want it.

@GuillaumeGomez
Copy link
Member

A table has a limit of 10.000 rows, so might be problematic on source code page with some very big files.

@aramrw
Copy link
Author

aramrw commented Jul 7, 2024

A table has a limit of 10.000 rows, so might be problematic on source code page with some very big files.

If that's the case then only wrapping code blocks is fine because (on iphone at least), the source code allows you to pinch in and out very easily to see all the text, or at least most of it.

However the code block examples don't have that, so to see all the text you need to go into the browsers menu and keep messing with the zoom in & out buttons.

@notriddle
Copy link
Contributor

A table has a limit of 10.000 rows

Is that spelled out anywhere? I wrote a cursory test for this, and it seems to work fine in Firefox and Safari (I haven't tried it in Chrome).

<!DOCTYPE html>
<script>
document.write("<table>");
for (i = 0; i < 10000; ++i) {
    document.write("<tr><th>" + i + "</th><td>Nam provident a nihil omnis iure nihil. Ipsum enim omnis rerum dolorum quibusdam ab dolore quis. Vitae consequatur id ipsam ipsum mollitia eaque. Maxime et officia temporibus debitis consequuntur doloremque.</td></tr>");
}
document.write("<tr><th>This test row is added to ensure things look reasonable with very long columns</th><td>Nam provident a nihil omnis iure nihil. Ipsum enim omnis rerum dolorum quibusdam ab dolore quis. Vitae consequatur id ipsam ipsum mollitia eaque. Maxime et officia temporibus debitis consequuntur doloremque.</td></tr>");
</script>

The closest thing I'm able to find is a bug related to grids being limited to 10,000 lines, but I can't find anything on tables being limited.

@jsha
Copy link
Contributor

jsha commented Jul 8, 2024 via email

@camelid
Copy link
Member

camelid commented Jul 8, 2024

One idea: instead of having an option for this, maybe just make wrapping the default on mobile devices (i.e., screens that are sufficiently narrow by some heuristic). I'm wary of requiring too much configuration of rustdoc.

@GuillaumeGomez
Copy link
Member

#88545 Looks like it was grid layout and 1000 items, maybe?

I was looking for this discussion, thanks so much! So it was not for tables but for grids, good to know! One less problem to worry about.

One idea: instead of having an option for this, maybe just make wrapping the default on mobile devices (i.e., screens that are sufficiently narrow by some heuristic). I'm wary of requiring too much configuration of rustdoc.

We enter in the tricky area of personal taste. It's a case where being able to ask all rust doc users would be super useful.

@jsha
Copy link
Contributor

jsha commented Jul 8, 2024

I've been meaning to come here and post exactly what @camelid said. Our default position should be to not introduce new settings but try to just do the best thing. If we put it behind a setting, very few people will actually know about it and use it, but we will still bear the full burden of maintaining both versions.

In this case, the right thing is clearly to line-wrap on all platforms (not just mobile). Horizontal scrolling is a terrible experience on desktop / wide screens too.

This page has an example of doing line numbers with wrapping. It uses CSS counters, and seems pretty tidy.

@GuillaumeGomez
Copy link
Member

It can work for code examples, but not for source code as we need them to be link. But noted for the horizontal scrolling. Will make our work simpler.

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Jul 17, 2024

Another issue I didn't think of is that we use style with CSS classes across lines (so if you have a multi-line comment, it's currently only one element). It would greatly increase the size of the DOM. This feature is definitely more tricky than I initially thought...

@aramrw
Copy link
Author

aramrw commented Oct 9, 2024

any updates...?

@GuillaumeGomez
Copy link
Member

Yep, found an acceptable middle-ground for this feature to be done in #136829. It's just the first step but adding this setting should be pretty easy/quick from there.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 12, 2025
…nto-code, r=notriddle

[rustdoc] Move line numbers into the `<code>` directly

Fixes rust-lang#84242.

This is the first for adding support for rust-lang#127334 and also for another feature I'm working on.

A side-effect of this change is that it also fixes source code pages display in lynx since they're not directly in the source code.

To allow having code wrapping, the grid approach doesn't work as the line numbers are in their own container, so we need to move them into the code. Now with this, it becomes much simpler to do what we want (with CSS mostly). One downside: the highlighting became more complex and slow as we need to generate some extra HTML tags directly into the highlighting process. However that also allows to not have a huge HTML size increase.

You can test the result [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/scrape_examples/fn.test_many.html) and [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/src/scrape_examples/lib.rs.html#10).

The appearance should have close to no changes.

r? ``@notriddle``
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 12, 2025
Rollup merge of rust-lang#136829 - GuillaumeGomez:move-line-numbers-into-code, r=notriddle

[rustdoc] Move line numbers into the `<code>` directly

Fixes rust-lang#84242.

This is the first for adding support for rust-lang#127334 and also for another feature I'm working on.

A side-effect of this change is that it also fixes source code pages display in lynx since they're not directly in the source code.

To allow having code wrapping, the grid approach doesn't work as the line numbers are in their own container, so we need to move them into the code. Now with this, it becomes much simpler to do what we want (with CSS mostly). One downside: the highlighting became more complex and slow as we need to generate some extra HTML tags directly into the highlighting process. However that also allows to not have a huge HTML size increase.

You can test the result [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/scrape_examples/fn.test_many.html) and [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/src/scrape_examples/lib.rs.html#10).

The appearance should have close to no changes.

r? ``@notriddle``
@GuillaumeGomez
Copy link
Member

Opened PR to add the feature in #136991.

jhpratt added a commit to jhpratt/rust that referenced this issue Feb 24, 2025
…triddle

[rustdoc] Add new setting to wrap source code lines when too long

Fixes rust-lang#127334.

Wrapped lines look like this:

![image](https://github.com/user-attachments/assets/92006a27-ed1e-4beb-91f2-f453b72c5e1a)

It works in both source code pages and doc pages.

You can test it [here](https://rustdoc.crud.net/imperio/code-wrapping/bar/index.html).

r? `@notriddle`
@bors bors closed this as completed in 8133652 Feb 24, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 24, 2025
Rollup merge of rust-lang#136991 - GuillaumeGomez:code-wrapping, r=notriddle

[rustdoc] Add new setting to wrap source code lines when too long

Fixes rust-lang#127334.

Wrapped lines look like this:

![image](https://github.com/user-attachments/assets/92006a27-ed1e-4beb-91f2-f453b72c5e1a)

It works in both source code pages and doc pages.

You can test it [here](https://rustdoc.crud.net/imperio/code-wrapping/bar/index.html).

r? ``@notriddle``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rustdoc-ui Area: Rustdoc UI (generated HTML) T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants