Skip to content

Commit 58907de

Browse files
committed
[ new ] specify data/record/interface
1 parent 370a273 commit 58907de

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed

src/Idris/Doc/String.idr

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ prettyName n =
115115
let root = nameRoot n in
116116
if isOpName n then parens (pretty root) else pretty root
117117

118+
prettyKindedName : Maybe String -> Doc IdrisDocAnn -> Doc IdrisDocAnn
119+
prettyKindedName Nothing nm = nm
120+
prettyKindedName (Just kw) nm
121+
= annotate (Syntax $ SynDecor Keyword) (pretty kw) <++> nm
122+
118123
export
119124
getDocsForPrimitive : {auto c : Ref Ctxt Defs} ->
120125
{auto s : Ref Syn SyntaxInfo} ->
@@ -284,61 +289,76 @@ getDocsForName fc n
284289
]
285290
_ => pure projDecl
286291

287-
getFieldsDoc : Name -> Core (List (Doc IdrisDocAnn))
292+
getFieldsDoc : Name -> Core (Maybe (Doc IdrisDocAnn))
288293
getFieldsDoc recName
289294
= do let (Just ns, n) = displayName recName
290-
| _ => pure []
295+
| _ => pure Nothing
291296
let recNS = ns <.> mkNamespace n
292297
defs <- get Ctxt
293298
let fields = getFieldNames (gamma defs) recNS
294299
syn <- get Syn
295300
case fields of
296-
[] => pure []
297-
[proj] => pure [header "Projection" <++> annotate Declarations !(getFieldDoc proj)]
298-
projs => pure [vcat [header "Projections"
299-
, annotate Declarations $
300-
vcat $ map (indent 2) $ !(traverse getFieldDoc projs)]]
301-
302-
getExtra : Name -> GlobalDef -> Core (List (Doc IdrisDocAnn))
301+
[] => pure Nothing
302+
[proj] => pure $ Just $ header "Projection" <++> annotate Declarations !(getFieldDoc proj)
303+
projs => pure $ Just $ vcat
304+
[ header "Projections"
305+
, annotate Declarations $ vcat $
306+
map (indent 2) $ !(traverse getFieldDoc projs)
307+
]
308+
309+
getExtra : Name -> GlobalDef -> Core (Maybe String, List (Doc IdrisDocAnn))
303310
getExtra n d = do
304311
do syn <- get Syn
305312
let [] = lookupName n (ifaces syn)
306-
| [ifacedata] => pure <$> getIFaceDoc ifacedata
307-
| _ => pure [] -- shouldn't happen, we've resolved ambiguity by now
313+
| [ifacedata] => (Just "interface",) . pure <$> getIFaceDoc ifacedata
314+
| _ => pure (Nothing, []) -- shouldn't happen, we've resolved ambiguity by now
308315
case definition d of
309-
PMDef _ _ _ _ _ => pure [showTotal n (totality d)]
316+
PMDef _ _ _ _ _ => pure (Nothing, [showTotal n (totality d)])
310317
TCon _ _ _ _ _ _ cons _ =>
311318
do let tot = [showTotal n (totality d)]
312319
cdocs <- traverse (getDConDoc <=< toFullNames) cons
313320
cdoc <- case cdocs of
314-
[] => pure []
315-
[doc] => pure
316-
$ (header "Constructor" <++> annotate Declarations doc)
317-
:: !(getFieldsDoc n)
318-
docs => pure [vcat [header "Constructors"
319-
, annotate Declarations $
320-
vcat $ map (indent 2) docs]]
321-
pure (tot ++ cdoc)
322-
_ => pure []
321+
[] => pure (Just "data", [])
322+
[doc] =>
323+
let cdoc = header "Constructor" <++> annotate Declarations doc in
324+
case !(getFieldsDoc n) of
325+
Nothing => pure (Just "data", [cdoc])
326+
Just fs => pure (Just "record", cdoc :: [fs])
327+
docs => pure (Just "data"
328+
, [vcat [header "Constructors"
329+
, annotate Declarations $
330+
vcat $ map (indent 2) docs]])
331+
pure (map (tot ++) cdoc)
332+
_ => pure (Nothing, [])
323333

