on master help sign in
Put operators after other names
2b1d013Mark Karpov
CHANGELOG.md+3
@@ -7,6 +7,9 @@
7 disabled part is still syntactically correct. [Issue
7 disabled part is still syntactically correct. [Issue
8 601](https://github.com/tweag/ormolu/issues/601).
8 601](https://github.com/tweag/ormolu/issues/601).
9
9
10+* Improved sorting of operators in imports. [Issue
11+ 602](https://github.com/tweag/ormolu/issues/602).
12+
10## Ormolu 0.1.0.0
13## Ormolu 0.1.0.0
11
14
12* Fixed rendering of type signatures concerning several identifiers. [Issue
15* Fixed rendering of type signatures concerning several identifiers. [Issue
data/examples/import/explicit-imports-out.hs+4−4
@@ -1,12 +1,12 @@
1import qualified MegaModule as M
1import qualified MegaModule as M
2− ( (<<<),
3− (>>>),
4− Either,
2+ ( Either,
5 Maybe (Just, Nothing),
3 Maybe (Just, Nothing),
6 MaybeT (..),
4 MaybeT (..),
7− Monad ((>>), (>>=), return),
5+ Monad (return, (>>), (>>=)),
8 MonadBaseControl,
6 MonadBaseControl,
9 join,
7 join,
10 liftIO,
8 liftIO,
11 void,
9 void,
10+ (<<<),
11+ (>>>),
12 )
12 )
data/examples/import/explicit-imports-with-comments-out.hs+3−2
@@ -1,6 +1,7 @@
1import qualified MegaModule as M
1import qualified MegaModule as M
2 ( -- (1)
2 ( -- (1)
3− (<<<), -- (2)
3+ -- (2)
4− (>>>),
5 Either, -- (3)
4 Either, -- (3)
5+ (<<<),
6+ (>>>),
6 )
7 )
data/examples/import/nested-explicit-imports-out.hs+6−6
@@ -1,10 +1,10 @@
1import qualified MegaModule as M
1import qualified MegaModule as M
2− ( (<<<),
3− (>>>),
4− Either,
2+ ( Either,
5 Monad
3 Monad
4+ ( return,
6− ( (>>),
5+ (>>),
7− (>>=),
6+ (>>=)
8− return
9 ),
7 ),
8+ (<<<),
9+ (>>>),
10 )
10 )
data/examples/import/qualified-prelude-out.hs+1−1
@@ -1,4 +1,4 @@
1module P where
1module P where
2
2
3import qualified Prelude
3import qualified Prelude
4−import Prelude hiding ((.), id)
4+import Prelude hiding (id, (.))
data/examples/import/sorted-export-list-out.hsnew file+1
@@ -0,0 +1 @@
1+import Linear.Vector (Additive (..), (*^), (^*))
data/examples/import/sorted-export-list.hsnew file+1
@@ -0,0 +1 @@
1+import Linear.Vector (Additive (..), (*^), (^*))
src/Ormolu/Imports.hs+20−4
@@ -9,13 +9,14 @@ module Ormolu.Imports
9where
9where
10
10
11import Data.Bifunctor
11import Data.Bifunctor
12+import Data.Char (isAlphaNum)
12import Data.Function (on)
13import Data.Function (on)
13import Data.Generics (gcompare)
14import Data.Generics (gcompare)
14import Data.List (nubBy, sortBy)
15import Data.List (nubBy, sortBy)
15import GHC hiding (GhcPs, IE)
16import GHC hiding (GhcPs, IE)
16import GHC.Hs.Extension
17import GHC.Hs.Extension
17import GHC.Hs.ImpExp (IE (..))
18import GHC.Hs.ImpExp (IE (..))
18−import Ormolu.Utils (notImplemented)
19+import Ormolu.Utils (notImplemented, showOutputable)
19
20
20-- | Sort imports by module name. This also sorts explicit import lists for
21-- | Sort imports by module name. This also sorts explicit import lists for
21-- each declaration.
22-- each declaration.
@@ -77,12 +78,27 @@ getIewn = \case
77
78
78-- | Compare two @'IEWrapppedName' 'RdrName'@ things.
79-- | Compare two @'IEWrapppedName' 'RdrName'@ things.
79compareIewn :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
80compareIewn :: 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
81compareIewn (IEName _) (IEPattern _) = LT
82compareIewn (IEName _) (IEPattern _) = LT
82compareIewn (IEName _) (IEType _) = LT
83compareIewn (IEName _) (IEType _) = LT
83compareIewn (IEPattern _) (IEName _) = GT
84compareIewn (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
85compareIewn (IEPattern _) (IEType _) = LT
86compareIewn (IEPattern _) (IEType _) = LT
86compareIewn (IEType _) (IEName _) = GT
87compareIewn (IEType _) (IEName _) = GT
87compareIewn (IEType _) (IEPattern _) = GT
88compareIewn (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
src/Ormolu/Printer/Meat/Declaration.hs+1−1
@@ -12,7 +12,7 @@ module Ormolu.Printer.Meat.Declaration
12where
12where
13
13
14import Data.List (sort)
14import Data.List (sort)
15−import Data.List.NonEmpty ((<|), NonEmpty (..))
15+import Data.List.NonEmpty (NonEmpty (..), (<|))
16import qualified Data.List.NonEmpty as NE
16import qualified Data.List.NonEmpty as NE
17import GHC hiding (InlinePragma)
17import GHC hiding (InlinePragma)
18import OccName (occNameFS)
18import OccName (occNameFS)
src/Ormolu/Printer/Meat/Declaration/Value.hs+1−1
@@ -21,7 +21,7 @@ import Data.Char (isPunctuation, isSymbol)
21import Data.Data hiding (Infix, Prefix)
21import Data.Data hiding (Infix, Prefix)
22import Data.Functor ((<&>))
22import Data.Functor ((<&>))
23import Data.List (intersperse, sortOn)
23import Data.List (intersperse, sortOn)
24−import Data.List.NonEmpty ((<|), NonEmpty (..))
24+import Data.List.NonEmpty (NonEmpty (..), (<|))
25import qualified Data.List.NonEmpty as NE
25import qualified Data.List.NonEmpty as NE
26import Data.Text (Text)
26import Data.Text (Text)
27import qualified Data.Text as Text
27import qualified Data.Text as Text