Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module is currently under heavy development, and no promises are made about API stability. Use with care.
We welcome any feedback / contributions on this, as it is the main point of the library.
Synopsis
- type Transform = TransformT Identity
- newtype TransformT m a = TransformT {
- unTransformT :: RWST () [String] (Anns, Int) m a
- hoistTransform :: (forall x. m x -> n x) -> TransformT m a -> TransformT n a
- runTransform :: Anns -> Transform a -> (a, (Anns, Int), [String])
- runTransformT :: Anns -> TransformT m a -> m (a, (Anns, Int), [String])
- runTransformFrom :: Int -> Anns -> Transform a -> (a, (Anns, Int), [String])
- runTransformFromT :: Int -> Anns -> TransformT m a -> m (a, (Anns, Int), [String])
- logTr :: Monad m => String -> TransformT m ()
- logDataWithAnnsTr :: Monad m => Data a => String -> a -> TransformT m ()
- getAnnsT :: Monad m => TransformT m Anns
- putAnnsT :: Monad m => Anns -> TransformT m ()
- modifyAnnsT :: Monad m => (Anns -> Anns) -> TransformT m ()
- uniqueSrcSpanT :: Monad m => TransformT m SrcSpan
- cloneT :: (Data a, Monad m) => a -> TransformT m (a, [(SrcSpan, SrcSpan)])
- graftT :: (Data a, Monad m) => Anns -> a -> TransformT m a
- getEntryDPT :: (Data a, Monad m) => Located a -> TransformT m DeltaPos
- setEntryDPT :: Monad m => LocatedA a -> DeltaPos -> TransformT m ()
- transferEntryDPT :: Monad m => LocatedA a -> LocatedA b -> TransformT m (LocatedA b)
- setPrecedingLinesDeclT :: Monad m => LHsDecl GhcPs -> Int -> Int -> TransformT m ()
- setPrecedingLinesT :: Monad m => LocatedA a -> Int -> Int -> TransformT m ()
- addSimpleAnnT :: (Data a, Monad m) => Located a -> DeltaPos -> [(KeywordId, DeltaPos)] -> TransformT m ()
- addTrailingCommaT :: (Data a, Monad m) => Located a -> TransformT m ()
- removeTrailingCommaT :: (Data a, Monad m) => Located a -> TransformT m ()
- class Monad m => HasTransform m where
- class Data t => HasDecls t where
- hsDecls :: Monad m => t -> TransformT m [LHsDecl GhcPs]
- replaceDecls :: Monad m => t -> [LHsDecl GhcPs] -> TransformT m t
- hasDeclsSybTransform :: (Data t2, Monad m) => (forall t. HasDecls t => t -> m t) -> (LHsBind GhcPs -> m (LHsBind GhcPs)) -> t2 -> m t2
- hsDeclsGeneric :: (Data t, Monad m) => t -> TransformT m [LHsDecl GhcPs]
- hsDeclsPatBind :: Monad m => LHsBind GhcPs -> TransformT m [LHsDecl GhcPs]
- hsDeclsPatBindD :: Monad m => LHsDecl GhcPs -> TransformT m [LHsDecl GhcPs]
- replaceDeclsPatBind :: Monad m => LHsBind GhcPs -> [LHsDecl GhcPs] -> TransformT m (LHsBind GhcPs)
- replaceDeclsPatBindD :: Monad m => LHsDecl GhcPs -> [LHsDecl GhcPs] -> TransformT m (LHsDecl GhcPs)
- modifyDeclsT :: (HasDecls t, HasTransform m) => ([LHsDecl GhcPs] -> m [LHsDecl GhcPs]) -> t -> m t
- modifyValD :: forall m t. HasTransform m => SrcSpan -> Decl -> (PMatch -> [Decl] -> m ([Decl], Maybe t)) -> m (Decl, Maybe t)
- hsDeclsValBinds :: Monad m => HsLocalBinds GhcPs -> TransformT m [LHsDecl GhcPs]
- replaceDeclsValbinds :: Monad m => WithWhere -> HsLocalBinds GhcPs -> [LHsDecl GhcPs] -> TransformT m (HsLocalBinds GhcPs)
- data WithWhere
- noAnnSrcSpanDP :: Monoid ann => SrcSpan -> DeltaPos -> SrcSpanAnn' (EpAnn ann)
- noAnnSrcSpanDP0 :: Monoid ann => SrcSpan -> SrcSpanAnn' (EpAnn ann)
- noAnnSrcSpanDP1 :: Monoid ann => SrcSpan -> SrcSpanAnn' (EpAnn ann)
- noAnnSrcSpanDPn :: Monoid ann => SrcSpan -> Int -> SrcSpanAnn' (EpAnn ann)
- d0 :: EpaLocation
- d1 :: EpaLocation
- dn :: Int -> EpaLocation
- m0 :: AnchorOperation
- m1 :: AnchorOperation
- mn :: Int -> AnchorOperation
- addComma :: SrcSpanAnnA -> SrcSpanAnnA
- insertAt :: HasDecls ast => (LHsDecl GhcPs -> [LHsDecl GhcPs] -> [LHsDecl GhcPs]) -> ast -> LHsDecl GhcPs -> Transform ast
- insertAtStart :: HasDecls ast => ast -> LHsDecl GhcPs -> Transform ast
- insertAtEnd :: HasDecls ast => ast -> LHsDecl GhcPs -> Transform ast
- insertAfter :: HasDecls (LocatedA ast) => LocatedA old -> LocatedA ast -> LHsDecl GhcPs -> Transform (LocatedA ast)
- insertBefore :: HasDecls (LocatedA ast) => LocatedA old -> LocatedA ast -> LHsDecl GhcPs -> Transform (LocatedA ast)
- balanceComments :: Monad m => LHsDecl GhcPs -> LHsDecl GhcPs -> TransformT m (LHsDecl GhcPs, LHsDecl GhcPs)
- balanceCommentsList :: Monad m => [LHsDecl GhcPs] -> TransformT m [LHsDecl GhcPs]
- balanceCommentsList' :: Monad m => [LocatedA a] -> TransformT m [LocatedA a]
- balanceTrailingComments :: Monad m => (Data a, Data b) => Located a -> Located b -> TransformT m [(Comment, DeltaPos)]
- moveTrailingComments :: (Data a, Data b) => Located a -> Located b -> Transform ()
- anchorEof :: ParsedSource -> ParsedSource
- captureOrder :: [LocatedA b] -> AnnSortKey
- captureLineSpacing :: Monoid t => [LocatedAn t e] -> [GenLocated (SrcSpanAnn' (EpAnn t)) e]
- captureMatchLineSpacing :: LHsDecl GhcPs -> LHsDecl GhcPs
- captureTypeSigSpacing :: LHsDecl GhcPs -> LHsDecl GhcPs
- isUniqueSrcSpan :: SrcSpan -> Bool
- mergeAnns :: Anns -> Anns -> Anns
- mergeAnnList :: [Anns] -> Anns
- setPrecedingLinesDecl :: LHsDecl GhcPs -> Int -> Int -> Anns -> Anns
- setPrecedingLines :: LocatedA a -> Int -> Int -> Anns -> Anns
- getEntryDP :: Data a => Anns -> Located a -> DeltaPos
- setEntryDP :: LocatedA a -> DeltaPos -> Anns -> Anns
- setEntryDP' :: Monoid t => LocatedAn t a -> DeltaPos -> LocatedAn t a
- transferEntryDP :: (Monad m, Monoid t) => LocatedAn t a -> LocatedAn t b -> TransformT m (LocatedAn t b)
- transferEntryDP' :: Monad m => LHsDecl GhcPs -> LHsDecl GhcPs -> TransformT m (LHsDecl GhcPs)
- addTrailingComma :: Data a => Located a -> DeltaPos -> Anns -> Anns
- wrapSig :: LSig GhcPs -> LHsDecl GhcPs
- wrapDecl :: LHsBind GhcPs -> LHsDecl GhcPs
- decl2Sig :: LHsDecl GhcPs -> [LSig GhcPs]
- decl2Bind :: LHsDecl GhcPs -> [LHsBind GhcPs]
- deltaAnchor :: Anchor -> RealSrcSpan -> Anchor
The Transform Monad
type Transform = TransformT Identity Source #
Monad type for updating the AST and managing the annotations at the same time. The W state is used to generate logging information if required.
newtype TransformT m a Source #
Monad transformer version of Transform
monad
TransformT | |
|
Instances
hoistTransform :: (forall x. m x -> n x) -> TransformT m a -> TransformT n a Source #
Change inner monad of TransformT
.
runTransformT :: Anns -> TransformT m a -> m (a, (Anns, Int), [String]) Source #
runTransformFromT :: Int -> Anns -> TransformT m a -> m (a, (Anns, Int), [String]) Source #
Run a monad transformer stack for the TransformT
monad transformer
Transform monad operations
logDataWithAnnsTr :: Monad m => Data a => String -> a -> TransformT m () Source #
Log a representation of the given AST with annotations to the output of the Monad
getAnnsT :: Monad m => TransformT m Anns Source #
Access the Anns
being modified in this transformation
modifyAnnsT :: Monad m => (Anns -> Anns) -> TransformT m () Source #
Change the stored Anns
uniqueSrcSpanT :: Monad m => TransformT m SrcSpan Source #
cloneT :: (Data a, Monad m) => a -> TransformT m (a, [(SrcSpan, SrcSpan)]) Source #
Make a copy of an AST element, replacing the existing SrcSpans with new ones, and duplicating the matching annotations.
graftT :: (Data a, Monad m) => Anns -> a -> TransformT m a Source #
Slightly more general form of cloneT
getEntryDPT :: (Data a, Monad m) => Located a -> TransformT m DeltaPos Source #
Transform
monad version of getEntryDP
setEntryDPT :: Monad m => LocatedA a -> DeltaPos -> TransformT m () Source #
Transform
monad version of getEntryDP
transferEntryDPT :: Monad m => LocatedA a -> LocatedA b -> TransformT m (LocatedA b) Source #
Transform
monad version of transferEntryDP
setPrecedingLinesDeclT :: Monad m => LHsDecl GhcPs -> Int -> Int -> TransformT m () Source #
Transform
monad version of setPrecedingLinesDecl
setPrecedingLinesT :: Monad m => LocatedA a -> Int -> Int -> TransformT m () Source #
Transform
monad version of setPrecedingLines
addSimpleAnnT :: (Data a, Monad m) => Located a -> DeltaPos -> [(KeywordId, DeltaPos)] -> TransformT m () Source #
Create a simple Annotation
without comments, and attach it to the first
parameter.
addTrailingCommaT :: (Data a, Monad m) => Located a -> TransformT m () Source #
Add a trailing comma annotation, unless there is already one
removeTrailingCommaT :: (Data a, Monad m) => Located a -> TransformT m () Source #
Remove a trailing comma annotation, if there is one one
Managing declarations, in Transform monad
class Monad m => HasTransform m where Source #
Used to integrate a Transform
into other Monad stacks
Instances
Monad m => HasTransform (TransformT m) Source # | |
Defined in Language.Haskell.GHC.ExactPrint.Transform liftT :: Transform a -> TransformT m a Source # |
class Data t => HasDecls t where Source #
Provide a means to get and process the immediate child declartions of a given AST element.
hsDecls :: Monad m => t -> TransformT m [LHsDecl GhcPs] Source #
Return the HsDecl
s that are directly enclosed in the
given syntax phrase. They are always returned in the wrapped HsDecl
form, even if orginating in local decls. This is safe, as annotations
never attach to the wrapper, only to the wrapped item.
replaceDecls :: Monad m => t -> [LHsDecl GhcPs] -> TransformT m t Source #
Replace the directly enclosed decl list by the given
decl list. Runs in the Transform
monad to be able to update list order
annotations, and rebalance comments and other layout changes as needed.
For example, a call on replaceDecls for a wrapped FunBind
having no
where clause will convert
-- |This is a function foo = x -- comment1
in to
-- |This is a function foo = x -- comment1 where nn = 2
Instances
:: (Data t2, Monad m) | |
=> (forall t. HasDecls t => t -> m t) | Worker function for the general case |
-> (LHsBind GhcPs -> m (LHsBind GhcPs)) | Worker function for FunBind/PatBind |
-> t2 | Item to be updated |
-> m t2 |
Do a transformation on an AST fragment by providing a function to process
the general case and one specific for a LHsBind
. This is required
because a FunBind
may have multiple Match
items, so we cannot
gurantee that replaceDecls
after hsDecls
is idempotent.
hsDeclsGeneric :: (Data t, Monad m) => t -> TransformT m [LHsDecl GhcPs] Source #
hsDeclsPatBind :: Monad m => LHsBind GhcPs -> TransformT m [LHsDecl GhcPs] Source #
Extract the immediate declarations for a PatBind
. This
cannot be a member of HasDecls
because a FunBind
is not idempotent
for hsDecls
/ replaceDecls
. hsDeclsPatBind
/ replaceDeclsPatBind
is
idempotent.
hsDeclsPatBindD :: Monad m => LHsDecl GhcPs -> TransformT m [LHsDecl GhcPs] Source #
Extract the immediate declarations for a PatBind
wrapped in a ValD
. This
cannot be a member of HasDecls
because a FunBind
is not idempotent
for hsDecls
/ replaceDecls
. hsDeclsPatBindD
/ replaceDeclsPatBindD
is
idempotent.
replaceDeclsPatBind :: Monad m => LHsBind GhcPs -> [LHsDecl GhcPs] -> TransformT m (LHsBind GhcPs) Source #
Replace the immediate declarations for a PatBind
. This
cannot be a member of HasDecls
because a FunBind
is not idempotent
for hsDecls
/ replaceDecls
. hsDeclsPatBind
/ replaceDeclsPatBind
is
idempotent.
replaceDeclsPatBindD :: Monad m => LHsDecl GhcPs -> [LHsDecl GhcPs] -> TransformT m (LHsDecl GhcPs) Source #
Replace the immediate declarations for a PatBind
wrapped in a ValD
. This
cannot be a member of HasDecls
because a FunBind
is not idempotent
for hsDecls
/ replaceDecls
. hsDeclsPatBindD
/ replaceDeclsPatBindD
is
idempotent.
modifyDeclsT :: (HasDecls t, HasTransform m) => ([LHsDecl GhcPs] -> m [LHsDecl GhcPs]) -> t -> m t Source #
Apply a transformation to the decls contained in t
modifyValD :: forall m t. HasTransform m => SrcSpan -> Decl -> (PMatch -> [Decl] -> m ([Decl], Maybe t)) -> m (Decl, Maybe t) Source #
Utility, does not manage layout
hsDeclsValBinds :: Monad m => HsLocalBinds GhcPs -> TransformT m [LHsDecl GhcPs] Source #
replaceDeclsValbinds :: Monad m => WithWhere -> HsLocalBinds GhcPs -> [LHsDecl GhcPs] -> TransformT m (HsLocalBinds GhcPs) Source #
Utility function for returning decls to HsLocalBinds
. Use with
care, as this does not manage the declaration order, the
ordering should be done by the calling function from the HsLocalBinds
context in the AST.
New gen functions
noAnnSrcSpanDP :: Monoid ann => SrcSpan -> DeltaPos -> SrcSpanAnn' (EpAnn ann) Source #
Create a SrcSpanAnn
with a MovedAnchor
operation using the
given DeltaPos
.
noAnnSrcSpanDP0 :: Monoid ann => SrcSpan -> SrcSpanAnn' (EpAnn ann) Source #
noAnnSrcSpanDP1 :: Monoid ann => SrcSpan -> SrcSpanAnn' (EpAnn ann) Source #
noAnnSrcSpanDPn :: Monoid ann => SrcSpan -> Int -> SrcSpanAnn' (EpAnn ann) Source #
d0 :: EpaLocation Source #
d1 :: EpaLocation Source #
dn :: Int -> EpaLocation Source #
m0 :: AnchorOperation Source #
m1 :: AnchorOperation Source #
mn :: Int -> AnchorOperation Source #
addComma :: SrcSpanAnnA -> SrcSpanAnnA Source #
Managing lists, Transform monad
insertAt :: HasDecls ast => (LHsDecl GhcPs -> [LHsDecl GhcPs] -> [LHsDecl GhcPs]) -> ast -> LHsDecl GhcPs -> Transform ast Source #
Insert a declaration into an AST element having sub-declarations
(HasDecls
) according to the given location function.
insertAtStart :: HasDecls ast => ast -> LHsDecl GhcPs -> Transform ast Source #
Insert a declaration at the beginning or end of the subdecls of the given AST item
insertAtEnd :: HasDecls ast => ast -> LHsDecl GhcPs -> Transform ast Source #
Insert a declaration at the beginning or end of the subdecls of the given AST item
insertAfter :: HasDecls (LocatedA ast) => LocatedA old -> LocatedA ast -> LHsDecl GhcPs -> Transform (LocatedA ast) Source #
Insert a declaration at a specific location in the subdecls of the given AST item
insertBefore :: HasDecls (LocatedA ast) => LocatedA old -> LocatedA ast -> LHsDecl GhcPs -> Transform (LocatedA ast) Source #
Insert a declaration at a specific location in the subdecls of the given AST item
Low level operations used in HasDecls
balanceComments :: Monad m => LHsDecl GhcPs -> LHsDecl GhcPs -> TransformT m (LHsDecl GhcPs, LHsDecl GhcPs) Source #
The relatavise phase puts all comments appearing between the end of one AST
item and the beginning of the next as annPriorComments
for the second one.
This function takes two adjacent AST items and moves any annPriorComments
from the second one to the annFollowingComments
of the first if they belong
to it instead. This is typically required before deleting or duplicating
either of the AST elements.
balanceCommentsList :: Monad m => [LHsDecl GhcPs] -> TransformT m [LHsDecl GhcPs] Source #
balanceCommentsList' :: Monad m => [LocatedA a] -> TransformT m [LocatedA a] Source #
balanceTrailingComments :: Monad m => (Data a, Data b) => Located a -> Located b -> TransformT m [(Comment, DeltaPos)] Source #
After moving an AST element, make sure any comments that may belong with the following element in fact do. Of necessity this is a heuristic process, to be tuned later. Possibly a variant should be provided with a passed-in decision function.
moveTrailingComments :: (Data a, Data b) => Located a -> Located b -> Transform () Source #
Move any annFollowingComments
values from the Annotation
associated to
the first parameter to that of the second.
anchorEof :: ParsedSource -> ParsedSource Source #
Managing lists, pure functions
captureOrder :: [LocatedA b] -> AnnSortKey Source #
If a list has been re-ordered or had items added, capture the new order in
the appropriate AnnSortKey
attached to the Annotation
for the list.
captureLineSpacing :: Monoid t => [LocatedAn t e] -> [GenLocated (SrcSpanAnn' (EpAnn t)) e] Source #
Operations
isUniqueSrcSpan :: SrcSpan -> Bool Source #
Test whether a given SrcSpan
was generated by uniqueSrcSpanT
Pure functions
mergeAnnList :: [Anns] -> Anns Source #
Combine a list of annotations
setPrecedingLinesDecl :: LHsDecl GhcPs -> Int -> Int -> Anns -> Anns Source #
Unwrap a HsDecl and call setPrecedingLines on it ++AZ++ TODO: get rid of this, it is a synonym only
setPrecedingLines :: LocatedA a -> Int -> Int -> Anns -> Anns Source #
Adjust the entry annotations to provide an n
line preceding gap
transferEntryDP :: (Monad m, Monoid t) => LocatedAn t a -> LocatedAn t b -> TransformT m (LocatedAn t b) Source #
Take the annEntryDelta associated with the first item and associate it with the second. Also transfer any comments occuring before it.
transferEntryDP' :: Monad m => LHsDecl GhcPs -> LHsDecl GhcPs -> TransformT m (LHsDecl GhcPs) Source #
Take the annEntryDelta associated with the first item and associate it with the second. Also transfer any comments occuring before it. TODO: call transferEntryDP, and use pushDeclDP
decl2Sig :: LHsDecl GhcPs -> [LSig GhcPs] Source #
Pure function to convert a LSig
to a LHsBind
. This does
nothing to any annotations that may be attached to either of the elements.
It is used as a utility function in replaceDecls
decl2Bind :: LHsDecl GhcPs -> [LHsBind GhcPs] Source #
Pure function to convert a LHsDecl
to a LHsBind
. This does
nothing to any annotations that may be attached to either of the elements.
It is used as a utility function in replaceDecls
deltaAnchor :: Anchor -> RealSrcSpan -> Anchor Source #
Take an anchor and a preceding location, and generate an
equivalent one with a MovedAnchor
delta.