@@ -12,7 +12,6 @@ module Language.PureScript.Docs.Types
12import Control.Arrow (first, (***))
12import Control.Arrow (first, (***))
13import Control.Applicative ((<$>), (<*>))
13import Control.Applicative ((<$>), (<*>))
14import Data.Functor ((<$))
14import Data.Functor ((<$))
16import Data.Maybe (mapMaybe)
15import Data.Maybe (mapMaybe)
17import Data.Version
16import Data.Version
18import Data.Aeson ((.=))
17import Data.Aeson ((.=))
@@ -87,27 +86,32 @@ data RenderedDeclaration = RenderedDeclaration
87data RenderedChildDeclaration = RenderedChildDeclaration
86data RenderedChildDeclaration = RenderedChildDeclaration
88 { rcdTitle :: String
87 { rcdTitle :: String
89 , rcdComments :: Maybe String
88 , rcdComments :: Maybe String
90− , rcdCode :: RenderedCode
91 , rcdSourceSpan :: Maybe P.SourceSpan
89 , rcdSourceSpan :: Maybe P.SourceSpan
92− , rcdType :: RenderedChildDeclarationType
90+ , rcdInfo :: RenderedChildDeclarationInfo
94 deriving (Show, Eq, Ord)
92 deriving (Show, Eq, Ord)
96−data RenderedChildDeclarationType
94+data RenderedChildDeclarationInfo
97− = ChildInstance
95+ = ChildInstance RenderedCode
96+ -- ^ A type instance declaration. The rendered code value looks like
97+ -- `instance showUnit :: Show Unit`
98− | ChildDataConstructor
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`.
99− | ChildTypeClassMember
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.
100− deriving (Show, Eq, Ord, Bounded, Enum)
109+ deriving (Show, Eq, Ord)
102−childDeclTypeToString :: RenderedChildDeclarationType -> String
103−childDeclTypeToString = withHead toLower . drop 5 . show
105− withHead f (x:xs) = f x : xs
108−childDeclarationTypes :: [(String, RenderedChildDeclarationType)]
111+childDeclTypeToString :: RenderedChildDeclarationInfo -> String
109−childDeclarationTypes =
112+childDeclTypeToString (ChildInstance _) = "instance"
113+childDeclTypeToString (ChildDataConstructor _ _) = "dataConstructor"
110− map (\t -> (childDeclTypeToString t, t)) [minBound .. maxBound]
114+childDeclTypeToString (ChildTypeClassMember _ _) = "typeClassMember"
112newtype GithubUser
116newtype GithubUser
113 = GithubUser { runGithubUser :: String }
117 = GithubUser { runGithubUser :: String }
@@ -120,7 +124,7 @@ newtype GithubRepo
120data PackageError
124data PackageError
121 = ErrorInPackageMeta BowerError
125 = ErrorInPackageMeta BowerError
122 | InvalidVersion
126 | InvalidVersion
123− | InvalidDeclarationType
127+ | InvalidChildDeclarationType String
124 | InvalidRenderedCode String
128 | InvalidRenderedCode String
125 | InvalidFixity
129 | InvalidFixity
126 deriving (Show, Eq, Ord)
130 deriving (Show, Eq, Ord)
@@ -232,16 +236,23 @@ asRenderedChildDeclaration :: Parse PackageError RenderedChildDeclaration
232asRenderedChildDeclaration =
236asRenderedChildDeclaration =
233 RenderedChildDeclaration <$> key "title" asString
237 RenderedChildDeclaration <$> key "title" asString
234 <*> key "comments" (perhaps asString)
238 <*> key "comments" (perhaps asString)
235− <*> key "code" asRenderedCode .! InvalidRenderedCode
236 <*> key "sourceSpan" (perhaps asSourceSpan)
239 <*> key "sourceSpan" (perhaps asSourceSpan)
237− <*> key "type" asRenderedChildDeclarationType .!! InvalidDeclarationType
240+ <*> key "info" asRenderedChildDeclarationInfo
242+asRenderedChildDeclarationInfo :: Parse PackageError RenderedChildDeclarationInfo
243+asRenderedChildDeclarationInfo = do
244+ ty <- key "declType" asString
247+ ChildInstance <$> codeKey "code"
248+ "dataConstructor" ->
249+ ChildDataConstructor <$> codeKey "signature" <*> codeKey "type"
250+ "typeClassMember" ->
251+ ChildTypeClassMember <$> codeKey "contextualType" <*> codeKey "type"
253+ throwCustomError $ InvalidChildDeclarationType other
239− p .!! err = p .! const err
255+ codeKey k = key k asRenderedCode .! InvalidRenderedCode
241−asRenderedChildDeclarationType :: Parse PackageError RenderedChildDeclarationType
242−asRenderedChildDeclarationType =
243− withString (maybe (Left InvalidDeclarationType) Right .
244− flip lookup childDeclarationTypes)
246asSourcePos :: Parse e P.SourcePos
257asSourcePos :: Parse e P.SourcePos
247asSourcePos = P.SourcePos <$> nth 0 asIntegral
258asSourcePos = P.SourcePos <$> nth 0 asIntegral
@@ -317,13 +328,17 @@ instance A.ToJSON RenderedChildDeclaration where
317 toJSON RenderedChildDeclaration{..} =
328 toJSON RenderedChildDeclaration{..} =
318 A.object [ "title" .= rcdTitle
329 A.object [ "title" .= rcdTitle
319 , "comments" .= rcdComments
330 , "comments" .= rcdComments
321 , "sourceSpan" .= rcdSourceSpan
331 , "sourceSpan" .= rcdSourceSpan
322− , "type" .= rcdType
332+ , "info" .= rcdInfo
325−instance A.ToJSON RenderedChildDeclarationType where
335+instance A.ToJSON RenderedChildDeclarationInfo where
326− toJSON = A.toJSON . childDeclTypeToString
336+ toJSON info = A.object $ "declType" .= childDeclTypeToString info : props
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]
328instance A.ToJSON GithubUser where
343instance A.ToJSON GithubUser where
329 toJSON = A.toJSON . runGithubUser
344 toJSON = A.toJSON . runGithubUser