@@ -208,7 +208,7 @@ printToken (CloseDelim Bracket) = "]"
208
208
printToken (CloseDelim Brace ) = " }"
209
209
printToken (CloseDelim NoDelim ) = " "
210
210
-- Literals
211
- printToken (LiteralTok l s) = printLitTok l <> perhaps printName s
211
+ printToken (LiteralTok l s) = noIndent $ printLitTok l <> perhaps printName s
212
212
-- Name components
213
213
printToken (IdentTok i) = printIdent i
214
214
printToken Underscore = " _"
@@ -232,10 +232,11 @@ printLitTok (ByteTok n) = "b'" <> printName n <> "'"
232
232
printLitTok (CharTok n) = " '" <> printName n <> " '"
233
233
printLitTok (IntegerTok n) = printName n
234
234
printLitTok (FloatTok n) = printName n
235
- printLitTok (StrTok n) = " \" " <> printName n <> " \" "
236
- printLitTok (StrRawTok n m) = let pad = pretty (replicate m ' #' ) in " r" <> pad <> " \" " <> printName n <> " \" " <> pad
237
- printLitTok (ByteStrTok n) = " b\" " <> printName n <> " \" "
238
- printLitTok (ByteStrRawTok n m) = let pad = pretty (replicate m ' #' ) in " rb" <> pad <> " \" " <> printName n <> " \" " <> pad
235
+ printLitTok (StrTok n) = " \" " <> string hardline n <> " \" "
236
+ printLitTok (StrRawTok n m) = let pad = pretty (replicate m ' #' )
237
+ in " r" <> pad <> " \" " <> string hardline n <> " \" " <> pad
238
+ printLitTok (ByteStrTok n) = " b\" " <> string hardline n <> " \" "
239
+ printLitTok (ByteStrRawTok n m) = let pad = pretty (replicate m ' #' ) in " br" <> pad <> " \" " <> string hardline n <> " \" " <> pad
239
240
240
241
-- | Print a nonterminal
241
242
printNonterminal :: Nonterminal a -> Doc a
@@ -298,7 +299,7 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
298
299
Vec as exprs x -> annotate x (block Bracket True " ," (printInnerAttrs as) (printExpr <$> exprs))
299
300
Call _ func [arg] x -> annotate x (printExpr func <> parens (printExpr arg))
300
301
Call _ func args x -> annotate x (printExpr func <> block Paren True " ," mempty (printExpr <$> args))
301
- MethodCall {} -> chainedMethodCalls expr id
302
+ MethodCall {} -> chainedMethodCalls expr False id
302
303
TupExpr as [e] x -> annotate x (block Paren True " " (printInnerAttrs as) [ printExpr e <> " ," ])
303
304
TupExpr as es x -> annotate x (block Paren True " ," (printInnerAttrs as) (printExpr <$> es))
304
305
Binary _ op lhs rhs x -> annotate x (hsep [ printExpr lhs, printBinOp op, printExpr rhs ])
@@ -319,10 +320,10 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
319
320
Catch attrs blk x -> annotate x (" do catch" <+> printBlockWithAttrs True blk attrs)
320
321
Assign _ lhs rhs x -> annotate x (hsep [ printExpr lhs, " =" , printExpr rhs ])
321
322
AssignOp _ op lhs rhs x -> annotate x (hsep [ printExpr lhs, printBinOp op <> " =" , printExpr rhs ])
322
- FieldAccess {} -> chainedMethodCalls expr id
323
- TupField {} -> chainedMethodCalls expr id
324
- Index {} -> chainedMethodCalls expr id
325
- Range _ start end limits x -> annotate x (hcat [ perhaps printExpr start, printRangeLimits limits, perhaps printExpr end ] )
323
+ FieldAccess {} -> chainedMethodCalls expr False id
324
+ TupField {} -> chainedMethodCalls expr False id
325
+ Index {} -> chainedMethodCalls expr False id
326
+ Range _ start end limits x -> annotate x (perhaps printExpr start <+> printRangeLimits limits <+> perhaps printExpr end)
326
327
PathExpr _ Nothing path x -> annotate x (printPath path True )
327
328
PathExpr _ (Just qs) path x -> annotate x (printQPath path qs True )
328
329
AddrOf _ mut e x -> annotate x (" &" <> printMutability mut <+> printExpr e)
@@ -335,7 +336,7 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
335
336
in annotate x (printPath p True <+> block Brace True mempty (printInnerAttrs as) body)
336
337
Repeat attrs e cnt x -> annotate x (brackets (printInnerAttrs attrs <+> printExpr e <> " ;" <+> printExpr cnt))
337
338
ParenExpr attrs e x -> annotate x (parens (printInnerAttrs attrs <+> printExpr e))
338
- Try {} -> chainedMethodCalls expr id
339
+ Try {} -> chainedMethodCalls expr False id
339
340
where
340
341
printLbl = perhaps (\ i -> printLifetime i <> " :" )
341
342
glue = if isInline then (<+>) else (</>)
@@ -352,23 +353,26 @@ printExprOuterAttrStyle expr isInline = glue (printEitherAttrs (expressionAttrs
352
353
-- * try
353
354
--
354
355
chainedMethodCalls :: Expr a -- ^ expression
356
+ -> Bool -- ^ last expression was a 'TupField' (if we have two
357
+ -- successive 'TupField's, we need a space between them
358
+ -- to prevent them from looking like a float literal)
355
359
-> (Doc a -> Doc a ) -- ^ suffix to the expression
356
360
-> Doc a
357
- chainedMethodCalls (MethodCall _ s i ts' as x) fdoc
361
+ chainedMethodCalls (MethodCall _ s i ts' as x) _ fdoc
358
362
= let tys = perhaps (\ ts -> " ::<" <> commas ts printType <> " >" ) ts'
359
363
as' = case as of
360
364
[a] -> parens (printExpr a)
361
365
_ -> block Paren True " ," mempty (printExpr <$> as)
362
- in chainedMethodCalls s (annotate x . (<##> fdoc (indent n (hcat [ " ." , printIdent i, tys, as' ]))))
363
- chainedMethodCalls (FieldAccess _ s i x) fdoc
364
- = chainedMethodCalls s (annotate x . (<##> fdoc (indent n (hcat [ " ." , printIdent i ]))))
365
- chainedMethodCalls (Try _ s x) fdoc
366
- = chainedMethodCalls s (annotate x . (<> fdoc " ?" ))
367
- chainedMethodCalls (Index _ s i x) fdoc
368
- = chainedMethodCalls s (annotate x . (<> fdoc (" [" <> block NoDelim True mempty mempty [printExpr i] <> " ]" )))
369
- chainedMethodCalls (TupField _ s i x) fdoc
370
- = chainedMethodCalls s (annotate x . (<> fdoc (" ." <> pretty i)))
371
- chainedMethodCalls e fdoc = group (fdoc (printExpr e))
366
+ in chainedMethodCalls s False (annotate x . (<##> fdoc (indent n (hcat [ " ." , printIdent i, tys, as' ]))))
367
+ chainedMethodCalls (FieldAccess _ s i x) _ fdoc
368
+ = chainedMethodCalls s False (annotate x . (<##> fdoc (indent n (hcat [ " ." , printIdent i ]))))
369
+ chainedMethodCalls (Try _ s x) _ fdoc
370
+ = chainedMethodCalls s False (annotate x . (<> fdoc " ?" ))
371
+ chainedMethodCalls (Index _ s i x) _ fdoc
372
+ = chainedMethodCalls s False (annotate x . (<> fdoc (" [" <> block NoDelim True mempty mempty [printExpr i] <> " ]" )))
373
+ chainedMethodCalls (TupField _ s i x) t fdoc
374
+ = chainedMethodCalls s True (annotate x . (<> fdoc (" ." <> pretty i <> when t " " )))
375
+ chainedMethodCalls e _ fdoc = group (fdoc (printExpr e))
372
376
373
377
-- | Print a string literal
374
378
printStr :: StrStyle -> String -> Doc a
@@ -508,12 +512,12 @@ printEitherAttrs attrs kind inline = unless (null attrs') (glue attrs')
508
512
509
513
-- | Print an attribute (@print_attribute_inline@ or @print_attribute@)
510
514
printAttr :: Attribute a -> Bool -> Doc a
511
- printAttr (Attribute Inner p ts x) _ = annotate x (" #![" <> printPath p True <> printTokenStreamSp ts <> printTokenStream ts <> " ]" )
512
- printAttr (Attribute Outer p ts x) _ = annotate x (" #[" <> printPath p True <> printTokenStreamSp ts <> printTokenStream ts <> " ]" )
513
- printAttr (SugaredDoc Inner _ c x) True = annotate x (" /*!" <> pretty c <> " */" )
514
- printAttr (SugaredDoc Outer _ c x) True = annotate x (" /**" <> pretty c <> " */" )
515
- printAttr a @ (SugaredDoc Inner _ c x) False = annotate x (flatAlt (" //!" <+ > pretty c) (printAttr a True ))
516
- printAttr a @ (SugaredDoc Outer _ c x) False = annotate x (flatAlt (" ///" <+ > pretty c) (printAttr a True ))
515
+ printAttr (Attribute Inner p ts x) _ = annotate x (" #![" <> printPath p True <> printTokenStreamSp ts <> printTokenStream ts <> " ]" )
516
+ printAttr (Attribute Outer p ts x) _ = annotate x (" #[" <> printPath p True <> printTokenStreamSp ts <> printTokenStream ts <> " ]" )
517
+ printAttr (SugaredDoc Inner True c x) _ = annotate x (noIndent ( " /*!" <> string hardline c <> " */" ) )
518
+ printAttr (SugaredDoc Outer True c x) _ = annotate x (noIndent ( " /**" <> string hardline c <> " */" ) )
519
+ printAttr (SugaredDoc Inner False c x) _ = annotate x (flatAlt (" //!" <> pretty c) (" /*! " <> pretty c <> " */ " ))
520
+ printAttr (SugaredDoc Outer False c x) _ = annotate x (flatAlt (" ///" <> pretty c) (" /** " <> pretty c <> " */ " ))
517
521
518
522
-- | Print an identifier as is, or as cooked string if containing a hyphen
519
523
printCookedIdent :: Ident -> Doc a
@@ -667,9 +671,9 @@ printVis InheritedV = mempty
667
671
-- | Print a foreign item (@print_foreign_item@)
668
672
printForeignItem :: ForeignItem a -> Doc a
669
673
printForeignItem (ForeignFn attrs vis ident decl generics x) = annotate x $
670
- printOuterAttrs attrs <+ > printFn decl Normal NotConst Rust (Just ident) generics vis Nothing
674
+ printOuterAttrs attrs <# > printFn decl Normal NotConst Rust (Just ident) generics vis Nothing
671
675
printForeignItem (ForeignStatic attrs vis ident ty mut x) = annotate x $
672
- printOuterAttrs attrs <+ > printVis vis <+> " static" <+> printMutability mut <+> printIdent ident <> " :" <+> printType ty <> " ;"
676
+ printOuterAttrs attrs <# > printVis vis <+> " static" <+> printMutability mut <+> printIdent ident <> " :" <+> printType ty <> " ;"
673
677
674
678
-- | Print a struct definition (@print_struct@)
675
679
printStruct :: VariantData a -> Generics a -> Ident -> Bool -> Bool -> Doc a
@@ -776,8 +780,8 @@ printFullMutability Immutable = "const"
776
780
printPat :: Pat a -> Doc a
777
781
printPat (WildP x) = annotate x " _"
778
782
printPat (IdentP bm p s x) = annotate x (printBindingMode bm <+> printIdent p <+> perhaps (\ p' -> " @" <+> printPat p') s)
779
- printPat (StructP p fs b x) = annotate x (printPath p True <+> block Brace True " ," mempty body )
780
- where body = ( printFieldPat `map` fs) ++ [ " .." | b ]
783
+ printPat (StructP p fs False x) = annotate x (printPath p True <+> block Brace True " ," mempty (printFieldPat `map` fs) )
784
+ printPat ( StructP p fs True x) = annotate x (printPath p True <+> block Brace True mempty mempty ([ printFieldPat f <> " , " | f <- fs ] ++ [ " .." ]))
781
785
printPat (TupleStructP p es Nothing x) = annotate x (printPath p True <> " (" <> commas es printPat <> " )" )
782
786
printPat (TupleStructP p es (Just d) x) = let (before,after) = splitAt d es
783
787
in annotate x (printPath p True <> " (" <> commas before printPat <> when (d /= 0 ) " ,"
0 commit comments