2022-05-19 22:36:21 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2022 Henrik Böving. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Authors: Henrik Böving
|
|
|
|
|
-/
|
|
|
|
|
import Lean
|
|
|
|
|
|
|
|
|
|
import DocGen4.Process.Base
|
|
|
|
|
import DocGen4.Process.Attributes
|
|
|
|
|
|
|
|
|
|
namespace DocGen4.Process
|
|
|
|
|
open Lean Meta
|
|
|
|
|
|
|
|
|
|
def NameInfo.ofTypedName (n : Name) (t : Expr) : MetaM NameInfo := do
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
pure { name := n, type := ←prettyPrintTerm t, doc := ←findDocString? env n}
|
|
|
|
|
|
|
|
|
|
partial def typeToArgsType (e : Expr) : (Array (Name × Expr × BinderInfo) × Expr) :=
|
|
|
|
|
let helper := λ name type body data =>
|
|
|
|
|
-- Once we hit a name with a macro scope we stop traversing the expression
|
|
|
|
|
-- and print what is left after the : instead. The only exception
|
|
|
|
|
-- to this is instances since these almost never have a name
|
|
|
|
|
-- but should still be printed as arguments instead of after the :.
|
2022-07-20 14:18:57 +00:00
|
|
|
|
if name.hasMacroScopes ∧ ¬data.isInstImplicit then
|
2022-05-19 22:36:21 +00:00
|
|
|
|
(#[], e)
|
|
|
|
|
else
|
|
|
|
|
let name := name.eraseMacroScopes
|
2022-07-20 14:18:57 +00:00
|
|
|
|
let arg := (name, type, data)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
let (args, final) := typeToArgsType (Expr.instantiate1 body (mkFVar ⟨name⟩))
|
|
|
|
|
(#[arg] ++ args, final)
|
|
|
|
|
match e.consumeMData with
|
2022-07-20 14:18:57 +00:00
|
|
|
|
| Expr.lam name type body binderInfo => helper name type body binderInfo
|
|
|
|
|
| Expr.forallE name type body binderInfo => helper name type body binderInfo
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| _ => (#[], e)
|
|
|
|
|
|
|
|
|
|
def Info.ofConstantVal (v : ConstantVal) : MetaM Info := do
|
|
|
|
|
let (args, type) := typeToArgsType v.type
|
2022-07-23 11:01:25 +00:00
|
|
|
|
let args ← args.mapM (λ (n, e, b) => do pure <| Arg.mk n (←prettyPrintTerm e) b)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
let nameInfo ← NameInfo.ofTypedName v.name type
|
|
|
|
|
match ←findDeclarationRanges? v.name with
|
|
|
|
|
-- TODO: Maybe selection range is more relevant? Figure this out in the future
|
2022-07-23 11:37:17 +00:00
|
|
|
|
| some range => pure {
|
|
|
|
|
toNameInfo := nameInfo,
|
|
|
|
|
args,
|
|
|
|
|
declarationRange := range.range,
|
|
|
|
|
attrs := (←getAllAttributes v.name)
|
|
|
|
|
}
|
2022-05-19 22:36:21 +00:00
|
|
|
|
| none => panic! s!"{v.name} is a declaration without position"
|
|
|
|
|
|
|
|
|
|
end DocGen4.Process
|