@@ -9,13 +9,14 @@ module Ormolu.Imports
99where
1010
1111import Data.Bifunctor
12+import Data.Char (isAlphaNum)
1213import Data.Function (on)
1314import Data.Generics (gcompare)
1415import Data.List (nubBy, sortBy)
1516import GHC hiding (GhcPs, IE)
1617import GHC.Hs.Extension
1718import GHC.Hs.ImpExp (IE (..))
18−import Ormolu.Utils (notImplemented)
19+import Ormolu.Utils (notImplemented, showOutputable)
1920
2021-- | Sort imports by module name. This also sorts explicit import lists for
2122-- each declaration.
@@ -77,12 +78,27 @@ getIewn = \case
7778
7879-- | Compare two @'IEWrapppedName' 'RdrName'@ things.
7980compareIewn :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
80−compareIewn (IEName x) (IEName y) = unLoc x `compare` unLoc y
81+compareIewn (IEName x) (IEName y) = unLoc x `compareRdrName` unLoc y
8182compareIewn (IEName _) (IEPattern _) = LT
8283compareIewn (IEName _) (IEType _) = LT
8384compareIewn (IEPattern _) (IEName _) = GT
84−compareIewn (IEPattern x) (IEPattern y) = unLoc x `compare` unLoc y
85+compareIewn (IEPattern x) (IEPattern y) = unLoc x `compareRdrName` unLoc y
8586compareIewn (IEPattern _) (IEType _) = LT
8687compareIewn (IEType _) (IEName _) = GT
8788compareIewn (IEType _) (IEPattern _) = GT
88−compareIewn (IEType x) (IEType y) = unLoc x `compare` unLoc y
89+compareIewn (IEType x) (IEType y) = unLoc x `compareRdrName` unLoc y
90+
91+compareRdrName :: RdrName -> RdrName -> Ordering
92+compareRdrName x y =
93+ case (getNameStr x, getNameStr y) of
94+ ([], []) -> EQ
95+ ((_ : _), []) -> GT
96+ ([], (_ : _)) -> LT
97+ ((x' : _), (y' : _)) ->
98+ case (isAlphaNum x', isAlphaNum y') of
99+ (False, False) -> x `compare` y
100+ (True, False) -> LT
101+ (False, True) -> GT
102+ (True, True) -> x `compare` y
103+ where
104+ getNameStr = showOutputable . rdrNameOcc