Skip to content

rustdoc: syntax highlight macro definitions, colour $... substitutions. #12647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use html::escape::Escape;
use t = syntax::parse::token;

/// Highlights some source code, returning the HTML output.
pub fn highlight(src: &str) -> ~str {
pub fn highlight(src: &str, class: Option<&str>) -> ~str {
let sess = parse::new_parse_sess();
let handler = diagnostic::default_handler();
let span_handler = diagnostic::mk_span_handler(handler, sess.cm);
Expand All @@ -35,6 +35,7 @@ pub fn highlight(src: &str) -> ~str {
let mut out = io::MemWriter::new();
doit(sess,
lexer::new_string_reader(span_handler, fm),
class,
&mut out).unwrap();
str::from_utf8_lossy(out.unwrap()).into_owned()
}
Expand All @@ -46,14 +47,15 @@ pub fn highlight(src: &str) -> ~str {
/// it's used. All source code emission is done as slices from the source map,
/// not from the tokens themselves, in order to stay true to the original
/// source.
fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, class: Option<&str>,
out: &mut Writer) -> io::IoResult<()> {
use syntax::parse::lexer::Reader;

try!(write!(out, "<pre class='rust'>\n"));
try!(write!(out, "<pre class='rust {}'>\n", class.unwrap_or("")));
let mut last = BytePos(0);
let mut is_attribute = false;
let mut is_macro = false;
let mut is_macro_nonterminal = false;
loop {
let next = lexer.next_token();
let test = if next.tok == t::EOF {lexer.pos.get()} else {next.sp.lo};
Expand Down Expand Up @@ -101,8 +103,15 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,
// miscellaneous, no highlighting
t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI |
t::COLON | t::MOD_SEP | t::LARROW | t::DARROW | t::LPAREN |
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE |
t::DOLLAR => "",
t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE => "",
t::DOLLAR => {
if t::is_ident(&lexer.peek().tok) {
is_macro_nonterminal = true;
"macro-nonterminal"
} else {
""
}
}

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

_ if t::is_any_keyword(&next.tok) => "kw",
_ => {
if lexer.peek().tok == t::NOT {
if is_macro_nonterminal {
is_macro_nonterminal = false;
"macro-nonterminal"
} else if lexer.peek().tok == t::NOT {
is_macro = true;
"macro"
} else {
Expand Down Expand Up @@ -171,4 +183,3 @@ fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader,

write!(out, "</pre>\n")
}

2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result {
};

if !rendered {
let output = highlight::highlight(text).to_c_str();
let output = highlight::highlight(text, None).to_c_str();
output.with_ref(|r| {
bufputs(ob, r)
})
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,13 +1630,13 @@ impl<'a> fmt::Show for Source<'a> {
try!(write!(fmt.buf, "<span id='{0:u}'>{0:1$u}</span>\n", i, cols));
}
try!(write!(fmt.buf, "</pre>"));
try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice())));
try!(write!(fmt.buf, "{}", highlight::highlight(s.as_slice(), None)));
Ok(())
}
}

fn item_macro(w: &mut Writer, it: &clean::Item,
t: &clean::Macro) -> fmt::Result {
try!(write!(w, "<pre class='macro'>{}</pre>", t.source));
try!(w.write_str(highlight::highlight(t.source, Some("macro"))));
document(w, it)
}
1 change: 1 addition & 0 deletions src/librustdoc/html/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pre.rust .op { color: #cc782f; }
pre.rust .comment { color: #533add; }
pre.rust .doccomment { color: #d343d0; }
pre.rust .macro { color: #d343d0; }
pre.rust .macro-nonterminal { color: #d343d0; }
pre.rust .string { color: #c13928; }
pre.rust .lifetime { color: #d343d0; }
pre.rust .attribute { color: #d343d0 !important; }