@@ -14,17 +14,21 @@ module Ormolu.Printer.Combinators
1414 , atom
1515 , newline
1616 , inci
17+ , relaxComments
1718 , located
1819 , locatedVia
1920 , located'
21+ , switchLayout
2022 , velt
2123 , velt'
2224 , vlayout
25+ , breakpoint
2326 , withSep
2427 , spaceSep
2528 , newlineSep
2629 -- ** Wrapping
2730 , line
31+ , backticks
2832 , braces
2933 , brackets
3034 , bracketsPar
@@ -32,14 +36,11 @@ module Ormolu.Printer.Combinators
3236 , parensHash
3337 -- ** Literals
3438 , comma
35− , ofType
36− , larrow
37− , rarrow
38− , darrow
3939 , space
4040 )
4141where
4242
43+import Data.Bool (bool)
4344import Data.Data (Data)
4445import Data.List (intersperse)
4546import Data.Text (Text)
@@ -86,47 +87,41 @@ locatedVia
8687 -> R ()
8788locatedVia ml loc@(L l a) f = do
8889 mann <- lookupAnn loc
89− layout <- currentLayout
90− let m = enterLayout
91− (case ml of
92− Nothing -> layout
93− Just l' ->
94− if isOneLineSpan l'
95− then SingleLine
96− else MultiLine)
97− (f a)
90+ relaxed <- relaxedComments
91+ let m = case ml of
92+ Nothing -> f a
93+ Just l' -> switchLayout l' (f a)
9894 case mann of
9995 Nothing -> m
100− Just Ann {..} ->
101− sitcc $ do
102− -- There are three things in 'Ann' which contain comments:
96+ Just Ann {..} -> bool sitcc id relaxed $ do
97+ -- There are three things in 'Ann' which contain comments:
10398
104− let cmode =
105− if annGetConstr a == CN "HsModule"
106− then Module
107− else Other
108− (before, after) = partitionDPs cmode l annsDP
99+ let cmode =
100+ if annGetConstr a == CN "HsModule"
101+ then Module
102+ else Other
103+ (before, after) = partitionDPs cmode l annsDP
109104
110− -- 'annPriorComments' contains comments that were directly placed
111− -- before entities such as comments (in both styles) before function
112− -- definitions and inline comments before smaller things like types
113− -- and literals.
105+ -- 'annPriorComments' contains comments that were directly placed
106+ -- before entities such as comments (in both styles) before function
107+ -- definitions and inline comments before smaller things like types
108+ -- and literals.
114109
115− spitComments (addDecoration cmode Before l <$> annPriorComments)
110+ spitComments (addDecoration cmode Before l <$> annPriorComments)
116111
117− -- Comments inside 'annsDP' marked with 'AnnComment' are trickier,
118− -- they seem to contain everything that goes after the thing they
119− -- are attached to and in some cases (e.g. for modules) they contain
120− -- comments that go before things. Exact location can only be
121− -- deduced by analyzing the associated span.
112+ -- Comments inside 'annsDP' marked with 'AnnComment' are trickier,
113+ -- they seem to contain everything that goes after the thing they
114+ -- are attached to and in some cases (e.g. for modules) they contain
115+ -- comments that go before things. Exact location can only be
116+ -- deduced by analyzing the associated span.
122117
123− spitComments before
124− m
125− spitComments after
118+ spitComments before
119+ m
120+ spitComments after
126121
127− -- I wasn't able to find any case when 'annFollowingComments' is
128− -- populated, so we'll ignore that one for now and fix it when we
129− -- have an example of source code where it matters.
122+ -- I wasn't able to find any case when 'annFollowingComments' is
123+ -- populated, so we'll ignore that one for now and fix it when we
124+ -- have an example of source code where it matters.
130125
131126-- | A version of 'located' with arguments flipped.
132127
@@ -137,6 +132,20 @@ located'
137132 -> R ()
138133located' = flip located
139134
135+-- | Set layout according to given 'SrcSpan' for a given computation. Use
136+-- this only when you need to set layout based on e.g. combined span of
137+-- several elements when there is no corresponding 'Located' wrapper
138+-- provided by GHC AST.
139+
140+switchLayout
141+ :: SrcSpan -- ^ Span that controls layout
142+ -> R () -- ^ Computation to run with changed layout
143+ -> R ()
144+switchLayout spn = enterLayout
145+ (if isOneLineSpan spn
146+ then SingleLine
147+ else MultiLine)
148+
140149-- | Element of variable layout. This means that the sub-components may be
141150-- rendered either on single line or each on its own line depending on
142151-- current layout.
@@ -159,6 +168,12 @@ velt' xs = sitcc $ sequence_ (intersperse sep (sitcc <$> xs))
159168 where
160169 sep = vlayout (spit " ") newline
161170
171+-- | Insert a space if enclosing layout is single-line, or newline if it's
172+-- multiline.
173+
174+breakpoint :: R ()
175+breakpoint = vlayout space newline
176+
162177-- | Put separator between renderings of items of a list.
163178
164179withSep
@@ -198,6 +213,11 @@ line m = do
198213 m
199214 newline
200215
216+-- | Surround given entity by backticks.
217+
218+backticks :: R () -> R ()
219+backticks m = txt "`" >> m >> txt "`"
220+
201221-- | Surround given entity by curly braces.
202222
203223braces :: R () -> R ()
@@ -252,26 +272,6 @@ ospaces m = vlayout m (txt " " >> m >> newline)
252272comma :: R ()
253273comma = txt ", "
254274
255−-- | Print @::@ followed by a space.
256−
257−ofType :: R ()
258−ofType = txt ":: "
259−
260−-- | Print @<-@ followed by a space.
261−
262−larrow :: R ()
263−larrow = txt "<- "
264−
265−-- | Print @->@ followed by a space.
266−
267−rarrow :: R ()
268−rarrow = txt "-> "
269−
270−-- | Print @=>@ followed by a space.
271−
272−darrow :: R ()
273−darrow = txt "=> "
274−
275275-- | Print single space.
276276
277277space :: R ()