rust-lang / docs.rs

crates.io documentation generator
https://docs.rs
MIT License
997 stars 198 forks source link

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

Closed xFrednet closed 4 months ago

xFrednet commented 4 months ago

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:

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:

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

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.

syphar commented 4 months ago

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:

xFrednet commented 4 months ago

I'll look into this :D

@rustbot claim

syphar commented 4 months ago

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.

xFrednet commented 4 months ago

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

syphar commented 4 months ago

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

xFrednet commented 4 months ago

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.

syphar commented 4 months ago

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

@GuillaumeGomez what do you think?

( also about the piece of code )

GuillaumeGomez commented 4 months ago

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