Skip to content

Use rustc_lexer for rustdoc syntax highlighting - #75775

Merged
bors merged 2 commits into
rust-lang:masterfrom
matklad:rustc-lexer-rustdoc-highlight
Aug 30, 2020
Merged

Use rustc_lexer for rustdoc syntax highlighting#75775
bors merged 2 commits into
rust-lang:masterfrom
matklad:rustc-lexer-rustdoc-highlight

Conversation

@matklad

Copy link
Copy Markdown
Contributor

@matklad

Copy link
Copy Markdown
ContributorAuthor

This is not ready for review yet, but

r? @GuillaumeGomez

just to give a heads up

Comment threadsrc/librustdoc/html/highlight.rs Outdated
@GuillaumeGomez

Copy link
Copy Markdown
Member

It looks like a nice start after a quick read. Don't forget to put back tests. :p

Comment threadsrc/librustdoc/html/highlight.rs Outdated

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
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
Copy Markdown
ContributorAuthor

@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
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch 2 times, most recently from ab1987c to 1d083ceCompareAugust 21, 2020 17:27
@petrochenkovpetrochenkov self-assigned this Aug 21, 2020
@petrochenkovpetrochenkov added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 21, 2020
@ollie27ollie27 added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Aug 22, 2020
Comment threadsrc/librustdoc/html/highlight.rs Outdated
@petrochenkovpetrochenkov removed their assignment Aug 22, 2020
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch 3 times, most recently from 3788b43 to 63f6533CompareAugust 27, 2020 10:52
@matkladmatklad changed the title WIP: Use rustc_lexer for rustdoc syntax highlightingUse rustc_lexer for rustdoc syntax highlightingAug 27, 2020
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch from 63f6533 to 957511eCompareAugust 27, 2020 10:59
@matklad

Copy link
Copy Markdown
ContributorAuthor

This is ready for review @GuillaumeGomez !

Comment threadsrc/librustdoc/Cargo.toml Outdated
Comment threadsrc/librustdoc/html/highlight/sample.html Outdated
Comment threadsrc/librustdoc/html/highlight/sample.rs Outdated
Comment threadsrc/librustdoc/html/highlight/tests.rs Outdated
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch 2 times, most recently from 5f18bab to f54359dCompareAugust 27, 2020 11:34
@matklad

Copy link
Copy Markdown
ContributorAuthor

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

@GuillaumeGomez

Copy link
Copy Markdown
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. :)

@jyn514jyn514 left a comment

Copy link
Copy Markdown
Member

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?

Comment threadsrc/librustdoc/html/highlight.rs Outdated
Comment threadsrc/librustdoc/html/highlight.rs Outdated
Comment threadsrc/librustdoc/test/fixtures/sample.html Outdated
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch from f54359d to 22f709fCompareAugust 27, 2020 12:28
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch from 22f709f to fe9d84dCompareAugust 27, 2020 12:49
@matklad

Copy link
Copy Markdown
ContributorAuthor

@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
Copy Markdown
Member

Looks great, thanks!

Comment threadsrc/test/rustdoc/bad-codeblock-syntax.rs
Comment threadsrc/librustdoc/test/tests.rs Outdated
}

#[test]
fn test_html_highlighting() {

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch from fe9d84d to 21c4bd9CompareAugust 27, 2020 16:01
Comment threadsrc/librustdoc/html/highlight/fixtures/sample.rs Outdated
@matklad
matkladforce-pushed the rustc-lexer-rustdoc-highlight branch from 21c4bd9 to 0b16ff3CompareAugust 27, 2020 16:35
@matklad

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown
Member

Thanks!

@bors: r+

@bors

bors commented Aug 29, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit b4f4db9 has been approved by GuillaumeGomez

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

bors commented Aug 29, 2020

Copy link
Copy Markdown
Collaborator

⌛ Testing commit b4f4db9 with merge ced37a5...

@bors

bors commented Aug 30, 2020

Copy link
Copy Markdown
Collaborator

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

@borsbors added the merged-by-bors This PR was explicitly merged by bors. label Aug 30, 2020
@bors
bors merged commit ced37a5 into rust-lang:masterAug 30, 2020
@cuvipercuviper added this to the 1.48.0 milestone May 2, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-rustdocRelevant 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

@matklad@GuillaumeGomez@jyn514@bors@euclio@petrochenkov@ollie27@tesuji@cuviper