@@ -16,6 +16,7 @@ module Ormolu.Printer.Internal
1616 , spit
1717 , newline
1818 , modNewline
19+ , isNewlineModified
1920 , ensureIndent
2021 , inci
2122 , sitcc
@@ -39,7 +40,7 @@ where
3940import Control.Monad.Reader
4041import Control.Monad.State.Strict
4142import Data.Coerce
42−import Data.Maybe (listToMaybe)
43+import Data.Maybe (fromMaybe, isJust, listToMaybe)
4344import Data.Text (Text)
4445import Data.Text.Lazy.Builder
4546import Debug.Trace
@@ -89,8 +90,8 @@ data SC = SC
8990 -- ^ Span stream
9091 , scCommentStream :: CommentStream
9192 -- ^ Comment stream
92− , scNewline :: R ()
93− -- ^ What to render as newline
93+ , scNewline :: Maybe (R ())
94+ -- ^ What to render as newline, or 'Nothing' ('newlineRaw' will be used)
9495 }
9596
9697-- | 'Layout' options.
@@ -125,7 +126,7 @@ runR debug (R m) sstream cstream anns =
125126 , scBuilder = mempty
126127 , scSpanStream = sstream
127128 , scCommentStream = cstream
128− , scNewline = newlineRaw
129+ , scNewline = Nothing
129130 }
130131
131132----------------------------------------------------------------------------
@@ -149,9 +150,9 @@ newline :: R ()
149150newline = do
150151 n <- R (gets scNewline)
151152 R . modify $ \sc -> sc
152− { scNewline = newlineRaw
153+ { scNewline = Nothing
153154 }
154− n
155+ fromMaybe newlineRaw n
155156
156157-- | Low-level newline primitive. This one always just inserts a newline, no
157158-- hooks can be attached.
@@ -179,9 +180,14 @@ modNewline :: (R () -> R ()) -> R ()
179180modNewline f = R $ do
180181 old <- gets scNewline
181182 modify $ \sc -> sc
182− { scNewline = f old
183+ { scNewline = Just $ f (fromMaybe newlineRaw old)
183184 }
184185
186+-- | Check if newline is in modified state.
187+
188+isNewlineModified :: R Bool
189+isNewlineModified = isJust <$> R (gets scNewline)
190+
185191-- | Ensure that indentation level is satisfied. Insert correct number of
186192-- spaces if it isn't.
187193