|
| 1 | +open Infix |
| 2 | +open TopTypes |
| 3 | + |
| 4 | +let isMl path = |
| 5 | + Filename.check_suffix path ".ml" || Filename.check_suffix path ".mli" |
| 6 | + |
| 7 | +let odocToMd text = MarkdownOfOCamldoc.convert text |
| 8 | + |
| 9 | +let compose fn1 fn2 arg = fn1 arg |> fn2 |
| 10 | + |
| 11 | +let converter src = |
| 12 | + let mlToOutput s = [compose odocToMd Omd.to_markdown s] in |
| 13 | + fold src mlToOutput (fun src -> |
| 14 | + match isMl src with true -> mlToOutput | false -> fun x -> [x]) |
| 15 | + |
| 16 | +let newDocsForCmt ~moduleName cmtCache changed cmt src = |
| 17 | + let uri = Uri2.fromPath (src |? cmt) in |
| 18 | + match |
| 19 | + Process_406.fileForCmt ~moduleName ~uri cmt (converter src) |
| 20 | + |> RResult.toOptionAndLog |
| 21 | + with |
| 22 | + | None -> None |
| 23 | + | Some file -> |
| 24 | + Hashtbl.replace cmtCache cmt (changed, file); |
| 25 | + Some file |
| 26 | + |
| 27 | +let docsForCmt ~moduleName cmt src state = |
| 28 | + if Hashtbl.mem state.cmtCache cmt then |
| 29 | + let mtime, docs = Hashtbl.find state.cmtCache cmt in |
| 30 | + (* TODO: I should really throttle this mtime checking to like every 50 ms or so *) |
| 31 | + match Files.getMtime cmt with |
| 32 | + | None -> |
| 33 | + Log.log |
| 34 | + ("\226\154\160\239\184\143 cannot get docs for nonexistant cmt " ^ cmt); |
| 35 | + None |
| 36 | + | Some changed -> |
| 37 | + if changed > mtime then |
| 38 | + newDocsForCmt ~moduleName state.cmtCache changed cmt src |
| 39 | + else Some docs |
| 40 | + else |
| 41 | + match Files.getMtime cmt with |
| 42 | + | None -> |
| 43 | + Log.log |
| 44 | + ("\226\154\160\239\184\143 cannot get docs for nonexistant cmt " ^ cmt); |
| 45 | + None |
| 46 | + | Some changed -> newDocsForCmt ~moduleName state.cmtCache changed cmt src |
| 47 | + |
| 48 | +open Infix |
| 49 | + |
| 50 | +let getFullFromCmt ~state ~uri = |
| 51 | + let path = Uri2.toPath uri in |
| 52 | + match Packages.getPackage uri state with |
| 53 | + | Error e -> Error e |
| 54 | + | Ok package -> ( |
| 55 | + let moduleName = |
| 56 | + BuildSystem.namespacedName package.namespace (FindFiles.getName path) |
| 57 | + in |
| 58 | + match Hashtbl.find_opt package.pathsForModule moduleName with |
| 59 | + | Some paths -> ( |
| 60 | + let cmt = SharedTypes.getCmt ~interface:(Utils.endsWith path "i") paths in |
| 61 | + match Process_406.fullForCmt ~moduleName ~uri cmt (fun x -> [x]) with |
| 62 | + | Error e -> Error e |
| 63 | + | Ok full -> |
| 64 | + Hashtbl.replace package.interModuleDependencies moduleName |
| 65 | + (SharedTypes.hashList full.extra.externalReferences |> List.map fst); |
| 66 | + Ok (package, full) ) |
| 67 | + | None -> Error ("can't find module " ^ moduleName) ) |
| 68 | + |
| 69 | +let docsForModule modname state ~package = |
| 70 | + if Hashtbl.mem package.pathsForModule modname then ( |
| 71 | + let paths = Hashtbl.find package.pathsForModule modname in |
| 72 | + (* TODO: do better *) |
| 73 | + let cmt = SharedTypes.getCmt paths in |
| 74 | + let src = SharedTypes.getSrc paths in |
| 75 | + Log.log ("FINDING docs for module " ^ SharedTypes.showPaths paths); |
| 76 | + Log.log ("FINDING " ^ cmt ^ " src " ^ (src |? "")); |
| 77 | + match docsForCmt ~moduleName:modname cmt src state with |
| 78 | + | None -> None |
| 79 | + | Some docs -> Some (docs, src) ) |
| 80 | + else ( |
| 81 | + Log.log ("No path for module " ^ modname); |
| 82 | + None ) |
| 83 | + |
| 84 | +let fileForUri state uri = |
| 85 | + match getFullFromCmt ~state ~uri with |
| 86 | + | Error e -> Error e |
| 87 | + | Ok (_package, {extra; file}) -> Ok (file, extra) |
| 88 | + |
| 89 | +let fileForModule state ~package modname = |
| 90 | + match docsForModule modname state ~package with |
| 91 | + | None -> None |
| 92 | + | Some (file, _) -> Some file |
0 commit comments