@@ -4,6 +4,7 @@ import Core.Context
4
4
import Core.Context.Log
5
5
import Core.Core
6
6
import Core.Env
7
+ import Core.Metadata
7
8
import Core.TT
8
9
9
10
import Idris.Pretty
@@ -13,6 +14,7 @@ import Idris.Resugar
13
14
import Idris.Syntax
14
15
15
16
import TTImp.TTImp
17
+ import TTImp.TTImp.Functor
16
18
import TTImp.Elab.Prim
17
19
18
20
import Data.List
@@ -50,6 +52,7 @@ styleAnn (TCon _) = color BrightBlue
50
52
styleAnn DCon = color BrightRed
51
53
styleAnn (Fun _ ) = color BrightGreen
52
54
styleAnn Header = underline
55
+ styleAnn (Syntax syn) = syntaxAnn syn
53
56
styleAnn _ = []
54
57
55
58
export
@@ -99,17 +102,51 @@ addDocStringNS ns n_in doc
99
102
put Syn (record { docstrings $= addName n' doc,
100
103
saveDocstrings $= insert n' () } syn)
101
104
105
+ prettyTerm : IPTerm -> Doc IdrisDocAnn
106
+ prettyTerm = reAnnotate Syntax . Idris . Pretty . prettyTerm
107
+
108
+ showCategory : GlobalDef -> Doc IdrisDocAnn -> Doc IdrisDocAnn
109
+ showCategory d = case defDecoration (definition d) of
110
+ Nothing => id
111
+ Just decor => annotate (Syntax $ SynDecor decor)
112
+
113
+ prettyName : Name -> Doc IdrisDocAnn
114
+ prettyName n =
115
+ let root = nameRoot n in
116
+ if isOpName n then parens (pretty root) else pretty root
117
+
102
118
export
103
119
getDocsForPrimitive : {auto c : Ref Ctxt Defs} ->
104
120
{auto s : Ref Syn SyntaxInfo} ->
105
- Constant -> Core (List String )
121
+ Constant -> Core (Doc IdrisDocAnn )
106
122
getDocsForPrimitive constant = do
107
123
let (_ , type) = checkPrim EmptyFC constant
108
- let typeString = show constant ++ " : " ++ show ! (resugar [] type)
109
- pure [typeString ++ " \n\t Primitive" ]
110
-
111
- prettyTerm : PTerm -> Doc IdrisDocAnn
112
- prettyTerm = reAnnotate Syntax . Idris . Pretty . prettyTerm
124
+ let typeString = pretty (show constant)
125
+ <++> colon <++> prettyTerm ! (resugar [] type)
126
+ pure (typeString <+> Line <+> indent 2 " Primitive" )
127
+
128
+ data Config : Type where
129
+ ||| Configuration of the printer for a name
130
+ ||| @ longNames Do we print qualified names?
131
+ ||| @ dropFirst Do we drop the first argument in the type?
132
+ ||| @ getTotality Do we print the totality status of the function?
133
+ MkConfig : {default True longNames : Bool } ->
134
+ {default False dropFirst : Bool } ->
135
+ {default True getTotality : Bool } ->
136
+ Config
137
+
138
+ ||| Printer configuration for interface methods
139
+ ||| * longNames turned off for interface methods because the namespace is
140
+ ||| already spelt out for the interface itself
141
+ ||| * dropFirst turned on for interface methods because the first argument
142
+ ||| is always the interface constraint
143
+ ||| * totality turned off for interface methods because the methods themselves
144
+ ||| are just projections out of a record and so are total
145
+ methodsConfig : Config
146
+ methodsConfig
147
+ = MkConfig {longNames = False }
148
+ {dropFirst = True }
149
+ {getTotality = False }
113
150
114
151
export
115
152
getDocsForName : {auto o : Ref ROpts REPLOpts} ->
@@ -127,10 +164,12 @@ getDocsForName fc n
127
164
| _ => undefinedName fc n
128
165
let ns@(_ :: _ ) = concatMap (\ n => lookupName n (docstrings syn)) all
129
166
| [] => pure $ pretty (" No documentation for " ++ show n)
130
- docs <- traverse showDoc ns
167
+ docs <- traverse ( showDoc MkConfig ) ns
131
168
pure $ vcat (punctuate Line docs)
132
169
where
133
170
171
+ showDoc : Config -> (Name, String) -> Core (Doc IdrisDocAnn)
172
+
134
173
-- Avoid generating too much whitespace by not returning a single empty line
135
174
reflowDoc : String -> List (Doc IdrisDocAnn)
136
175
reflowDoc " " = []
@@ -142,11 +181,6 @@ getDocsForName fc n
142
181
Unchecked => " "
143
182
_ => header " Totality" <++> pretty tot
144
183
145
- prettyName : Name -> Doc IdrisDocAnn
146
- prettyName n =
147
- let root = nameRoot n in
148
- if isOpName n then parens (pretty root) else pretty root
149
-
150
184
getDConDoc : Name -> Core (Doc IdrisDocAnn)
151
185
getDConDoc con
152
186
= do defs <- get Ctxt
@@ -156,12 +190,13 @@ getDocsForName fc n
156
190
syn <- get Syn
157
191
ty <- resugar [] =<< normaliseHoles defs [] (type def)
158
192
let conWithTypeDoc = annotate (Decl con) (hsep [dCon (prettyName con), colon, prettyTerm ty])
159
- let [(n, str)] = lookupName con (docstrings syn)
160
- | _ => pure conWithTypeDoc
161
- pure $ vcat
162
- [ conWithTypeDoc
163
- , annotate DocStringBody $ vcat $ reflowDoc str
164
- ]
193
+ case lookupName con (docstrings syn) of
194
+ [(n, " " )] => pure conWithTypeDoc
195
+ [(n, str)] => pure $ vcat
196
+ [ conWithTypeDoc
197
+ , annotate DocStringBody $ vcat $ reflowDoc str
198
+ ]
199
+ _ => pure conWithTypeDoc
165
200
166
201
getImplDoc : Name -> Core (List (Doc IdrisDocAnn))
167
202
getImplDoc n
@@ -174,16 +209,9 @@ getDocsForName fc n
174
209
getMethDoc : Method -> Core (List (Doc IdrisDocAnn))
175
210
getMethDoc meth
176
211
= do syn <- get Syn
177
- let [(n, str) ] = lookupName meth. name (docstrings syn)
212
+ let [nstr ] = lookupName meth. name (docstrings syn)
178
213
| _ => pure []
179
- ty <- pterm meth. type
180
- let nm = prettyName meth. name
181
- pure $ pure $ vcat [
182
- annotate (Decl meth. name) (hsep [fun (meth. name) nm, colon, prettyTerm ty])
183
- , annotate DocStringBody $ vcat (
184
- toList (indent 2 . pretty . show <$> meth. totalReq)
185
- ++ reflowDoc str)
186
- ]
214
+ pure <$> showDoc methodsConfig nstr
187
215
188
216
getInfixDoc : Name -> Core (List (Doc IdrisDocAnn))
189
217
getInfixDoc n
@@ -217,9 +245,12 @@ getDocsForName fc n
217
245
[] => []
218
246
ps => [hsep (header " Parameters" :: punctuate comma (map (pretty . show ) ps))]
219
247
let constraints =
220
- case ! (traverse pterm (parents iface)) of
248
+ case ! (traverse ( pterm . map ( MkKindedName Nothing )) (parents iface)) of
221
249
[] => []
222
250
ps => [hsep (header " Constraints" :: punctuate comma (map (pretty . show ) ps))]
251
+ let icon = case dropNS (iconstructor iface) of
252
+ DN _ _ => [] -- machine inserted
253
+ nm => [hsep [header " Constructor" , dCon (prettyName nm)]]
223
254
mdocs <- traverse getMethDoc (methods iface)
224
255
let meths = case concat mdocs of
225
256
[] => []
@@ -233,7 +264,7 @@ getDocsForName fc n
233
264
[doc] => [header " Implementation" <++> annotate Declarations doc]
234
265
docs => [vcat [header " Implementations"
235
266
, annotate Declarations $ vcat $ map (indent 2 ) docs]]
236
- pure (vcat (params ++ constraints ++ meths ++ insts))
267
+ pure (vcat (params ++ constraints ++ icon ++ meths ++ insts))
237
268
238
269
getFieldDoc : Name -> Core (Doc IdrisDocAnn)
239
270
getFieldDoc nm
@@ -243,7 +274,7 @@ getDocsForName fc n
243
274
-- should never happen, since we know that the DCon exists:
244
275
| Nothing => pure Empty
245
276
ty <- resugar [] =<< normaliseHoles defs [] (type def)
246
- let prettyName = pretty (nameRoot nm)
277
+ let prettyName = prettyName nm
247
278
let projDecl = annotate (Decl nm) $ hsep [ fun nm prettyName, colon, prettyTerm ty ]
248
279
case lookupName nm (docstrings syn) of
249
280
[(_ , " " )] => pure projDecl
@@ -290,26 +321,24 @@ getDocsForName fc n
290
321
pure (tot ++ cdoc)
291
322
_ => pure []
292
323
293
- showCategory : GlobalDef -> Doc IdrisDocAnn -> Doc IdrisDocAnn
294
- showCategory d = case definition d of
295
- TCon _ _ _ _ _ _ _ _ => tCon (fullname d)
296
- DCon _ _ _ => dCon
297
- PMDef _ _ _ _ _ => fun (fullname d)
298
- ForeignDef _ _ => fun (fullname d)
299
- Builtin _ => fun (fullname d)
300
- _ => id
301
-
302
- showDoc : (Name, String) -> Core (Doc IdrisDocAnn)
303
- showDoc (n, str)
324
+ showDoc (MkConfig {longNames, dropFirst, getTotality}) (n, str)
304
325
= do defs <- get Ctxt
305
326
Just def <- lookupCtxtExact n (gamma defs)
306
327
| Nothing => undefinedName fc n
307
328
ty <- resugar [] =<< normaliseHoles defs [] (type def)
329
+ -- when printing e.g. interface methods there is no point in
330
+ -- repeating the interface's name
331
+ let ty = ifThenElse (not dropFirst) ty $ case ty of
332
+ PPi _ _ AutoImplicit _ _ sc => sc
333
+ _ => ty
308
334
let cat = showCategory def
309
335
nm <- aliasName n
310
- let docDecl = annotate (Decl n) (hsep [cat (pretty (show nm)), colon, prettyTerm ty])
336
+ -- when printing e.g. interface methods there is no point in
337
+ -- 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])
311
340
let docText = reflowDoc str
312
- extra <- getExtra n def
341
+ extra <- ifThenElse getTotality ( getExtra n def) (pure [])
313
342
fixes <- getFixityDoc n
314
343
let docBody = annotate DocStringBody $ vcat $ docText ++ (map (indent 2 ) (extra ++ fixes))
315
344
pure (vcat [docDecl, docBody])
@@ -320,7 +349,8 @@ getDocsForPTerm : {auto o : Ref ROpts REPLOpts} ->
320
349
{auto s : Ref Syn SyntaxInfo} ->
321
350
PTerm -> Core (List String)
322
351
getDocsForPTerm (PRef fc name) = pure $ [! (render styleAnn ! (getDocsForName fc name))]
323
- getDocsForPTerm (PPrimVal _ constant) = getDocsForPrimitive constant
352
+ getDocsForPTerm (PPrimVal _ constant)
353
+ = pure [! (render styleAnn ! (getDocsForPrimitive constant))]
324
354
getDocsForPTerm (PType _ ) = pure [" Type : Type\n\t The type of all types is Type. The type of Type is Type." ]
325
355
getDocsForPTerm (PString _ _ ) = pure [" String Literal\n\t Desugars to a fromString call" ]
326
356
getDocsForPTerm (PList _ _ _ ) = pure [" List Literal\n\t Desugars to (::) and Nil" ]
@@ -332,24 +362,26 @@ getDocsForPTerm pterm = pure ["Docs not implemented for " ++ show pterm ++ " yet
332
362
333
363
summarise : {auto c : Ref Ctxt Defs} ->
334
364
{auto s : Ref Syn SyntaxInfo} ->
335
- Name -> Core String
365
+ Name -> Core (Doc IdrisDocAnn)
336
366
summarise n -- n is fully qualified
337
367
= do syn <- get Syn
338
368
defs <- get Ctxt
339
369
Just def <- lookupCtxtExact n (gamma defs)
340
370
| _ => pure " "
341
- let doc = case lookupName n (docstrings syn) of
342
- [(_ , doc)] => case Extra . lines doc of
343
- (" " ::: _ ) => Nothing
344
- (d ::: _ ) => Just d
345
- _ => Nothing
371
+ -- let doc = case lookupName n (docstrings syn) of
372
+ -- [(_, doc)] => case Extra.lines doc of
373
+ -- ("" ::: _) => Nothing
374
+ -- (d ::: _) => Just d
375
+ -- _ => Nothing
346
376
ty <- normaliseHoles defs [] (type def)
347
- pure (nameRoot n ++ " : " ++ show ! (resugar [] ty) ++
348
- maybe " " ((++ ) " \n\t " ) doc)
377
+ pure $ showCategory def (prettyName n)
378
+ <++> colon <++> hang 0 (prettyTerm ! (resugar [] ty))
379
+ -- <+> maybe "" ((Line <+>) . indent 2 . pretty) doc)
349
380
350
381
-- Display all the exported names in the given namespace
351
382
export
352
- getContents : {auto c : Ref Ctxt Defs} ->
383
+ getContents : {auto o : Ref ROpts REPLOpts} ->
384
+ {auto c : Ref Ctxt Defs} ->
353
385
{auto s : Ref Syn SyntaxInfo} ->
354
386
Namespace -> Core (List String)
355
387
getContents ns
@@ -359,7 +391,7 @@ getContents ns
359
391
ns <- allNames (gamma defs)
360
392
let allNs = filter inNS ns
361
393
allNs <- filterM (visible defs) allNs
362
- traverse summarise (sort allNs)
394
+ traverse ( \ ns => render styleAnn ! ( summarise ns)) (sort allNs)
363
395
where
364
396
visible : Defs -> Name -> Core Bool
365
397
visible defs n
0 commit comments