Skip to content

Commit c602c81

Browse files
committed
rustdoc: syntax highlight macro definitions, colour $... substitutions.
Macro definitions are just their raw source code, and so should be highlighted where possible. Also, $ident non-terminal substitutions are special, and so are worth of a little special treatment.
1 parent 999d55d commit c602c81

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/librustdoc/html/highlight.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use html::escape::Escape;
2626
use t = syntax::parse::token;
2727

2828
/// Highlights some source code, returning the HTML output.
29-
pub fn highlight(src: &str) -> ~str {
29+
pub fn highlight(src: &str, class: Option<&str>) -> ~str {
3030
let sess = parse::new_parse_sess();
3131
let handler = diagnostic::default_handler();
3232
let span_handler = diagnostic::mk_span_handler(handler, sess.cm);
@@ -35,6 +35,7 @@ pub fn highlight(src: &str) -> ~str {
3535
let mut out = io::MemWriter::new();
3636
doit(sess,
3737
lexer::new_string_reader(span_handler, fm),
38+
class,
3839
&mut out).unwrap();
3940
str::from_utf8_lossy(out.unwrap()).into_owned()
4041
}
@@ -46,14 +47,15 @@ pub fn highlight(src: &str) -> ~str {
4647
/// it's used. All source code emission is done as slices from the source map,
4748
/// not from the tokens themselves, in order to stay true to the original
4849
/// source.
49-
fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
50+
fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, class: Option<&str>,
5051
out: &mut Writer) -> io::IoResult<()> {
5152
use syntax::parse::lexer::Reader;
5253
53-
try!(write!(out, "<pre class='rust'>\n"));
54+
try!(write!(out, "<pre class='rust {}'>\n", class.unwrap_or("")));
5455
let mut last = BytePos(0);
5556
let mut is_attribute = false;
5657
let mut is_macro = false;
58+
let mut is_macro_nonterminal = false;
5759
loop {
5860
let next = lexer.next_token();
5961
let test = if next.tok == t::EOF {lexer.pos.get()} else {next.sp.lo};
@@ -101,8 +103,15 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
101103
// miscellaneous, no highlighting
102104
t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI |
103105
t::COLON | t::MOD_SEP | t::LARROW | t::DARROW | t::LPAREN |
104-
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE |
105-
t::DOLLAR => "",
106+
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE => "",
107+
t::DOLLAR => {
108+
if t::is_ident(&lexer.peek().tok) {
109+
is_macro_nonterminal = true;
110+
"macro-nonterminal"
111+
} else {
112+
""
113+
}
114+
}
106115

107116
// This is the start of an attribute. We're going to want to
108117
// continue highlighting it as an attribute until the ending ']' is
@@ -143,7 +152,10 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
143152

144153
_ if t::is_any_keyword(&next.tok) => "kw",
145154
_ => {
146-
if lexer.peek().tok == t::NOT {
155+
if is_macro_nonterminal {
156+
is_macro_nonterminal = false;
157+
"macro-nonterminal"
158+
} else if lexer.peek().tok == t::NOT {
147159
is_macro = true;
148160
"macro"
149161
} else {
@@ -171,4 +183,3 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
171183

172184
write!(out, "</pre>\n")
173185
}
174-

src/librustdoc/html/markdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result {
146146
};
147147

148148
if !rendered {
149-
let output = highlight::highlight(text).to_c_str();
149+
let output = highlight::highlight(text, None).to_c_str();
150150
output.with_ref(|r| {
151151
bufputs(ob, r)
152152
})

src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1630,13 +1630,13 @@ impl<'a> fmt::Show for Source<'a> {
16301630
try!(write!(fmt.buf, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols));
16311631
}
16321632
try!(write!(fmt.buf, "</pre>"));
1633-
try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice())));
1633+
try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice(), None)));
16341634
Ok(())
16351635
}
16361636
}
16371637

16381638
fn item_macro(w: &mut Writer, it: &clean::Item,
16391639
t: &clean::Macro) -> fmt::Result {
1640-
try!(write!(w, "<pre class='macro'>{}</pre>", t.source));
1640+
try!(w.write_str(highlight::highlight(t.source, Some("macro"))));
16411641
document(w, it)
16421642
}

src/librustdoc/html/static/main.css

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pre.rust .op { color: #cc782f; }
315315
pre.rust .comment { color: #533add; }
316316
pre.rust .doccomment { color: #d343d0; }
317317
pre.rust .macro { color: #d343d0; }
318+
pre.rust .macro-nonterminal { color: #d343d0; }
318319
pre.rust .string { color: #c13928; }
319320
pre.rust .lifetime { color: #d343d0; }
320321
pre.rust .attribute { color: #d343d0 !important; }

0 commit comments

Comments
 (0)