@@ -15,7 +15,7 @@ pub enum Breaks {
15
15
#[ derive( Clone , Copy ) ]
16
16
pub struct BreakToken {
17
17
pub offset : isize ,
18
- pub blank_space : isize ,
18
+ pub blank_space : usize ,
19
19
}
20
20
21
21
#[ derive( Clone , Copy ) ]
@@ -38,7 +38,7 @@ pub enum Token {
38
38
#[ derive( Copy , Clone ) ]
39
39
enum PrintFrame {
40
40
Fits ,
41
- Broken ( isize , Breaks ) ,
41
+ Broken ( usize , Breaks ) ,
42
42
}
43
43
44
44
pub const SIZE_INFINITY : isize = 0xffff ;
@@ -63,8 +63,10 @@ pub struct Printer {
63
63
scan_stack : VecDeque < usize > ,
64
64
// Stack of blocks-in-progress being flushed by print
65
65
print_stack : Vec < PrintFrame > ,
66
+ // Level of indentation of current line
67
+ indent : usize ,
66
68
// Buffered indentation to avoid writing trailing whitespace
67
- pending_indentation : isize ,
69
+ pending_indentation : usize ,
68
70
}
69
71
70
72
#[ derive( Clone ) ]
@@ -85,6 +87,7 @@ impl Printer {
85
87
right_total : 0 ,
86
88
scan_stack : VecDeque :: new ( ) ,
87
89
print_stack : Vec :: new ( ) ,
90
+ indent : 0 ,
88
91
pending_indentation : 0 ,
89
92
}
90
93
}
@@ -135,7 +138,7 @@ impl Printer {
135
138
size : -self . right_total ,
136
139
} ) ;
137
140
self . scan_stack . push_back ( right) ;
138
- self . right_total += token. blank_space ;
141
+ self . right_total += token. blank_space as isize ;
139
142
}
140
143
141
144
pub fn scan_string ( & mut self , string : Cow < ' static , str > ) {
@@ -172,7 +175,7 @@ impl Printer {
172
175
self . print_string ( string) ;
173
176
}
174
177
Token :: Break ( token) => {
175
- self . left_total += token. blank_space ;
178
+ self . left_total += token. blank_space as isize ;
176
179
self . print_break ( token, left. size ) ;
177
180
}
178
181
Token :: Begin ( token) => self . print_begin ( token, left. size ) ,
@@ -220,42 +223,41 @@ impl Printer {
220
223
221
224
fn print_begin ( & mut self , token : BeginToken , size : isize ) {
222
225
if size > self . space {
223
- let col = self . margin - self . space + token. offset as isize ;
224
- self . print_stack . push ( PrintFrame :: Broken ( col, token. breaks ) ) ;
226
+ self . print_stack
227
+ . push ( PrintFrame :: Broken ( self . indent , token. breaks ) ) ;
228
+ self . indent += token. offset ;
225
229
} else {
226
230
self . print_stack . push ( PrintFrame :: Fits ) ;
227
231
}
228
232
}
229
233
230
234
fn print_end ( & mut self ) {
231
- self . print_stack . pop ( ) . unwrap ( ) ;
235
+ if let PrintFrame :: Broken ( indent, ..) = self . print_stack . pop ( ) . unwrap ( ) {
236
+ self . indent = indent;
237
+ }
232
238
}
233
239
234
240
fn print_break ( & mut self , token : BreakToken , size : isize ) {
235
- if let Some ( offset) = match self . get_top ( ) {
236
- PrintFrame :: Fits => None ,
237
- PrintFrame :: Broken ( offset, Breaks :: Consistent ) => Some ( offset) ,
238
- PrintFrame :: Broken ( offset, Breaks :: Inconsistent ) => {
239
- if size > self . space {
240
- Some ( offset)
241
- } else {
242
- None
243
- }
244
- }
245
- } {
246
- self . out . push ( '\n' ) ;
247
- self . pending_indentation = offset + token. offset ;
248
- self . space = self . margin - self . pending_indentation ;
249
- } else {
241
+ let fits = match self . get_top ( ) {
242
+ PrintFrame :: Fits => true ,
243
+ PrintFrame :: Broken ( .., Breaks :: Consistent ) => false ,
244
+ PrintFrame :: Broken ( .., Breaks :: Inconsistent ) => size <= self . space ,
245
+ } ;
246
+ if fits {
250
247
self . pending_indentation += token. blank_space ;
251
- self . space -= token. blank_space ;
248
+ self . space -= token. blank_space as isize ;
249
+ } else {
250
+ self . out . push ( '\n' ) ;
251
+ let indent = self . indent as isize + token. offset ;
252
+ self . pending_indentation = indent as usize ;
253
+ self . space = self . margin - indent;
252
254
}
253
255
}
254
256
255
257
fn print_string ( & mut self , string : Cow < ' static , str > ) {
256
- self . out . reserve ( self . pending_indentation as usize ) ;
258
+ self . out . reserve ( self . pending_indentation ) ;
257
259
self . out
258
- . extend ( iter:: repeat ( ' ' ) . take ( self . pending_indentation as usize ) ) ;
260
+ . extend ( iter:: repeat ( ' ' ) . take ( self . pending_indentation ) ) ;
259
261
self . pending_indentation = 0 ;
260
262
261
263
self . out . push_str ( & string) ;
0 commit comments