324334
showDoc (MkConfig {longNames, dropFirst, getTotality}) (n, str)
325335
= do defs <- get Ctxt
326336
Just def <- lookupCtxtExact n (gamma defs)
327337
| Nothing => undefinedName fc n
338+
-- First get the extra stuff because this also tells us whether a
339+
-- definition is `data`, `record`, or `interface`.
340+
(typ, extra) <- ifThenElse getTotality
341+
(getExtra n def)
342+
(pure (Nothing, []))
343+
344+
-- Then form the type declaration
328345
ty <- resugar [] =<< normaliseHoles defs [] (type def)
329346
-- when printing e.g. interface methods there is no point in
330347
-- repeating the interface's name
331348
let ty = ifThenElse (not dropFirst) ty $ case ty of
332349
PPi _ _ AutoImplicit _ _ sc => sc
333350
_ => ty
334-
let cat = showCategory def
335351
nm <- aliasName n
336352
-- when printing e.g. interface methods there is no point in
337353
-- repeating the namespace the interface lives in
338-
let nm = if longNames then pretty (show nm) else prettyName nm
339-
let docDecl = annotate (Decl n) (hsep [cat nm, colon, prettyTerm ty])
354+
let cat = showCategory def
355+
let nm = ifThenElse longNames
356+
(prettyKindedName typ $ cat $ pretty (show nm))
357+
(cat $ prettyName nm)
358+
let docDecl = annotate (Decl n) (hsep [nm, colon, prettyTerm ty])
359+
360+
-- Finally add the user-provided docstring
340361
let docText = reflowDoc str
341-
extra <- ifThenElse getTotality (getExtra n def) (pure [])
342362
fixes <- getFixityDoc n
343363
let docBody = annotate DocStringBody $ vcat $ docText ++ (map (indent 2) (extra ++ fixes))
344364
pure (vcat [docDecl, docBody])

tests/idris2/docs001/expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ Main> Prelude.plus : Nat -> Nat -> Nat
33
@ x the number to case-split on
44
@ y the other numberpublic export
55
Totality: total
6-
Main> Prelude.Nat : Type
6+
Main> data Prelude.Nat : Type
77
Natural numbers: unbounded, unsigned integers which can be pattern matched.
88
Totality: total
99
Constructors:
1010
Z : Nat
1111
Zero.
1212
S : Nat -> Nat
1313
Successor.
14-
Main> Prelude.List : Type -> Type
14+
Main> data Prelude.List : Type -> Type
1515
Generic lists.
1616
Totality: total
1717
Constructors:
1818
Nil : List a
1919
Empty list
2020
(::) : a -> List a -> List a
2121
A non-empty list, consisting of a head element and the rest of the list.
22-
Main> Prelude.Show : Type -> Type
22+
Main> interface Prelude.Show : Type -> Type
2323
Things that have a canonical `String` representation.
2424
Parameters: ty
2525
Constructor: MkShow
@@ -67,7 +67,7 @@ Main> Prelude.show : Show ty => ty -> String
6767
Convert a value to its `String` representation.
6868
@ x the value to convert
6969
Totality: total
70-
Main> Prelude.Monad : (Type -> Type) -> Type
70+
Main> interface Prelude.Monad : (Type -> Type) -> Type
7171
Parameters: m
7272
Constraints: Applicative m
7373
Constructor: MkMonad

tests/idris2/docs002/expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ world : Nat -> Nat
1010
Doc> Doc.NS.NestedNS.Foo : Type
1111
A type of Foo
1212

13-
Doc> Doc.WrappedInt : Type
13+
Doc> data Doc.WrappedInt : Type
1414
Totality: total
1515
Constructor: MkWrappedInt : Int -> WrappedInt
16-
Doc> Doc.SimpleRec : Type
16+
Doc> record Doc.SimpleRec : Type
1717
Totality: total
1818
Constructor: MkSimpleRec : Int -> String -> SimpleRec
1919
Projections:

tests/idris2/docs003/expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
1/1: Building RecordDoc (RecordDoc.idr)
22
RecordDoc>
3-
RecordDoc> RecordDoc.A : Type -> Type
3+
RecordDoc> record RecordDoc.A : Type -> Type
44
Totality: total
55
Constructor: __mkA : _
66
Projection: anA : A a -> a
7-
RecordDoc> RecordDoc.Tuple : Type -> Type -> Type
7+
RecordDoc> record RecordDoc.Tuple : Type -> Type -> Type
88
Totality: total
99
Constructor: __mkTuple : _
1010
Projections:
1111
proj1 : Tuple a b -> a
1212
proj2 : Tuple a b -> b
13-
RecordDoc> RecordDoc.Singleton : a -> Type
13+
RecordDoc> record RecordDoc.Singleton : a -> Type
1414
Totality: total
1515
Constructor: __mkSingleton : _
1616
Projections:

tests/idris2/interactive030/expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Main> Prelude.<$> : Functor f => (a -> b) -> f a -> f b
1818
@ func the function to apply
1919
Totality: total
2020
Fixity Declaration: infixr operator, level 4
21-
Main> Prelude.Monad : (Type -> Type) -> Type
21+
Main> interface Prelude.Monad : (Type -> Type) -> Type
2222
Parameters: m
2323
Constraints: Applicative m
2424
Constructor: MkMonad

0 commit comments

Comments
 (0)