git.iko.soy/rowtype-yoga/purescript-docs-search
on master help sign in
Prettify data constructor output
commitea33a04b1323ece91a54490b53641ed04af59ea0 authorPureFunctor <purefunctor@gmail.com> dateSun 7 Nov 2021, 02:34 (5 years ago) parentce23a66
3 files changed+56−4
src/Docs/Search/Declarations.purs+28−3 src/Docs/Search/Interactive.purs+26 src/Docs/Search/SearchResult.purs+2−1
src/Docs/Search/Declarations.purs+28−3
@@ -11,7 +11,7 @@ import Prim hiding (Type)
1111import Control.Alt ((<|>))
1212import Data.Array ((!!))
1313import Data.Array as Array
14−import Data.Foldable (foldr)
14+import Data.Foldable (foldl, foldr)
1515import Data.List (List, (:))
1616import Data.List as List
1717import Data.Maybe (Maybe(..), fromMaybe)
@@ -244,8 +244,33 @@ mkChildInfo
244244 (SearchResult { info: parentInfo, moduleName, name: resultName })
245245 (ChildDeclaration { info } )
246246
247− | ChildDeclDataConstructor <- info.declType =
248− info.arguments <#> \arguments -> DataConstructorResult { arguments }
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
270+ info.arguments <#> \arguments ->
271+ DataConstructorResult { dataDeclType
272+ , "type": makeType arguments
273+ }
249274
250275 | ChildDeclTypeClassMember <- info.declType
251276 , TypeClassResult { arguments } <- parentInfo =
src/Docs/Search/Interactive.purs+26
@@ -194,6 +194,9 @@ showSignature result@{ name, info } =
194194 ValueAliasResult ->
195195 yellow ("(" <> unwrap name <> ")")
196196
197+ DataConstructorResult info' ->
198+ showDataConstructorSignature info' result
199+
197200 _ -> yellow $ unwrap name
198201
199202
@@ -299,6 +302,29 @@ showExternDataSignature { kind } { name } =
299302 showType kind
300303
301304
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+
302328leftShift :: Int -> String -> String
303329leftShift shift str =
304330 Array.intercalate "\n" $
src/Docs/Search/SearchResult.purs+2−1
@@ -22,7 +22,8 @@ data ResultInfo
2222 | ExternDataResult { kind :: Type }
2323 | TypeSynonymResult { arguments :: Array TypeArgument
2424 , type :: Type }
25− | DataConstructorResult { arguments :: Array Type }
25+ | DataConstructorResult { dataDeclType :: DataDeclType
26+ , type :: Type }
2627 | TypeClassMemberResult { type :: Type
2728 , typeClass :: QualifiedName
2829 , typeClassArguments :: Array TypeArgument }