From 3a79370367b6b606c5682b17cf601f34df9690fd Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 25 Oct 2021 10:43:55 -0700 Subject: [PATCH] rustdoc: use better highlighting for *const, *mut, and &mut This generates more consistent HTML for these RefKeyWord combinations. Before: ![image](https://user-images.githubusercontent.com/1593513/138742752-7e00a3f7-4621-4c62-82d1-3e4c2ef503d1.png) After: ![image](https://user-images.githubusercontent.com/1593513/138743955-90abcdcd-fc88-4e2f-95bb-c1b1635c0001.png) --- src/librustdoc/html/highlight.rs | 27 ++++++++++++++----- .../html/highlight/fixtures/sample.html | 4 +-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index fa8ad2a37e7..de194d5e5d2 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -469,22 +469,37 @@ impl<'a> Classifier<'a> { // Assume that '&' or '*' is the reference or dereference operator // or a reference or pointer type. Unless, of course, it looks like // a logical and or a multiplication operator: `&&` or `* `. - TokenKind::Star => match self.peek() { - Some(TokenKind::Whitespace) => Class::Op, + TokenKind::Star => match self.tokens.peek() { + Some((TokenKind::Whitespace, _)) => Class::Op, + Some((TokenKind::Ident, "mut")) => { + self.next(); + sink(Highlight::Token { text: "*mut", class: Some(Class::RefKeyWord) }); + return; + } + Some((TokenKind::Ident, "const")) => { + self.next(); + sink(Highlight::Token { text: "*const", class: Some(Class::RefKeyWord) }); + return; + } _ => Class::RefKeyWord, }, - TokenKind::And => match lookahead { - Some(TokenKind::And) => { + TokenKind::And => match self.tokens.peek() { + Some((TokenKind::And, _)) => { self.next(); sink(Highlight::Token { text: "&&", class: Some(Class::Op) }); return; } - Some(TokenKind::Eq) => { + Some((TokenKind::Eq, _)) => { self.next(); sink(Highlight::Token { text: "&=", class: Some(Class::Op) }); return; } - Some(TokenKind::Whitespace) => Class::Op, + Some((TokenKind::Whitespace, _)) => Class::Op, + Some((TokenKind::Ident, "mut")) => { + self.next(); + sink(Highlight::Token { text: "&mut", class: Some(Class::RefKeyWord) }); + return; + } _ => Class::RefKeyWord, }, diff --git a/src/librustdoc/html/highlight/fixtures/sample.html b/src/librustdoc/html/highlight/fixtures/sample.html index 22e650af7e2..b117a12e39f 100644 --- a/src/librustdoc/html/highlight/fixtures/sample.html +++ b/src/librustdoc/html/highlight/fixtures/sample.html @@ -15,11 +15,11 @@ #[cfg(target_os = "linux")] fn main() -> () { let foo = true && false || true; - let _: *const () = 0; + let _: *const () = 0; let _ = &foo; let _ = &&foo; let _ = *foo; - mac!(foo, &mut bar); + mac!(foo, &mut bar); assert!(self.length < N && index <= self.length); ::std::env::var("gateau").is_ok(); #[rustfmt::skip]