Skip to content
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

Merged
merged 2 commits into from
Aug 30, 2020

Conversation

matklad
Copy link
Member

@matklad matklad commented Aug 21, 2020

r? @ghost

@matklad
Copy link
Member Author

matklad commented Aug 21, 2020

This is not ready for review yet, but

r? @GuillaumeGomez

just to give a heads up

@GuillaumeGomez
Copy link
Member

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()?;
}
Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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!

Copy link
Member

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);?

Copy link
Member Author

Choose a reason for hiding this comment

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

image

Added the test. There are two spans for <= and one span for && but they all are ops, 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.

Copy link
Member

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.

@matklad
Copy link
Member Author

matklad commented Aug 21, 2020

@GuillaumeGomez that particular test is deliberately removed, see the commit message in b48b66c.

I do plan to add more unit-tests here, using #75773

@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch 2 times, most recently from ab1987c to 1d083ce Compare August 21, 2020 17:27
@petrochenkov petrochenkov self-assigned this Aug 21, 2020
@petrochenkov petrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 21, 2020
@ollie27 ollie27 added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Aug 22, 2020
@petrochenkov petrochenkov removed their assignment Aug 22, 2020
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch 3 times, most recently from 3788b43 to 63f6533 Compare August 27, 2020 10:52
@matklad matklad changed the title WIP: Use rustc_lexer for rustdoc syntax highlighting Use rustc_lexer for rustdoc syntax highlighting Aug 27, 2020
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from 63f6533 to 957511e Compare August 27, 2020 10:59
@matklad
Copy link
Member Author

matklad commented Aug 27, 2020

This is ready for review @GuillaumeGomez !

@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch 2 times, most recently from 5f18bab to f54359d Compare August 27, 2020 11:34
@matklad
Copy link
Member Author

matklad commented Aug 27, 2020

Moved the tests, will open the issue about more precise highlighting shortly

@GuillaumeGomez
Copy link
Member

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. :)

Copy link
Member

@jyn514 jyn514 left a 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?

src/librustdoc/html/highlight.rs Outdated Show resolved Hide resolved
src/librustdoc/html/highlight.rs Show resolved Hide resolved
src/librustdoc/test/fixtures/sample.html Outdated Show resolved Hide resolved
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from f54359d to 22f709f Compare August 27, 2020 12:28
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from 22f709f to fe9d84d Compare August 27, 2020 12:49
@matklad
Copy link
Member Author

matklad commented Aug 27, 2020

@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)

@jyn514
Copy link
Member

jyn514 commented Aug 27, 2020

Looks great, thanks!

src/test/rustdoc/bad-codeblock-syntax.rs Show resolved Hide resolved
@@ -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() {
Copy link
Member

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.

Copy link
Member

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.
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from fe9d84d to 21c4bd9 Compare August 27, 2020 16:01
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from 21c4bd9 to 0b16ff3 Compare August 27, 2020 16:35
@matklad
Copy link
Member Author

matklad commented Aug 27, 2020

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
@matklad matklad force-pushed the rustc-lexer-rustdoc-highlight branch from 0b16ff3 to b4f4db9 Compare August 27, 2020 16:52
matklad added a commit to matklad/rust that referenced this pull request Aug 29, 2020
@GuillaumeGomez
Copy link
Member

Thanks!

@bors: r+

@bors
Copy link
Contributor

bors commented Aug 29, 2020

📌 Commit b4f4db9 has been approved by GuillaumeGomez

@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 Aug 29, 2020
@bors
Copy link
Contributor

bors commented Aug 29, 2020

⌛ Testing commit b4f4db9 with merge ced37a5...

@bors
Copy link
Contributor

bors commented Aug 30, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: GuillaumeGomez
Pushing ced37a5 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 30, 2020
@bors bors merged commit ced37a5 into rust-lang:master Aug 30, 2020
@cuviper cuviper added this to the 1.48.0 milestone May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants