1{-# LANGUAGE PatternGuards #-}
2module Idris.Docs (pprintDocs, getDocs, pprintConstDocs, FunDoc(..), Docs (..)) where
5import Idris.AbsSyntaxTree
8import Idris.Core.Evaluate
15import qualified Data.Text as T
17-- TODO: Only include names with public/abstract accessibility
19data FunDoc = FD Name Docstring
20 [(Name, PTerm, Plicity, Maybe Docstring)] -- args: name, ty, implicit, docs
21 PTerm -- function type
25data Docs = FunDoc FunDoc
26 | DataDoc FunDoc -- type constructor docs
27 [FunDoc] -- data constructor docs
28 | ClassDoc Name Docstring -- class docs
29 [FunDoc] -- method docs
30 [(Name, Maybe Docstring)] -- parameters and their docstrings
32 [PTerm] -- superclasses
35showDoc d | nullDocstring d = empty
36 | otherwise = text " -- " <> renderDocstring d
38pprintFD :: IState -> FunDoc -> Doc OutputAnnotation
39pprintFD ist (FD n doc args ty f)
40 = nest 4 (prettyName True (ppopt_impl ppo) [] n <+> colon <+>
41 pprintPTerm ppo [] [ n | (n@(UN n'),_,_,_) <- args
42 , not (T.isPrefixOf (T.pack "__") n') ] infixes ty <$>
43 renderDocstring doc <$>
44 maybe empty (\f -> text (show f) <> line) f <>
45 let argshow = showArgs args [] in
47 then nest 4 $ text "Arguments:" <$> vsep argshow
50 where ppo = ppOptionIst ist
51 infixes = idris_infixes ist
52 showArgs ((n, ty, Exp {}, Just d):args) bnd
53 = bindingOf n False <+> colon <+>
54 pprintPTerm ppo bnd [] infixes ty <>
57 showArgs args ((n, False):bnd)
58 showArgs ((n, ty, Constraint {}, Just d):args) bnd
59 = text "Class constraint" <+>
60 pprintPTerm ppo bnd [] infixes ty <> showDoc d <> line
62 showArgs args ((n, True):bnd)
63 showArgs ((n, ty, Imp {}, Just d):args) bnd
64 = text "(implicit)" <+>
65 bindingOf n True <+> colon <+>
66 pprintPTerm ppo bnd [] infixes ty <>
69 showArgs args ((n, True):bnd)
70 showArgs ((n, _, _, _):args) bnd = showArgs args ((n, True):bnd)
74pprintDocs :: IState -> Docs -> Doc OutputAnnotation
75pprintDocs ist (FunDoc d) = pprintFD ist d
76pprintDocs ist (DataDoc t args)
77 = text "Data type" <+> pprintFD ist t <$>
78 if null args then text "No constructors."
79 else nest 4 (text "Constructors:" <> line <>
80 vsep (map (pprintFD ist) args))
81pprintDocs ist (ClassDoc n doc meths params instances superclasses)
82 = nest 4 (text "Type class" <+> prettyName True (ppopt_impl ppo) [] n <>
83 if nullDocstring doc then empty else line <> renderDocstring doc)
85 nest 4 (text "Parameters:" <$> prettyParameters)
87 nest 4 (text "Methods:" <$>
88 vsep (map (pprintFD ist) meths))
90 nest 4 (text "Instances:" <$>
91 vsep (if null instances' then [text "<no instances>"]
92 else map dumpInstance instances'))
94 (if null subclasses then empty
95 else line <$> nest 4 (text "Subclasses:" <$>
96 vsep (map (dumpInstance . prettifySubclasses) subclasses)))
98 (if null superclasses then empty
99 else line <$> nest 4 (text "Default superclass instances:" <$>
100 vsep (map dumpInstance superclasses)))
102 params' = zip pNames (repeat False)
104 pNames = map fst params
106 ppo = ppOptionIst ist
107 infixes = idris_infixes ist
109 dumpInstance :: PTerm -> Doc OutputAnnotation
110 dumpInstance = pprintPTerm ppo params' [] infixes
112 prettifySubclasses (PPi (Constraint _ _) _ tm _) = prettifySubclasses tm
113 prettifySubclasses (PPi plcity nm t1 t2) = PPi plcity (safeHead nm pNames) (prettifySubclasses t1) (prettifySubclasses t2)
114 prettifySubclasses (PApp fc ref args) = PApp fc ref $ updateArgs pNames args
115 prettifySubclasses tm = tm
120 updateArgs (p:ps) ((PExp prty opts _ ref):as) = (PExp prty opts p (updateRef p ref)) : updateArgs ps as
121 updateArgs ps (a:as) = a : updateArgs ps as
124 updateRef nm (PRef fc _) = PRef fc nm
127 isSubclass (PPi (Constraint _ _) _ (PApp _ _ args) (PApp _ (PRef _ nm) args')) = nm == n && map getTm args == map getTm args'
128 isSubclass (PPi _ _ _ pt) = isSubclass pt
131 (subclasses, instances') = partition isSubclass instances
133 prettyParameters = if any (isJust . snd) params
134 then vsep (map (\(nm,md) -> prettyName True False params' nm <+> maybe empty showDoc md) params)
135 else hsep (punctuate comma (map (prettyName True False params' . fst) params))
137getDocs :: Name -> Idris Docs
140 case lookupCtxt n (idris_classes i) of
141 [ci] -> docClass n ci
142 _ -> case lookupCtxt n (idris_datatypes i) of
144 _ -> do fd <- docFun n
147docData :: Name -> TypeInfo -> Idris Docs
149 = do tdoc <- docFun n
150 cdocs <- mapM docFun (con_names ti)
151 return (DataDoc tdoc cdocs)
153docClass :: Name -> ClassInfo -> Idris Docs
156 let docStrings = listToMaybe $ lookupCtxt n $ idris_docstrings i
157 docstr = maybe emptyDocstring fst docStrings
158 params = map (\pn -> (pn, docStrings >>= (lookup pn . snd))) (class_params ci)
159 instances = map (delabTy i) (class_instances ci)
160 superclasses = catMaybes $ map getDInst (class_default_superclasses ci)
161 mdocs <- mapM (docFun . fst) (class_methods ci)
162 return $ ClassDoc n docstr mdocs params instances superclasses
164 getDInst (PInstance _ _ _ _ _ t _ _) = Just t
167docFun :: Name -> Idris FunDoc
170 let (docstr, argDocs) = case lookupCtxt n (idris_docstrings i) of
174 let args = getPArgNames ty argDocs
175 let infixes = idris_infixes i
176 let fixdecls = filter (\(Fix _ x) -> x == funName n) infixes
177 let f = case fixdecls of
179 (Fix x _:_) -> Just x
181 return (FD n docstr args ty f)
182 where funName :: Name -> String
183 funName (UN n) = str n
184 funName (NS n _) = funName n
186 | n == falseTy = "_|_"
188getPArgNames :: PTerm -> [(Name, Docstring)] -> [(Name, PTerm, Plicity, Maybe Docstring)]
189getPArgNames (PPi plicity name ty body) ds =
190 (name, ty, plicity, lookup name ds) : getPArgNames body ds
193pprintConstDocs :: IState -> Const -> String -> Doc OutputAnnotation
194pprintConstDocs ist c str = text "Primitive" <+> text (if constIsType c then "type" else "value") <+>
195 pprintPTerm (ppOptionIst ist) [] [] [] (PConstant c) <+> colon <+>
196 pprintPTerm (ppOptionIst ist) [] [] [] (t c) <>
197 nest 4 (line <> text str)
199 where t (Fl _) = PConstant $ AType ATFloat
200 t (BI _) = PConstant $ AType (ATInt ITBig)
201 t (Str _) = PConstant StrType
202 t (Ch c) = PConstant $ AType (ATInt ITChar)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list