]> git.proxmox.com Git - rustc.git/blobdiff - src/librustdoc/html/highlight.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustdoc / html / highlight.rs
index 5bea1b56141591d90ee7e386d5f579ea0a40249e..02f1947c99e5fd1e469075d9ef5cc85f93a822ba 100644 (file)
@@ -1,6 +1,6 @@
 //! Basic syntax highlighting functionality.
 //!
-//! This module uses libsyntax's lexer to provide token-based highlighting for
+//! This module uses librustc_ast's lexer to provide token-based highlighting for
 //! the HTML documentation generated by rustdoc.
 //!
 //! Use the `render_with_highlighting` to highlight some rust code.
@@ -11,18 +11,18 @@ use std::fmt::Display;
 use std::io;
 use std::io::prelude::*;
 
+use rustc_ast::token::{self, Token};
 use rustc_parse::lexer;
+use rustc_session::parse::ParseSess;
 use rustc_span::source_map::SourceMap;
 use rustc_span::symbol::{kw, sym};
 use rustc_span::{FileName, Span};
-use syntax::sess::ParseSess;
-use syntax::token::{self, Token};
 
 /// Highlights `src`, returning the HTML output.
 pub fn render_with_highlighting(
     src: &str,
     class: Option<&str>,
-    extension: Option<&str>,
+    playground_button: Option<&str>,
     tooltip: Option<(&str, &str)>,
 ) -> String {
     debug!("highlighting: ================\n{}\n==============", src);
@@ -38,11 +38,11 @@ pub fn render_with_highlighting(
     }
 
     let sess = ParseSess::with_silent_emitter();
-    let fm = sess
+    let sf = sess
         .source_map()
         .new_source_file(FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned());
     let highlight_result = rustc_driver::catch_fatal_errors(|| {
-        let lexer = lexer::StringReader::new(&sess, fm, None);
+        let lexer = lexer::StringReader::new(&sess, sf, None);
         let mut classifier = Classifier::new(lexer, sess.source_map());
 
         let mut highlighted_source = vec![];
@@ -58,10 +58,7 @@ pub fn render_with_highlighting(
         Ok(highlighted_source) => {
             write_header(class, &mut out).unwrap();
             write!(out, "{}", highlighted_source).unwrap();
-            if let Some(extension) = extension {
-                write!(out, "{}", extension).unwrap();
-            }
-            write_footer(&mut out).unwrap();
+            write_footer(&mut out, playground_button).unwrap();
         }
         Err(()) => {
             // If errors are encountered while trying to highlight, just emit
@@ -138,7 +135,7 @@ trait Writer {
     fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()>;
 }
 
-// Implement `Writer` for anthing that can be written to, this just implements
+// Implement `Writer` for anything that can be written to, this just implements
 // the default rustdoc behaviour.
 impl<U: Write> Writer for U {
     fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()> {
@@ -433,6 +430,6 @@ fn write_header(class: Option<&str>, out: &mut dyn Write) -> io::Result<()> {
     write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or(""))
 }
 
-fn write_footer(out: &mut dyn Write) -> io::Result<()> {
-    write!(out, "</pre></div>\n")
+fn write_footer(out: &mut dyn Write, playground_button: Option<&str>) -> io::Result<()> {
+    write!(out, "</pre>{}</div>\n", if let Some(button) = playground_button { button } else { "" })
 }