Skip to content

Commit ea33a04

Browse files
purefunctorklntsky
authored andcommitted
Prettify data constructor output
1 parent ce23a66 commit ea33a04

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/Docs/Search/Declarations.purs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Prim hiding (Type)
1111
import Control.Alt ((<|>))
1212
import Data.Array ((!!))
1313
import Data.Array as Array
14-
import Data.Foldable (foldr)
14+
import Data.Foldable (foldl, foldr)
1515
import Data.List (List, (:))
1616
import Data.List as List
1717
import 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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
302328
leftShift :: Int -> String -> String
303329
leftShift shift str =
304330
Array.intercalate "\n" $

src/Docs/Search/SearchResult.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 }

0 commit comments

Comments
 (0)