1
Fork 0

Rollup merge of #79069 - jyn514:class-none, r=GuillaumeGomez

Get rid of `highlight::Class::None`

This is mostly me learning the codebase for https://github.com/rust-lang/rust/pull/77939, so feel free to close the PR.
It does have the small benefit that we statically know rustdoc isn't generating useless `span`s, though.

r? `@GuillaumeGomez`
cc `@matklad`
This commit is contained in:
Mara Bos 2020-11-17 16:13:51 +01:00 committed by GitHub
commit dda479815a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -64,7 +64,6 @@ fn write_footer(out: &mut String, playground_button: Option<&str>) {
/// How a span of text is classified. Mostly corresponds to token kinds. /// How a span of text is classified. Mostly corresponds to token kinds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Class { enum Class {
None,
Comment, Comment,
DocComment, DocComment,
Attribute, Attribute,
@ -89,7 +88,6 @@ impl Class {
/// Returns the css class expected by rustdoc for each `Class`. /// Returns the css class expected by rustdoc for each `Class`.
fn as_html(self) -> &'static str { fn as_html(self) -> &'static str {
match self { match self {
Class::None => "",
Class::Comment => "comment", Class::Comment => "comment",
Class::DocComment => "doccomment", Class::DocComment => "doccomment",
Class::Attribute => "attribute", Class::Attribute => "attribute",
@ -112,7 +110,7 @@ impl Class {
} }
enum Highlight<'a> { enum Highlight<'a> {
Token { text: &'a str, class: Class }, Token { text: &'a str, class: Option<Class> },
EnterSpan { class: Class }, EnterSpan { class: Class },
ExitSpan, ExitSpan,
} }
@ -166,8 +164,9 @@ impl<'a> Classifier<'a> {
/// a couple of following ones as well. /// a couple of following ones as well.
fn advance(&mut self, token: TokenKind, text: &'a str, sink: &mut dyn FnMut(Highlight<'a>)) { fn advance(&mut self, token: TokenKind, text: &'a str, sink: &mut dyn FnMut(Highlight<'a>)) {
let lookahead = self.peek(); let lookahead = self.peek();
let no_highlight = |sink: &mut dyn FnMut(_)| sink(Highlight::Token { text, class: None });
let class = match token { let class = match token {
TokenKind::Whitespace => Class::None, TokenKind::Whitespace => return no_highlight(sink),
TokenKind::LineComment { doc_style } | TokenKind::BlockComment { doc_style, .. } => { TokenKind::LineComment { doc_style } | TokenKind::BlockComment { doc_style, .. } => {
if doc_style.is_some() { if doc_style.is_some() {
Class::DocComment Class::DocComment
@ -192,12 +191,12 @@ impl<'a> Classifier<'a> {
TokenKind::And => match lookahead { TokenKind::And => match lookahead {
Some(TokenKind::And) => { Some(TokenKind::And) => {
let _and = self.tokens.next(); let _and = self.tokens.next();
sink(Highlight::Token { text: "&&", class: Class::Op }); sink(Highlight::Token { text: "&&", class: Some(Class::Op) });
return; return;
} }
Some(TokenKind::Eq) => { Some(TokenKind::Eq) => {
let _eq = self.tokens.next(); let _eq = self.tokens.next();
sink(Highlight::Token { text: "&=", class: Class::Op }); sink(Highlight::Token { text: "&=", class: Some(Class::Op) });
return; return;
} }
Some(TokenKind::Whitespace) => Class::Op, Some(TokenKind::Whitespace) => Class::Op,
@ -228,7 +227,7 @@ impl<'a> Classifier<'a> {
| TokenKind::At | TokenKind::At
| TokenKind::Tilde | TokenKind::Tilde
| TokenKind::Colon | TokenKind::Colon
| TokenKind::Unknown => Class::None, | TokenKind::Unknown => return no_highlight(sink),
TokenKind::Question => Class::QuestionMark, TokenKind::Question => Class::QuestionMark,
@ -237,7 +236,7 @@ impl<'a> Classifier<'a> {
self.in_macro_nonterminal = true; self.in_macro_nonterminal = true;
Class::MacroNonTerminal Class::MacroNonTerminal
} }
_ => Class::None, _ => return no_highlight(sink),
}, },
// This might be the start of an attribute. We're going to want to // This might be the start of an attribute. We're going to want to
@ -253,8 +252,8 @@ impl<'a> Classifier<'a> {
self.in_attribute = true; self.in_attribute = true;
sink(Highlight::EnterSpan { class: Class::Attribute }); sink(Highlight::EnterSpan { class: Class::Attribute });
} }
sink(Highlight::Token { text: "#", class: Class::None }); sink(Highlight::Token { text: "#", class: None });
sink(Highlight::Token { text: "!", class: Class::None }); sink(Highlight::Token { text: "!", class: None });
return; return;
} }
// Case 2: #[outer_attribute] // Case 2: #[outer_attribute]
@ -264,16 +263,16 @@ impl<'a> Classifier<'a> {
} }
_ => (), _ => (),
} }
Class::None return no_highlight(sink);
} }
TokenKind::CloseBracket => { TokenKind::CloseBracket => {
if self.in_attribute { if self.in_attribute {
self.in_attribute = false; self.in_attribute = false;
sink(Highlight::Token { text: "]", class: Class::None }); sink(Highlight::Token { text: "]", class: None });
sink(Highlight::ExitSpan); sink(Highlight::ExitSpan);
return; return;
} }
Class::None return no_highlight(sink);
} }
TokenKind::Literal { kind, .. } => match kind { TokenKind::Literal { kind, .. } => match kind {
// Text literals. // Text literals.
@ -309,7 +308,7 @@ impl<'a> Classifier<'a> {
}; };
// Anything that didn't return above is the simple case where we the // Anything that didn't return above is the simple case where we the
// class just spans a single token, so we can use the `string` method. // class just spans a single token, so we can use the `string` method.
sink(Highlight::Token { text, class }); sink(Highlight::Token { text, class: Some(class) });
} }
fn peek(&mut self) -> Option<TokenKind> { fn peek(&mut self) -> Option<TokenKind> {
@ -339,10 +338,10 @@ fn exit_span(out: &mut String) {
/// ``` /// ```
/// The latter can be thought of as a shorthand for the former, which is more /// The latter can be thought of as a shorthand for the former, which is more
/// flexible. /// flexible.
fn string<T: Display>(out: &mut String, text: T, klass: Class) { fn string<T: Display>(out: &mut String, text: T, klass: Option<Class>) {
match klass { match klass {
Class::None => write!(out, "{}", text).unwrap(), None => write!(out, "{}", text).unwrap(),
klass => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(), Some(klass) => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
} }
} }