on master help sign in
Improve descriptions of rendering combinators
5dcee95mrkkrp
src/Ormolu/Printer/Combinators.hs+23−6
@@ -2,7 +2,9 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
4
4
5−-- | Printing combinators.
5+-- | Printing combinators. The definitions here are presented in such an
6+-- order so you can just go through the Haddocks and by the end of the file
7+-- you should have a pretty good idea how to program rendering logic.
6
8
7module Ormolu.Printer.Combinators
9module Ormolu.Printer.Combinators
8 ( -- * The 'R' monad
10 ( -- * The 'R' monad
@@ -56,19 +58,24 @@ import qualified Data.Text as T
56-- Basic
58-- Basic
57
59
58-- | Output a fixed 'Text' fragment. The argument may not contain any line
60-- | Output a fixed 'Text' fragment. The argument may not contain any line
59−-- breaks or tab characters.
61+-- breaks or tab characters. 'txt' is used to output all sorts of “fixed”
62+-- bits of syntax like keywords and pipes @|@ in functional dependencies.
60
63
61txt :: Text -> R ()
64txt :: Text -> R ()
62txt t = ensureIndent >> spit t
65txt t = ensureIndent >> spit t
63
66
64−-- | Output 'Outputable' fragment of AST.
67+-- | Output 'Outputable' fragment of AST. This can be used to output numeric
68+-- literals and similar. Everything that doesn't have inner structure but
69+-- does have an 'Outputable' instance.
65
70
66atom :: Outputable a => a -> R ()
71atom :: Outputable a => a -> R ()
67atom = txt . T.pack . showSDocUnsafe . ppr
72atom = txt . T.pack . showSDocUnsafe . ppr
68
73
69-- | Enter a 'Located' entity. This combinator handles outputting comments
74-- | Enter a 'Located' entity. This combinator handles outputting comments
70−-- that may be associated with the primitive and sets corresponding layout
71−-- for the inner computation.
75+-- and sets layout (single-line vs multi-line) for the inner computation.
76+-- Roughly, the rule for using 'located' is that every time there is a
77+-- 'Located' wrapper, it should be “discharged” with a corresponding
78+-- 'located' invocation.
72
79
73located
80located
74 :: Data a
81 :: Data a
@@ -118,7 +125,7 @@ located' = flip located
118-- | Set layout according to given 'SrcSpan' for a given computation. Use
125-- | Set layout according to given 'SrcSpan' for a given computation. Use
119-- this only when you need to set layout based on e.g. combined span of
126-- this only when you need to set layout based on e.g. combined span of
120-- several elements when there is no corresponding 'Located' wrapper
127-- several elements when there is no corresponding 'Located' wrapper
121−-- provided by GHC AST.
128+-- provided by GHC AST. It is relatively rare that this one is needed.
122
129
123switchLayout
130switchLayout
124 :: SrcSpan -- ^ Span that controls layout
131 :: SrcSpan -- ^ Span that controls layout
@@ -131,6 +138,8 @@ switchLayout spn = enterLayout
131
138
132-- | Insert a space if enclosing layout is single-line, or newline if it's
139-- | Insert a space if enclosing layout is single-line, or newline if it's
133-- multiline.
140-- multiline.
141+--
142+-- > breakpoint = vlayout space newline
134
143
135breakpoint :: R ()
144breakpoint :: R ()
136breakpoint = vlayout space newline
145breakpoint = vlayout space newline
@@ -174,6 +183,8 @@ withSep sep f = \case
174 in f x : fmap g xs
183 in f x : fmap g xs
175
184
176-- | Render space-separated elements.
185-- | Render space-separated elements.
186+--
187+-- > spaceSep f = sequence_ . withSep space f
177
188
178spaceSep
189spaceSep
179 :: (a -> R ()) -- ^ How to render list items
190 :: (a -> R ()) -- ^ How to render list items
@@ -182,6 +193,8 @@ spaceSep
182spaceSep f = sequence_ . withSep space f
193spaceSep f = sequence_ . withSep space f
183
194
184-- | Render newline-separated elements.
195-- | Render newline-separated elements.
196+--
197+-- > newlineSep f = sequence_ . withSep newline f
185
198
186newlineSep
199newlineSep
187 :: (a -> R ()) -- ^ How to render list items
200 :: (a -> R ()) -- ^ How to render list items
@@ -193,6 +206,10 @@ newlineSep f = sequence_ . withSep newline f
193-- Wrapping
206-- Wrapping
194
207
195-- | Finish given entity by a 'newline'.
208-- | Finish given entity by a 'newline'.
209+--
210+-- > line m = do
211+-- > m
212+-- > newline
196
213
197line :: R () -> R ()
214line :: R () -> R ()
198line m = do
215line m = do
src/Ormolu/Printer/Internal.hs+4−1
@@ -188,7 +188,10 @@ ensureIndent = do
188 spit (T.replicate (i - c) " ")
188 spit (T.replicate (i - c) " ")
189
189
190-- | Increase indentation level by one indentation step for the inner
190-- | Increase indentation level by one indentation step for the inner
191−-- computation.
191+-- computation. 'inci' should be used when a part of code must be more
192+-- indented relative to the parts outside of 'inci' in order for the output
193+-- to be valid Haskell. When layout is single-line there is no obvious
194+-- effect, but with multi-line layout correct indentation levels matter.
192
195
193inci :: R () -> R ()
196inci :: R () -> R ()
194inci m' = do
197inci m' = do