@@ -91,7 +91,7 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
9191 replacer. visit_expr_mut ( & mut modified_expr) ;
9292
9393 // now unparsed with the modified expression
94- let mut unparsed = unparse_inner ( & modified_expr) ;
94+ let mut unparsed = unparse_inner ( & modified_expr, cfg ) ;
9595
9696 // now we can replace the macros with the formatted blocks
9797 for fmted in replacer. formatted_stack . drain ( ..) {
@@ -105,11 +105,11 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
105105 out_fmt. push ( ' ' ) ;
106106 }
107107
108- let mut whitespace = 0 ;
108+ let mut indent_level = 0 ;
109109
110110 for line in unparsed. lines ( ) {
111111 if line. contains ( MARKER ) {
112- whitespace = line. matches ( cfg. indent_str ( ) ) . count ( ) ;
112+ indent_level = line. matches ( cfg. indent_str ( ) ) . count ( ) ;
113113 break ;
114114 }
115115 }
@@ -119,7 +119,7 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
119119 while let Some ( ( _idx, fmt_line) ) = lines. next ( ) {
120120 // Push the indentation (but not for blank lines - avoid trailing whitespace)
121121 if is_multiline && !fmt_line. is_empty ( ) {
122- out_fmt. push_str ( & cfg. indent_str ( ) . repeat ( whitespace ) ) ;
122+ out_fmt. push_str ( & cfg. indent_str ( ) . repeat ( indent_level ) ) ;
123123 }
124124
125125 // Calculate delta between indentations - the block indentation is too much
@@ -133,7 +133,7 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
133133
134134 if is_multiline {
135135 out_fmt. push ( '\n' ) ;
136- out_fmt. push_str ( & cfg. indent_str ( ) . repeat ( whitespace ) ) ;
136+ out_fmt. push_str ( & cfg. indent_str ( ) . repeat ( indent_level ) ) ;
137137 } else if !is_empty {
138138 out_fmt. push ( ' ' ) ;
139139 }
@@ -161,10 +161,10 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
161161///
162162/// This creates a new temporary file, parses the expression into it, and then formats the file.
163163/// This is a bit of a hack, but dtonlay doesn't want to support this very simple usecase, forcing us to clone the expr
164- pub fn unparse_inner ( expr : & Expr ) -> String {
164+ pub fn unparse_inner ( expr : & Expr , cfg : & IndentOptions ) -> String {
165165 let file = wrapped ( expr) ;
166166 let wrapped = prettyplease:: unparse ( & file) ;
167- unwrapped ( wrapped)
167+ unwrapped ( wrapped, cfg )
168168}
169169
170170/// Unparse a pattern into a properly formatted string using prettyplease.
@@ -194,14 +194,27 @@ pub fn unparse_pat(pat: &syn::Pat) -> String {
194194}
195195
196196// Split off the fn main and then cut the tabs off the front
197- fn unwrapped ( raw : String ) -> String {
197+ fn unwrapped ( raw : String , cfg : & IndentOptions ) -> String {
198+ const INDENT : & str = " " ;
199+
198200 let mut o = raw
199201 . strip_prefix ( "fn main() {\n " )
200202 . unwrap ( )
201203 . strip_suffix ( "}\n " )
202204 . unwrap ( )
203205 . lines ( )
204- . map ( |line| line. strip_prefix ( " " ) . unwrap_or_default ( ) ) // todo: set this to tab level
206+ . map ( |line| line. strip_prefix ( INDENT ) . unwrap_or ( line) )
207+ . map ( |mut line| {
208+ let indent_level = line. matches ( INDENT ) . count ( ) ;
209+
210+ for _ in 0 ..indent_level {
211+ if let Some ( remaining) = line. strip_prefix ( INDENT ) {
212+ line = remaining;
213+ }
214+ }
215+
216+ cfg. indent_str ( ) . repeat ( indent_level) + line
217+ } )
205218 . collect :: < Vec < _ > > ( )
206219 . join ( "\n " ) ;
207220
0 commit comments