{-# 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.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 =
  ([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
 -> [GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)])
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
forall a b. (a -> b) -> [a] -> [b]
map
    ( ((ImportId,
  GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
 -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
-> [(ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ImportId,
 GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
forall a b. (a, b) -> b
snd
        ([(ImportId,
   GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
 -> [GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)])
-> ([GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
    -> [(ImportId,
         GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))])
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map
  ImportId
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
-> [(ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
forall k a. Map k a -> [(k, a)]
M.toAscList
        (Map
   ImportId
   (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
 -> [(ImportId,
      GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))])
-> ([GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
    -> Map
         ImportId
         (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)))
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [(ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
-> [(ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
-> Map
     ImportId
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
M.fromListWith LImportDecl GhcPs -> LImportDecl GhcPs -> LImportDecl GhcPs
GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
combineImports
        ([(ImportId,
   GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
 -> Map
      ImportId
      (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)))
-> ([GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
    -> [(ImportId,
         GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))])
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> Map
     ImportId
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> (ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)))
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [(ImportId,
     GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
x -> (LImportDecl GhcPs -> ImportId
importId LImportDecl GhcPs
GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
x, LImportDecl GhcPs -> LImportDecl GhcPs
g LImportDecl GhcPs
GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
x))
    )
    ([[GenLocated
     (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
 -> [[GenLocated
        (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]])
-> ([GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
    -> [[GenLocated
           (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]])
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. if Bool
preserveGroups
      then (NonEmpty
   (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
 -> [GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)])
-> [NonEmpty
      (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
forall a b. (a -> b) -> [a] -> [b]
map NonEmpty
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList ([NonEmpty
    (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
 -> [[GenLocated
        (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]])
-> ([GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
    -> [NonEmpty
          (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))])
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> Bool)
-> [GenLocated
      (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [NonEmpty
      (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs))]
forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
groupBy' (\GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
x GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
y -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
 -> SrcSpan)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
-> Bool
forall a. (a -> SrcSpan) -> a -> a -> Bool
separatedByBlank GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
-> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
x GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
y)
      else [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]
-> [[GenLocated
       (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)]]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  where
    g :: LImportDecl GhcPs -> LImportDecl GhcPs
    g :: LImportDecl GhcPs -> LImportDecl GhcPs
g (L l ImportDecl {..}) =
      SrcSpanAnn' (EpAnn AnnListItem)
-> ImportDecl GhcPs
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
forall l e. l -> e -> GenLocated l e
L
        SrcSpanAnn' (EpAnn AnnListItem)
l
        ImportDecl :: forall pass.
XCImportDecl pass
-> SourceText
-> XRec pass ModuleName
-> Maybe StringLiteral
-> IsBootInterface
-> Bool
-> ImportDeclQualifiedStyle
-> Bool
-> Maybe (XRec pass ModuleName)
-> Maybe (Bool, XRec pass [LIE pass])
-> ImportDecl pass
ImportDecl
          { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding = (GenLocated
   SrcSpanAnnL
   [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
 -> GenLocated
      SrcSpanAnnL
      [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> (Bool,
    GenLocated
      SrcSpanAnnL
      [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> (Bool,
    GenLocated
      SrcSpanAnnL
      [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
 -> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [LIE GhcPs] -> [LIE GhcPs]
[GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
normalizeLies) ((Bool,
  GenLocated
    SrcSpanAnnL
    [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
 -> (Bool,
     GenLocated
       SrcSpanAnnL
       [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]))
-> Maybe
     (Bool,
      GenLocated
        SrcSpanAnnL
        [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> Maybe
     (Bool,
      GenLocated
        SrcSpanAnnL
        [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Bool, XRec GhcPs [LIE GhcPs])
Maybe
  (Bool,
   GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
ideclHiding,
            Bool
Maybe (XRec GhcPs ModuleName)
Maybe StringLiteral
ImportDeclQualifiedStyle
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
ideclExt :: XCImportDecl GhcPs
ideclSourceSrc :: SourceText
ideclName :: XRec GhcPs ModuleName
ideclPkgQual :: Maybe StringLiteral
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 :: Maybe StringLiteral
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 lx ImportDecl {..}) (L _ y) =
  SrcSpanAnn' (EpAnn AnnListItem)
-> ImportDecl GhcPs
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (ImportDecl GhcPs)
forall l e. l -> e -> GenLocated l e
L
    SrcSpanAnn' (EpAnn AnnListItem)
lx
    ImportDecl :: forall pass.
XCImportDecl pass
-> SourceText
-> XRec pass ModuleName
-> Maybe StringLiteral
-> IsBootInterface
-> Bool
-> ImportDeclQualifiedStyle
-> Bool
-> Maybe (XRec pass ModuleName)
-> Maybe (Bool, XRec pass [LIE pass])
-> ImportDecl pass
ImportDecl
      { ideclHiding :: Maybe (Bool, XRec GhcPs [LIE GhcPs])
ideclHiding = case (Maybe (Bool, XRec GhcPs [LIE GhcPs])
Maybe
  (Bool,
   GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE 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 (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
xs), Just (Bool
_, L SrcSpanAnnL
_ [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
ys)) ->
            (Bool,
 GenLocated
   SrcSpanAnnL
   [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> Maybe
     (Bool,
      GenLocated
        SrcSpanAnnL
        [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
forall a. a -> Maybe a
Just (Bool
hiding, (SrcSpanAnnL
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
l' ([LIE GhcPs] -> [LIE GhcPs]
normalizeLies ([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
xs [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall a. [a] -> [a] -> [a]
++ [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
ys))))
          (Maybe
   (Bool,
    GenLocated
      SrcSpanAnnL
      [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]),
 Maybe
   (Bool,
    GenLocated
      SrcSpanAnnL
      [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]))
_ -> Maybe (Bool, XRec GhcPs [LIE GhcPs])
forall a. Maybe a
Nothing,
        Bool
Maybe (XRec GhcPs ModuleName)
Maybe StringLiteral
ImportDeclQualifiedStyle
XRec GhcPs ModuleName
XCImportDecl GhcPs
SourceText
IsBootInterface
ideclAs :: Maybe (XRec GhcPs ModuleName)
ideclImplicit :: Bool
ideclQualified :: ImportDeclQualifiedStyle
ideclSafe :: Bool
ideclSource :: IsBootInterface
ideclPkgQual :: Maybe StringLiteral
ideclName :: XRec GhcPs ModuleName
ideclSourceSrc :: SourceText
ideclExt :: XCImportDecl GhcPs
ideclExt :: XCImportDecl GhcPs
ideclSourceSrc :: SourceText
ideclName :: XRec GhcPs ModuleName
ideclPkgQual :: Maybe StringLiteral
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 -> ModuleName
importIdName :: ModuleName,
    ImportId -> Maybe LexicalFastString
importPkgQual :: Maybe LexicalFastString,
    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
/= :: ImportId -> ImportId -> Bool
$c/= :: ImportId -> ImportId -> Bool
== :: ImportId -> ImportId -> Bool
$c== :: 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
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
$cp1Ord :: Eq ImportId
Ord)

-- | Obtain an 'ImportId' for a given import.
importId :: LImportDecl GhcPs -> ImportId
importId :: LImportDecl GhcPs -> ImportId
importId (L _ ImportDecl {..}) =
  ImportId :: Bool
-> ModuleName
-> Maybe LexicalFastString
-> IsBootInterface
-> Bool
-> Bool
-> Bool
-> Maybe ModuleName
-> Maybe Bool
-> ImportId
ImportId
    { importIsPrelude :: Bool
importIsPrelude = Bool
isPrelude,
      importIdName :: ModuleName
importIdName = ModuleName
moduleName,
      importPkgQual :: Maybe LexicalFastString
importPkgQual = FastString -> LexicalFastString
LexicalFastString (FastString -> LexicalFastString)
-> (StringLiteral -> FastString)
-> StringLiteral
-> LexicalFastString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StringLiteral -> FastString
sl_fs (StringLiteral -> LexicalFastString)
-> Maybe StringLiteral -> Maybe LexicalFastString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StringLiteral
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 (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName
-> ModuleName
forall l e. GenLocated l e -> e
unLoc (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName
 -> ModuleName)
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName)
-> Maybe ModuleName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (XRec GhcPs ModuleName)
Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName)
ideclAs,
      importHiding :: Maybe Bool
importHiding = (Bool,
 GenLocated
   SrcSpanAnnL
   [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> Bool
forall a b. (a, b) -> a
fst ((Bool,
  GenLocated
    SrcSpanAnnL
    [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
 -> Bool)
-> Maybe
     (Bool,
      GenLocated
        SrcSpanAnnL
        [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> Maybe Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Bool, XRec GhcPs [LIE GhcPs])
Maybe
  (Bool,
   GenLocated
     SrcSpanAnnL
     [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE 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 (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName
-> ModuleName
forall l e. GenLocated l e -> e
unLoc XRec GhcPs ModuleName
GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) ModuleName
ideclName

-- | Normalize a collection of import\/export items.
normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
normalizeLies :: [LIE GhcPs] -> [LIE GhcPs]
normalizeLies = (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
 -> IEWrappedNameOrd)
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (IE GhcPs -> IEWrappedNameOrd
getIewn (IE GhcPs -> IEWrappedNameOrd)
-> (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
    -> IE GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
-> IEWrappedNameOrd
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs) -> IE GhcPs
forall l e. GenLocated l e -> e
unLoc) ([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
 -> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> ([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
    -> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map
  IEWrappedNameOrd
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall k a. Map k a -> [a]
M.elems (Map
   IEWrappedNameOrd
   (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
 -> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)])
-> ([GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
    -> Map
         IEWrappedNameOrd
         (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)))
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map
   IEWrappedNameOrd
   (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
 -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
 -> Map
      IEWrappedNameOrd
      (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)))
-> Map
     IEWrappedNameOrd
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> [GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)]
-> Map
     IEWrappedNameOrd
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Map IEWrappedNameOrd (LIE GhcPs)
-> LIE GhcPs -> Map IEWrappedNameOrd (LIE GhcPs)
Map
  IEWrappedNameOrd
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
-> Map
     IEWrappedNameOrd
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
combine Map
  IEWrappedNameOrd
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (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 new_l 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 (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
alter = \case
            Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
Nothing -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
forall a. a -> Maybe a
Just (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
 -> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)))
-> (IE GhcPs
    -> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> IE GhcPs
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SrcSpanAnn' (EpAnn AnnListItem)
-> IE GhcPs
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnn' (EpAnn AnnListItem)
new_l (IE GhcPs
 -> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)))
-> IE GhcPs
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (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
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 (SrcSpanAnn' (EpAnn AnnListItem)) (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 NoExtField
XIEVar GhcPs
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
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 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
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
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
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 Int
_ HsDocString
_ -> String -> IE GhcPs
forall a. String -> a
notImplemented String
"IEGroup"
                        IEDoc XIEDoc GhcPs
NoExtField HsDocString
_ -> String -> IE GhcPs
forall a. String -> a
notImplemented String
"IEDoc"
                        IEDocNamed XIEDocNamed GhcPs
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 Int
_ HsDocString
_ -> String -> IE GhcPs
forall a. String -> a
notImplemented String
"IEGroup"
                    IEDoc XIEDoc GhcPs
NoExtField HsDocString
_ -> String -> IE GhcPs
forall a. String -> a
notImplemented String
"IEDoc"
                    IEDocNamed XIEDocNamed GhcPs
NoExtField String
_ -> String -> IE GhcPs
forall a. String -> a
notImplemented String
"IEDocNamed"
               in GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
forall a. a -> Maybe a
Just (IE GhcPs -> IE GhcPs
f (IE GhcPs -> IE GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
-> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)
old)
       in (Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
 -> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs)))
-> IEWrappedNameOrd
-> Map
     IEWrappedNameOrd
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> Map
     IEWrappedNameOrd
     (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
M.alter Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
-> Maybe (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE GhcPs))
alter IEWrappedNameOrd
wname Map IEWrappedNameOrd (LIE GhcPs)
Map
  IEWrappedNameOrd
  (GenLocated (SrcSpanAnn' (EpAnn AnnListItem)) (IE 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
/= :: 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 XIEVar GhcPs
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 Int
_ HsDocString
_ -> String -> IEWrappedNameOrd
forall a. String -> a
notImplemented String
"IEGroup"
  IEDoc XIEDoc GhcPs
NoExtField HsDocString
_ -> String -> IEWrappedNameOrd
forall a. String -> a
notImplemented String
"IEDoc"
  IEDocNamed XIEDocNamed GhcPs
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