{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
module Language.Haskell.Tools.Refactor.Utils.BindingElem where
import Control.Reference
import Language.Haskell.Tools.AST
import Language.Haskell.Tools.Rewrite
import SrcLoc (RealSrcSpan)
class NamedElement d => BindingElem d where
sigBind :: Simple Partial (Ann d IdDom SrcTemplateStage) TypeSignature
valBind :: Simple Partial (Ann d IdDom SrcTemplateStage) ValueBind
fixitySig :: Simple Partial (Ann d IdDom SrcTemplateStage) FixitySignature
createTypeSig :: TypeSignature -> Ann d IdDom SrcTemplateStage
createBinding :: ValueBind -> Ann d IdDom SrcTemplateStage
createFixitySig :: FixitySignature -> Ann d IdDom SrcTemplateStage
isTypeSig :: Ann d IdDom SrcTemplateStage -> Bool
isBinding :: Ann d IdDom SrcTemplateStage -> Bool
isFixitySig :: Ann d IdDom SrcTemplateStage -> Bool
instance BindingElem UDecl where
sigBind = declTypeSig
valBind = declValBind
fixitySig = declFixity
createTypeSig = mkTypeSigDecl
createBinding = mkValueBinding
createFixitySig = mkFixityDecl
isTypeSig TypeSigDecl {} = True
isTypeSig _ = False
isBinding ValueBinding {} = True
isBinding _ = False
isFixitySig FixityDecl {} = True
isFixitySig _ = False
instance BindingElem ULocalBind where
sigBind = localSig
valBind = localVal
fixitySig = localFixity
createTypeSig = mkLocalTypeSig
createBinding = mkLocalValBind
createFixitySig = mkLocalFixity
isTypeSig LocalTypeSig {} = True
isTypeSig _ = False
isBinding LocalValBind {} = True
isBinding _ = False
isFixitySig LocalFixity {} = True
isFixitySig _ = False
getValBindInList :: BindingElem d => RealSrcSpan -> AnnList d -> Maybe ValueBind
getValBindInList sp ls = case ls ^? valBindsInList & filtered (isInside sp) of
[] -> Nothing
[n] -> Just n
_ -> error "getValBindInList: Multiple nodes"
valBindsInList :: BindingElem d => Simple Traversal (AnnList d) ValueBind
valBindsInList = annList & valBind