77
88use crate :: html:: escape:: Escape ;
99
10- use std:: fmt:: { Display , Write } ;
10+ use std:: fmt:: Display ;
1111use std:: iter:: Peekable ;
1212
1313use rustc_lexer:: { LiteralKind , TokenKind } ;
1414use rustc_span:: edition:: Edition ;
1515use rustc_span:: symbol:: Symbol ;
1616use rustc_span:: with_default_session_globals;
1717
18+ use super :: format:: Buffer ;
19+
1820/// Highlights `src`, returning the HTML output.
1921crate fn render_with_highlighting (
20- src : String ,
22+ src : & str ,
23+ out : & mut Buffer ,
2124 class : Option < & str > ,
2225 playground_button : Option < & str > ,
2326 tooltip : Option < ( Option < Edition > , & str ) > ,
2427 edition : Edition ,
25- ) -> String {
28+ ) {
2629 debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
27- let mut out = String :: with_capacity ( src. len ( ) ) ;
2830 if let Some ( ( edition_info, class) ) = tooltip {
2931 write ! (
3032 out,
@@ -35,23 +37,19 @@ crate fn render_with_highlighting(
3537 } else {
3638 String :: new( )
3739 } ,
38- )
39- . unwrap ( ) ;
40+ ) ;
4041 }
4142
42- write_header ( & mut out, class) ;
43- write_code ( & mut out, & src, edition) ;
44- write_footer ( & mut out, playground_button) ;
45-
46- out
43+ write_header ( out, class) ;
44+ write_code ( out, & src, edition) ;
45+ write_footer ( out, playground_button) ;
4746}
4847
49- fn write_header ( out : & mut String , class : Option < & str > ) {
50- write ! ( out, "<div class=\" example-wrap\" ><pre class=\" rust {}\" >\n " , class. unwrap_or_default( ) )
51- . unwrap ( )
48+ fn write_header ( out : & mut Buffer , class : Option < & str > ) {
49+ write ! ( out, "<div class=\" example-wrap\" ><pre class=\" rust {}\" >\n " , class. unwrap_or_default( ) ) ;
5250}
5351
54- fn write_code ( out : & mut String , src : & str , edition : Edition ) {
52+ fn write_code ( out : & mut Buffer , src : & str , edition : Edition ) {
5553 // This replace allows to fix how the code source with DOS backline characters is displayed.
5654 let src = src. replace ( "\r \n " , "\n " ) ;
5755 Classifier :: new ( & src, edition) . highlight ( & mut |highlight| {
@@ -63,8 +61,8 @@ fn write_code(out: &mut String, src: &str, edition: Edition) {
6361 } ) ;
6462}
6563
66- fn write_footer ( out : & mut String , playground_button : Option < & str > ) {
67- write ! ( out, "</pre>{}</div>\n " , playground_button. unwrap_or_default( ) ) . unwrap ( )
64+ fn write_footer ( out : & mut Buffer , playground_button : Option < & str > ) {
65+ write ! ( out, "</pre>{}</div>\n " , playground_button. unwrap_or_default( ) ) ;
6866}
6967
7068/// How a span of text is classified. Mostly corresponds to token kinds.
@@ -331,13 +329,13 @@ impl<'a> Classifier<'a> {
331329
332330/// Called when we start processing a span of text that should be highlighted.
333331/// The `Class` argument specifies how it should be highlighted.
334- fn enter_span ( out : & mut String , klass : Class ) {
335- write ! ( out, "<span class=\" {}\" >" , klass. as_html( ) ) . unwrap ( )
332+ fn enter_span ( out : & mut Buffer , klass : Class ) {
333+ write ! ( out, "<span class=\" {}\" >" , klass. as_html( ) ) ;
336334}
337335
338336/// Called at the end of a span of highlighted text.
339- fn exit_span ( out : & mut String ) {
340- write ! ( out, "</span>" ) . unwrap ( )
337+ fn exit_span ( out : & mut Buffer ) {
338+ out. write_str ( "</span>" ) ;
341339}
342340
343341/// Called for a span of text. If the text should be highlighted differently
@@ -351,10 +349,10 @@ fn exit_span(out: &mut String) {
351349/// ```
352350/// The latter can be thought of as a shorthand for the former, which is more
353351/// flexible.
354- fn string < T : Display > ( out : & mut String , text : T , klass : Option < Class > ) {
352+ fn string < T : Display > ( out : & mut Buffer , text : T , klass : Option < Class > ) {
355353 match klass {
356- None => write ! ( out, "{}" , text) . unwrap ( ) ,
357- Some ( klass) => write ! ( out, "<span class=\" {}\" >{}</span>" , klass. as_html( ) , text) . unwrap ( ) ,
354+ None => write ! ( out, "{}" , text) ,
355+ Some ( klass) => write ! ( out, "<span class=\" {}\" >{}</span>" , klass. as_html( ) , text) ,
358356 }
359357}
360358
0 commit comments