-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Use rustc_lexer for rustdoc syntax highlighting #75775
Conversation
This is not ready for review yet, but just to give a heads up |
It looks like a nice start after a quick read. Don't forget to put back tests. :p |
if let Some(joint) = next.glue(self.peek()?) { | ||
next = joint; | ||
let _ = self.try_next_token()?; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bit is now gone, and, for example, &&
will always produce two spans now. I wonder if that is important? We need a real parser to do this precisely, but an approximate gluing heuristic won't be hard to add, if we need that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now it's fine, but please open an issue about it so it's not forgotten.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I wonder if this would fix rust-lang/docs.rs#484.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, that'd be nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matklad could you add a test/screenshot of how this highlights assert!(self.length < N && index <= self.length);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the test. There are two spans for <=
and one span for &&
but they all are op
s, so the end result looks fine.
On a meta note, I would be surprised if this doesn't regress some corner cases, but I think it should be fine to fix them as they come up, until we complete #75981.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
On a meta note, I would be surprised if this doesn't regress some corner cases, but I think it should be fine to fix them as they come up, until we complete #75981.
I agree, and I don't expect this to be any more broken than the existing code.
@GuillaumeGomez that particular test is deliberately removed, see the commit message in b48b66c. I do plan to add more unit-tests here, using #75773 |
ab1987c
to
1d083ce
Compare
3788b43
to
63f6533
Compare
63f6533
to
957511e
Compare
This is ready for review @GuillaumeGomez ! |
5f18bab
to
f54359d
Compare
Moved the tests, will open the issue about more precise highlighting shortly |
Looks good to me now! However, considering this is a big PR, I'd like to have another reviewer from @rust-lang/rustdoc to check if I didn't miss anything. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you post a screenshot of what the sample file looks like when highlighted?
f54359d
to
22f709f
Compare
22f709f
to
fe9d84d
Compare
@jyn514 thanks for the review, comments addressed (unless I've missed something)! Filed #75981 for further improvements. FYI, here's the html bit of rust-analyzer's syntax highlighting. Here the bulk of actual highlighting. Screenshot it #75775 (comment) |
Looks great, thanks! |
src/librustdoc/test/tests.rs
Outdated
@@ -277,3 +278,26 @@ test_wrapper! { | |||
let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION); | |||
assert_eq!(output, (expected, 1)); | |||
} | |||
|
|||
#[test] | |||
fn test_html_highlighting() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to put this test here. Syntax highlighting doesn't have anything to do with running doctests which is what this file is a submodule of. I suggest keeping these tests in src/librustdoc/html/highlight/tests.rs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with you for the location, however I think those tests should be put into a file on their own (like test/highlight.rs
).
rustc_lexer is the lossless lexer, which is a better fit for approximate syntax highlighting. As a side-effect, we can now syntax-highlight even broken code.
fe9d84d
to
21c4bd9
Compare
21c4bd9
to
0b16ff3
Compare
Move tests back to where they originally were, and submitted #75989 to not be confused with this next time. |
It's a unit-test in a sense that it only checks syntax highlighting. However, the resulting HTML is written to disk and can be easily inspected in the browser. To update the test, run with `--bless` argument or set `UPDATE_EXPEC=1` env var
0b16ff3
to
b4f4db9
Compare
Thanks! @bors: r+ |
📌 Commit b4f4db9 has been approved by |
☀️ Test successful - checks-actions, checks-azure |
r? @ghost