@@ -59,43 +59,22 @@ impl<'a> FmtVisitor<'a> {
59
59
let span = codemap:: mk_sp ( start, end) ;
60
60
let snippet = self . snippet ( span) ;
61
61
62
- self . write_snippet ( & snippet, true , & process_last_snippet) ;
62
+ self . write_snippet ( & snippet, & process_last_snippet) ;
63
63
}
64
64
65
65
fn write_snippet < F : Fn ( & mut FmtVisitor , & str , & str ) > ( & mut self ,
66
66
snippet : & str ,
67
- last_snippet : bool ,
68
67
process_last_snippet : F ) {
69
- // Trim whitespace from the right hand side of each line.
70
- // Annoyingly, the library functions for splitting by lines etc. are not
71
- // quite right, so we must do it ourselves.
72
- let mut line_start = 0 ;
73
- let mut last_wspace = None ;
74
- for ( i, c) in snippet. char_indices ( ) {
75
- if c == '\n' {
76
- if let Some ( lw) = last_wspace {
77
- self . buffer . push_str ( & snippet[ line_start..lw] ) ;
78
- self . buffer . push_str ( "\n " ) ;
79
- } else {
80
- self . buffer . push_str ( & snippet[ line_start..i + 1 ] ) ;
81
- }
82
-
83
- line_start = i + 1 ;
84
- last_wspace = None ;
85
- } else {
86
- if c. is_whitespace ( ) {
87
- if last_wspace. is_none ( ) {
88
- last_wspace = Some ( i) ;
89
- }
90
- } else {
91
- last_wspace = None ;
92
- }
93
- }
94
- }
95
- if last_snippet {
96
- process_last_snippet ( self , & snippet[ line_start..] , snippet) ;
68
+ let mut lines: Vec < & str > = snippet. lines ( ) . collect ( ) ;
69
+ let last_snippet = if snippet. ends_with ( "\n " ) {
70
+ ""
97
71
} else {
98
- self . buffer . push_str ( & snippet[ line_start..] ) ;
72
+ lines. pop ( ) . unwrap ( )
73
+ } ;
74
+ for line in lines. iter ( ) {
75
+ self . buffer . push_str ( line. trim_right ( ) ) ;
76
+ self . buffer . push_str ( "\n " ) ;
99
77
}
78
+ process_last_snippet ( self , & last_snippet, snippet) ;
100
79
}
101
80
}
0 commit comments