@@ -12,7 +12,6 @@ module Language.PureScript.Docs.Types
1212import Control.Arrow (first, (***))
1313import Control.Applicative ((<$>), (<*>))
1414import Data.Functor ((<$))
15−import Data.Char
1615import Data.Maybe (mapMaybe)
1716import Data.Version
1817import Data.Aeson ((.=))
@@ -87,27 +86,32 @@ data RenderedDeclaration = RenderedDeclaration
8786data RenderedChildDeclaration = RenderedChildDeclaration
8887 { rcdTitle :: String
8988 , rcdComments :: Maybe String
90− , rcdCode :: RenderedCode
9189 , rcdSourceSpan :: Maybe P.SourceSpan
92− , rcdType :: RenderedChildDeclarationType
90+ , rcdInfo :: RenderedChildDeclarationInfo
9391 }
9492 deriving (Show, Eq, Ord)
9593
96−data RenderedChildDeclarationType
97− = ChildInstance
98− | ChildDataConstructor
99− | ChildTypeClassMember
100− deriving (Show, Eq, Ord, Bounded, Enum)
101−
102−childDeclTypeToString :: RenderedChildDeclarationType -> String
103−childDeclTypeToString = withHead toLower . drop 5 . show
104− where
105− withHead f (x:xs) = f x : xs
106− withHead _ [] = []
94+data RenderedChildDeclarationInfo
95+ = ChildInstance RenderedCode
96+ -- ^ A type instance declaration. The rendered code value looks like
97+ -- `instance showUnit :: Show Unit`
98+ | ChildDataConstructor RenderedCode RenderedCode
99+ -- ^ A data constructor, with rendered code for both how it appears in the
100+ -- data declaration, and the constructor with its actual type. For example,
101+ -- the constructor `Just` from the type `Maybe` might have the rendered
102+ -- code values being `Just a` and `Just :: forall a. a -> Maybe a`.
103+ | ChildTypeClassMember RenderedCode RenderedCode
104+ -- ^ A type class member, with rendered code for its type in the context of
105+ -- the parent type class declaration (that is, without a constraint), and
106+ -- also for its actual type. For example, `add` from `Semiring` might have
107+ -- the rendered code values as `add :: a -> a -> a` and
108+ -- `add :: forall a. (Semiring a) => a -> a -> a` respectively.
109+ deriving (Show, Eq, Ord)
107110
108−childDeclarationTypes :: [(String, RenderedChildDeclarationType)]
109−childDeclarationTypes =
110− map (\t -> (childDeclTypeToString t, t)) [minBound .. maxBound]
111+childDeclTypeToString :: RenderedChildDeclarationInfo -> String
112+childDeclTypeToString (ChildInstance _) = "instance"
113+childDeclTypeToString (ChildDataConstructor _ _) = "dataConstructor"
114+childDeclTypeToString (ChildTypeClassMember _ _) = "typeClassMember"
111115
112116newtype GithubUser
113117 = GithubUser { runGithubUser :: String }
@@ -120,7 +124,7 @@ newtype GithubRepo
120124data PackageError
121125 = ErrorInPackageMeta BowerError
122126 | InvalidVersion
123− | InvalidDeclarationType
127+ | InvalidChildDeclarationType String
124128 | InvalidRenderedCode String
125129 | InvalidFixity
126130 deriving (Show, Eq, Ord)
@@ -232,16 +236,23 @@ asRenderedChildDeclaration :: Parse PackageError RenderedChildDeclaration
232236asRenderedChildDeclaration =
233237 RenderedChildDeclaration <$> key "title" asString
234238 <*> key "comments" (perhaps asString)
235− <*> key "code" asRenderedCode .! InvalidRenderedCode
236239 <*> key "sourceSpan" (perhaps asSourceSpan)
237− <*> key "type" asRenderedChildDeclarationType .!! InvalidDeclarationType
240+ <*> key "info" asRenderedChildDeclarationInfo
241+
242+asRenderedChildDeclarationInfo :: Parse PackageError RenderedChildDeclarationInfo
243+asRenderedChildDeclarationInfo = do
244+ ty <- key "declType" asString
245+ case ty of
246+ "instance" ->
247+ ChildInstance <$> codeKey "code"
248+ "dataConstructor" ->
249+ ChildDataConstructor <$> codeKey "signature" <*> codeKey "type"
250+ "typeClassMember" ->
251+ ChildTypeClassMember <$> codeKey "contextualType" <*> codeKey "type"
252+ other ->
253+ throwCustomError $ InvalidChildDeclarationType other
238254 where
239− p .!! err = p .! const err
240−
241−asRenderedChildDeclarationType :: Parse PackageError RenderedChildDeclarationType
242−asRenderedChildDeclarationType =
243− withString (maybe (Left InvalidDeclarationType) Right .
244− flip lookup childDeclarationTypes)
255+ codeKey k = key k asRenderedCode .! InvalidRenderedCode
245256
246257asSourcePos :: Parse e P.SourcePos
247258asSourcePos = P.SourcePos <$> nth 0 asIntegral
@@ -317,13 +328,17 @@ instance A.ToJSON RenderedChildDeclaration where
317328 toJSON RenderedChildDeclaration{..} =
318329 A.object [ "title" .= rcdTitle
319330 , "comments" .= rcdComments
320− , "code" .= rcdCode
321331 , "sourceSpan" .= rcdSourceSpan
322− , "type" .= rcdType
332+ , "info" .= rcdInfo
323333 ]
324334
325−instance A.ToJSON RenderedChildDeclarationType where
326− toJSON = A.toJSON . childDeclTypeToString
335+instance A.ToJSON RenderedChildDeclarationInfo where
336+ toJSON info = A.object $ "declType" .= childDeclTypeToString info : props
337+ where
338+ props = case info of
339+ ChildInstance code -> ["code" .= code]
340+ ChildDataConstructor sig ty -> ["signature" .= sig, "type" .= ty]
341+ ChildTypeClassMember cTy ty -> ["contextualType" .= cTy, "type" .= ty]
327342
328343instance A.ToJSON GithubUser where
329344 toJSON = A.toJSON . runGithubUser