on master help sign in
Improve display of documenation for named instances
beda221David Raymond Christiansen
idris.cabal+4−1
@@ -628,7 +628,10 @@ Extra-source-files:
628 test/docs002/input
628 test/docs002/input
629 test/docs002/*.idr
629 test/docs002/*.idr
630 test/docs002/expected
630 test/docs002/expected
631−
631+ test/docs003/run
632+ test/docs003/input
633+ test/docs003/*.idr
634+ test/docs003/expected
632
635
633
636
634source-repository head
637source-repository head
src/Idris/Docs.hs+23−7
@@ -1,4 +1,5 @@
1−{-# LANGUAGE DeriveFunctor, PatternGuards #-}
1+{-# LANGUAGE DeriveFunctor, PatternGuards, MultiWayIf #-}
2+{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
2module Idris.Docs (pprintDocs, getDocs, pprintConstDocs, FunDoc, FunDoc'(..), Docs, Docs'(..)) where
3module Idris.Docs (pprintDocs, getDocs, pprintConstDocs, FunDoc, FunDoc'(..), Docs, Docs'(..)) where
3
4
4import Idris.AbsSyntax
5import Idris.AbsSyntax
@@ -37,6 +38,7 @@ data Docs' d = FunDoc (FunDoc' d)
37 [(Maybe Name, PTerm, (d, [(Name, d)]))] -- instances: name for named instances, the constraint term, the docs
38 [(Maybe Name, PTerm, (d, [(Name, d)]))] -- instances: name for named instances, the constraint term, the docs
38 [PTerm] -- subclasses
39 [PTerm] -- subclasses
39 [PTerm] -- superclasses
40 [PTerm] -- superclasses
41+ | NamedInstanceDoc Name (FunDoc' d) -- name is class
40 | ModDoc [String] -- Module name
42 | ModDoc [String] -- Module name
41 d
43 d
42 deriving Functor
44 deriving Functor
@@ -183,6 +185,9 @@ pprintDocs ist (ClassDoc n doc meths params instances subclasses superclasses)
183 then vsep (map (\(nm,md) -> prettyName True False params' nm <+> maybe empty (showDoc ist) md) params)
185 then vsep (map (\(nm,md) -> prettyName True False params' nm <+> maybe empty (showDoc ist) md) params)
184 else hsep (punctuate comma (map (prettyName True False params' . fst) params))
186 else hsep (punctuate comma (map (prettyName True False params' . fst) params))
185
187
188+pprintDocs ist (NamedInstanceDoc _cls doc)
189+ = nest 4 (text "Named instance:" <$> pprintFD ist doc)
190+
186pprintDocs ist (ModDoc mod docs)
191pprintDocs ist (ModDoc mod docs)
187 = nest 4 $ text "Module" <+> text (concat (intersperse "." mod)) <> colon <$>
192 = nest 4 $ text "Module" <+> text (concat (intersperse "." mod)) <> colon <$>
188 renderDocstring (renderDocTerm (pprintDelab ist) (normaliseAll (tt_ctxt ist) [])) docs
193 renderDocstring (renderDocTerm (pprintDelab ist) (normaliseAll (tt_ctxt ist) [])) docs
@@ -203,13 +208,23 @@ getDocs n@(NS n' ns) w | n' == modDocName
203 " do not exist! This shouldn't have happened and is a bug."
208 " do not exist! This shouldn't have happened and is a bug."
204getDocs n w
209getDocs n w
205 = do i <- getIState
210 = do i <- getIState
206− docs <- case lookupCtxt n (idris_classes i) of
211+ docs <- if | Just ci <- lookupCtxtExact n (idris_classes i)
207− [ci] -> docClass n ci
212+ -> docClass n ci
208− _ -> case lookupCtxt n (idris_datatypes i) of
213+ | Just ti <- lookupCtxtExact n (idris_datatypes i)
209− [ti] -> docData n ti
214+ -> docData n ti
215+ | Just class_ <- classNameForInst i n
216+ -> do fd <- docFun n
217+ return $ NamedInstanceDoc class_ fd
218+ | otherwise
210− _ -> do fd <- docFun n
219+ -> do fd <- docFun n
211− return (FunDoc fd)
220+ return (FunDoc fd)
212 return $ fmap (howMuch w) docs
221 return $ fmap (howMuch w) docs
222+ where classNameForInst :: IState -> Name -> Maybe Name
223+ classNameForInst ist n =
224+ listToMaybe [ cn
225+ | (cn, ci) <- toAlist (idris_classes ist)
226+ , n `elem` class_instances ci
227+ ]
213
228
214docData :: Name -> TypeInfo -> Idris Docs
229docData :: Name -> TypeInfo -> Idris Docs
215docData n ti
230docData n ti
@@ -270,6 +285,7 @@ docFun n
270 where funName :: Name -> String
285 where funName :: Name -> String
271 funName (UN n) = str n
286 funName (UN n) = str n
272 funName (NS n _) = funName n
287 funName (NS n _) = funName n
288+ funName n = show n
273
289
274getPArgNames :: PTerm -> [(Name, Docstring DocTerm)] -> [(Name, PTerm, Plicity, Maybe (Docstring DocTerm))]
290getPArgNames :: PTerm -> [(Name, Docstring DocTerm)] -> [(Name, PTerm, Plicity, Maybe (Docstring DocTerm))]
275getPArgNames (PPi plicity name ty body) ds =
291getPArgNames (PPi plicity name ty body) ds =
test/docs003/docs003.idrnew file+9
@@ -0,0 +1,9 @@
1+module docs003
2+
3+instance [mine] Functor List where
4+ map m [] = []
5+ map m (x :: xs) = m x :: map m xs
6+
7+||| More functors!
8+instance [another] Functor List where
9+ map f xs = map @{mine} f xs
test/docs003/expectednew file+37
@@ -0,0 +1,37 @@
1+Type checking ./docs003.idr
2+Type class Functor
3+ Functors
4+
5+Parameters:
6+ f -- the action of the functor on objects
7+
8+Methods:
9+ map : Functor f => (m : a -> b) -> f a -> f b
10+ The action of the functor on morphisms
11+
12+Instances:
13+ Functor List
14+ Functor Stream
15+ Functor Provider
16+ Functor Binder
17+ Functor PrimIO
18+ Functor (IO' ffi)
19+ Functor Maybe
20+ Functor (Either e)
21+
22+Named instances:
23+ docs003.mine : Functor List
24+ docs003.another : Functor List
25+ More functors!
26+
27+Subclasses:
28+ Traversable f
29+ Applicative f
30+Named instance:
31+ mine : Functor List
32+
33+
34+Named instance:
35+ another : Functor List
36+ More functors!
37+
test/docs003/inputnew file+3
@@ -0,0 +1,3 @@
1+:doc Functor
2+:doc mine
3+:doc another
test/docs003/runnew file+3
@@ -0,0 +1,3 @@
1+#!/usr/bin/env bash
2+idris --consolewidth 80 --quiet --nocolor docs003.idr < input
3+rm *.ibc