{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Ormolu.Printer.Internal
(
R,
runR,
txt,
interferingTxt,
atom,
space,
newline,
askSourceType,
askModuleFixityMap,
askDebug,
inci,
sitcc,
Layout (..),
enterLayout,
vlayout,
getLayout,
useBraces,
dontUseBraces,
canUseBraces,
CommentPosition (..),
registerPendingCommentLine,
trimSpanStream,
nextEltSpan,
popComment,
getEnclosingComments,
getEnclosingSpan,
getEnclosingSpanWhere,
withEnclosingSpan,
thisLineSpans,
SpanMark (..),
spanMarkSpan,
HaddockStyle (..),
setSpanMark,
getSpanMark,
isExtensionEnabled,
)
where
import Control.Monad
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Bool (bool)
import Data.Choice (Choice)
import Data.Choice qualified as Choice
import Data.Coerce
import Data.Functor ((<&>))
import Data.List (find)
import Data.Maybe (listToMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Builder
import GHC.Data.EnumSet (EnumSet)
import GHC.Data.EnumSet qualified as EnumSet
import GHC.LanguageExtensions.Type
import GHC.Types.SrcLoc
import GHC.Utils.Outputable (Outputable)
import Ormolu.Config (SourceType (..))
import Ormolu.Fixity (ModuleFixityMap)
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 -> b) -> R a -> R b)
-> (forall a b. a -> R b -> R a) -> Functor R
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
$cfmap :: forall a b. (a -> b) -> R a -> R b
fmap :: forall a b. (a -> b) -> R a -> R b
$c<$ :: forall a b. a -> R b -> R a
<$ :: forall a b. a -> R b -> R a
Functor, Functor R
Functor R =>
(forall a. a -> R a)
-> (forall a b. R (a -> b) -> R a -> R b)
-> (forall a b c. (a -> b -> c) -> R a -> R b -> R c)
-> (forall a b. R a -> R b -> R b)
-> (forall a b. R a -> R b -> R a)
-> Applicative 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
$cpure :: forall a. a -> R a
pure :: forall a. a -> R a
$c<*> :: forall a b. R (a -> b) -> R a -> R b
<*> :: forall a b. R (a -> b) -> R a -> R b
$cliftA2 :: forall a b c. (a -> b -> c) -> R a -> R b -> R c
liftA2 :: forall a b c. (a -> b -> c) -> R a -> R b -> R c
$c*> :: forall a b. R a -> R b -> R b
*> :: forall a b. R a -> R b -> R b
$c<* :: forall a b. R a -> R b -> R a
<* :: forall a b. R a -> R b -> R a
Applicative, Applicative R
Applicative R =>
(forall a b. R a -> (a -> R b) -> R b)
-> (forall a b. R a -> R b -> R b)
-> (forall a. a -> R a)
-> Monad 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
$c>>= :: forall a b. R a -> (a -> R b) -> R b
>>= :: forall a b. R a -> (a -> R b) -> R b
$c>> :: forall a b. R a -> R b -> R b
>> :: forall a b. R a -> R b -> R b
$creturn :: forall a. a -> R a
return :: forall a. a -> R a
Monad)
data RC = RC
{
RC -> Int
rcIndent :: !Int,
RC -> Layout
rcLayout :: Layout,
RC -> [RealSrcSpan]
rcEnclosingSpans :: [RealSrcSpan],
RC -> Bool
rcCanUseBraces :: Bool,
RC -> EnumSet Extension
rcExtensions :: EnumSet Extension,
RC -> SourceType
rcSourceType :: SourceType,
RC -> ModuleFixityMap
rcModuleFixityMap :: ModuleFixityMap,
RC -> Bool
rcDebug :: !Bool
}
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)
}
data RequestedDelimiter
=
RequestedSpace
|
RequestedNewline
|
RequestedNothing
|
AfterNewline
|
VeryBeginning
deriving (RequestedDelimiter -> RequestedDelimiter -> Bool
(RequestedDelimiter -> RequestedDelimiter -> Bool)
-> (RequestedDelimiter -> RequestedDelimiter -> Bool)
-> Eq RequestedDelimiter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RequestedDelimiter -> RequestedDelimiter -> Bool
== :: RequestedDelimiter -> RequestedDelimiter -> Bool
$c/= :: RequestedDelimiter -> RequestedDelimiter -> Bool
/= :: RequestedDelimiter -> RequestedDelimiter -> Bool
Eq, Int -> RequestedDelimiter -> ShowS
[RequestedDelimiter] -> ShowS
RequestedDelimiter -> String
(Int -> RequestedDelimiter -> ShowS)
-> (RequestedDelimiter -> String)
-> ([RequestedDelimiter] -> ShowS)
-> Show RequestedDelimiter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RequestedDelimiter -> ShowS
showsPrec :: Int -> RequestedDelimiter -> ShowS
$cshow :: RequestedDelimiter -> String
show :: RequestedDelimiter -> String
$cshowList :: [RequestedDelimiter] -> ShowS
showList :: [RequestedDelimiter] -> ShowS
Show)
data Layout
=
SingleLine
|
MultiLine
deriving (Layout -> Layout -> Bool
(Layout -> Layout -> Bool)
-> (Layout -> Layout -> Bool) -> Eq Layout
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Layout -> Layout -> Bool
== :: Layout -> Layout -> Bool
$c/= :: Layout -> Layout -> Bool
/= :: Layout -> Layout -> Bool
Eq, Int -> Layout -> ShowS
[Layout] -> ShowS
Layout -> String
(Int -> Layout -> ShowS)
-> (Layout -> String) -> ([Layout] -> ShowS) -> Show Layout
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Layout -> ShowS
showsPrec :: Int -> Layout -> ShowS
$cshow :: Layout -> String
show :: Layout -> String
$cshowList :: [Layout] -> ShowS
showList :: [Layout] -> ShowS
Show)
data
=
OnTheSameLine
|
OnNextLine
deriving (CommentPosition -> CommentPosition -> Bool
(CommentPosition -> CommentPosition -> Bool)
-> (CommentPosition -> CommentPosition -> Bool)
-> Eq CommentPosition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommentPosition -> CommentPosition -> Bool
== :: CommentPosition -> CommentPosition -> Bool
$c/= :: CommentPosition -> CommentPosition -> Bool
/= :: CommentPosition -> CommentPosition -> Bool
Eq, Int -> CommentPosition -> ShowS
[CommentPosition] -> ShowS
CommentPosition -> String
(Int -> CommentPosition -> ShowS)
-> (CommentPosition -> String)
-> ([CommentPosition] -> ShowS)
-> Show CommentPosition
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommentPosition -> ShowS
showsPrec :: Int -> CommentPosition -> ShowS
$cshow :: CommentPosition -> String
show :: CommentPosition -> String
$cshowList :: [CommentPosition] -> ShowS
showList :: [CommentPosition] -> ShowS
Show)
runR ::
R () ->
SpanStream ->
CommentStream ->
SourceType ->
EnumSet Extension ->
ModuleFixityMap ->
Bool ->
Text
runR :: R ()
-> SpanStream
-> CommentStream
-> SourceType
-> EnumSet Extension
-> ModuleFixityMap
-> Bool
-> Text
runR (R ReaderT RC (State SC) ()
m) SpanStream
sstream CommentStream
cstream SourceType
sourceType EnumSet Extension
extensions ModuleFixityMap
moduleFixityMap Bool
debug =
LazyText -> Text
TL.toStrict (LazyText -> Text) -> (SC -> LazyText) -> SC -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> LazyText
toLazyText (Builder -> LazyText) -> (SC -> Builder) -> SC -> LazyText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SC -> Builder
scBuilder (SC -> Text) -> SC -> Text
forall a b. (a -> b) -> a -> b
$ State SC () -> SC -> SC
forall s a. State s a -> s -> s
execState (ReaderT RC (State SC) () -> RC -> State SC ()
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,
rcExtensions :: EnumSet Extension
rcExtensions = EnumSet Extension
extensions,
rcSourceType :: SourceType
rcSourceType = SourceType
sourceType,
rcModuleFixityMap :: ModuleFixityMap
rcModuleFixityMap = ModuleFixityMap
moduleFixityMap,
rcDebug :: Bool
rcDebug = Bool
debug
}
sc :: SC
sc =
SC
{ scColumn :: Int
scColumn = Int
0,
scIndent :: Int
scIndent = Int
0,
scBuilder :: Builder
scBuilder = Builder
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 = Maybe SpanMark
forall a. Maybe a
Nothing
}
data SpitType
=
SimpleText
|
InterferingText
|
Atom
|
deriving (Int -> SpitType -> ShowS
[SpitType] -> ShowS
SpitType -> String
(Int -> SpitType -> ShowS)
-> (SpitType -> String) -> ([SpitType] -> ShowS) -> Show SpitType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SpitType -> ShowS
showsPrec :: Int -> SpitType -> ShowS
$cshow :: SpitType -> String
show :: SpitType -> String
$cshowList :: [SpitType] -> ShowS
showList :: [SpitType] -> ShowS
Show, SpitType -> SpitType -> Bool
(SpitType -> SpitType -> Bool)
-> (SpitType -> SpitType -> Bool) -> Eq SpitType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SpitType -> SpitType -> Bool
== :: SpitType -> SpitType -> Bool
$c/= :: SpitType -> SpitType -> Bool
/= :: 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 (Text -> R ()) -> (a -> Text) -> a -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall o. Outputable o => o -> String
showOutputable
spit ::
SpitType ->
Text ->
R ()
spit :: SpitType -> Text -> R ()
spit SpitType
_ Text
"" = () -> R ()
forall a. a -> R a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
spit SpitType
stype Text
text = do
requestedDel <- ReaderT RC (State SC) RequestedDelimiter -> R RequestedDelimiter
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> RequestedDelimiter)
-> ReaderT RC (State SC) RequestedDelimiter
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> RequestedDelimiter
scRequestedDelimiter)
pendingComments <- R (gets scPendingComments)
when (stype == InterferingText && not (null pendingComments)) newline
case requestedDel of
RequestedDelimiter
RequestedNewline -> do
ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scRequestedDelimiter = RequestedNothing
}
case SpitType
stype of
SpitType
CommentPart -> R ()
newlineRaw
SpitType
_ -> R ()
newline
RequestedDelimiter
_ -> () -> R ()
forall a. a -> R a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
R $ do
i <- asks rcIndent
c <- gets scColumn
closestEnclosing <- asks (listToMaybe . rcEnclosingSpans)
let indentedTxt = Text
spaces Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
text
spaces = Int -> Text -> Text
T.replicate Int
spacesN Text
" "
spacesN =
if Int
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int
i
else Int -> Int -> Bool -> Int
forall a. a -> a -> Bool -> a
bool Int
0 Int
1 (RequestedDelimiter
requestedDel RequestedDelimiter -> RequestedDelimiter -> Bool
forall a. Eq a => a -> a -> Bool
== RequestedDelimiter
RequestedSpace)
modify $ \SC
sc ->
SC
sc
{ scBuilder = scBuilder sc <> fromText indentedTxt,
scColumn = scColumn sc + T.length indentedTxt,
scIndent =
if c == 0
then i
else scIndent sc,
scThisLineSpans =
let xs = SC -> [RealSrcSpan]
scThisLineSpans SC
sc
in case stype of
SpitType
Atom -> case Maybe RealSrcSpan
closestEnclosing of
Maybe RealSrcSpan
Nothing -> [RealSrcSpan]
xs
Just RealSrcSpan
x -> RealSrcSpan
x RealSrcSpan -> [RealSrcSpan] -> [RealSrcSpan]
forall a. a -> [a] -> [a]
: [RealSrcSpan]
xs
SpitType
_ -> [RealSrcSpan]
xs,
scRequestedDelimiter = RequestedNothing,
scSpanMark =
if (stype == CommentPart) || (not . null . scPendingComments) sc
then scSpanMark sc
else Nothing
}
space :: R ()
space :: R ()
space = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scRequestedDelimiter = case scRequestedDelimiter sc of
RequestedDelimiter
RequestedNothing -> RequestedDelimiter
RequestedSpace
RequestedDelimiter
other -> RequestedDelimiter
other
}
newline :: R ()
newline :: R ()
newline = do
indent <- ReaderT RC (State SC) Int -> R Int
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> Int) -> ReaderT RC (State SC) Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> Int
scIndent)
cs <- reverse <$> R (gets scPendingComments)
case cs of
[] -> R ()
newlineRaw
((CommentPosition
position, Text
_) : [(CommentPosition, Text)]
_) -> do
case CommentPosition
position of
CommentPosition
OnTheSameLine -> R ()
space
CommentPosition
OnNextLine -> R ()
newlineRaw
ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> (((CommentPosition, Text) -> ReaderT RC (State SC) ())
-> ReaderT RC (State SC) ())
-> ((CommentPosition, Text) -> ReaderT RC (State SC) ())
-> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(CommentPosition, Text)]
-> ((CommentPosition, Text) -> ReaderT RC (State SC) ())
-> ReaderT RC (State SC) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(CommentPosition, Text)]
cs (((CommentPosition, Text) -> ReaderT RC (State SC) ()) -> R ())
-> ((CommentPosition, Text) -> ReaderT RC (State SC) ()) -> R ()
forall a b. (a -> b) -> a -> b
$ \(CommentPosition
_, Text
text) ->
let modRC :: RC -> RC
modRC RC
rc =
RC
rc
{ rcIndent = indent
}
R ReaderT RC (State SC) ()
m = do
Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Bool
T.null Text
text) (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$
SpitType -> Text -> R ()
spit SpitType
CommentPart Text
text
R ()
newlineRaw
in (RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local RC -> RC
modRC ReaderT RC (State SC) ()
m
ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scPendingComments = []
}
newlineRaw :: R ()
newlineRaw :: R ()
newlineRaw = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
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
in SC
sc
{ scBuilder = case requestedDel of
RequestedDelimiter
AfterNewline -> Builder
builderSoFar
RequestedDelimiter
RequestedNewline -> Builder
builderSoFar
RequestedDelimiter
VeryBeginning -> Builder
builderSoFar
RequestedDelimiter
_ -> Builder
builderSoFar Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"\n",
scColumn = 0,
scIndent = 0,
scThisLineSpans = [],
scRequestedDelimiter = case scRequestedDelimiter sc of
RequestedDelimiter
AfterNewline -> RequestedDelimiter
RequestedNewline
RequestedDelimiter
RequestedNewline -> RequestedDelimiter
RequestedNewline
RequestedDelimiter
VeryBeginning -> RequestedDelimiter
VeryBeginning
RequestedDelimiter
_ -> RequestedDelimiter
AfterNewline
}
askSourceType :: R SourceType
askSourceType :: R SourceType
askSourceType = ReaderT RC (State SC) SourceType -> R SourceType
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> SourceType) -> ReaderT RC (State SC) SourceType
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> SourceType
rcSourceType)
askModuleFixityMap :: R ModuleFixityMap
askModuleFixityMap :: R ModuleFixityMap
askModuleFixityMap = ReaderT RC (State SC) ModuleFixityMap -> R ModuleFixityMap
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> ModuleFixityMap) -> ReaderT RC (State SC) ModuleFixityMap
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> ModuleFixityMap
rcModuleFixityMap)
askDebug :: R (Choice "debug")
askDebug :: R (Choice "debug")
askDebug = ReaderT RC (State SC) (Choice "debug") -> R (Choice "debug")
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> Choice "debug") -> ReaderT RC (State SC) (Choice "debug")
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Bool -> Choice "debug"
forall (a :: Symbol). Bool -> Choice a
Choice.fromBool (Bool -> Choice "debug") -> (RC -> Bool) -> RC -> Choice "debug"
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RC -> Bool
rcDebug))
inciBy :: Int -> R () -> R ()
inciBy :: Int -> R () -> R ()
inciBy Int
step (R ReaderT RC (State SC) ()
m) = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
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 = rcIndent rc + step
}
inci :: R () -> R ()
inci :: R () -> R ()
inci = Int -> R () -> R ()
inciBy Int
indentStep
sitcc :: R () -> R ()
sitcc :: R () -> R ()
sitcc (R ReaderT RC (State SC) ()
m) = do
requestedDel <- ReaderT RC (State SC) RequestedDelimiter -> R RequestedDelimiter
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> RequestedDelimiter)
-> ReaderT RC (State SC) RequestedDelimiter
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> RequestedDelimiter
scRequestedDelimiter)
i <- R (asks rcIndent)
c <- R (gets scColumn)
let modRC RC
rc =
RC
rc
{ rcIndent = max i (c + bool 0 1 (requestedDel == RequestedSpace))
}
R (local modRC m)
enterLayout :: Layout -> R () -> R ()
enterLayout :: Layout -> R () -> R ()
enterLayout Layout
l (R ReaderT RC (State SC) ()
m) = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
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 = 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
l <- R Layout
getLayout
case l of
Layout
SingleLine -> R a
sline
Layout
MultiLine -> R a
mline
getLayout :: R Layout
getLayout :: R Layout
getLayout = ReaderT RC (State SC) Layout -> R Layout
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> Layout) -> ReaderT RC (State SC) Layout
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Layout
rcLayout)
registerPendingCommentLine ::
CommentPosition ->
Text ->
R ()
CommentPosition
position Text
text = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ReaderT RC (State SC) () -> R ()
forall a b. (a -> b) -> a -> b
$ do
(SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> ReaderT RC (State SC) ())
-> (SC -> SC) -> ReaderT RC (State SC) ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scPendingComments = (position, text) : scPendingComments 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 RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
<= RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
ref
ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scSpanStream = coerce (dropWhile leRef) (scSpanStream sc)
}
nextEltSpan :: R (Maybe RealSrcSpan)
nextEltSpan :: R (Maybe RealSrcSpan)
nextEltSpan = [RealSrcSpan] -> Maybe RealSrcSpan
forall a. [a] -> Maybe a
listToMaybe ([RealSrcSpan] -> Maybe RealSrcSpan)
-> (SpanStream -> [RealSrcSpan]) -> SpanStream -> Maybe RealSrcSpan
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SpanStream -> [RealSrcSpan]
forall a b. Coercible a b => a -> b
coerce (SpanStream -> Maybe RealSrcSpan)
-> R SpanStream -> R (Maybe RealSrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT RC (State SC) SpanStream -> R SpanStream
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> SpanStream) -> ReaderT RC (State SC) SpanStream
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> SpanStream
scSpanStream)
popComment ::
(LComment -> Bool) ->
R (Maybe LComment)
LComment -> Bool
f = ReaderT RC (State SC) (Maybe LComment) -> R (Maybe LComment)
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) (Maybe LComment) -> R (Maybe LComment))
-> ReaderT RC (State SC) (Maybe LComment) -> R (Maybe LComment)
forall a b. (a -> b) -> a -> b
$ do
CommentStream cstream <- (SC -> CommentStream) -> ReaderT RC (State SC) CommentStream
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets SC -> CommentStream
scCommentStream
case cstream of
(LComment
x : [LComment]
xs) | LComment -> Bool
f LComment
x -> do
(SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> ReaderT RC (State SC) ())
-> (SC -> SC) -> ReaderT RC (State SC) ()
forall a b. (a -> b) -> a -> b
$ \SC
sc -> SC
sc {scCommentStream = CommentStream xs}
Maybe LComment -> ReaderT RC (State SC) (Maybe LComment)
forall a. a -> ReaderT RC (State SC) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe LComment -> ReaderT RC (State SC) (Maybe LComment))
-> Maybe LComment -> ReaderT RC (State SC) (Maybe LComment)
forall a b. (a -> b) -> a -> b
$ LComment -> Maybe LComment
forall a. a -> Maybe a
Just LComment
x
[LComment]
_ -> Maybe LComment -> ReaderT RC (State SC) (Maybe LComment)
forall a. a -> ReaderT RC (State SC) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe LComment
forall a. Maybe a
Nothing
getEnclosingComments :: R [LComment]
= do
isEnclosed <-
R (Maybe RealSrcSpan)
getEnclosingSpan R (Maybe RealSrcSpan)
-> (Maybe RealSrcSpan -> RealSrcSpan -> Bool)
-> R (RealSrcSpan -> Bool)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Just RealSrcSpan
enclSpan -> RealSrcSpan -> RealSrcSpan -> Bool
containsSpan RealSrcSpan
enclSpan
Maybe RealSrcSpan
Nothing -> Bool -> RealSrcSpan -> Bool
forall a b. a -> b -> a
const Bool
False
CommentStream cstream <- R $ gets scCommentStream
pure $ takeWhile (isEnclosed . getLoc) cstream
getEnclosingSpan :: R (Maybe RealSrcSpan)
getEnclosingSpan :: R (Maybe RealSrcSpan)
getEnclosingSpan = (RealSrcSpan -> Bool) -> R (Maybe RealSrcSpan)
getEnclosingSpanWhere (Bool -> RealSrcSpan -> Bool
forall a b. a -> b -> a
const Bool
True)
getEnclosingSpanWhere ::
(RealSrcSpan -> Bool) ->
R (Maybe RealSrcSpan)
getEnclosingSpanWhere :: (RealSrcSpan -> Bool) -> R (Maybe RealSrcSpan)
getEnclosingSpanWhere RealSrcSpan -> Bool
f =
(RealSrcSpan -> Bool) -> [RealSrcSpan] -> Maybe RealSrcSpan
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find RealSrcSpan -> Bool
f ([RealSrcSpan] -> Maybe RealSrcSpan)
-> R [RealSrcSpan] -> R (Maybe RealSrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReaderT RC (State SC) [RealSrcSpan] -> R [RealSrcSpan]
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> [RealSrcSpan]) -> ReaderT RC (State SC) [RealSrcSpan]
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) = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
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 = spn : rcEnclosingSpans rc
}
thisLineSpans :: R [RealSrcSpan]
thisLineSpans :: R [RealSrcSpan]
thisLineSpans = ReaderT RC (State SC) [RealSrcSpan] -> R [RealSrcSpan]
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> [RealSrcSpan]) -> ReaderT RC (State SC) [RealSrcSpan]
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 = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) () -> R ())
-> ((SC -> SC) -> ReaderT RC (State SC) ()) -> (SC -> SC) -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SC -> SC) -> ReaderT RC (State SC) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((SC -> SC) -> R ()) -> (SC -> SC) -> R ()
forall a b. (a -> b) -> a -> b
$ \SC
sc ->
SC
sc
{ scSpanMark = Just spnMark
}
getSpanMark :: R (Maybe SpanMark)
getSpanMark :: R (Maybe SpanMark)
getSpanMark = ReaderT RC (State SC) (Maybe SpanMark) -> R (Maybe SpanMark)
forall a. ReaderT RC (State SC) a -> R a
R ((SC -> Maybe SpanMark) -> ReaderT RC (State SC) (Maybe SpanMark)
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) = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\RC
i -> RC
i {rcCanUseBraces = True}) ReaderT RC (State SC) ()
r)
dontUseBraces :: R () -> R ()
dontUseBraces :: R () -> R ()
dontUseBraces (R ReaderT RC (State SC) ()
r) = ReaderT RC (State SC) () -> R ()
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> RC) -> ReaderT RC (State SC) () -> ReaderT RC (State SC) ()
forall a.
(RC -> RC) -> ReaderT RC (State SC) a -> ReaderT RC (State SC) a
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local (\RC
i -> RC
i {rcCanUseBraces = False}) ReaderT RC (State SC) ()
r)
canUseBraces :: R Bool
canUseBraces :: R Bool
canUseBraces = ReaderT RC (State SC) Bool -> R Bool
forall a. ReaderT RC (State SC) a -> R a
R ((RC -> Bool) -> ReaderT RC (State SC) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks RC -> Bool
rcCanUseBraces)
indentStep :: Int
indentStep :: Int
indentStep = Int
2
isExtensionEnabled :: Extension -> R Bool
isExtensionEnabled :: Extension -> R Bool
isExtensionEnabled Extension
ext = ReaderT RC (State SC) Bool -> R Bool
forall a. ReaderT RC (State SC) a -> R a
R (ReaderT RC (State SC) Bool -> R Bool)
-> ((RC -> Bool) -> ReaderT RC (State SC) Bool)
-> (RC -> Bool)
-> R Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RC -> Bool) -> ReaderT RC (State SC) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks ((RC -> Bool) -> R Bool) -> (RC -> Bool) -> R Bool
forall a b. (a -> b) -> a -> b
$ Extension -> EnumSet Extension -> Bool
forall a. Enum a => a -> EnumSet a -> Bool
EnumSet.member Extension
ext (EnumSet Extension -> Bool)
-> (RC -> EnumSet Extension) -> RC -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RC -> EnumSet Extension
rcExtensions