git.iko.soy/rowtype-yoga/purescript-docs-search
on master help sign in
Prettify data constructor output
ea33a04PureFunctor
src/Docs/Search/Declarations.purs+28−3
@@ -11,7 +11,7 @@ import Prim hiding (Type)
11import Control.Alt ((<|>))
11import Control.Alt ((<|>))
12import Data.Array ((!!))
12import Data.Array ((!!))
13import Data.Array as Array
13import Data.Array as Array
14−import Data.Foldable (foldr)
14+import Data.Foldable (foldl, foldr)
15import Data.List (List, (:))
15import Data.List (List, (:))
16import Data.List as List
16import Data.List as List
17import Data.Maybe (Maybe(..), fromMaybe)
17import Data.Maybe (Maybe(..), fromMaybe)
@@ -244,8 +244,33 @@ mkChildInfo
244 (SearchResult { info: parentInfo, moduleName, name: resultName })
244 (SearchResult { info: parentInfo, moduleName, name: resultName })
245 (ChildDeclaration { info } )
245 (ChildDeclaration { info } )
246
246
247− | ChildDeclDataConstructor <- info.declType =
247+ | ChildDeclDataConstructor <- info.declType
248+ , DataResult { dataDeclType, typeArguments } <- parentInfo =
249+ let
250+ parentTypeCtor :: Type
251+ parentTypeCtor = TypeConstructor
252+ $ QualifiedName { moduleNameParts:
253+ String.split (wrap ".") (unwrap moduleName)
254+ , name: resultName }
255+
256+ parentTypeArgs :: Array Type
257+ parentTypeArgs = typeArguments <#> unwrap >>> \{ name } -> TypeVar name
258+
259+ parentType :: Type
260+ parentType = foldl TypeApp parentTypeCtor parentTypeArgs
261+
262+ typeArrow :: Type -> Type
263+ typeArrow =
264+ TypeApp (TypeConstructor (QualifiedName { moduleNameParts: [ "Prim" ]
265+ , name: Identifier "Function" }))
266+
267+ makeType :: Array Type -> Type
268+ makeType = foldr (\a b -> TypeApp (typeArrow a) b) parentType
269+ in
248− info.arguments <#> \arguments -> DataConstructorResult { arguments }
270+ info.arguments <#> \arguments ->
271+ DataConstructorResult { dataDeclType
272+ , "type": makeType arguments
273+ }
249
274
250 | ChildDeclTypeClassMember <- info.declType
275 | ChildDeclTypeClassMember <- info.declType
251 , TypeClassResult { arguments } <- parentInfo =
276 , TypeClassResult { arguments } <- parentInfo =
src/Docs/Search/Interactive.purs+26
@@ -194,6 +194,9 @@ showSignature result@{ name, info } =
194 ValueAliasResult ->
194 ValueAliasResult ->
195 yellow ("(" <> unwrap name <> ")")
195 yellow ("(" <> unwrap name <> ")")
196
196
197+ DataConstructorResult info' ->
198+ showDataConstructorSignature info' result
199+
197 _ -> yellow $ unwrap name
200 _ -> yellow $ unwrap name
198
201
199
202
@@ -299,6 +302,29 @@ showExternDataSignature { kind } { name } =
299 showType kind
302 showType kind
300
303
301
304
305+showDataConstructorSignature
306+ :: forall rest
307+ . { dataDeclType :: DataDeclType
308+ , "type" :: Type
309+ }
310+ -> { name :: Identifier
311+ | rest
312+ }
313+ -> String
314+showDataConstructorSignature { dataDeclType, type: ctorType } { name } =
315+ ( keyword
316+ case dataDeclType of
317+ NewtypeDataDecl -> "newtype constructor"
318+ DataDataDecl -> "data constructor"
319+ ) <>
320+ space <>
321+ yellow (unwrap name) <>
322+ space <>
323+ syntax "::" <>
324+ space <>
325+ showType ctorType
326+
327+
302leftShift :: Int -> String -> String
328leftShift :: Int -> String -> String
303leftShift shift str =
329leftShift shift str =
304 Array.intercalate "\n" $
330 Array.intercalate "\n" $
src/Docs/Search/SearchResult.purs+2−1
@@ -22,7 +22,8 @@ data ResultInfo
22 | ExternDataResult { kind :: Type }
22 | ExternDataResult { kind :: Type }
23 | TypeSynonymResult { arguments :: Array TypeArgument
23 | TypeSynonymResult { arguments :: Array TypeArgument
24 , type :: Type }
24 , type :: Type }
25− | DataConstructorResult { arguments :: Array Type }
25+ | DataConstructorResult { dataDeclType :: DataDeclType
26+ , type :: Type }
26 | TypeClassMemberResult { type :: Type
27 | TypeClassMemberResult { type :: Type
27 , typeClass :: QualifiedName
28 , typeClass :: QualifiedName
28 , typeClassArguments :: Array TypeArgument }
29 , typeClassArguments :: Array TypeArgument }