@@ -109,9 +109,9 @@ instance (Pretty a) => Pretty (Item a) where
109109 pretty (Comments trivia) = pretty trivia
110110 pretty (Item x) = group x
111111
112- -- For lists, attribute sets and let bindings
113- prettyItems :: (Pretty a ) => Items a -> Doc
114- prettyItems (Items items) = go items
112+ -- Render items (for lists, attribute sets and let bindings) separated by the given separator
113+ renderItems :: (Pretty a ) => Doc - > Items a -> Doc
114+ renderItems separator (Items items) = go items
115115 where
116116 go [] = mempty
117117 go [item] = pretty item
@@ -120,9 +120,22 @@ prettyItems (Items items) = go items
120120 pretty (LanguageAnnotation lang)
121121 <> hardspace
122122 <> group stringItem
123- <> if null rest then mempty else hardline <> go rest
123+ <> if null rest then mempty else separator <> go rest
124124 go (item : rest) =
125- pretty item <> if null rest then mempty else hardline <> go rest
125+ pretty item <> if null rest then mempty else separator <> go rest
126+
127+ -- Render a list given the separator between items
128+ renderList :: Doc -> Ann Token -> Items Term -> Ann Token -> Doc
129+ renderList itemSep paropen@ Ann {trailComment = post} items parclose =
130+ pretty (paropen{trailComment = Nothing })
131+ <> surroundWith sur (nest $ pretty post <> renderItems itemSep items)
132+ <> pretty parclose
133+ where
134+ -- If the brackets are on different lines, keep them like that
135+ sur
136+ | sourceLine paropen /= sourceLine parclose = hardline
137+ | null $ unItems items = hardspace
138+ | otherwise = line
126139
127140instance Pretty Trivia where
128141 pretty [] = mempty
@@ -197,7 +210,7 @@ prettySet _ (krec, paropen@(LoneAnn _), Items [], parclose@Ann{preTrivia = []})
197210-- Singleton sets are allowed to fit onto one line,
198211-- but apart from that always expand.
199212prettySet wide (krec, paropen@ Ann {trailComment = post}, binders, parclose) =
200- let ! surrounded = surroundWith sep (nest $ pretty post <> prettyItems binders)
213+ let ! surrounded = surroundWith sep (nest $ pretty post <> renderItems hardline binders)
201214 in pretty (fmap (,hardspace) krec)
202215 <> pretty (paropen{trailComment = Nothing })
203216 <> surrounded
@@ -243,13 +256,8 @@ prettyTerm (List paropen@Ann{trailComment = Nothing} (Items []) parclose@Ann{pre
243256 sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace
244257-- General list
245258-- Always expand if len > 1
246- prettyTerm (List paropen@ Ann {trailComment = post} items parclose) =
247- pretty (paropen{trailComment = Nothing })
248- <> surroundWith sur (nest $ pretty post <> prettyItems items)
249- <> pretty parclose
250- where
251- -- If the brackets are on different lines, keep them like that
252- sur = if sourceLine paropen /= sourceLine parclose then hardline else line
259+ prettyTerm (List paropen items parclose) =
260+ renderList hardline paropen items parclose
253261prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose)
254262-- Parentheses
255263prettyTerm (Parenthesized paropen expr parclose@ Ann {preTrivia = closePre}) =
@@ -428,18 +436,10 @@ prettyApp indentFunction pre hasPost f a =
428436 -- Render the inner arguments of a function call
429437 absorbInner :: Expression -> Doc
430438 -- If lists have only simple items, try to render them single-line instead of expanding
431- -- This is just a copy of the list rendering code, but with `sepBy line` instead of `sepBy hardline`
432- absorbInner (Term (List paropen@ Ann {trailComment = post'} items parclose))
439+ -- Uses renderList with `line` separator instead of `hardline`
440+ absorbInner (Term (List paropen items parclose))
433441 | length (unItems items) <= 6 && all (isSimple . Term ) items =
434- pretty (paropen{trailComment = Nothing })
435- <> surroundWith sur (nest $ pretty post' <> sepBy line (unItems items))
436- <> pretty parclose
437- where
438- -- If the brackets are on different lines, keep them like that
439- sur
440- | sourceLine paropen /= sourceLine parclose = hardline
441- | null $ unItems items = hardspace
442- | otherwise = line
442+ renderList line paropen items parclose
443443 absorbInner expr = pretty expr
444444
445445 -- Render the last argument of a function call
@@ -696,7 +696,7 @@ instance Pretty Expression where
696696 convertTrailing (Just (TrailingComment t)) = [LineComment (" " <> t)]
697697
698698 letPart = group $ pretty let_ <> hardline <> letBody
699- letBody = nest $ prettyItems binders
699+ letBody = nest $ renderItems hardline binders
700700 inPart =
701701 group $
702702 pretty in_
0 commit comments