@@ -47,6 +47,7 @@ impl<'a> Generator<'a> {
47
47
contexts : & ' n HashMap < & ' n Rc < Path > , Context < ' n > > ,
48
48
heritage : Option < & ' n Heritage < ' _ > > ,
49
49
locals : MapChain < ' n , Cow < ' n , str > , LocalMeta > ,
50
+ buf_writable_discard : bool ,
50
51
) -> Generator < ' n > {
51
52
Generator {
52
53
input,
@@ -57,7 +58,7 @@ impl<'a> Generator<'a> {
57
58
skip_ws : WhitespaceHandling :: Preserve ,
58
59
super_block : None ,
59
60
buf_writable : WritableBuffer {
60
- discard : input . block . is_some ( ) ,
61
+ discard : buf_writable_discard ,
61
62
..Default :: default ( )
62
63
} ,
63
64
named : 0 ,
@@ -90,6 +91,7 @@ impl<'a> Generator<'a> {
90
91
buf. write ( CRATE ) ;
91
92
buf. writeln ( "::Result<()> {" ) ?;
92
93
94
+ buf. discard = self . buf_writable . discard ;
93
95
// Make sure the compiler understands that the generated code depends on the template files.
94
96
for path in self . contexts . keys ( ) {
95
97
// Skip the fake path of templates defined in rust source.
@@ -113,6 +115,7 @@ impl<'a> Generator<'a> {
113
115
} else {
114
116
self . handle ( ctx, ctx. nodes , buf, AstLevel :: Top )
115
117
} ?;
118
+ buf. discard = false ;
116
119
117
120
self . flush_ws ( Ws ( None , None ) ) ;
118
121
buf. write ( CRATE ) ;
@@ -829,7 +832,13 @@ impl<'a> Generator<'a> {
829
832
None => child_ctx,
830
833
} ;
831
834
let locals = MapChain :: with_parent ( & self . locals ) ;
832
- let mut child = Self :: new ( self . input , self . contexts , heritage. as_ref ( ) , locals) ;
835
+ let mut child = Self :: new (
836
+ self . input ,
837
+ self . contexts ,
838
+ heritage. as_ref ( ) ,
839
+ locals,
840
+ self . buf_writable . discard ,
841
+ ) ;
833
842
let mut size_hint = child. handle ( handle_ctx, handle_ctx. nodes , buf, AstLevel :: Top ) ?;
834
843
size_hint += child. write_buf_writable ( handle_ctx, buf) ?;
835
844
self . prepare_ws ( i. ws ) ;
@@ -992,6 +1001,7 @@ impl<'a> Generator<'a> {
992
1001
Some ( heritage) ,
993
1002
// Variables are NOT inherited from the parent scope.
994
1003
MapChain :: default ( ) ,
1004
+ self . buf_writable . discard ,
995
1005
) ;
996
1006
child. buf_writable = mem:: take ( & mut self . buf_writable ) ;
997
1007
@@ -1013,6 +1023,13 @@ impl<'a> Generator<'a> {
1013
1023
// succeeding whitespace according to the outer WS spec
1014
1024
self . prepare_ws ( outer) ;
1015
1025
1026
+ // If we are rendering a specific block and the discard changed, it means that we're done
1027
+ // with the block we want to render and that from this point, everything will be discarded.
1028
+ //
1029
+ // To get this block content rendered as well, we need to write to the buffer before then.
1030
+ if buf. discard != prev_buf_discard {
1031
+ self . write_buf_writable ( ctx, buf) ?;
1032
+ }
1016
1033
// Restore the original buffer discarding state
1017
1034
if block_fragment_write {
1018
1035
self . buf_writable . discard = true ;
@@ -1910,6 +1927,7 @@ impl<'a> Generator<'a> {
1910
1927
}
1911
1928
}
1912
1929
1930
+ #[ derive( Debug ) ]
1913
1931
struct Buffer {
1914
1932
// The buffer to generate the code into
1915
1933
buf : String ,
@@ -2269,6 +2287,7 @@ struct WriteParts {
2269
2287
/// ```ignore
2270
2288
/// let var = format!(format, expr);
2271
2289
/// ```
2290
+ #[ derive( Debug ) ]
2272
2291
struct WritePartsBuffers {
2273
2292
format : Buffer ,
2274
2293
expr : Option < Buffer > ,
0 commit comments