{-|
Description : Syntax tree types
-}
module Language.Haskell.Formatter.Process.Code
       (LocatableCode, CommentableCode, LocatableCommentableCode, tryZipCode,
        tryZipLocationsComments, dropComments, dropLocations)
       where
import qualified Language.Haskell.Formatter.Location as Location
import qualified Language.Haskell.Formatter.Process.Note as Note
import qualified Language.Haskell.Formatter.Result as Result
import qualified Language.Haskell.Formatter.Source as Source
import qualified Language.Haskell.Formatter.Toolkit.Visit as Visit

type LocatableCode = Source.Module Location.SrcSpanInfo

type CommentableCode = Source.Module Note.CommentNote

type LocatableCommentableCode = Source.Module Note.LocationCommentNote

tryZipCode ::
           (a -> b -> c) ->
             Source.Module a ->
               Source.Module b -> Result.Result (Source.Module c)
tryZipCode :: (a -> b -> c) -> Module a -> Module b -> Result (Module c)
tryZipCode a -> b -> c
merge Module a
left Module b
right
  = case Maybe (Module c)
maybeZipped of
        Maybe (Module c)
Nothing -> String -> Result (Module c)
forall a. String -> Result a
Result.fatalAssertionError String
message
          where message :: String
message = String
"The code notes could not be zipped."
        Just Module c
zipped -> Module c -> Result (Module c)
forall (m :: * -> *) a. Monad m => a -> m a
return Module c
zipped
  where maybeZipped :: Maybe (Module c)
maybeZipped
          = if Bool
isActualCodeSame then (a -> b -> c) -> Module a -> Module b -> Maybe (Module c)
forall (t :: * -> *) (f :: * -> *) a b c.
(Traversable t, Foldable f) =>
(a -> b -> c) -> t a -> f b -> Maybe (t c)
Visit.halfZipWith a -> b -> c
merge Module a
left Module b
right else
              Maybe (Module c)
forall a. Maybe a
Nothing
        isActualCodeSame :: Bool
isActualCodeSame = Module a
left Module a -> Module b -> Bool
forall (a :: * -> *) l1 l2.
(Annotated a, Eq (a ())) =>
a l1 -> a l2 -> Bool
Source.=~= Module b
right

tryZipLocationsComments ::
                        LocatableCode ->
                          CommentableCode ->
                            Result.Result LocatableCommentableCode
tryZipLocationsComments :: LocatableCode -> CommentableCode -> Result LocatableCommentableCode
tryZipLocationsComments = (SrcSpanInfo -> CommentNote -> LocationCommentNote)
-> LocatableCode
-> CommentableCode
-> Result LocatableCommentableCode
forall a b c.
(a -> b -> c) -> Module a -> Module b -> Result (Module c)
tryZipCode SrcSpanInfo -> CommentNote -> LocationCommentNote
Note.createLocationCommentNote

dropComments :: LocatableCommentableCode -> LocatableCode
dropComments :: LocatableCommentableCode -> LocatableCode
dropComments = (LocationCommentNote -> SrcSpanInfo)
-> LocatableCommentableCode -> LocatableCode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LocationCommentNote -> SrcSpanInfo
Note.locationNote

dropLocations :: LocatableCommentableCode -> CommentableCode
dropLocations :: LocatableCommentableCode -> CommentableCode
dropLocations = (LocationCommentNote -> CommentNote)
-> LocatableCommentableCode -> CommentableCode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LocationCommentNote -> CommentNote
Note.commentNote