@@ -201,34 +201,31 @@ do_expr_to_algebra({macro_string, _Meta, Name}) ->
201201do_expr_to_algebra ({remote , _Meta , Left , Right }) ->
202202 concat (expr_to_algebra (Left ), <<" :" >>, expr_to_algebra (Right ));
203203do_expr_to_algebra ({block , Meta , Exprs }) ->
204- surround_block (<<" begin" >>, block_to_algebra (Meta , Exprs ) , <<" end" >>);
204+ surround_block (<<" begin" >>, Exprs , fun ( X ) -> block_to_algebra (Meta , X ) end , <<" end" >>);
205205do_expr_to_algebra ({'fun' , _Meta , Expr }) ->
206206 fun_to_algebra (Expr );
207207do_expr_to_algebra ({args , Meta , Values }) ->
208208 container (Meta , Values , <<" (" >>, <<" )" >>);
209209do_expr_to_algebra ({'case' , _Meta , Expr , Clauses }) ->
210210 Prefix = surround (<<" case" >>, <<" " >>, expr_to_algebra (Expr ), <<" " >>, <<" of" >>),
211- surround_block (Prefix , clauses_to_algebra ( Clauses ) , <<" end" >>);
211+ surround_block (Prefix , Clauses , fun clauses_to_algebra / 1 , <<" end" >>);
212212do_expr_to_algebra ({'maybe' , _Meta , MaybeExpressions }) ->
213- surround_block (<<" maybe" >>, maybe_expressions_to_algebra ( MaybeExpressions ) , <<" end" >>);
213+ surround_block (<<" maybe" >>, MaybeExpressions , fun maybe_expressions_to_algebra / 1 , <<" end" >>);
214214do_expr_to_algebra ({'maybe' , _Meta , MaybeExpressions , Else }) ->
215215 ElseD = expr_to_algebra (Else ),
216216 surround_block (
217- <<" maybe" >>, maybe_expressions_to_algebra ( MaybeExpressions ) , line (ElseD , <<" end" >>)
217+ <<" maybe" >>, MaybeExpressions , fun maybe_expressions_to_algebra / 1 , line (ElseD , <<" end" >>)
218218 );
219219do_expr_to_algebra ({else_clause , _Meta , Clauses }) ->
220220 concat (
221221 force_breaks (),
222222 group (concat (<<" else" >>, nest (concat (line (), clauses_to_algebra (Clauses )), ? INDENT )))
223223 );
224224do_expr_to_algebra ({'receive' , _Meta , Clauses }) ->
225- surround_block (<<" receive" >>, expr_to_algebra (Clauses ), <<" end" >>);
226- do_expr_to_algebra ({'receive' , _Meta , empty , After }) ->
227- AfterD = expr_to_algebra (After ),
228- concat (force_breaks (), (line (<<" receive" >>, line (AfterD , <<" end" >>))));
225+ surround_block (<<" receive" >>, Clauses , fun expr_to_algebra /1 , <<" end" >>);
229226do_expr_to_algebra ({'receive' , _Meta , Clauses , After }) ->
230227 AfterD = expr_to_algebra (After ),
231- surround_block (<<" receive" >>, expr_to_algebra ( Clauses ) , line (AfterD , <<" end" >>));
228+ surround_block (<<" receive" >>, Clauses , fun expr_to_algebra / 1 , line (AfterD , <<" end" >>));
232229do_expr_to_algebra ({after_clause , _Meta , Expr , Body }) ->
233230 receive_after_to_algebra (Expr , Body );
234231do_expr_to_algebra ({'try' , _Meta , Exprs , OfClauses , CatchClauses , After }) ->
@@ -237,7 +234,7 @@ do_expr_to_algebra({'catch', _Meta, Exprs}) ->
237234 ExprsD = lists :map (fun expr_to_algebra /1 , Exprs ),
238235 fold_doc (fun (Doc , Acc ) -> concat (Doc , <<" :" >>, Acc ) end , ExprsD );
239236do_expr_to_algebra ({'if' , _Meta , Clauses }) ->
240- surround_block (<<" if" >>, clauses_to_algebra ( Clauses ) , <<" end" >>);
237+ surround_block (<<" if" >>, Clauses , fun clauses_to_algebra / 1 , <<" end" >>);
241238do_expr_to_algebra ({spec , _Meta , Name , [SingleClause ]}) ->
242239 single_clause_spec_to_algebra (Name , SingleClause );
243240do_expr_to_algebra ({spec , _Meta , Name , Clauses }) ->
@@ -261,13 +258,8 @@ do_expr_to_algebra({clauses, _Meta, Clauses}) ->
261258do_expr_to_algebra ({body , _Meta , Exprs }) ->
262259 block_to_algebra (Exprs );
263260do_expr_to_algebra ({sigil , _Meta , Prefix , Content , Suffix }) ->
264- concat (
265- concat (
266- concat (<<" ~ " >>, do_expr_to_algebra (Prefix )),
267- do_expr_to_algebra (Content )
268- ),
269- do_expr_to_algebra (Suffix )
270- );
261+ PrefixDoc = concat (<<" ~ " >>, do_expr_to_algebra (Prefix )),
262+ concat (concat (PrefixDoc , do_expr_to_algebra (Content )), do_expr_to_algebra (Suffix ));
271263do_expr_to_algebra ({sigil_prefix , _Meta , '' }) ->
272264 <<" " >>;
273265do_expr_to_algebra ({sigil_prefix , _Meta , SigilName }) ->
@@ -288,7 +280,10 @@ surround(Left, LeftSpace, Doc, RightSpace, Right) ->
288280 )
289281 ).
290282
291- surround_block (Left , Doc , Right ) ->
283+ surround_block (Left , Empty , _Mapper , Right ) when Empty =:= []; Empty =:= empty ->
284+ line (Left , Right );
285+ surround_block (Left , Clauses , Mapper , Right ) ->
286+ Doc = Mapper (Clauses ),
292287 concat (
293288 force_breaks (),
294289 group (line (concat (Left , nest (concat (line (), Doc ), ? INDENT )), Right ))
@@ -752,8 +747,7 @@ fun_to_algebra({clauses, _Anno, [Clause]}) ->
752747 _ -> group (break (space (<<" fun" >>, ClauseD ), <<" end" >>))
753748 end ;
754749fun_to_algebra ({clauses , _Anno , Clauses }) ->
755- ClausesD = clauses_to_algebra (Clauses ),
756- surround_block (<<" fun" >>, ClausesD , <<" end" >>);
750+ surround_block (<<" fun" >>, Clauses , fun clauses_to_algebra /1 , <<" end" >>);
757751fun_to_algebra (type ) ->
758752 <<" fun()" >>;
759753fun_to_algebra ({type , Meta , Args , Result }) ->
0 commit comments