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
|
2022-10-05 10:05:58 +00:00
|
|
|
|
import Lean.Data.HashMap
|
|
|
|
|
import Lean.Data.HashSet
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
import DocGen4.Process.Base
|
|
|
|
|
import DocGen4.Process.Hierarchy
|
|
|
|
|
import DocGen4.Process.DocInfo
|
|
|
|
|
|
|
|
|
|
namespace DocGen4.Process
|
|
|
|
|
|
2022-10-05 10:05:58 +00:00
|
|
|
|
open Lean Meta
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
Member of a module, either a declaration or some module doc string.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
inductive ModuleMember where
|
|
|
|
|
| docInfo (info : DocInfo) : ModuleMember
|
|
|
|
|
| modDoc (doc : ModuleDoc) : ModuleMember
|
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
A Lean module.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
structure Module where
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
Name of the module.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
name : Name
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
All members of the module, sorted according to their line numbers.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
members : Array ModuleMember
|
2022-07-22 12:48:36 +00:00
|
|
|
|
imports : Array Name
|
2022-05-19 22:36:21 +00:00
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
The result of running a full doc-gen analysis on a project.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
structure AnalyzerResult where
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
The map from module names to indices of the `moduleNames` array.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
name2ModIdx : HashMap Name ModuleIdx
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
The list of all modules, accessible nicely via `name2ModIdx`.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
moduleNames : Array Name
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
A map from module names to information about these modules.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
moduleInfo : HashMap Name Module
|
|
|
|
|
deriving Inhabited
|
|
|
|
|
|
|
|
|
|
namespace ModuleMember
|
|
|
|
|
|
|
|
|
|
def getDeclarationRange : ModuleMember → DeclarationRange
|
|
|
|
|
| docInfo i => i.getDeclarationRange
|
|
|
|
|
| modDoc i => i.declarationRange
|
|
|
|
|
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
An order for module members, based on their declaration range.
|
|
|
|
|
-/
|
2022-05-19 22:36:21 +00:00
|
|
|
|
def order (l r : ModuleMember) : Bool :=
|
|
|
|
|
Position.lt l.getDeclarationRange.pos r.getDeclarationRange.pos
|
|
|
|
|
|
|
|
|
|
def getName : ModuleMember → Name
|
|
|
|
|
| docInfo i => i.getName
|
2022-06-19 14:41:59 +00:00
|
|
|
|
| modDoc _ => Name.anonymous
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def getDocString : ModuleMember → Option String
|
|
|
|
|
| docInfo i => i.getDocString
|
|
|
|
|
| modDoc i => i.doc
|
|
|
|
|
|
|
|
|
|
end ModuleMember
|
|
|
|
|
|
2022-07-21 16:26:01 +00:00
|
|
|
|
inductive AnalyzeTask where
|
|
|
|
|
| loadAll (load : List Name) : AnalyzeTask
|
2022-07-21 20:43:33 +00:00
|
|
|
|
| loadAllLimitAnalysis (analyze : List Name) : AnalyzeTask
|
2022-07-21 16:26:01 +00:00
|
|
|
|
|
|
|
|
|
def AnalyzeTask.getLoad : AnalyzeTask → List Name
|
|
|
|
|
| loadAll load => load
|
2022-07-21 20:43:33 +00:00
|
|
|
|
| loadAllLimitAnalysis load => load
|
2022-07-21 16:26:01 +00:00
|
|
|
|
|
|
|
|
|
def getAllModuleDocs (relevantModules : Array Name) : MetaM (HashMap Name Module) := do
|
|
|
|
|
let env ← getEnv
|
|
|
|
|
let mut res := mkHashMap relevantModules.size
|
|
|
|
|
for module in relevantModules do
|
|
|
|
|
let modDocs := getModuleDoc? env module |>.getD #[] |>.map .modDoc
|
2022-07-22 12:48:36 +00:00
|
|
|
|
let some modIdx := env.getModuleIdx? module | unreachable!
|
2022-07-21 16:26:01 +00:00
|
|
|
|
let moduleData := env.header.moduleData.get! modIdx
|
2022-07-22 12:48:36 +00:00
|
|
|
|
let imports := moduleData.imports.map Import.module
|
2022-07-23 11:01:25 +00:00
|
|
|
|
res := res.insert module <| Module.mk module modDocs imports
|
2022-07-22 12:48:36 +00:00
|
|
|
|
pure res
|
2022-07-21 16:26:01 +00:00
|
|
|
|
|
2022-05-20 07:30:59 +00:00
|
|
|
|
/--
|
|
|
|
|
Run the doc-gen analysis on all modules that are loaded into the `Environment`
|
2022-07-21 16:26:01 +00:00
|
|
|
|
of this `MetaM` run and mentioned by the `AnalyzeTask`.
|
2022-05-20 07:30:59 +00:00
|
|
|
|
-/
|
2022-07-21 16:26:01 +00:00
|
|
|
|
def process (task : AnalyzeTask) : MetaM (AnalyzerResult × Hierarchy) := do
|
2022-05-19 22:36:21 +00:00
|
|
|
|
let env ← getEnv
|
2022-07-23 11:01:25 +00:00
|
|
|
|
let relevantModules := match task with
|
|
|
|
|
| .loadAll _ => HashSet.fromArray env.header.moduleNames
|
|
|
|
|
| .loadAllLimitAnalysis analysis => HashSet.fromArray analysis.toArray
|
2022-07-21 16:26:01 +00:00
|
|
|
|
let allModules := env.header.moduleNames
|
|
|
|
|
|
|
|
|
|
let mut res ← getAllModuleDocs relevantModules.toArray
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
|
for (name, cinfo) in env.constants.toList do
|
|
|
|
|
let some modidx := env.getModuleIdxFor? name | unreachable!
|
|
|
|
|
let moduleName := env.allImportedModuleNames.get! modidx
|
2022-07-21 16:26:01 +00:00
|
|
|
|
if !relevantModules.contains moduleName then
|
2022-07-20 23:40:04 +00:00
|
|
|
|
continue
|
|
|
|
|
|
2022-05-19 22:36:21 +00:00
|
|
|
|
try
|
2022-06-19 14:41:59 +00:00
|
|
|
|
let config := {
|
|
|
|
|
maxHeartbeats := 5000000,
|
|
|
|
|
options := ←getOptions,
|
|
|
|
|
fileName := ←getFileName,
|
|
|
|
|
fileMap := ←getFileMap
|
|
|
|
|
}
|
2022-07-21 16:26:01 +00:00
|
|
|
|
let analysis := Prod.fst <$> Meta.MetaM.toIO (DocInfo.ofConstant (name, cinfo)) config { env := env } {} {}
|
2022-05-19 22:36:21 +00:00
|
|
|
|
if let some dinfo ← analysis then
|
|
|
|
|
let moduleName := env.allImportedModuleNames.get! modidx
|
|
|
|
|
let module := res.find! moduleName
|
|
|
|
|
res := res.insert moduleName {module with members := module.members.push (ModuleMember.docInfo dinfo)}
|
|
|
|
|
catch e =>
|
2022-07-20 23:40:04 +00:00
|
|
|
|
IO.println s!"WARNING: Failed to obtain information for: {name}: {←e.toMessageData.toString}"
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
2022-07-21 16:26:01 +00:00
|
|
|
|
-- TODO: This could probably be faster if we did sorted insert above instead
|
2022-05-19 22:36:21 +00:00
|
|
|
|
for (moduleName, module) in res.toArray do
|
|
|
|
|
res := res.insert moduleName {module with members := module.members.qsort ModuleMember.order}
|
|
|
|
|
|
2022-07-21 16:26:01 +00:00
|
|
|
|
let hierarchy := Hierarchy.fromArray allModules
|
2022-07-20 23:40:04 +00:00
|
|
|
|
let analysis := {
|
2022-05-19 22:36:21 +00:00
|
|
|
|
name2ModIdx := env.const2ModIdx,
|
2022-07-21 16:26:01 +00:00
|
|
|
|
moduleNames := allModules,
|
2022-05-19 22:36:21 +00:00
|
|
|
|
moduleInfo := res,
|
|
|
|
|
}
|
2022-07-20 23:40:04 +00:00
|
|
|
|
pure (analysis, hierarchy)
|
2022-05-19 22:36:21 +00:00
|
|
|
|
|
|
|
|
|
def filterMapDocInfo (ms : Array ModuleMember) : Array DocInfo :=
|
|
|
|
|
ms.filterMap filter
|
|
|
|
|
where
|
|
|
|
|
filter : ModuleMember → Option DocInfo
|
|
|
|
|
| ModuleMember.docInfo i => some i
|
|
|
|
|
| _ => none
|
|
|
|
|
|
|
|
|
|
end DocGen4.Process
|