@@ -14,17 +14,21 @@ module Ormolu.Printer.Combinators
18 , locatedVia
19 , locatedVia
19 , located'
20 , located'
24 , spaceSep
27 , spaceSep
25 , newlineSep
28 , newlineSep
26 -- ** Wrapping
29 -- ** Wrapping
29 , brackets
33 , brackets
30 , bracketsPar
34 , bracketsPar
@@ -32,14 +36,11 @@ module Ormolu.Printer.Combinators
32 , parensHash
36 , parensHash
33 -- ** Literals
37 -- ** Literals
43+import Data.Bool (bool)
43import Data.Data (Data)
44import Data.Data (Data)
44import Data.List (intersperse)
45import Data.List (intersperse)
45import Data.Text (Text)
46import Data.Text (Text)
@@ -86,47 +87,41 @@ locatedVia
87locatedVia ml loc@(L l a) f = do
88locatedVia ml loc@(L l a) f = do
88 mann <- lookupAnn loc
89 mann <- lookupAnn loc
89− layout <- currentLayout
90+ relaxed <- relaxedComments
91− (case ml of
91+ let m = case ml of
92− Nothing -> layout
92+ Nothing -> f a
93− Just l' ->
93+ Just l' -> switchLayout l' (f a)
98 case mann of
94 case mann of
99 Nothing -> m
95 Nothing -> m
100− Just Ann {..} ->
96+ Just Ann {..} -> bool sitcc id relaxed $ do
102− -- There are three things in 'Ann' which contain comments:
97+ -- There are three things in 'Ann' which contain comments:
104− let cmode =
99+ let cmode =
105− if annGetConstr a == CN "HsModule"
100+ if annGetConstr a == CN "HsModule"
106− then Module
101+ then Module
107− else Other
102+ else Other
108− (before, after) = partitionDPs cmode l annsDP
103+ (before, after) = partitionDPs cmode l annsDP
110− -- 'annPriorComments' contains comments that were directly placed
105+ -- 'annPriorComments' contains comments that were directly placed
111− -- before entities such as comments (in both styles) before function
106+ -- before entities such as comments (in both styles) before function
112− -- definitions and inline comments before smaller things like types
107+ -- definitions and inline comments before smaller things like types
113− -- and literals.
108+ -- and literals.
115− spitComments (addDecoration cmode Before l <$> annPriorComments)
110+ spitComments (addDecoration cmode Before l <$> annPriorComments)
117− -- Comments inside 'annsDP' marked with 'AnnComment' are trickier,
112+ -- Comments inside 'annsDP' marked with 'AnnComment' are trickier,
118− -- they seem to contain everything that goes after the thing they
113+ -- 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
114+ -- are attached to and in some cases (e.g. for modules) they contain
120− -- comments that go before things. Exact location can only be
115+ -- comments that go before things. Exact location can only be
121− -- deduced by analyzing the associated span.
116+ -- deduced by analyzing the associated span.
123− spitComments before
118+ spitComments before
125− spitComments after
120+ spitComments after
127− -- I wasn't able to find any case when 'annFollowingComments' is
122+ -- 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
123+ -- populated, so we'll ignore that one for now and fix it when we
129− -- have an example of source code where it matters.
124+ -- have an example of source code where it matters.
131-- | A version of 'located' with arguments flipped.
126-- | A version of 'located' with arguments flipped.
@@ -137,6 +132,20 @@ located'
138located' = flip located
133located' = flip located
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.
141+ :: SrcSpan -- ^ Span that controls layout
142+ -> R () -- ^ Computation to run with changed layout
144+switchLayout spn = enterLayout
145+ (if isOneLineSpan spn
140-- | Element of variable layout. This means that the sub-components may be
149-- | Element of variable layout. This means that the sub-components may be
141-- rendered either on single line or each on its own line depending on
150-- rendered either on single line or each on its own line depending on
142-- current layout.
151-- current layout.
@@ -159,6 +168,12 @@ velt' xs = sitcc $ sequence_ (intersperse sep (sitcc <$> xs))
160 sep = vlayout (spit " ") newline
169 sep = vlayout (spit " ") newline
171+-- | Insert a space if enclosing layout is single-line, or newline if it's
175+breakpoint = vlayout space newline
162-- | Put separator between renderings of items of a list.
177-- | Put separator between renderings of items of a list.
@@ -198,6 +213,11 @@ line m = do
216+-- | Surround given entity by backticks.
218+backticks :: R () -> R ()
219+backticks m = txt "`" >> m >> txt "`"
201-- | Surround given entity by curly braces.
221-- | Surround given entity by curly braces.
203braces :: R () -> R ()
223braces :: R () -> R ()
@@ -252,26 +272,6 @@ ospaces m = vlayout m (txt " " >> m >> newline)
252comma :: R ()
272comma :: R ()
253comma = txt ", "
273comma = txt ", "
255−-- | Print @::@ followed by a space.
260−-- | Print @<-@ followed by a space.
265−-- | Print @->@ followed by a space.
270−-- | Print @=>@ followed by a space.
275-- | Print single space.
275-- | Print single space.
277space :: R ()
277space :: R ()