From ba1f7d1fb5c8d02a8a07fc05cf7bd5ce722cffb9 Mon Sep 17 00:00:00 2001 From: EncodePanda Date: Fri, 5 Feb 2021 12:51:59 +0100 Subject: [PATCH] Modify Signature to look top level func signatures and prepare for formatting --- .../Haskell/Stylish/Step/Signature.hs | 40 ++++++++++++++++++- .../Haskell/Stylish/Step/Signature/Tests.hs | 4 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Step/Signature.hs b/lib/Language/Haskell/Stylish/Step/Signature.hs index 6b1124ce..b4b067a5 100644 --- a/lib/Language/Haskell/Stylish/Step/Signature.hs +++ b/lib/Language/Haskell/Stylish/Step/Signature.hs @@ -1,10 +1,46 @@ +{-# LANGUAGE BlockArguments #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE TypeApplications #-} module Language.Haskell.Stylish.Step.Signature where +import RdrName (RdrName) +import SrcLoc (GenLocated (..), Located) +import GHC.Hs.Decls (HsDecl(..)) +import GHC.Hs.Binds (Sig(..)) +import GHC.Hs.Extension (GhcPs) + +-------------------------------------------------------------------------------- +import Language.Haskell.Stylish.Block import Language.Haskell.Stylish.Step +import Language.Haskell.Stylish.Module +import Language.Haskell.Stylish.Editor (change) +import Language.Haskell.Stylish.GHC (getStartLineUnsafe, getEndLineUnsafe) +import Language.Haskell.Stylish.Editor (Change, applyChanges) + +-- TODO unify with type alias from Data.hs +type ChangeLine = Change String data Config = Config - { maxColumnLength :: Int + { cMaxColumns :: Int } step :: Config -> Step -step _ = makeStep "Signature" (\ls _ -> ls) +step cfg = makeStep "Signature" (\ls m -> applyChanges (changes cfg m) ls) + +changes :: Config -> Module -> [ChangeLine] +changes cfg m = fmap (formatSignatureDecl cfg m) (topLevelFunctionSignatures m) + +topLevelFunctionSignatures :: Module -> [Located SignatureDecl] +topLevelFunctionSignatures = queryModule @(Located (HsDecl GhcPs)) \case + L pos (SigD _ (TypeSig _ [name] _)) -> [L pos $ MkSignatureDecl name] + _ -> [] + +data SignatureDecl = MkSignatureDecl + { sigName :: Located RdrName + } + +formatSignatureDecl :: Config -> Module -> Located SignatureDecl -> ChangeLine +formatSignatureDecl _cfg _m ldecl = change originalDeclBlock id + where + originalDeclBlock = + Block (getStartLineUnsafe ldecl) (getEndLineUnsafe ldecl) diff --git a/tests/Language/Haskell/Stylish/Step/Signature/Tests.hs b/tests/Language/Haskell/Stylish/Step/Signature/Tests.hs index b2a8edbf..267c6ea0 100644 --- a/tests/Language/Haskell/Stylish/Step/Signature/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Signature/Tests.hs @@ -21,8 +21,8 @@ tests = testGroup "Language.Haskell.Stylish.Step.Signature.Tests" ] config :: Int -> Config -config maxColumnLength = Config - { maxColumnLength = maxColumnLength +config cMaxColumns = Config + { cMaxColumns = cMaxColumns } case00 :: Assertion