docs.rs icon indicating copy to clipboard operation
docs.rs copied to clipboard

Add lines labels to `/crate/**/source/`

Open xFrednet opened this issue 1 year ago • 8 comments

While generating some links, I noticed that source files under https://docs.rs/crate/{crate}/{version}/source/src/{file} don't support line labels. This is opposed to the sources under https://docs.rs/{crate}/{version}/src/{krate}/{file}.html#{line} that do.

Usually, I'd just use the second link with line label support for everything. However, rustdoc can sometimes remap paths, for example if a path is set in Cargo.toml. This means that the second link doesn't always work. At the end of the issue, I'll give a detailed example of this.

For my use case, the best solution would be to add line numbers and line labels to the sources under crate/{crate}/{version}/source/.

I'd be happy to give the implementation a shot, if this is something that would be accepted and I get some hints where to start :dagger:


Specific example:

We run Clippy in our CI and collect lint emissions. These diagnostics have paths as seen by rustc. It would, for example, look like this: ./src/lib.rs:15. I now want to take these file names and link to docs.rs to help us investigate the lint emissions. For most crates, the link is simply constructed like this:

  • https://docs.rs/{krate}/{version}/src/{krate}/{file}.html#{line}

And this works for most crates, expect crates that remap their path in Cargo.toml, like this (Taken from rust-lang/cargo):

[lib]
name = "cargo"
path = "src/cargo/lib.rs"

The lint emission then has the path ./src/cargo/lib.rs but the documentation generated by rustdoc removes the cargo from the path. So using the previous link template generates:

  • https://docs.rs/cargo/0.80.0/src/cargo/cargo/lib.rs.html#10

This doesn't work due to the cargo/cargo. The correct link with the remapped path is:

  • https://docs.rs/cargo/0.80.0/src/cargo/lib.rs.html#10

This remapping only happens for rustdoc and not the source files under crate/ so if those would support line labels, it would be a simple way to always use the same link template.

xFrednet avatar Jul 12 '24 09:07 xFrednet

I would be totally fine with having this feature and definitely see a use-case.

I'm happy to review any PR adding this. the interesting files are:

  • https://github.com/rust-lang/docs.rs/blob/master/src/web/source.rs
  • https://github.com/rust-lang/docs.rs/blob/master/templates/crate/source.html

syphar avatar Jul 12 '24 12:07 syphar

I'll look into this :D

@rustbot claim

xFrednet avatar Jul 16 '24 07:07 xFrednet

there is a current big PR (#2292) that will change template rendering, not sure if you want to wait with your changes until it lands, or base your work ontop of it.

syphar avatar Jul 16 '24 07:07 syphar

In that case, I'll wait a bit. Thank you for the early notice :D

xFrednet avatar Jul 16 '24 07:07 xFrednet

#2292 is merged, so feel free to start @xFrednet :)

syphar avatar Jul 16 '24 13:07 syphar

I've made some decent progress. My next step is now to highlight the line numbers. I found the implementation of rustdoc which seems to work well. Can I copy and adapt it, as long as I add a comment mentioning where it comes from? I'm asking regarding the license etc.

xFrednet avatar Jul 17 '24 20:07 xFrednet

IANAL, but IMO you can just copy & adapt the code, even without attribution.

@GuillaumeGomez what do you think?

( also about the piece of code )

syphar avatar Jul 18 '24 05:07 syphar

Yes it's completely fine. Just need to be careful that the line height of the line number is aligned to the code line.

GuillaumeGomez avatar Jul 18 '24 08:07 GuillaumeGomez