Skip to content

Improve CSS for source code block line numbers #143192

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

Merged
merged 2 commits into from
Jul 3, 2025

Conversation

GuillaumeGomez
Copy link
Member

Extract some changes from #137229 to make the PR smaller (thanks @yotamofek for the suggestion!).

r? notriddle

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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. labels Jun 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2025

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez, @jsha, @lolbinarycat

Copy link
Contributor

@lolbinarycat lolbinarycat left a comment

Choose a reason for hiding this comment

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

looks fine for the most part, just a few non-obvious things that could be clarified, and a few tiny edge cases that could hypothetically be improved, although I'm not sure if we care to, given how niche they are.

do we care about keeping git blame output clean? because rearranging things needlessly would degrade that slightly.

Comment on lines +927 to +935
.example-wrap.digits-1 { --example-wrap-digits-count: 1ch; }
.example-wrap.digits-2 { --example-wrap-digits-count: 2ch; }
.example-wrap.digits-3 { --example-wrap-digits-count: 3ch; }
.example-wrap.digits-4 { --example-wrap-digits-count: 4ch; }
.example-wrap.digits-5 { --example-wrap-digits-count: 5ch; }
.example-wrap.digits-6 { --example-wrap-digits-count: 6ch; }
.example-wrap.digits-7 { --example-wrap-digits-count: 7ch; }
.example-wrap.digits-8 { --example-wrap-digits-count: 8ch; }
.example-wrap.digits-9 { --example-wrap-digits-count: 9ch; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really the best way to do this? It very well may be "good enough", but here's some misc thoughts:

  1. pretty sure you can define -- variables in a style attribute then have the styles of child elements reference them with var. This way we could have the render logic pass an actual integer on to css instead of an integer-like enum.
  2. if a rust file used the maximum possible file size and made each line use an average of 4 bytes or less, including newlines, it would have a 10 digit number of lines. Do we ever generate a digits-10 css class?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. pretty sure you can define -- variables in a style attribute then have the styles of child elements reference them with var. This way we could have the render logic pass an actual integer on to css instead of an integer-like enum.

I'm not sure if I misunderstand what you mean or if you misunderstood what's going on in this code. Basically, example-wrap elements are everywhere, not just source code pages. Meaning we can have quite a wide number of lines (and of digits) around. I don't see how passing a variable to CSS could address this problem. Also, this is not an enum at all, just a class (digits-*) which is added by rustdoc when creating the <code>.

2. if a rust file used the maximum possible file size and made each line use an average of 4 bytes or less, including newlines, it would have a 10 digit number of lines. Do we ever generate a digits-10 css class?

I doubt a 10 billion lines source code will ever exist, so we should be on the safe side. Doing 1 billion is likely already far too much.

Copy link
Contributor

Choose a reason for hiding this comment

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

Basically, example-wrap elements are everywhere, not just source code pages.

Yes, and instead of generating class="digits-N" we could generate style="--digits: N;", removing the need to have 9 different CSS rules.

Also, this is not an enum at all

It is an enum in the sense that it has a small number of possible values, each of which is handled separately.

I doubt a 10 billion lines source code will ever exist, so we should be on the safe side. Doing 1 billion is likely already far too much.

This is fair, i might send a followup PR showing what I mean (as it does feel like a nicer way to handle this), but it's most definitely not significant enough to block the current PR on.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and instead of generating class="digits-N" we could generate style="--digits: N;", removing the need to have 9 different CSS rules.

Having inline style should be avoided at all cost as it cannot be cached by the web browser.

Also, I don't see how it's better to declare CSS variable everywhere compared to have 9 CSS rules. The complexity increase seems way too much (for the web browser).

@@ -8,6 +8,8 @@
3. Copy the filenames with updated suffixes from the directory.
*/

/* ignore-tidy-filelength */
Copy link
Contributor

Choose a reason for hiding this comment

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

This somewhat confuses me due to the fact that this file has actually gotten smaller, though it probably is correct to have here even if it isn't needed yet.

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 will be soon enough (like for #137229). We're already very close to the limit so better just allow it for everything to come.

@GuillaumeGomez
Copy link
Member Author

Reduced the diff by moving the .example-wrap .line-highlighted[data-nosnippet] rule up.

@GuillaumeGomez
Copy link
Member Author

Thanks for the reviews!

@bors r=lolbinary rollup

@bors
Copy link
Collaborator

bors commented Jul 2, 2025

📌 Commit 7c3bdda has been approved by lolbinary

It is now in the queue for this repository.

@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 Jul 2, 2025
bors added a commit that referenced this pull request Jul 3, 2025
Rollup of 9 pull requests

Successful merges:

 - #143192 (Improve CSS for source code block line numbers)
 - #143251 (bootstrap: add build.tidy-extra-checks option)
 - #143273 (Make the enum check work for negative discriminants)
 - #143292 (Explicitly handle all nodes in `generics_of` when computing parent)
 - #143316 (Add bootstrap check snapshot tests)
 - #143321 (byte-addresses memory -> byte-addressed memory)
 - #143324 (interpret: move the native call preparation logic into Miri)
 - #143325 (Use non-global interner in `test_string_interning` in bootstrap)
 - #143327 (miri: improve errors for type validity assertion failures)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6d54983 into rust-lang:master Jul 3, 2025
10 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 3, 2025
rust-timer added a commit that referenced this pull request Jul 3, 2025
Rollup merge of #143192 - GuillaumeGomez:code-line-number, r=lolbinary

Improve CSS for source code block line numbers

Extract some changes from #137229 to make the PR smaller (thanks `@yotamofek` for the suggestion!).

r? notriddle
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Jul 3, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#143192 (Improve CSS for source code block line numbers)
 - rust-lang/rust#143251 (bootstrap: add build.tidy-extra-checks option)
 - rust-lang/rust#143273 (Make the enum check work for negative discriminants)
 - rust-lang/rust#143292 (Explicitly handle all nodes in `generics_of` when computing parent)
 - rust-lang/rust#143316 (Add bootstrap check snapshot tests)
 - rust-lang/rust#143321 (byte-addresses memory -> byte-addressed memory)
 - rust-lang/rust#143324 (interpret: move the native call preparation logic into Miri)
 - rust-lang/rust#143325 (Use non-global interner in `test_string_interning` in bootstrap)
 - rust-lang/rust#143327 (miri: improve errors for type validity assertion failures)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. 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 this pull request may close these issues.

5 participants