    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE RecordWildCards #-}
    {-# LANGUAGE TypeFamilies #-}

    -- | Manipulations on import lists.
    module Ormolu.Imports
      ( normalizeImports,
      )
    where

    import Data.Bifunctor
    import Data.Char (isAlphaNum)
    import Data.Function (on)
    import Data.List (foldl', nubBy, sortBy, sortOn)
    import Data.Map.Strict (Map)
    import qualified Data.Map.Strict as M
    import GHC.Data.FastString
    import GHC.Hs
    import GHC.Hs.ImpExp as GHC
    import GHC.Types.Name.Reader
    import GHC.Types.PkgQual
    import GHC.Types.SourceText
    import GHC.Types.SrcLoc
    import GHC.Unit.Module.Name
    import GHC.Unit.Types
    import Ormolu.Utils (notImplemented, showOutputable)

    -- | Sort and normalize imports.
    normalizeImports :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
    normalizeImports :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
    normalizeImports =
      ((ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))
     -> GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    forall a b. (a -> b) -> [a] -> [b]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    forall a b. (a, b) -> b
    snd
        ([(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
     -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)])
    -> ([GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
        -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))])
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
    forall k a. Map k a -> [(k, a)]
    M.toAscList
        (Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs))
     -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))])
    -> ([GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
        -> Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs)))
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (GenLocated SrcSpanAnnA (ImportDecl GhcPs)
     -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
     -> GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
    -> Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
    M.fromListWith GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    LImportDecl GhcPs -> LImportDecl GhcPs -> LImportDecl GhcPs
    combineImports
        ([(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
     -> Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs)))
    -> ([GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
        -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))])
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    -> Map ImportId (GenLocated SrcSpanAnnA (ImportDecl GhcPs))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (GenLocated SrcSpanAnnA (ImportDecl GhcPs)
     -> (ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs)))
    -> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
    -> [(ImportId, GenLocated SrcSpanAnnA (ImportDecl GhcPs))]
    forall a b. (a -> b) -> [a] -> [b]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (\GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    x -> (LImportDecl GhcPs -> ImportId
    importId GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    LImportDecl GhcPs
    x, LImportDecl GhcPs -> LImportDecl GhcPs
    g GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    LImportDecl GhcPs
    x))
      where
        g :: LImportDecl GhcPs -> LImportDecl GhcPs
        g :: LImportDecl GhcPs -> LImportDecl GhcPs
    g (L SrcSpanAnnA
    l ImportDecl {Bool
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    Maybe (XRec GhcPs ModuleName)
    IsBootInterface
    SourceText
    XCImportDecl GhcPs
    XRec GhcPs ModuleName
    ImportDeclPkgQual GhcPs
    ImportDeclQualifiedStyle
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclExt :: forall pass. ImportDecl pass -> XCImportDecl pass
    ideclSourceSrc :: forall pass. ImportDecl pass -> SourceText
    ideclName :: forall pass. ImportDecl pass -> XRec pass ModuleName
    ideclPkgQual :: forall pass. ImportDecl pass -> ImportDeclPkgQual pass
    ideclSource :: forall pass. ImportDecl pass -> IsBootInterface
    ideclSafe :: forall pass. ImportDecl pass -> Bool
    ideclQualified :: forall pass. ImportDecl pass -> ImportDeclQualifiedStyle
    ideclImplicit :: forall pass. ImportDecl pass -> Bool
    ideclAs :: forall pass. ImportDecl pass -> Maybe (XRec pass ModuleName)
    ideclHiding :: forall pass. ImportDecl pass -> Maybe (Bool, XRec pass [LIE pass])
    ..}) =
          SrcSpanAnnA
    -> ImportDecl GhcPs -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    forall l e. l -> e -> GenLocated l e
    L
            SrcSpanAnnA
    l
            ImportDecl
              { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclHiding = (GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]
     -> GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> (Bool,
        GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> (Bool,
        GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    forall b c a. (b -> c) -> (a, b) -> (a, c)
    forall (p :: * -> * -> *) b c a.
    Bifunctor p =>
    (b -> c) -> p a b -> p a c
    second (([GenLocated SrcSpanAnnA (IE GhcPs)]
     -> [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall a b.
    (a -> b) -> GenLocated SrcSpanAnnL a -> GenLocated SrcSpanAnnL b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    [LIE GhcPs] -> [LIE GhcPs]
    normalizeLies) ((Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
     -> (Bool,
         GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]))
    -> Maybe
         (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> Maybe
         (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe
      (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclHiding,
                Bool
    Maybe (XRec GhcPs ModuleName)
    IsBootInterface
    SourceText
    XCImportDecl GhcPs
    XRec GhcPs ModuleName
    ImportDeclPkgQual GhcPs
    ImportDeclQualifiedStyle
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ..
              }

    -- | Combine two import declarations. It should be assumed that 'ImportId's
    -- are equal.
    combineImports ::
      LImportDecl GhcPs ->
      LImportDecl GhcPs ->
      LImportDecl GhcPs
    combineImports :: LImportDecl GhcPs -> LImportDecl GhcPs -> LImportDecl GhcPs
    combineImports (L SrcSpanAnnA
    lx ImportDecl {Bool
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    Maybe (XRec GhcPs ModuleName)
    IsBootInterface
    SourceText
    XCImportDecl GhcPs
    XRec GhcPs ModuleName
    ImportDeclPkgQual GhcPs
    ImportDeclQualifiedStyle
    ideclExt :: forall pass. ImportDecl pass -> XCImportDecl pass
    ideclSourceSrc :: forall pass. ImportDecl pass -> SourceText
    ideclName :: forall pass. ImportDecl pass -> XRec pass ModuleName
    ideclPkgQual :: forall pass. ImportDecl pass -> ImportDeclPkgQual pass
    ideclSource :: forall pass. ImportDecl pass -> IsBootInterface
    ideclSafe :: forall pass. ImportDecl pass -> Bool
    ideclQualified :: forall pass. ImportDecl pass -> ImportDeclQualifiedStyle
    ideclImplicit :: forall pass. ImportDecl pass -> Bool
    ideclAs :: forall pass. ImportDecl pass -> Maybe (XRec pass ModuleName)
    ideclHiding :: forall pass. ImportDecl pass -> Maybe (Bool, XRec pass [LIE pass])
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ..}) (L SrcSpanAnnA
    _ ImportDecl GhcPs
    y) =
      SrcSpanAnnA
    -> ImportDecl GhcPs -> GenLocated SrcSpanAnnA (ImportDecl GhcPs)
    forall l e. l -> e -> GenLocated l e
    L
        SrcSpanAnnA
    lx
        ImportDecl
          { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclHiding = case (Maybe
      (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclHiding, ImportDecl GhcPs -> Maybe (Bool, XRec GhcPs [LIE GhcPs])
    forall pass. ImportDecl pass -> Maybe (Bool, XRec pass [LIE pass])
    GHC.ideclHiding ImportDecl GhcPs
    y) of
              (Just (Bool
    hiding, L SrcSpanAnnL
    l' [GenLocated SrcSpanAnnA (IE GhcPs)]
    xs), Just (Bool
    _, L SrcSpanAnnL
    _ [GenLocated SrcSpanAnnA (IE GhcPs)]
    ys)) ->
                (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> Maybe
         (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    forall a. a -> Maybe a
    Just (Bool
    hiding, (SrcSpanAnnL
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnL
    l' ([LIE GhcPs] -> [LIE GhcPs]
    normalizeLies ([GenLocated SrcSpanAnnA (IE GhcPs)]
    xs [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall a. [a] -> [a] -> [a]
    ++ [GenLocated SrcSpanAnnA (IE GhcPs)]
    ys))))
              (Maybe
       (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]),
     Maybe
       (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)]))
    _ -> Maybe
      (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    forall a. Maybe a
    Nothing,
            Bool
    Maybe (XRec GhcPs ModuleName)
    IsBootInterface
    SourceText
    XCImportDecl GhcPs
    XRec GhcPs ModuleName
    ImportDeclPkgQual GhcPs
    ImportDeclQualifiedStyle
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ..
          }

    -- | Import id, a collection of all things that justify having a separate
    -- import entry. This is used for merging of imports. If two imports have
    -- the same 'ImportId' they can be merged.
    data ImportId = ImportId
      { ImportId -> Bool
    importIsPrelude :: Bool,
        ImportId -> Maybe LexicalFastString
    importPkgQual :: Maybe LexicalFastString,
        ImportId -> ModuleName
    importIdName :: ModuleName,
        ImportId -> IsBootInterface
    importSource :: IsBootInterface,
        ImportId -> Bool
    importSafe :: Bool,
        ImportId -> Bool
    importQualified :: Bool,
        ImportId -> Bool
    importImplicit :: Bool,
        ImportId -> Maybe ModuleName
    importAs :: Maybe ModuleName,
        ImportId -> Maybe Bool
    importHiding :: Maybe Bool
      }
      deriving (ImportId -> ImportId -> Bool
    (ImportId -> ImportId -> Bool)
    -> (ImportId -> ImportId -> Bool) -> Eq ImportId
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: ImportId -> ImportId -> Bool
    == :: ImportId -> ImportId -> Bool
    $c/= :: ImportId -> ImportId -> Bool
    /= :: ImportId -> ImportId -> Bool
    Eq, Eq ImportId
    Eq ImportId
    -> (ImportId -> ImportId -> Ordering)
    -> (ImportId -> ImportId -> Bool)
    -> (ImportId -> ImportId -> Bool)
    -> (ImportId -> ImportId -> Bool)
    -> (ImportId -> ImportId -> Bool)
    -> (ImportId -> ImportId -> ImportId)
    -> (ImportId -> ImportId -> ImportId)
    -> Ord ImportId
    ImportId -> ImportId -> Bool
    ImportId -> ImportId -> Ordering
    ImportId -> ImportId -> ImportId
    forall a.
    Eq a
    -> (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: ImportId -> ImportId -> Ordering
    compare :: ImportId -> ImportId -> Ordering
    $c< :: ImportId -> ImportId -> Bool
    < :: ImportId -> ImportId -> Bool
    $c<= :: ImportId -> ImportId -> Bool
    <= :: ImportId -> ImportId -> Bool
    $c> :: ImportId -> ImportId -> Bool
    > :: ImportId -> ImportId -> Bool
    $c>= :: ImportId -> ImportId -> Bool
    >= :: ImportId -> ImportId -> Bool
    $cmax :: ImportId -> ImportId -> ImportId
    max :: ImportId -> ImportId -> ImportId
    $cmin :: ImportId -> ImportId -> ImportId
    min :: ImportId -> ImportId -> ImportId
    Ord)

    -- | Obtain an 'ImportId' for a given import.
    importId :: LImportDecl GhcPs -> ImportId
    importId :: LImportDecl GhcPs -> ImportId
    importId (L SrcSpanAnnA
    _ ImportDecl {Bool
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    Maybe (XRec GhcPs ModuleName)
    IsBootInterface
    SourceText
    XCImportDecl GhcPs
    XRec GhcPs ModuleName
    ImportDeclPkgQual GhcPs
    ImportDeclQualifiedStyle
    ideclExt :: forall pass. ImportDecl pass -> XCImportDecl pass
    ideclSourceSrc :: forall pass. ImportDecl pass -> SourceText
    ideclName :: forall pass. ImportDecl pass -> XRec pass ModuleName
    ideclPkgQual :: forall pass. ImportDecl pass -> ImportDeclPkgQual pass
    ideclSource :: forall pass. ImportDecl pass -> IsBootInterface
    ideclSafe :: forall pass. ImportDecl pass -> Bool
    ideclQualified :: forall pass. ImportDecl pass -> ImportDeclQualifiedStyle
    ideclImplicit :: forall pass. ImportDecl pass -> Bool
    ideclAs :: forall pass. ImportDecl pass -> Maybe (XRec pass ModuleName)
    ideclHiding :: forall pass. ImportDecl pass -> Maybe (Bool, XRec pass [LIE pass])
    ideclExt :: XCImportDecl GhcPs
    ideclSourceSrc :: SourceText
    ideclName :: XRec GhcPs ModuleName
    ideclPkgQual :: ImportDeclPkgQual GhcPs
    ideclSource :: IsBootInterface
    ideclSafe :: Bool
    ideclQualified :: ImportDeclQualifiedStyle
    ideclImplicit :: Bool
    ideclAs :: Maybe (XRec GhcPs ModuleName)
    ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ..}) =
      ImportId
        { importIsPrelude :: Bool
    importIsPrelude = Bool
    isPrelude,
          importIdName :: ModuleName
    importIdName = ModuleName
    moduleName,
          importPkgQual :: Maybe LexicalFastString
    importPkgQual = RawPkgQual -> Maybe LexicalFastString
    rawPkgQualToLFS RawPkgQual
    ImportDeclPkgQual GhcPs
    ideclPkgQual,
          importSource :: IsBootInterface
    importSource = IsBootInterface
    ideclSource,
          importSafe :: Bool
    importSafe = Bool
    ideclSafe,
          importQualified :: Bool
    importQualified = case ImportDeclQualifiedStyle
    ideclQualified of
            ImportDeclQualifiedStyle
    QualifiedPre -> Bool
    True
            ImportDeclQualifiedStyle
    QualifiedPost -> Bool
    True
            ImportDeclQualifiedStyle
    NotQualified -> Bool
    False,
          importImplicit :: Bool
    importImplicit = Bool
    ideclImplicit,
          importAs :: Maybe ModuleName
    importAs = GenLocated SrcSpanAnnA ModuleName -> ModuleName
    forall l e. GenLocated l e -> e
    unLoc (GenLocated SrcSpanAnnA ModuleName -> ModuleName)
    -> Maybe (GenLocated SrcSpanAnnA ModuleName) -> Maybe ModuleName
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe (GenLocated SrcSpanAnnA ModuleName)
    Maybe (XRec GhcPs ModuleName)
    ideclAs,
          importHiding :: Maybe Bool
    importHiding = (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> Bool
    forall a b. (a, b) -> a
    fst ((Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
     -> Bool)
    -> Maybe
         (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> Maybe Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Maybe
      (Bool, GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (IE GhcPs)])
    Maybe (Bool, XRec GhcPs [LIE GhcPs])
    ideclHiding
        }
      where
        isPrelude :: Bool
    isPrelude = ModuleName -> String
    moduleNameString ModuleName
    moduleName String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    == String
    "Prelude"
        moduleName :: ModuleName
    moduleName = GenLocated SrcSpanAnnA ModuleName -> ModuleName
    forall l e. GenLocated l e -> e
    unLoc GenLocated SrcSpanAnnA ModuleName
    XRec GhcPs ModuleName
    ideclName
        rawPkgQualToLFS :: RawPkgQual -> Maybe LexicalFastString
    rawPkgQualToLFS = \case
          RawPkgQual StringLiteral
    fs -> LexicalFastString -> Maybe LexicalFastString
    forall a. a -> Maybe a
    Just (LexicalFastString -> Maybe LexicalFastString)
    -> (StringLiteral -> LexicalFastString)
    -> StringLiteral
    -> Maybe LexicalFastString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . FastString -> LexicalFastString
    LexicalFastString (FastString -> LexicalFastString)
    -> (StringLiteral -> FastString)
    -> StringLiteral
    -> LexicalFastString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . StringLiteral -> FastString
    sl_fs (StringLiteral -> Maybe LexicalFastString)
    -> StringLiteral -> Maybe LexicalFastString
    forall a b. (a -> b) -> a -> b
    $ StringLiteral
    fs
          RawPkgQual
    NoRawPkgQual -> Maybe LexicalFastString
    forall a. Maybe a
    Nothing

    -- | Normalize a collection of import\/export items.
    normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
    normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
    normalizeLies = (GenLocated SrcSpanAnnA (IE GhcPs) -> IEWrappedNameOrd)
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall b a. Ord b => (a -> b) -> [a] -> [a]
    sortOn (IE GhcPs -> IEWrappedNameOrd
    getIewn (IE GhcPs -> IEWrappedNameOrd)
    -> (GenLocated SrcSpanAnnA (IE GhcPs) -> IE GhcPs)
    -> GenLocated SrcSpanAnnA (IE GhcPs)
    -> IEWrappedNameOrd
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (IE GhcPs) -> IE GhcPs
    forall l e. GenLocated l e -> e
    unLoc) ([GenLocated SrcSpanAnnA (IE GhcPs)]
     -> [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> ([GenLocated SrcSpanAnnA (IE GhcPs)]
        -> [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall k a. Map k a -> [a]
    M.elems (Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
     -> [GenLocated SrcSpanAnnA (IE GhcPs)])
    -> ([GenLocated SrcSpanAnnA (IE GhcPs)]
        -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs)))
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
     -> GenLocated SrcSpanAnnA (IE GhcPs)
     -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs)))
    -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    -> [GenLocated SrcSpanAnnA (IE GhcPs)]
    -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    -> GenLocated SrcSpanAnnA (IE GhcPs)
    -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    Map IEWrappedNameOrd (LIE GhcPs)
    -> LIE GhcPs -> Map IEWrappedNameOrd (LIE GhcPs)
    combine Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    forall k a. Map k a
    M.empty
      where
        combine ::
          Map IEWrappedNameOrd (LIE GhcPs) ->
          LIE GhcPs ->
          Map IEWrappedNameOrd (LIE GhcPs)
        combine :: Map IEWrappedNameOrd (LIE GhcPs)
    -> LIE GhcPs -> Map IEWrappedNameOrd (LIE GhcPs)
    combine Map IEWrappedNameOrd (LIE GhcPs)
    m (L SrcSpanAnnA
    new_l IE GhcPs
    new) =
          let wname :: IEWrappedNameOrd
    wname = IE GhcPs -> IEWrappedNameOrd
    getIewn IE GhcPs
    new
              normalizeWNames :: [LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    normalizeWNames =
                (LIEWrappedName RdrName -> LIEWrappedName RdrName -> Bool)
    -> [LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    forall a. (a -> a -> Bool) -> [a] -> [a]
    nubBy (\LIEWrappedName RdrName
    x LIEWrappedName RdrName
    y -> LIEWrappedName RdrName -> LIEWrappedName RdrName -> Ordering
    compareLIewn LIEWrappedName RdrName
    x LIEWrappedName RdrName
    y Ordering -> Ordering -> Bool
    forall a. Eq a => a -> a -> Bool
    == Ordering
    EQ) ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName])
    -> ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName])
    -> [LIEWrappedName RdrName]
    -> [LIEWrappedName RdrName]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (LIEWrappedName RdrName -> LIEWrappedName RdrName -> Ordering)
    -> [LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    forall a. (a -> a -> Ordering) -> [a] -> [a]
    sortBy LIEWrappedName RdrName -> LIEWrappedName RdrName -> Ordering
    compareLIewn
              alter :: Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    alter = \case
                Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    Nothing -> GenLocated SrcSpanAnnA (IE GhcPs)
    -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    forall a. a -> Maybe a
    Just (GenLocated SrcSpanAnnA (IE GhcPs)
     -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs)))
    -> (IE GhcPs -> GenLocated SrcSpanAnnA (IE GhcPs))
    -> IE GhcPs
    -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . SrcSpanAnnA -> IE GhcPs -> GenLocated SrcSpanAnnA (IE GhcPs)
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnA
    new_l (IE GhcPs -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs)))
    -> IE GhcPs -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    forall a b. (a -> b) -> a -> b
    $
                  case IE GhcPs
    new of
                    IEThingWith XIEThingWith GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n IEWildcard
    wildcard [LIEWrappedName (IdP GhcPs)]
    g ->
                      XIEThingWith GhcPs
    -> LIEWrappedName (IdP GhcPs)
    -> IEWildcard
    -> [LIEWrappedName (IdP GhcPs)]
    -> IE GhcPs
    forall pass.
    XIEThingWith pass
    -> LIEWrappedName (IdP pass)
    -> IEWildcard
    -> [LIEWrappedName (IdP pass)]
    -> IE pass
    IEThingWith XIEThingWith GhcPs
    EpAnn [AddEpAnn]
    forall ann. EpAnn ann
    EpAnnNotUsed LIEWrappedName (IdP GhcPs)
    n IEWildcard
    wildcard ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    normalizeWNames [LIEWrappedName (IdP GhcPs)]
    [LIEWrappedName RdrName]
    g)
                    IE GhcPs
    other -> IE GhcPs
    other
                Just GenLocated SrcSpanAnnA (IE GhcPs)
    old ->
                  let f :: IE GhcPs -> IE GhcPs
    f = \case
                        IEVar XIEVar GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n -> XIEVar GhcPs -> LIEWrappedName (IdP GhcPs) -> IE GhcPs
    forall pass. XIEVar pass -> LIEWrappedName (IdP pass) -> IE pass
    IEVar XIEVar GhcPs
    NoExtField
    NoExtField LIEWrappedName (IdP GhcPs)
    n
                        IEThingAbs XIEThingAbs GhcPs
    _ LIEWrappedName (IdP GhcPs)
    _ -> IE GhcPs
    new
                        IEThingAll XIEThingAll GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n -> XIEThingAll GhcPs -> LIEWrappedName (IdP GhcPs) -> IE GhcPs
    forall pass.
    XIEThingAll pass -> LIEWrappedName (IdP pass) -> IE pass
    IEThingAll XIEThingAll GhcPs
    EpAnn [AddEpAnn]
    forall ann. EpAnn ann
    EpAnnNotUsed LIEWrappedName (IdP GhcPs)
    n
                        IEThingWith XIEThingWith GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n IEWildcard
    wildcard [LIEWrappedName (IdP GhcPs)]
    g ->
                          case IE GhcPs
    new of
                            IEVar XIEVar GhcPs
    NoExtField
    NoExtField LIEWrappedName (IdP GhcPs)
    _ ->
                              String -> IE GhcPs
    forall a. HasCallStack => String -> a
    error String
    "Ormolu.Imports broken presupposition"
                            IEThingAbs XIEThingAbs GhcPs
    _ LIEWrappedName (IdP GhcPs)
    _ ->
                              XIEThingWith GhcPs
    -> LIEWrappedName (IdP GhcPs)
    -> IEWildcard
    -> [LIEWrappedName (IdP GhcPs)]
    -> IE GhcPs
    forall pass.
    XIEThingWith pass
    -> LIEWrappedName (IdP pass)
    -> IEWildcard
    -> [LIEWrappedName (IdP pass)]
    -> IE pass
    IEThingWith XIEThingWith GhcPs
    EpAnn [AddEpAnn]
    forall ann. EpAnn ann
    EpAnnNotUsed LIEWrappedName (IdP GhcPs)
    n IEWildcard
    wildcard [LIEWrappedName (IdP GhcPs)]
    g
                            IEThingAll XIEThingAll GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n' ->
                              XIEThingAll GhcPs -> LIEWrappedName (IdP GhcPs) -> IE GhcPs
    forall pass.
    XIEThingAll pass -> LIEWrappedName (IdP pass) -> IE pass
    IEThingAll XIEThingAll GhcPs
    EpAnn [AddEpAnn]
    forall ann. EpAnn ann
    EpAnnNotUsed LIEWrappedName (IdP GhcPs)
    n'
                            IEThingWith XIEThingWith GhcPs
    _ LIEWrappedName (IdP GhcPs)
    n' IEWildcard
    wildcard' [LIEWrappedName (IdP GhcPs)]
    g' ->
                              let combinedWildcard :: IEWildcard
    combinedWildcard =
                                    case (IEWildcard
    wildcard, IEWildcard
    wildcard') of
                                      (IEWildcard Int
    _, IEWildcard
    _) -> Int -> IEWildcard
    IEWildcard Int
    0
                                      (IEWildcard
    _, IEWildcard Int
    _) -> Int -> IEWildcard
    IEWildcard Int
    0
                                      (IEWildcard, IEWildcard)
    _ -> IEWildcard
    NoIEWildcard
                               in XIEThingWith GhcPs
    -> LIEWrappedName (IdP GhcPs)
    -> IEWildcard
    -> [LIEWrappedName (IdP GhcPs)]
    -> IE GhcPs
    forall pass.
    XIEThingWith pass
    -> LIEWrappedName (IdP pass)
    -> IEWildcard
    -> [LIEWrappedName (IdP pass)]
    -> IE pass
    IEThingWith
                                    XIEThingWith GhcPs
    EpAnn [AddEpAnn]
    forall ann. EpAnn ann
    EpAnnNotUsed
                                    LIEWrappedName (IdP GhcPs)
    n'
                                    IEWildcard
    combinedWildcard
                                    ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    normalizeWNames ([LIEWrappedName (IdP GhcPs)]
    [LIEWrappedName RdrName]
    g [LIEWrappedName RdrName]
    -> [LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
    forall a. Semigroup a => a -> a -> a
    <> [LIEWrappedName (IdP GhcPs)]
    [LIEWrappedName RdrName]
    g'))
                            IEModuleContents XIEModuleContents GhcPs
    _ XRec GhcPs ModuleName
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEModuleContents"
                            IEGroup XIEGroup GhcPs
    NoExtField
    NoExtField Int
    _ LHsDoc GhcPs
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEGroup"
                            IEDoc XIEDoc GhcPs
    NoExtField
    NoExtField LHsDoc GhcPs
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEDoc"
                            IEDocNamed XIEDocNamed GhcPs
    NoExtField
    NoExtField String
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEDocNamed"
                        IEModuleContents XIEModuleContents GhcPs
    _ XRec GhcPs ModuleName
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEModuleContents"
                        IEGroup XIEGroup GhcPs
    NoExtField
    NoExtField Int
    _ LHsDoc GhcPs
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEGroup"
                        IEDoc XIEDoc GhcPs
    NoExtField
    NoExtField LHsDoc GhcPs
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEDoc"
                        IEDocNamed XIEDocNamed GhcPs
    NoExtField
    NoExtField String
    _ -> String -> IE GhcPs
    forall a. String -> a
    notImplemented String
    "IEDocNamed"
                   in GenLocated SrcSpanAnnA (IE GhcPs)
    -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    forall a. a -> Maybe a
    Just (IE GhcPs -> IE GhcPs
    f (IE GhcPs -> IE GhcPs)
    -> GenLocated SrcSpanAnnA (IE GhcPs)
    -> GenLocated SrcSpanAnnA (IE GhcPs)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> GenLocated SrcSpanAnnA (IE GhcPs)
    old)
           in (Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
     -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs)))
    -> IEWrappedNameOrd
    -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    -> Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    forall k a.
    Ord k =>
    (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
    M.alter Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    -> Maybe (GenLocated SrcSpanAnnA (IE GhcPs))
    alter IEWrappedNameOrd
    wname Map IEWrappedNameOrd (GenLocated SrcSpanAnnA (IE GhcPs))
    Map IEWrappedNameOrd (LIE GhcPs)
    m

    -- | A wrapper for @'IEWrappedName' 'RdrName'@ that allows us to define an
    -- 'Ord' instance for it.
    newtype IEWrappedNameOrd = IEWrappedNameOrd (IEWrappedName RdrName)
      deriving (IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
    (IEWrappedNameOrd -> IEWrappedNameOrd -> Bool)
    -> (IEWrappedNameOrd -> IEWrappedNameOrd -> Bool)
    -> Eq IEWrappedNameOrd
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
    == :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
    $c/= :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
    /= :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
    Eq)

    instance Ord IEWrappedNameOrd where
      compare :: IEWrappedNameOrd -> IEWrappedNameOrd -> Ordering
    compare (IEWrappedNameOrd IEWrappedName RdrName
    x) (IEWrappedNameOrd IEWrappedName RdrName
    y) = IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
    compareIewn IEWrappedName RdrName
    x IEWrappedName RdrName
    y

    -- | Project @'IEWrappedName' 'RdrName'@ from @'IE' 'GhcPs'@.
    getIewn :: IE GhcPs -> IEWrappedNameOrd
    getIewn :: IE GhcPs -> IEWrappedNameOrd
    getIewn = \case
      IEVar XIEVar GhcPs
    NoExtField
    NoExtField LIEWrappedName (IdP GhcPs)
    x -> IEWrappedName RdrName -> IEWrappedNameOrd
    IEWrappedNameOrd (LIEWrappedName RdrName -> IEWrappedName RdrName
    forall l e. GenLocated l e -> e
    unLoc LIEWrappedName (IdP GhcPs)
    LIEWrappedName RdrName
    x)
      IEThingAbs XIEThingAbs GhcPs
    _ LIEWrappedName (IdP GhcPs)
    x -> IEWrappedName RdrName -> IEWrappedNameOrd
    IEWrappedNameOrd (LIEWrappedName RdrName -> IEWrappedName RdrName
    forall l e. GenLocated l e -> e
    unLoc LIEWrappedName (IdP GhcPs)
    LIEWrappedName RdrName
    x)
      IEThingAll XIEThingAll GhcPs
    _ LIEWrappedName (IdP GhcPs)
    x -> IEWrappedName RdrName -> IEWrappedNameOrd
    IEWrappedNameOrd (LIEWrappedName RdrName -> IEWrappedName RdrName
    forall l e. GenLocated l e -> e
    unLoc LIEWrappedName (IdP GhcPs)
    LIEWrappedName RdrName
    x)
      IEThingWith XIEThingWith GhcPs
    _ LIEWrappedName (IdP GhcPs)
    x IEWildcard
    _ [LIEWrappedName (IdP GhcPs)]
    _ -> IEWrappedName RdrName -> IEWrappedNameOrd
    IEWrappedNameOrd (LIEWrappedName RdrName -> IEWrappedName RdrName
    forall l e. GenLocated l e -> e
    unLoc LIEWrappedName (IdP GhcPs)
    LIEWrappedName RdrName
    x)
      IEModuleContents XIEModuleContents GhcPs
    _ XRec GhcPs ModuleName
    _ -> String -> IEWrappedNameOrd
    forall a. String -> a
    notImplemented String
    "IEModuleContents"
      IEGroup XIEGroup GhcPs
    NoExtField
    NoExtField Int
    _ LHsDoc GhcPs
    _ -> String -> IEWrappedNameOrd
    forall a. String -> a
    notImplemented String
    "IEGroup"
      IEDoc XIEDoc GhcPs
    NoExtField
    NoExtField LHsDoc GhcPs
    _ -> String -> IEWrappedNameOrd
    forall a. String -> a
    notImplemented String
    "IEDoc"
      IEDocNamed XIEDocNamed GhcPs
    NoExtField
    NoExtField String
    _ -> String -> IEWrappedNameOrd
    forall a. String -> a
    notImplemented String
    "IEDocNamed"

    -- | Like 'compareIewn' for located wrapped names.
    compareLIewn :: LIEWrappedName RdrName -> LIEWrappedName RdrName -> Ordering
    compareLIewn :: LIEWrappedName RdrName -> LIEWrappedName RdrName -> Ordering
    compareLIewn = IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
    compareIewn (IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering)
    -> (LIEWrappedName RdrName -> IEWrappedName RdrName)
    -> LIEWrappedName RdrName
    -> LIEWrappedName RdrName
    -> Ordering
    forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
    `on` LIEWrappedName RdrName -> IEWrappedName RdrName
    forall l e. GenLocated l e -> e
    unLoc

    -- | Compare two @'IEWrapppedName' 'RdrName'@ things.
    compareIewn :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
    compareIewn :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
    compareIewn (IEName LocatedN RdrName
    x) (IEName LocatedN RdrName
    y) = LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    x RdrName -> RdrName -> Ordering
    `compareRdrName` LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    y
    compareIewn (IEName LocatedN RdrName
    _) (IEPattern EpaLocation
    _ LocatedN RdrName
    _) = Ordering
    LT
    compareIewn (IEName LocatedN RdrName
    _) (IEType EpaLocation
    _ LocatedN RdrName
    _) = Ordering
    LT
    compareIewn (IEPattern EpaLocation
    _ LocatedN RdrName
    _) (IEName LocatedN RdrName
    _) = Ordering
    GT
    compareIewn (IEPattern EpaLocation
    _ LocatedN RdrName
    x) (IEPattern EpaLocation
    _ LocatedN RdrName
    y) = LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    x RdrName -> RdrName -> Ordering
    `compareRdrName` LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    y
    compareIewn (IEPattern EpaLocation
    _ LocatedN RdrName
    _) (IEType EpaLocation
    _ LocatedN RdrName
    _) = Ordering
    LT
    compareIewn (IEType EpaLocation
    _ LocatedN RdrName
    _) (IEName LocatedN RdrName
    _) = Ordering
    GT
    compareIewn (IEType EpaLocation
    _ LocatedN RdrName
    _) (IEPattern EpaLocation
    _ LocatedN RdrName
    _) = Ordering
    GT
    compareIewn (IEType EpaLocation
    _ LocatedN RdrName
    x) (IEType EpaLocation
    _ LocatedN RdrName
    y) = LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    x RdrName -> RdrName -> Ordering
    `compareRdrName` LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc LocatedN RdrName
    y

    compareRdrName :: RdrName -> RdrName -> Ordering
    compareRdrName :: RdrName -> RdrName -> Ordering
    compareRdrName RdrName
    x RdrName
    y =
      case (RdrName -> String
    getNameStr RdrName
    x, RdrName -> String
    getNameStr RdrName
    y) of
        ([], []) -> Ordering
    EQ
        ((Char
    _ : String
    _), []) -> Ordering
    GT
        ([], (Char
    _ : String
    _)) -> Ordering
    LT
        ((Char
    x' : String
    _), (Char
    y' : String
    _)) ->
          case (Char -> Bool
    isAlphaNum Char
    x', Char -> Bool
    isAlphaNum Char
    y') of
            (Bool
    False, Bool
    False) -> RdrName
    x RdrName -> RdrName -> Ordering
    forall a. Ord a => a -> a -> Ordering
    `compare` RdrName
    y
            (Bool
    True, Bool
    False) -> Ordering
    LT
            (Bool
    False, Bool
    True) -> Ordering
    GT
            (Bool
    True, Bool
    True) -> RdrName
    x RdrName -> RdrName -> Ordering
    forall a. Ord a => a -> a -> Ordering
    `compare` RdrName
    y
      where
        getNameStr :: RdrName -> String
    getNameStr = OccName -> String
    forall o. Outputable o => o -> String
    showOutputable (OccName -> String) -> (RdrName -> OccName) -> RdrName -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RdrName -> OccName
    rdrNameOcc