{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Ormolu.Printer.Internal
(
R,
runR,
txt,
interferingTxt,
atom,
space,
newline,
declNewline,
askSourceType,
askFixityOverrides,
askFixityMap,
inci,
inciBy,
inciByFrac,
inciHalf,
inciByExact,
sitcc,
sitccIfTrailing,
Layout (..),
enterLayout,
vlayout,
getLayout,
getPrinterOpt,
useBraces,
dontUseBraces,
canUseBraces,
CommentPosition (..),
registerPendingCommentLine,
trimSpanStream,
nextEltSpan,
popComment,
getEnclosingSpan,
withEnclosingSpan,
thisLineSpans,
SpanMark (..),
spanMarkSpan,
HaddockStyle (..),
setSpanMark,
getSpanMark,
isExtensionEnabled,
PrevTypeCtx (..),
getPrevTypeCtx,
setPrevTypeCtx,
)
where
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Bool (bool)
import Data.Coerce
import Data.Functor.Identity (runIdentity)
import Data.Maybe (listToMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder
import GHC.Data.EnumSet (EnumSet)
import qualified GHC.Data.EnumSet as EnumSet
import GHC.LanguageExtensions.Type
import GHC.Types.SrcLoc
import GHC.Utils.Outputable (Outputable)
import Ormolu.Config
import Ormolu.Fixity (FixityMap, LazyFixityMap)
import Ormolu.Parser.CommentStream
import Ormolu.Printer.SpanStream
import Ormolu.Utils (showOutputable)
newtype R a = R (ReaderT RC (State SC) a)
deriving (forall a b. a -> R b -> R a
forall a b. (a -> b) -> R a -> R b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> R b -> R a
$c<$ :: forall a b. a -> R b -> R a
fmap :: forall a b. (a -> b) -> R a -> R b
$cfmap :: forall a b. (a -> b) -> R a -> R b
Functor, Functor R
forall a. a -> R a
forall a b. R a -> R b -> R a
forall a b. R a -> R b -> R b
forall a b. R (a -> b) -> R a -> R b
forall a b c. (a -> b -> c) -> R a -> R b -> R c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. R a -> R b -> R a
$c<* :: forall a b. R a -> R b -> R a
*> :: forall a b. R a -> R b -> R b
$c*> :: forall a b. R a -> R b -> R b
liftA2 :: forall a b c. (a -> b -> c) -> R a -> R b -> R c
$cliftA2 :: forall a b c. (a -> b -> c) -> R a -> R b -> R c
<*> :: forall a b. R (a -> b) -> R a -> R b
$c<*> :: forall a b. R (a -> b) -> R a -> R b
pure :: forall a. a -> R a
$cpure :: forall a. a -> R a
Applicative, Applicative R
forall a. a -> R a
forall a b. R a -> R b -> R b
forall a b. R a -> (a -> R b) -> R b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> R a
$creturn :: forall a. a -> R a
>> :: forall a b. R a -> R b -> R b
$c>> :: forall a b. R a -> R b -> R b
>>= :: forall a b. R a -> (a -> R b) -> R b
$c>>= :: forall a b. R a -> (a -> R b) -> R b
Monad)
data RC = RC
{
RC -> Int
rcIndent :: !Int,
RC -> Layout
rcLayout :: Layout,
RC -> [RealSrcSpan]
rcEnclosingSpans :: [RealSrcSpan],
RC -> Bool
rcCanUseBraces :: Bool,
RC -> PrinterOptsTotal
rcPrinterOpts :: PrinterOptsTotal,
RC -> EnumSet Extension
rcExtensions :: EnumSet Extension,
RC -> SourceType
rcSourceType :: SourceType,
RC -> FixityMap
rcFixityOverrides :: FixityMap,
RC -> LazyFixityMap
rcFixityMap :: LazyFixityMap
}
data SC = SC
{
SC -> Int
scColumn :: !Int,
SC -> Int
scIndent :: !Int,
SC -> Builder
scBuilder :: Builder,
SC -> SpanStream
scSpanStream :: SpanStream,
SC -> [RealSrcSpan]
scThisLineSpans :: [RealSrcSpan],
:: CommentStream,
:: ![(CommentPosition, Text)],
SC -> RequestedDelimiter
scRequestedDelimiter :: !RequestedDelimiter,
SC -> Maybe SpanMark
scSpanMark :: !(Maybe SpanMark),
SC -> PrevTypeCtx
scPrevTypeCtx :: PrevTypeCtx
}
data RequestedDelimiter
=
RequestedSpace
|
RequestedNewline
|
RequestedNothing
|
AfterNewline
|
VeryBeginning
deriving (RequestedDelimiter -> RequestedDelimiter -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RequestedDelimiter -> RequestedDelimiter -> Bool
$c/= :: RequestedDelimiter -> RequestedDelimiter -> Bool
== :: RequestedDelimiter -> RequestedDelimiter -> Bool
$c== :: RequestedDelimiter -> RequestedDelimiter -> Bool
Eq, Int -> RequestedDelimiter -> ShowS
[RequestedDelimiter] -> ShowS
RequestedDelimiter -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RequestedDelimiter] -> ShowS
$cshowList :: [RequestedDelimiter] -> ShowS
show :: RequestedDelimiter -> String
$cshow :: RequestedDelimiter -> String
showsPrec :: Int -> RequestedDelimiter -> ShowS
$cshowsPrec :: Int -> RequestedDelimiter -> ShowS
Show)
data Layout
=
SingleLine
|
MultiLine
deriving (Layout -> Layout -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Layout -> Layout -> Bool
$c/= :: Layout -> Layout -> Bool
== :: Layout -> Layout -> Bool
$c== :: Layout -> Layout -> Bool
Eq, Int -> Layout -> ShowS
[Layout] -> ShowS
Layout -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Layout] -> ShowS
$cshowList :: [Layout] -> ShowS
show :: Layout -> String
$cshow :: Layout -> String
showsPrec :: Int -> Layout -> ShowS
$cshowsPrec :: Int -> Layout -> ShowS
Show)
data
=
OnTheSameLine
|
OnNextLine
deriving (CommentPosition -> CommentPosition -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CommentPosition -> CommentPosition -> Bool
$c/= :: CommentPosition -> CommentPosition -> Bool
== :: CommentPosition -> CommentPosition -> Bool
$c== :: CommentPosition -> CommentPosition -> Bool
Eq, Int -> CommentPosition -> ShowS
[CommentPosition] -> ShowS
CommentPosition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CommentPosition] -> ShowS
$cshowList :: [CommentPosition] -> ShowS
show :: CommentPosition -> String
$cshow :: CommentPosition -> String
showsPrec :: Int -> CommentPosition -> ShowS
$cshowsPrec :: Int -> CommentPosition -> ShowS
Show)
runR ::
R () ->
SpanStream ->
CommentStream ->
PrinterOptsTotal ->
SourceType ->
EnumSet Extension ->
FixityMap ->
LazyFixityMap ->
Text
runR :: R ()
-> SpanStream
-> CommentStream
-> PrinterOptsTotal
-> SourceType
-> EnumSet Extension
-> FixityMap
-> LazyFixityMap
-> Text
runR (R ReaderT RC (State SC) ()
m) SpanStream
sstream CommentStream
cstream PrinterOptsTotal
printerOpts SourceType
sourceType EnumSet Extension
extensions FixityMap
fixityOverrides LazyFixityMap
fixityMap =
Text -> Text
TL.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
toLazyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. SC -> Builder
scBuilder forall a b. (a -> b) -> a -> b
$ forall s a. State s a -> s -> s
execState (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT RC (State SC) ()
m RC
rc) SC
sc
where
rc :: RC
rc =
RC
{ rcIndent :: Int
rcIndent = Int
0,
rcLayout :: Layout
rcLayout = Layout
MultiLine,
rcEnclosingSpans :: [RealSrcSpan]
rcEnclosingSpans = [],
rcCanUseBraces :: Bool
rcCanUseBraces = Bool
False,
rcPrinterOpts :: PrinterOptsTotal
rcPrinterOpts = PrinterOptsTotal
printerOpts,
rcExtensions :: EnumSet Extension
rcExtensions = EnumSet Extension
extensions,
rcSourceType :: SourceType
rcSourceType = SourceType
sourceType,
rcFixityOverrides :: FixityMap
rcFixityOverrides = FixityMap
fixityOverrides,
rcFixityMap :: LazyFixityMap
rcFixityMap = LazyFixityMap
fixityMap
}
sc :: SC
sc =
SC
{ scColumn :: Int
scColumn = Int
0,
scIndent :: Int
scIndent = Int
0,
scBuilder :: Builder
scBuilder = forall a. Monoid a => a
mempty,
scSpanStream :: SpanStream
scSpanStream = SpanStream
sstream,
scThisLineSpans :: [RealSrcSpan]
scThisLineSpans = [],
scCommentStream :: CommentStream
scCommentStream = CommentStream
cstream,
scPendingComments :: [(CommentPosition, Text)]
scPendingComments = [],
scRequestedDelimiter :: RequestedDelimiter
scRequestedDelimiter = RequestedDelimiter
VeryBeginning,
scSpanMark :: Maybe SpanMark
scSpanMark = forall a. Maybe a
Nothing,
scPrevTypeCtx :: PrevTypeCtx
scPrevTypeCtx = PrevTypeCtx
TypeCtxStart
}
data SpitType
=
SimpleText
|
InterferingText
|
Atom
|
deriving (Int -> SpitType -> ShowS
[SpitType] -> ShowS
SpitType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SpitType] -> ShowS
$cshowList :: [SpitType] -> ShowS
show :: SpitType -> String
$cshow :: SpitType -> String
showsPrec :: Int -> SpitType -> ShowS
$cshowsPrec :: Int -> SpitType -> ShowS
Show, SpitType -> SpitType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SpitType -> SpitType -> Bool
$c/= :: SpitType -> SpitType -> Bool
== :: SpitType -> SpitType -> Bool
$c== :: SpitType -> SpitType -> Bool
Eq)
txt ::
Text ->
R ()
txt :: Text -> R ()
txt = SpitType -> Text -> R ()
spit SpitType
SimpleText
interferingTxt ::
Text ->
R ()
interferingTxt :: Text -> R ()
interferingTxt = SpitType -> Text -> R ()
spit SpitType
InterferingText
atom ::
Outputable a =>
a ->
R ()
atom :: forall a. Outputable a => a -> R ()
atom = SpitType -> Text -> R ()
spit SpitType
Atom forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall o. Outputable o => o -> String
showOutputable
spit ::
SpitType ->
Text ->
R ()
spit :: SpitType -> Text -> R ()
spit SpitType
_ Text
"" = forall (m :: * -> *) a. Monad m => a -> m a
return ()
spit SpitType
stype Text
text = do
RequestedDelimiter
requestedDel <- forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> RequestedDelimiter
scRequestedDelimiter)
[(CommentPosition, Text)]
pendingComments <- forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> [(CommentPosition, Text)]
scPendingComments)
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SpitType
stype forall a. Eq a => a -> a -> Bool
== SpitType
InterferingText Bool -> Bool -> Bool
&& Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(CommentPosition, Text)]
pendingComments)) R ()
newline
case RequestedDelimiter
requestedDel of
RequestedDelimiter
RequestedNewline -> do
forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scRequestedDelimiter :: RequestedDelimiter
scRequestedDelimiter = RequestedDelimiter
RequestedNothing
}
case SpitType
stype of
SpitType
CommentPart -> R ()
newlineRaw
SpitType
_ -> R ()
newline
RequestedDelimiter
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall a. ReaderT RC (State SC) a -> R a
R forall a b. (a -> b) -> a -> b
$ do
Int
i <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Int
rcIndent
Int
c <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> Int
scColumn
Maybe RealSrcSpan
closestEnclosing <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. RC -> [RealSrcSpan]
rcEnclosingSpans)
let indentedTxt :: Text
indentedTxt = Text
spaces forall a. Semigroup a => a -> a -> a
<> Text
text
spaces :: Text
spaces = Int -> Text -> Text
T.replicate Int
spacesN Text
" "
spacesN :: Int
spacesN =
if Int
c forall a. Eq a => a -> a -> Bool
== Int
0
then Int
i
else forall a. a -> a -> Bool -> a
bool Int
0 Int
1 (RequestedDelimiter
requestedDel forall a. Eq a => a -> a -> Bool
== RequestedDelimiter
RequestedSpace)
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scBuilder :: Builder
scBuilder = SC -> Builder
scBuilder SC
sc forall a. Semigroup a => a -> a -> a
<> Text -> Builder
fromText Text
indentedTxt,
scColumn :: Int
scColumn = SC -> Int
scColumn SC
sc forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
indentedTxt,
scIndent :: Int
scIndent =
if Int
c forall a. Eq a => a -> a -> Bool
== Int
0
then Int
i
else SC -> Int
scIndent SC
sc,
scThisLineSpans :: [RealSrcSpan]
scThisLineSpans =
let xs :: [RealSrcSpan]
xs = SC -> [RealSrcSpan]
scThisLineSpans SC
sc
in case SpitType
stype of
SpitType
Atom -> case Maybe RealSrcSpan
closestEnclosing of
Maybe RealSrcSpan
Nothing -> [RealSrcSpan]
xs
Just RealSrcSpan
x -> RealSrcSpan
x forall a. a -> [a] -> [a]
: [RealSrcSpan]
xs
SpitType
_ -> [RealSrcSpan]
xs,
scRequestedDelimiter :: RequestedDelimiter
scRequestedDelimiter = RequestedDelimiter
RequestedNothing,
scSpanMark :: Maybe SpanMark
scSpanMark =
if (SpitType
stype forall a. Eq a => a -> a -> Bool
== SpitType
CommentPart) Bool -> Bool -> Bool
|| (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. SC -> [(CommentPosition, Text)]
scPendingComments) SC
sc
then SC -> Maybe SpanMark
scSpanMark SC
sc
else forall a. Maybe a
Nothing
}
space :: R ()
space :: R ()
space = forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scRequestedDelimiter :: RequestedDelimiter
scRequestedDelimiter = case SC -> RequestedDelimiter
scRequestedDelimiter SC
sc of
RequestedDelimiter
RequestedNothing -> RequestedDelimiter
RequestedSpace
RequestedDelimiter
other -> RequestedDelimiter
other
}
declNewline :: R ()
declNewline :: R ()
declNewline = Int -> R ()
newlineRawN forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. (forall (f :: * -> *). PrinterOpts f -> f a) -> R a
getPrinterOpt forall (f :: * -> *). PrinterOpts f -> f Int
poNewlinesBetweenDecls
newline :: R ()
newline :: R ()
newline = do
Int
indent <- forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> Int
scIndent)
[(CommentPosition, Text)]
cs <- forall a. [a] -> [a]
reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> [(CommentPosition, Text)]
scPendingComments)
case [(CommentPosition, Text)]
cs of
[] -> R ()
newlineRaw
((CommentPosition
position, Text
_) : [(CommentPosition, Text)]
_) -> do
case CommentPosition
position of
CommentPosition
OnTheSameLine -> R ()
space
CommentPosition
OnNextLine -> R ()
newlineRaw
forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(CommentPosition, Text)]
cs forall a b. (a -> b) -> a -> b
$ \(CommentPosition
_, Text
text) ->
let modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcIndent :: Int
rcIndent = Int
indent
}
R ReaderT RC (State SC) ()
m = do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Bool
T.null Text
text) forall a b. (a -> b) -> a -> b
$
SpitType -> Text -> R ()
spit SpitType
CommentPart Text
text
R ()
newlineRaw
in forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m
forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scPendingComments :: [(CommentPosition, Text)]
scPendingComments = []
}
newlineRaw :: R ()
newlineRaw :: R ()
newlineRaw = Int -> R ()
newlineRawN Int
1
newlineRawN :: Int -> R ()
newlineRawN :: Int -> R ()
newlineRawN Int
n = forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
let requestedDel :: RequestedDelimiter
requestedDel = SC -> RequestedDelimiter
scRequestedDelimiter SC
sc
builderSoFar :: Builder
builderSoFar = SC -> Builder
scBuilder SC
sc
n' :: Int
n' = case RequestedDelimiter
requestedDel of
RequestedDelimiter
AfterNewline -> Int
n forall a. Num a => a -> a -> a
- Int
1
RequestedDelimiter
RequestedNewline -> Int
n forall a. Num a => a -> a -> a
- Int
1
RequestedDelimiter
VeryBeginning -> Int
n forall a. Num a => a -> a -> a
- Int
1
RequestedDelimiter
_ -> Int
n
in SC
sc
{ scBuilder :: Builder
scBuilder = Builder
builderSoFar forall a. Semigroup a => a -> a -> a
<> forall a. Monoid a => [a] -> a
mconcat (forall a. Int -> a -> [a]
replicate Int
n' Builder
"\n"),
scColumn :: Int
scColumn = Int
0,
scIndent :: Int
scIndent = Int
0,
scThisLineSpans :: [RealSrcSpan]
scThisLineSpans = [],
scRequestedDelimiter :: RequestedDelimiter
scRequestedDelimiter = case SC -> RequestedDelimiter
scRequestedDelimiter SC
sc of
RequestedDelimiter
AfterNewline -> RequestedDelimiter
RequestedNewline
RequestedDelimiter
RequestedNewline -> RequestedDelimiter
RequestedNewline
RequestedDelimiter
VeryBeginning -> RequestedDelimiter
VeryBeginning
RequestedDelimiter
_ -> RequestedDelimiter
AfterNewline
}
askSourceType :: R SourceType
askSourceType :: R SourceType
askSourceType = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> SourceType
rcSourceType)
askFixityOverrides :: R FixityMap
askFixityOverrides :: R FixityMap
askFixityOverrides = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> FixityMap
rcFixityOverrides)
askFixityMap :: R LazyFixityMap
askFixityMap :: R LazyFixityMap
askFixityMap = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> LazyFixityMap
rcFixityMap)
inciBy :: Int -> R () -> R ()
inciBy :: Int -> R () -> R ()
inciBy Int
step (R ReaderT RC (State SC) ()
m) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m)
where
modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcIndent :: Int
rcIndent = forall {a}. Integral a => a -> a -> a
roundDownToNearest Int
step (RC -> Int
rcIndent RC
rc) forall a. Num a => a -> a -> a
+ Int
step
}
roundDownToNearest :: a -> a -> a
roundDownToNearest a
r a
n = (a
n forall {a}. Integral a => a -> a -> a
`div` a
r) forall a. Num a => a -> a -> a
* a
r
inciByFrac :: Int -> R () -> R ()
inciByFrac :: Int -> R () -> R ()
inciByFrac Int
x R ()
m = do
Int
indentStep <- forall a. (forall (f :: * -> *). PrinterOpts f -> f a) -> R a
getPrinterOpt forall (f :: * -> *). PrinterOpts f -> f Int
poIndentation
let step :: Int
step = Int
indentStep forall {a}. Integral a => a -> a -> a
`quot` Int
x
Int -> R () -> R ()
inciBy Int
step R ()
m
inci :: R () -> R ()
inci :: R () -> R ()
inci = Int -> R () -> R ()
inciByFrac Int
1
inciHalf :: R () -> R ()
inciHalf :: R () -> R ()
inciHalf = Int -> R () -> R ()
inciByFrac Int
2
inciByExact :: Int -> R () -> R ()
inciByExact :: Int -> R () -> R ()
inciByExact Int
spaces (R ReaderT RC (State SC) ()
m) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m)
where
modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcIndent :: Int
rcIndent = RC -> Int
rcIndent RC
rc forall a. Num a => a -> a -> a
+ Int
spaces
}
sitcc :: R () -> R ()
sitcc :: R () -> R ()
sitcc (R ReaderT RC (State SC) ()
m) = do
RequestedDelimiter
requestedDel <- forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> RequestedDelimiter
scRequestedDelimiter)
Int
i <- forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Int
rcIndent)
Int
c <- forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> Int
scColumn)
let modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcIndent :: Int
rcIndent = forall a. Ord a => a -> a -> a
max Int
i (Int
c forall a. Num a => a -> a -> a
+ forall a. a -> a -> Bool -> a
bool Int
0 Int
1 (RequestedDelimiter
requestedDel forall a. Eq a => a -> a -> Bool
== RequestedDelimiter
RequestedSpace))
}
forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m)
sitccIfTrailing :: R () -> R ()
sitccIfTrailing :: R () -> R ()
sitccIfTrailing R ()
x =
forall a. (forall (f :: * -> *). PrinterOpts f -> f a) -> R a
getPrinterOpt forall (f :: * -> *). PrinterOpts f -> f CommaStyle
poCommaStyle forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
CommaStyle
Leading -> forall a. a -> a
id R ()
x
CommaStyle
Trailing -> R () -> R ()
sitcc R ()
x
enterLayout :: Layout -> R () -> R ()
enterLayout :: Layout -> R () -> R ()
enterLayout Layout
l (R ReaderT RC (State SC) ()
m) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m)
where
modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcLayout :: Layout
rcLayout = Layout
l
}
vlayout ::
R a ->
R a ->
R a
vlayout :: forall a. R a -> R a -> R a
vlayout R a
sline R a
mline = do
Layout
l <- R Layout
getLayout
case Layout
l of
Layout
SingleLine -> R a
sline
Layout
MultiLine -> R a
mline
getLayout :: R Layout
getLayout :: R Layout
getLayout = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Layout
rcLayout)
getPrinterOpt :: (forall f. PrinterOpts f -> f a) -> R a
getPrinterOpt :: forall a. (forall (f :: * -> *). PrinterOpts f -> f a) -> R a
getPrinterOpt forall (f :: * -> *). PrinterOpts f -> f a
f = forall a. ReaderT RC (State SC) a -> R a
R forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall a b. (a -> b) -> a -> b
$ forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *). PrinterOpts f -> f a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. RC -> PrinterOptsTotal
rcPrinterOpts
registerPendingCommentLine ::
CommentPosition ->
Text ->
R ()
CommentPosition
position Text
text = forall a. ReaderT RC (State SC) a -> R a
R forall a b. (a -> b) -> a -> b
$ do
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scPendingComments :: [(CommentPosition, Text)]
scPendingComments = (CommentPosition
position, Text
text) forall a. a -> [a] -> [a]
: SC -> [(CommentPosition, Text)]
scPendingComments SC
sc
}
trimSpanStream ::
RealSrcSpan ->
R ()
trimSpanStream :: RealSrcSpan -> R ()
trimSpanStream RealSrcSpan
ref = do
let leRef :: RealSrcSpan -> Bool
leRef :: RealSrcSpan -> Bool
leRef RealSrcSpan
x = RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
x forall a. Ord a => a -> a -> Bool
<= RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
ref
forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scSpanStream :: SpanStream
scSpanStream = coerce :: forall a b. Coercible a b => a -> b
coerce (forall a. (a -> Bool) -> [a] -> [a]
dropWhile RealSrcSpan -> Bool
leRef) (SC -> SpanStream
scSpanStream SC
sc)
}
nextEltSpan :: R (Maybe RealSrcSpan)
nextEltSpan :: R (Maybe RealSrcSpan)
nextEltSpan = forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> SpanStream
scSpanStream)
popComment ::
(RealLocated Comment -> Bool) ->
R (Maybe (RealLocated Comment))
RealLocated Comment -> Bool
f = forall a. ReaderT RC (State SC) a -> R a
R forall a b. (a -> b) -> a -> b
$ do
CommentStream [RealLocated Comment]
cstream <- forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> CommentStream
scCommentStream
case [RealLocated Comment]
cstream of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
(RealLocated Comment
x : [RealLocated Comment]
xs) ->
if RealLocated Comment -> Bool
f RealLocated Comment
x
then
forall a. a -> Maybe a
Just RealLocated Comment
x
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify
( \SC
sc ->
SC
sc
{ scCommentStream :: CommentStream
scCommentStream = [RealLocated Comment] -> CommentStream
CommentStream [RealLocated Comment]
xs
}
)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
getEnclosingSpan ::
(RealSrcSpan -> Bool) ->
R (Maybe RealSrcSpan)
getEnclosingSpan :: (RealSrcSpan -> Bool) -> R (Maybe RealSrcSpan)
getEnclosingSpan RealSrcSpan -> Bool
f =
forall a. [a] -> Maybe a
listToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter RealSrcSpan -> Bool
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> [RealSrcSpan]
rcEnclosingSpans)
withEnclosingSpan :: RealSrcSpan -> R () -> R ()
withEnclosingSpan :: RealSrcSpan -> R () -> R ()
withEnclosingSpan RealSrcSpan
spn (R ReaderT RC (State SC) ()
m) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m)
where
modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcEnclosingSpans :: [RealSrcSpan]
rcEnclosingSpans = RealSrcSpan
spn forall a. a -> [a] -> [a]
: RC -> [RealSrcSpan]
rcEnclosingSpans RC
rc
}
thisLineSpans :: R [RealSrcSpan]
thisLineSpans :: R [RealSrcSpan]
thisLineSpans = forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> [RealSrcSpan]
scThisLineSpans)
data SpanMark
=
HaddockSpan HaddockStyle RealSrcSpan
|
RealSrcSpan
|
StatementSpan RealSrcSpan
spanMarkSpan :: SpanMark -> RealSrcSpan
spanMarkSpan :: SpanMark -> RealSrcSpan
spanMarkSpan = \case
HaddockSpan HaddockStyle
_ RealSrcSpan
s -> RealSrcSpan
s
CommentSpan RealSrcSpan
s -> RealSrcSpan
s
StatementSpan RealSrcSpan
s -> RealSrcSpan
s
data HaddockStyle
=
Pipe
|
Caret
|
Asterisk Int
|
Named String
setSpanMark ::
SpanMark ->
R ()
setSpanMark :: SpanMark -> R ()
setSpanMark SpanMark
spnMark = forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scSpanMark :: Maybe SpanMark
scSpanMark = forall a. a -> Maybe a
Just SpanMark
spnMark
}
getSpanMark :: R (Maybe SpanMark)
getSpanMark :: R (Maybe SpanMark)
getSpanMark = forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> Maybe SpanMark
scSpanMark)
useBraces :: R () -> R ()
useBraces :: R () -> R ()
useBraces (R ReaderT RC (State SC) ()
r) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\RC
i -> RC
i {rcCanUseBraces :: Bool
rcCanUseBraces = Bool
True}) ReaderT RC (State SC) ()
r)
dontUseBraces :: R () -> R ()
dontUseBraces :: R () -> R ()
dontUseBraces (R ReaderT RC (State SC) ()
r) = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\RC
i -> RC
i {rcCanUseBraces :: Bool
rcCanUseBraces = Bool
False}) ReaderT RC (State SC) ()
r)
canUseBraces :: R Bool
canUseBraces :: R Bool
canUseBraces = forall a. ReaderT RC (State SC) a -> R a
R (forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Bool
rcCanUseBraces)
isExtensionEnabled :: Extension -> R Bool
isExtensionEnabled :: Extension -> R Bool
isExtensionEnabled Extension
ext = forall a. ReaderT RC (State SC) a -> R a
R forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks forall a b. (a -> b) -> a -> b
$ forall a. Enum a => a -> EnumSet a -> Bool
EnumSet.member Extension
ext forall b c a. (b -> c) -> (a -> b) -> a -> c
. RC -> EnumSet Extension
rcExtensions
data PrevTypeCtx
= TypeCtxStart
| TypeCtxForall
| TypeCtxContext
| TypeCtxArgument
deriving (PrevTypeCtx -> PrevTypeCtx -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PrevTypeCtx -> PrevTypeCtx -> Bool
$c/= :: PrevTypeCtx -> PrevTypeCtx -> Bool
== :: PrevTypeCtx -> PrevTypeCtx -> Bool
$c== :: PrevTypeCtx -> PrevTypeCtx -> Bool
Eq, Int -> PrevTypeCtx -> ShowS
[PrevTypeCtx] -> ShowS
PrevTypeCtx -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PrevTypeCtx] -> ShowS
$cshowList :: [PrevTypeCtx] -> ShowS
show :: PrevTypeCtx -> String
$cshow :: PrevTypeCtx -> String
showsPrec :: Int -> PrevTypeCtx -> ShowS
$cshowsPrec :: Int -> PrevTypeCtx -> ShowS
Show)
getPrevTypeCtx :: R PrevTypeCtx
getPrevTypeCtx :: R PrevTypeCtx
getPrevTypeCtx = forall a. ReaderT RC (State SC) a -> R a
R (forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> PrevTypeCtx
scPrevTypeCtx)
setPrevTypeCtx :: PrevTypeCtx -> R ()
setPrevTypeCtx :: PrevTypeCtx -> R ()
setPrevTypeCtx PrevTypeCtx
prevTypeCtx =
forall a. ReaderT RC (State SC) a -> R a
R forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\SC
sc -> SC
sc {scPrevTypeCtx :: PrevTypeCtx
scPrevTypeCtx = PrevTypeCtx
prevTypeCtx})