module Language.PureScript.Ide.Filter.Imports where


import Protolude                     hiding (isPrefixOf)

import Language.PureScript.Ide.Types (IdeDataConstructor(..), IdeDeclaration(..), IdeDeclarationAnn(..), IdeType(..), IdeTypeClass(..), IdeTypeOperator(..), IdeTypeSynonym(..), IdeValue(..), IdeValueOperator(..))
import Language.PureScript.Ide.Imports (Import(..))

import Language.PureScript qualified as P

matchImport :: Maybe P.ModuleName -> P.ModuleName -> IdeDeclarationAnn -> Import -> Bool
matchImport :: Maybe ModuleName
-> ModuleName -> IdeDeclarationAnn -> Import -> Bool
matchImport Maybe ModuleName
matchQualifier ModuleName
declMod (IdeDeclarationAnn Annotation
_ IdeDeclaration
decl) (Import ModuleName
importMod ImportDeclarationType
declTy Maybe ModuleName
qualifier) | ModuleName
declMod forall a. Eq a => a -> a -> Bool
== ModuleName
importMod Bool -> Bool -> Bool
&& Maybe ModuleName
matchQualifier forall a. Eq a => a -> a -> Bool
== Maybe ModuleName
qualifier =
  case ImportDeclarationType
declTy of 
    ImportDeclarationType
P.Implicit -> Bool
True
    P.Explicit [DeclarationRef]
refs -> forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (IdeDeclaration -> DeclarationRef -> Bool
matchRef IdeDeclaration
decl) [DeclarationRef]
refs
    P.Hiding [DeclarationRef]
refs -> Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (IdeDeclaration -> DeclarationRef -> Bool
matchRef IdeDeclaration
decl) [DeclarationRef]
refs
  where
    matchRef :: IdeDeclaration -> DeclarationRef -> Bool
matchRef (IdeDeclValue (IdeValue Ident
ident SourceType
_)) (P.ValueRef SourceSpan
_ Ident
ident') = Ident
ident forall a. Eq a => a -> a -> Bool
== Ident
ident'
    matchRef (IdeDeclType (IdeType ProperName 'TypeName
tname SourceType
_kind [(ProperName 'ConstructorName, SourceType)]
_dctors)) (P.TypeRef SourceSpan
_ ProperName 'TypeName
tname' Maybe [ProperName 'ConstructorName]
_dctors') = ProperName 'TypeName
tname forall a. Eq a => a -> a -> Bool
== ProperName 'TypeName
tname'
    matchRef (IdeDeclTypeSynonym (IdeTypeSynonym ProperName 'TypeName
tname SourceType
_type SourceType
_kind)) (P.TypeRef SourceSpan
_ ProperName 'TypeName
tname' Maybe [ProperName 'ConstructorName]
_dctors) = ProperName 'TypeName
tname forall a. Eq a => a -> a -> Bool
== ProperName 'TypeName
tname' -- Can this occur?
    
    matchRef (IdeDeclDataConstructor (IdeDataConstructor ProperName 'ConstructorName
dcname ProperName 'TypeName
tname SourceType
_type)) (P.TypeRef SourceSpan
_ ProperName 'TypeName
tname' Maybe [ProperName 'ConstructorName]
dctors) =
      ProperName 'TypeName
tname forall a. Eq a => a -> a -> Bool
== ProperName 'TypeName
tname'
      Bool -> Bool -> Bool
&& forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (ProperName 'ConstructorName
dcname forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem`) Maybe [ProperName 'ConstructorName]
dctors -- (..) or explicitly lists constructor
      
    matchRef (IdeDeclTypeClass (IdeTypeClass ProperName 'ClassName
tcname SourceType
_kind [IdeInstance]
_instances)) (P.TypeClassRef SourceSpan
_ ProperName 'ClassName
tcname') = ProperName 'ClassName
tcname forall a. Eq a => a -> a -> Bool
== ProperName 'ClassName
tcname'
    matchRef (IdeDeclValueOperator (IdeValueOperator{ OpName 'ValueOpName
_ideValueOpName :: IdeValueOperator -> OpName 'ValueOpName
_ideValueOpName :: OpName 'ValueOpName
_ideValueOpName })) (P.ValueOpRef SourceSpan
_ OpName 'ValueOpName
opname) = OpName 'ValueOpName
_ideValueOpName forall a. Eq a => a -> a -> Bool
== OpName 'ValueOpName
opname
    matchRef (IdeDeclTypeOperator (IdeTypeOperator{ OpName 'TypeOpName
_ideTypeOpName :: IdeTypeOperator -> OpName 'TypeOpName
_ideTypeOpName :: OpName 'TypeOpName
_ideTypeOpName })) (P.TypeOpRef SourceSpan
_ OpName 'TypeOpName
opname) = OpName 'TypeOpName
_ideTypeOpName forall a. Eq a => a -> a -> Bool
== OpName 'TypeOpName
opname
    matchRef IdeDeclaration
_ DeclarationRef
_ = Bool
False

matchImport Maybe ModuleName
_ ModuleName
_ IdeDeclarationAnn
_ Import
_ = Bool
False