7
7
8
8
use crate :: html:: escape:: Escape ;
9
9
10
- use std:: fmt:: { Display , Write } ;
10
+ use std:: fmt:: Display ;
11
11
use std:: iter:: Peekable ;
12
12
13
13
use rustc_lexer:: { LiteralKind , TokenKind } ;
14
14
use rustc_span:: edition:: Edition ;
15
15
use rustc_span:: symbol:: Symbol ;
16
16
use rustc_span:: with_default_session_globals;
17
17
18
+ use super :: format:: Buffer ;
19
+
18
20
/// Highlights `src`, returning the HTML output.
19
21
crate fn render_with_highlighting (
20
22
src : & str ,
23
+ out : & mut Buffer ,
21
24
class : Option < & str > ,
22
25
playground_button : Option < & str > ,
23
26
tooltip : Option < ( Option < Edition > , & str ) > ,
24
27
edition : Edition ,
25
- ) -> String {
28
+ ) {
26
29
debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
27
- let mut out = String :: with_capacity ( src. len ( ) ) ;
28
30
if let Some ( ( edition_info, class) ) = tooltip {
29
31
write ! (
30
32
out,
@@ -35,23 +37,19 @@ crate fn render_with_highlighting(
35
37
} else {
36
38
String :: new( )
37
39
} ,
38
- )
39
- . unwrap ( ) ;
40
+ ) ;
40
41
}
41
42
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) ;
47
46
}
48
47
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( ) ) ;
52
50
}
53
51
54
- fn write_code ( out : & mut String , src : & str , edition : Edition ) {
52
+ fn write_code ( out : & mut Buffer , src : & str , edition : Edition ) {
55
53
// This replace allows to fix how the code source with DOS backline characters is displayed.
56
54
let src = src. replace ( "\r \n " , "\n " ) ;
57
55
Classifier :: new ( & src, edition) . highlight ( & mut |highlight| {
@@ -63,8 +61,8 @@ fn write_code(out: &mut String, src: &str, edition: Edition) {
63
61
} ) ;
64
62
}
65
63
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( ) ) ;
68
66
}
69
67
70
68
/// How a span of text is classified. Mostly corresponds to token kinds.
@@ -331,13 +329,13 @@ impl<'a> Classifier<'a> {
331
329
332
330
/// Called when we start processing a span of text that should be highlighted.
333
331
/// 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( ) ) ;
336
334
}
337
335
338
336
/// 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>" ) ;
341
339
}
342
340
343
341
/// Called for a span of text. If the text should be highlighted differently
@@ -351,10 +349,10 @@ fn exit_span(out: &mut String) {
351
349
/// ```
352
350
/// The latter can be thought of as a shorthand for the former, which is more
353
351
/// 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 > ) {
355
353
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) ,
358
356
}
359
357
}
360
358
0 commit comments