{-# 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.Foldable (toList)
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 (groupBy', notImplemented, separatedByBlank, showOutputable)

-- | Sort, group and normalize imports.
--
-- Assumes input list is sorted by source location. Output list is not necessarily
-- sorted by source location, so this function should be called at most once on a
-- given input list.
normalizeImports :: Bool -> [LImportDecl GhcPs] -> [[LImportDecl GhcPs]]
normalizeImports :: Bool -> [LImportDecl GhcPs] -> [[LImportDecl GhcPs]]
normalizeImports Bool
preserveGroups =
  forall a b. (a -> b) -> [a] -> [b]
map
    ( forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [(k, a)]
M.toAscList
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith LImportDecl GhcPs -> LImportDecl GhcPs -> LImportDecl GhcPs
combineImports
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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)
x, LImportDecl GhcPs -> LImportDecl GhcPs
g GenLocated SrcSpanAnnA (ImportDecl GhcPs)
x))
    )
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. if Bool
preserveGroups
      then forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> [a]
toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
groupBy' (\GenLocated SrcSpanAnnA (ImportDecl GhcPs)
x GenLocated SrcSpanAnnA (ImportDecl GhcPs)
y -> Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall a. (a -> SrcSpan) -> a -> a -> Bool
separatedByBlank forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA GenLocated SrcSpanAnnA (ImportDecl GhcPs)
x GenLocated SrcSpanAnnA (ImportDecl GhcPs)
y)
      else forall (f :: * -> *) a. Applicative f => a -> f a
pure
  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)
ImportDeclQualifiedStyle
ImportDeclPkgQual GhcPs
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
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])
ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: ImportDeclPkgQual GhcPs
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl GhcPs
..}) =
      forall l e. l -> e -> GenLocated l e
L
        SrcSpanAnnA
l
        ImportDecl
          { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding = forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [LIE GhcPs] -> [LIE GhcPs]
normalizeLies) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding,
            Bool
Maybe (XRec GhcPs ModuleName)
ImportDeclQualifiedStyle
ImportDeclPkgQual GhcPs
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
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)
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: ImportDeclPkgQual GhcPs
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl GhcPs
..
          }

-- | 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)
ImportDeclQualifiedStyle
ImportDeclPkgQual GhcPs
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: ImportDeclPkgQual GhcPs
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl 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])
..}) (L SrcSpanAnnA
_ ImportDecl GhcPs
y) =
  forall l e. l -> e -> GenLocated l e
L
    SrcSpanAnnA
lx
    ImportDecl
      { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding = case (Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding, 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)) ->
            forall a. a -> Maybe a
Just (Bool
hiding, (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
l' ([LIE GhcPs] -> [LIE GhcPs]
normalizeLies ([GenLocated SrcSpanAnnA (IE GhcPs)]
xs 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)]))
_ -> forall a. Maybe a
Nothing,
        Bool
Maybe (XRec GhcPs ModuleName)
ImportDeclQualifiedStyle
ImportDeclPkgQual GhcPs
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: ImportDeclPkgQual GhcPs
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl GhcPs
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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ImportId -> ImportId -> Bool
$c/= :: ImportId -> ImportId -> Bool
== :: ImportId -> ImportId -> Bool
$c== :: ImportId -> ImportId -> Bool
Eq, Eq 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
min :: ImportId -> ImportId -> ImportId
$cmin :: ImportId -> ImportId -> ImportId
max :: ImportId -> ImportId -> ImportId
$cmax :: ImportId -> ImportId -> ImportId
>= :: ImportId -> ImportId -> Bool
$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
compare :: ImportId -> ImportId -> Ordering
$ccompare :: ImportId -> ImportId -> Ordering
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)
ImportDeclQualifiedStyle
ImportDeclPkgQual GhcPs
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: ImportDeclPkgQual GhcPs
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl 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])
..}) =
  ImportId
    { importIsPrelude :: Bool
importIsPrelude = Bool
isPrelude,
      importIdName :: ModuleName
importIdName = ModuleName
moduleName,
      importPkgQual :: Maybe LexicalFastString
importPkgQual = RawPkgQual -> Maybe LexicalFastString
rawPkgQualToLFS 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 = forall l e. GenLocated l e -> e
unLoc forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (XRec GhcPs ModuleName)
ideclAs,
      importHiding :: Maybe Bool
importHiding = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding
    }
  where
    isPrelude :: Bool
