@@ -375,15 +375,34 @@ instance Pretty Parameter where
375375-- then start on a new line instead".
376376prettyApp :: Bool -> Doc -> Bool -> Expression -> Expression -> Doc
377377prettyApp indentFunction pre hasPost f a =
378- let -- This is very hacky, but selections shouldn't be in a priority group,
378+ let -- Walk the function call chain
379+ absorbApp :: Expression -> Doc
380+ -- This is very hacky, but selections shouldn't be in a priority group,
379381 -- because if they get expanded before anything else,
380382 -- only the `.`-and-after part gets to a new line, which looks very odd
381- absorbApp (Application f' a'@ (Term Selection {})) = group' Transparent (absorbApp f') <> line <> nest (group' RegularG a')
382- absorbApp (Application f' a') = group' Transparent (absorbApp f') <> line <> nest (group' Priority a')
383+ absorbApp (Application f' a'@ (Term Selection {})) = group' Transparent (absorbApp f') <> line <> nest (group' RegularG $ absorbInner a')
384+ absorbApp (Application f' a') = group' Transparent (absorbApp f') <> line <> nest (group' Priority $ absorbInner a')
385+ -- First argument
383386 absorbApp expr
384387 | indentFunction && null comment' = nest $ group' RegularG $ line' <> pretty expr
385388 | otherwise = pretty expr
386389
390+ -- Render the inner arguments of a function call
391+ absorbInner :: Expression -> Doc
392+ -- If lists have only simple items, try to render them single-line instead of expanding
393+ -- This is just a copy of the list rendering code, but with `sepBy line` instead of `sepBy hardline`
394+ absorbInner (Term (List paropen@ Ann {trailComment = post'} items parclose))
395+ | length (unItems items) <= 4 && all (isSimple . Term ) items =
396+ pretty (paropen{trailComment = Nothing })
397+ <> surroundWith sur (nest $ pretty post' <> sepBy line (unItems items))
398+ <> pretty parclose
399+ where
400+ -- If the brackets are on different lines, keep them like that
401+ sur = if sourceLine paropen /= sourceLine parclose then hardline else line
402+ absorbInner expr = pretty expr
403+
404+ -- Render the last argument of a function call
405+ absorbLast :: Expression -> Doc
387406 absorbLast (Term t)
388407 | isAbsorbable t =
389408 group' Priority $ nest $ prettyTerm t
@@ -522,7 +541,11 @@ absorbExpr _ expr = pretty expr
522541-- Render the RHS value of an assignment or function parameter default value
523542absorbRHS :: Expression -> Doc
524543absorbRHS expr = case expr of
525- -- Absorbable expression. Always start on the same line
544+ -- Exception to the case below: Don't force-expand attrsets if they only contain a single inherit statement
545+ (Term (Set _ _ binders _))
546+ | case unItems binders of [Item (Inherit {})] -> True ; _ -> False ->
547+ hardspace <> group (absorbExpr False expr)
548+ -- Absorbable expression. Always start on the same line, and force-expand attrsets
526549 _ | isAbsorbableExpr expr -> hardspace <> group (absorbExpr True expr)
527550 -- Parenthesized expression. Same thing as the special case for parenthesized last argument in function calls.
528551 (Term (Parenthesized open expr' close)) -> hardspace <> absorbParen open expr' close
0 commit comments