Skip to content

Commit

Permalink
Modify Signature to look top level func signatures and prepare for fo…
Browse files Browse the repository at this point in the history
…rmatting
  • Loading branch information
EncodePanda committed Feb 5, 2021
1 parent 2b80feb commit ba1f7d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
40 changes: 38 additions & 2 deletions lib/Language/Haskell/Stylish/Step/Signature.hs
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions tests/Language/Haskell/Stylish/Step/Signature/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba1f7d1

Please sign in to comment.