isPrelude = ModuleName -> String
moduleNameString ModuleName
moduleName forall a. Eq a => a -> a -> Bool
== String
"Prelude"
    moduleName :: ModuleName
moduleName = forall l e. GenLocated l e -> e
unLoc XRec GhcPs ModuleName
ideclName
    rawPkgQualToLFS :: RawPkgQual -> Maybe LexicalFastString
rawPkgQualToLFS = \case
      RawPkgQual StringLiteral
fs -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. FastString -> LexicalFastString
LexicalFastString forall b c a. (b -> c) -> (a -> b) -> a -> c
. StringLiteral -> FastString
sl_fs forall a b. (a -> b) -> a -> b
$ StringLiteral
fs
      RawPkgQual
NoRawPkgQual -> forall a. Maybe a
Nothing

-- | Normalize a collection of import\/export items.
normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
normalizeLies = forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (IE GhcPs -> IEWrappedNameOrd
getIewn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [a]
M.elems forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Map IEWrappedNameOrd (LIE GhcPs)
-> LIE GhcPs -> Map IEWrappedNameOrd (LIE GhcPs)
combine 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 =
            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 forall a. Eq a => a -> a -> Bool
== Ordering
EQ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
new_l 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 ->
                  forall pass.
XIEThingWith pass
-> LIEWrappedName (IdP pass)
-> IEWildcard
-> [LIEWrappedName (IdP pass)]
-> IE pass
IEThingWith forall ann. EpAnn ann
EpAnnNotUsed LIEWrappedName (IdP GhcPs)
n IEWildcard
wildcard ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
normalizeWNames [LIEWrappedName (IdP GhcPs)]
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 -> forall pass. XIEVar pass -> LIEWrappedName (IdP pass) -> IE pass
IEVar NoExtField
NoExtField LIEWrappedName (IdP GhcPs)
n
                    IEThingAbs XIEThingAbs GhcPs
_ LIEWrappedName (IdP GhcPs)
_ -> IE GhcPs
new
                    IEThingAll XIEThingAll GhcPs
_ LIEWrappedName (IdP GhcPs)
n -> forall pass.
XIEThingAll pass -> LIEWrappedName (IdP pass) -> IE pass
IEThingAll 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 NoExtField
XIEVar GhcPs
NoExtField LIEWrappedName (IdP GhcPs)
_ ->
                          forall a. HasCallStack => String -> a
error String
"Ormolu.Imports broken presupposition"
                        IEThingAbs XIEThingAbs GhcPs
_ LIEWrappedName (IdP GhcPs)
_ ->
                          forall pass.
XIEThingWith pass
-> LIEWrappedName (IdP pass)
-> IEWildcard
-> [LIEWrappedName (IdP pass)]
-> IE pass
IEThingWith forall ann. EpAnn ann
EpAnnNotUsed LIEWrappedName (IdP GhcPs)
n IEWildcard
wildcard [LIEWrappedName (IdP GhcPs)]
g
                        IEThingAll XIEThingAll GhcPs
_ LIEWrappedName (IdP GhcPs)
n' ->
                          forall pass.
XIEThingAll pass -> LIEWrappedName (IdP pass) -> IE pass
IEThingAll 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 forall pass.
XIEThingWith pass
-> LIEWrappedName (IdP pass)
-> IEWildcard
-> [LIEWrappedName (IdP pass)]
-> IE pass
IEThingWith
                                forall ann. EpAnn ann
EpAnnNotUsed
                                LIEWrappedName (IdP GhcPs)
n'
                                IEWildcard
combinedWildcard
                                ([LIEWrappedName RdrName] -> [LIEWrappedName RdrName]
normalizeWNames ([LIEWrappedName (IdP GhcPs)]
g forall a. Semigroup a => a -> a -> a
<> [LIEWrappedName (IdP GhcPs)]
g'))
                        IEModuleContents XIEModuleContents GhcPs
_ XRec GhcPs ModuleName
_ -> forall a. String -> a
notImplemented String
"IEModuleContents"
                        IEGroup NoExtField
XIEGroup GhcPs
NoExtField Int
_ LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEGroup"
                        IEDoc NoExtField
XIEDoc GhcPs
NoExtField LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEDoc"
                        IEDocNamed NoExtField
XIEDocNamed GhcPs
NoExtField String
_ -> forall a. String -> a
notImplemented String
"IEDocNamed"
                    IEModuleContents XIEModuleContents GhcPs
_ XRec GhcPs ModuleName
_ -> forall a. String -> a
notImplemented String
"IEModuleContents"
                    IEGroup NoExtField
XIEGroup GhcPs
NoExtField Int
_ LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEGroup"
                    IEDoc NoExtField
XIEDoc GhcPs
NoExtField LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEDoc"
                    IEDocNamed NoExtField
XIEDocNamed GhcPs
NoExtField String
_ -> forall a. String -> a
notImplemented String
"IEDocNamed"
               in forall a. a -> Maybe a
Just (IE GhcPs -> IE GhcPs
f forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated SrcSpanAnnA (IE GhcPs)
old)
       in 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 (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
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
$c/= :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
== :: IEWrappedNameOrd -> IEWrappedNameOrd -> Bool
$c== :: 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 NoExtField
XIEVar GhcPs
NoExtField LIEWrappedName (IdP GhcPs)
x -> IEWrappedName RdrName -> IEWrappedNameOrd
IEWrappedNameOrd (forall l e. GenLocated l e -> e
unLoc LIEWrappedName (IdP GhcPs)
x)
  IEThingAbs XIEThingAbs GhcPs
_ LIEWrappedName (IdP GhcPs)
x -> IEWrappedName RdrName -> IEWrappedNameOrd
IEWrappedNameOrd (forall l e. GenLocated l e -> e
unLoc LIEWrappedName (IdP GhcPs)
x)
  IEThingAll XIEThingAll GhcPs
_ LIEWrappedName (IdP GhcPs)
x -> IEWrappedName RdrName -> IEWrappedNameOrd
IEWrappedNameOrd (forall l e. GenLocated l e -> e
unLoc LIEWrappedName (IdP GhcPs)
x)
  IEThingWith XIEThingWith GhcPs
_ LIEWrappedName (IdP GhcPs)
x IEWildcard
_ [LIEWrappedName (IdP GhcPs)]
_ -> IEWrappedName RdrName -> IEWrappedNameOrd
IEWrappedNameOrd (forall l e. GenLocated l e -> e
unLoc LIEWrappedName (IdP GhcPs)
x)
  IEModuleContents XIEModuleContents GhcPs
_ XRec GhcPs ModuleName
_ -> forall a. String -> a
notImplemented String
"IEModuleContents"
  IEGroup NoExtField
XIEGroup GhcPs
NoExtField Int
_ LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEGroup"
  IEDoc NoExtField
XIEDoc GhcPs
NoExtField LHsDoc GhcPs
_ -> forall a. String -> a
notImplemented String
"IEDoc"
  IEDocNamed NoExtField
XIEDocNamed GhcPs
NoExtField String
_ -> 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 forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` 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) = forall l e. GenLocated l e -> e
unLoc LocatedN RdrName
x RdrName -> RdrName -> Ordering
`compareRdrName` 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) = forall l e. GenLocated l e -> e
unLoc LocatedN RdrName
x RdrName -> RdrName -> Ordering
`compareRdrName` 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) = forall l e. GenLocated l e -> e
unLoc LocatedN RdrName
x RdrName -> RdrName -> Ordering
`compareRdrName` 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 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 forall a. Ord a => a -> a -> Ordering
`compare` RdrName
y
  where
    getNameStr :: RdrName -> String
getNameStr = forall o. Outputable o => o -> String
showOutputable forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc