diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2021-01-17 11:15:37 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2021-01-17 11:15:37 -0700 |
commit | 3130faccf7c9a9a7697e246884e2b60fd4b1f9de (patch) | |
tree | ab171724845fe928ef05692c27351be933228ec2 /lib/Language/Haskell/Stylish/Ordering.hs | |
parent | fd8bfa2853825504c2dbc7678154ac8d56d47035 (diff) | |
parent | 84770e33bb6286c163c3b2b10fa98d264f6672b8 (diff) | |
download | stylish-haskell-3130faccf7c9a9a7697e246884e2b60fd4b1f9de.tar.gz |
Merge tag 'v0.12.2.0'
v0.12.2.0
- 0.12.2.0 (2020-10-08)
* align: Add a new option for aligning only adjacent items (by 1Computer1)
* align: Add support for aligning MultiWayIf syntax (by 1Computer1)
* data: Fix some issues with record field padding
* module_header: Add separate_lists option
* imports: Respect separate_lists for (..) imports
* data: Make sorting deriving list optional (by Maxim Koltsov)
Diffstat (limited to 'lib/Language/Haskell/Stylish/Ordering.hs')
-rw-r--r-- | lib/Language/Haskell/Stylish/Ordering.hs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/Language/Haskell/Stylish/Ordering.hs b/lib/Language/Haskell/Stylish/Ordering.hs new file mode 100644 index 0000000..1a05eb4 --- /dev/null +++ b/lib/Language/Haskell/Stylish/Ordering.hs @@ -0,0 +1,61 @@ +-------------------------------------------------------------------------------- +-- | There are a number of steps that sort items: 'Imports' and 'ModuleHeader', +-- and maybe more in the future. This module provides consistent sorting +-- utilities. +{-# LANGUAGE LambdaCase #-} +module Language.Haskell.Stylish.Ordering + ( compareLIE + , compareWrappedName + , unwrapName + ) where + + +-------------------------------------------------------------------------------- +import Data.Char (isUpper) +import Data.Ord (comparing) +import GHC.Hs +import RdrName (RdrName) +import SrcLoc (unLoc) + + +-------------------------------------------------------------------------------- +import Language.Haskell.Stylish.GHC (showOutputable) +import Outputable (Outputable) + + +-------------------------------------------------------------------------------- +-- | NOTE: Can we get rid off this by adding a properly sorting newtype around +-- 'RdrName'? +compareLIE :: LIE GhcPs -> LIE GhcPs -> Ordering +compareLIE = comparing $ ieKey . unLoc + where + -- | The implementation is a bit hacky to get proper sorting for input specs: + -- constructors first, followed by functions, and then operators. + ieKey :: IE GhcPs -> (Int, String) + ieKey = \case + IEVar _ n -> nameKey n + IEThingAbs _ n -> nameKey n + IEThingAll _ n -> nameKey n + IEThingWith _ n _ _ _ -> nameKey n + IEModuleContents _ n -> nameKey n + _ -> (2, "") + + +-------------------------------------------------------------------------------- +compareWrappedName :: IEWrappedName RdrName -> IEWrappedName RdrName -> Ordering +compareWrappedName = comparing nameKey + + +-------------------------------------------------------------------------------- +unwrapName :: IEWrappedName n -> n +unwrapName (IEName n) = unLoc n +unwrapName (IEPattern n) = unLoc n +unwrapName (IEType n) = unLoc n + + +-------------------------------------------------------------------------------- +nameKey :: Outputable name => name -> (Int, String) +nameKey n = case showOutputable n of + o@('(' : _) -> (2, o) + o@(o0 : _) | isUpper o0 -> (0, o) + o -> (1, o) |