Put operators after other names

Previously ‘RdrName’s were sorted by simple comparison of their names.
However, in ASCII not all “operator-like” symbols go after alpha-numeric
characters. This resulted in this sort of output:

  import Linear.Vector ((*^), Additive (..), (^*))

The new ordering scheme allows us to separate operators and other names:

  import Linear.Vector (Additive (..), (*^), (^*))
This commit is contained in:
Mark Karpov 2020-06-03 17:23:13 +02:00
parent 67e43e0057
commit 2b1d013d7c
10 changed files with 41 additions and 19 deletions

View File

@ -7,6 +7,9 @@
disabled part is still syntactically correct. [Issue
601](https://github.com/tweag/ormolu/issues/601).
* Improved sorting of operators in imports. [Issue
602](https://github.com/tweag/ormolu/issues/602).
## Ormolu 0.1.0.0
* Fixed rendering of type signatures concerning several identifiers. [Issue

View File

@ -1,12 +1,12 @@
import qualified MegaModule as M
( (<<<),
(>>>),
Either,
( Either,
Maybe (Just, Nothing),
MaybeT (..),
Monad ((>>), (>>=), return),
Monad (return, (>>), (>>=)),
MonadBaseControl,
join,
liftIO,
void,
(<<<),
(>>>),
)

View File

@ -1,6 +1,7 @@
import qualified MegaModule as M
( -- (1)
(<<<), -- (2)
(>>>),
-- (2)
Either, -- (3)
(<<<),
(>>>),
)

View File

@ -1,10 +1,10 @@
import qualified MegaModule as M
( (<<<),
(>>>),
Either,
( Either,
Monad
( (>>),
(>>=),
return
( return,
(>>),
(>>=)
),
(<<<),
(>>>),
)

View File

@ -1,4 +1,4 @@
module P where
import qualified Prelude
import Prelude hiding ((.), id)
import Prelude hiding (id, (.))

View File

@ -0,0 +1 @@
import Linear.Vector (Additive (..), (*^), (^*))

View File

@ -0,0 +1 @@
import Linear.Vector (Additive (..), (*^), (^*))

View File

@ -9,13 +9,14 @@ module Ormolu.Imports
where
import Data.Bifunctor
import Data.Char (isAlphaNum)
import Data.Function (on)
import Data.Generics (gcompare)
import Data.List (nubBy, sortBy)
import GHC hiding (GhcPs, IE)
import GHC.Hs.Extension
import GHC.Hs.ImpExp (IE (..))
import Ormolu.Utils (notImplemented)
import Ormolu.Utils (notImplemented, showOutputable)
-- | Sort imports by module name. This also sorts explicit import lists for
-- each declaration.
@ -77,12 +78,27 @@ getIewn = \case
-- | Compare two @'IEWrapppedName' 'RdrName'@ things.
compareIewn :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering
compareIewn (IEName x) (IEName y) = unLoc x `compare` unLoc y
compareIewn (IEName x) (IEName y) = unLoc x `compareRdrName` unLoc y
compareIewn (IEName _) (IEPattern _) = LT
compareIewn (IEName _) (IEType _) = LT
compareIewn (IEPattern _) (IEName _) = GT
compareIewn (IEPattern x) (IEPattern y) = unLoc x `compare` unLoc y
compareIewn (IEPattern x) (IEPattern y) = unLoc x `compareRdrName` unLoc y
compareIewn (IEPattern _) (IEType _) = LT
compareIewn (IEType _) (IEName _) = GT
compareIewn (IEType _) (IEPattern _) = GT
compareIewn (IEType x) (IEType y) = unLoc x `compare` unLoc y
compareIewn (IEType x) (IEType y) = unLoc x `compareRdrName` unLoc y
compareRdrName :: RdrName -> RdrName -> Ordering
compareRdrName x y =
case (getNameStr x, getNameStr y) of
([], []) -> EQ
((_ : _), []) -> GT
([], (_ : _)) -> LT
((x' : _), (y' : _)) ->
case (isAlphaNum x', isAlphaNum y') of
(False, False) -> x `compare` y
(True, False) -> LT
(False, True) -> GT
(True, True) -> x `compare` y
where
getNameStr = showOutputable . rdrNameOcc

View File

@ -12,7 +12,7 @@ module Ormolu.Printer.Meat.Declaration
where
import Data.List (sort)
import Data.List.NonEmpty ((<|), NonEmpty (..))
import Data.List.NonEmpty (NonEmpty (..), (<|))
import qualified Data.List.NonEmpty as NE
import GHC hiding (InlinePragma)
import OccName (occNameFS)

View File

@ -21,7 +21,7 @@ import Data.Char (isPunctuation, isSymbol)
import Data.Data hiding (Infix, Prefix)
import Data.Functor ((<&>))
import Data.List (intersperse, sortOn)
import Data.List.NonEmpty ((<|), NonEmpty (..))
import Data.List.NonEmpty (NonEmpty (..), (<|))
import qualified Data.List.NonEmpty as NE
import Data.Text (Text)
import qualified Data.Text as Text