Skip to content

Commit 279bf29

Browse files
committed
Get rid of Class::None
This is mostly me learning the codebase, 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.
1 parent 98d6634 commit 279bf29

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

Diff for: src/librustdoc/html/highlight.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ fn write_footer(out: &mut String, playground_button: Option<&str>) {
6262
/// How a span of text is classified. Mostly corresponds to token kinds.
6363
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6464
enum Class {
65-
None,
6665
Comment,
6766
DocComment,
6867
Attribute,
@@ -87,7 +86,6 @@ impl Class {
8786
/// Returns the css class expected by rustdoc for each `Class`.
8887
fn as_html(self) -> &'static str {
8988
match self {
90-
Class::None => "",
9189
Class::Comment => "comment",
9290
Class::DocComment => "doccomment",
9391
Class::Attribute => "attribute",
@@ -110,7 +108,7 @@ impl Class {
110108
}
111109

112110
enum Highlight<'a> {
113-
Token { text: &'a str, class: Class },
111+
Token { text: &'a str, class: Option<Class> },
114112
EnterSpan { class: Class },
115113
ExitSpan,
116114
}
@@ -164,8 +162,9 @@ impl<'a> Classifier<'a> {
164162
/// a couple of following ones as well.
165163
fn advance(&mut self, token: TokenKind, text: &'a str, sink: &mut dyn FnMut(Highlight<'a>)) {
166164
let lookahead = self.peek();
165+
let no_highlight = |sink: &mut dyn FnMut(_)| sink(Highlight::Token { text, class: None });
167166
let class = match token {
168-
TokenKind::Whitespace => Class::None,
167+
TokenKind::Whitespace => return no_highlight(sink),
169168
TokenKind::LineComment { doc_style } | TokenKind::BlockComment { doc_style, .. } => {
170169
if doc_style.is_some() {
171170
Class::DocComment
@@ -190,12 +189,12 @@ impl<'a> Classifier<'a> {
190189
TokenKind::And => match lookahead {
191190
Some(TokenKind::And) => {
192191
let _and = self.tokens.next();
193-
sink(Highlight::Token { text: "&&", class: Class::Op });
192+
sink(Highlight::Token { text: "&&", class: Some(Class::Op) });
194193
return;
195194
}
196195
Some(TokenKind::Eq) => {
197196
let _eq = self.tokens.next();
198-
sink(Highlight::Token { text: "&=", class: Class::Op });
197+
sink(Highlight::Token { text: "&=", class: Some(Class::Op) });
199198
return;
200199
}
201200
Some(TokenKind::Whitespace) => Class::Op,
@@ -226,7 +225,7 @@ impl<'a> Classifier<'a> {
226225
| TokenKind::At
227226
| TokenKind::Tilde
228227
| TokenKind::Colon
229-
| TokenKind::Unknown => Class::None,
228+
| TokenKind::Unknown => return no_highlight(sink),
230229

231230
TokenKind::Question => Class::QuestionMark,
232231

@@ -235,7 +234,7 @@ impl<'a> Classifier<'a> {
235234
self.in_macro_nonterminal = true;
236235
Class::MacroNonTerminal
237236
}
238-
_ => Class::None,
237+
_ => return no_highlight(sink),
239238
},
240239

241240
// This might be the start of an attribute. We're going to want to
@@ -251,8 +250,8 @@ impl<'a> Classifier<'a> {
251250
self.in_attribute = true;
252251
sink(Highlight::EnterSpan { class: Class::Attribute });
253252
}
254-
sink(Highlight::Token { text: "#", class: Class::None });
255-
sink(Highlight::Token { text: "!", class: Class::None });
253+
sink(Highlight::Token { text: "#", class: None });
254+
sink(Highlight::Token { text: "!", class: None });
256255
return;
257256
}
258257
// Case 2: #[outer_attribute]
@@ -262,16 +261,16 @@ impl<'a> Classifier<'a> {
262261
}
263262
_ => (),
264263
}
265-
Class::None
264+
return no_highlight(sink);
266265
}
267266
TokenKind::CloseBracket => {
268267
if self.in_attribute {
269268
self.in_attribute = false;
270-
sink(Highlight::Token { text: "]", class: Class::None });
269+
sink(Highlight::Token { text: "]", class: None });
271270
sink(Highlight::ExitSpan);
272271
return;
273272
}
274-
Class::None
273+
return no_highlight(sink);
275274
}
276275
TokenKind::Literal { kind, .. } => match kind {
277276
// Text literals.
@@ -307,7 +306,7 @@ impl<'a> Classifier<'a> {
307306
};
308307
// Anything that didn't return above is the simple case where we the
309308
// class just spans a single token, so we can use the `string` method.
310-
sink(Highlight::Token { text, class });
309+
sink(Highlight::Token { text, class: Some(class) });
311310
}
312311

313312
fn peek(&mut self) -> Option<TokenKind> {
@@ -337,10 +336,10 @@ fn exit_span(out: &mut String) {
337336
/// ```
338337
/// The latter can be thought of as a shorthand for the former, which is more
339338
/// flexible.
340-
fn string<T: Display>(out: &mut String, text: T, klass: Class) {
339+
fn string<T: Display>(out: &mut String, text: T, klass: Option<Class>) {
341340
match klass {
342-
Class::None => write!(out, "{}", text).unwrap(),
343-
klass => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
341+
None => write!(out, "{}", text).unwrap(),
342+
Some(klass) => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
344343
}
345344
}
346345

0 commit comments

Comments
 (0)