@@ -225,113 +225,125 @@ let makeId modulePath ~identifier =
225
225
226
226
let extractDocs ~path ~debug =
227
227
if debug then Printf. printf " extracting docs for %s\n " path;
228
- if
229
- FindFiles. isImplementation path = false
230
- && FindFiles. isInterface path = false
231
- then (
232
- Printf. eprintf " error: failed to read %s, expected an .res or .resi file\n "
233
- path;
234
- exit 1 );
235
- let path =
236
- if FindFiles. isImplementation path then
237
- let pathAsResi =
238
- (path |> Filename. dirname) ^ " /"
239
- ^ (path |> Filename. basename |> Filename. chop_extension)
240
- ^ " .resi"
228
+ let result =
229
+ match
230
+ FindFiles. isImplementation path = false
231
+ && FindFiles. isInterface path = false
232
+ with
233
+ | false -> (
234
+ let path =
235
+ if FindFiles. isImplementation path then
236
+ let pathAsResi =
237
+ (path |> Filename. dirname) ^ " /"
238
+ ^ (path |> Filename. basename |> Filename. chop_extension)
239
+ ^ " .resi"
240
+ in
241
+ if Sys. file_exists pathAsResi then (
242
+ if debug then
243
+ Printf. printf " preferring found resi file for impl: %s\n "
244
+ pathAsResi;
245
+ pathAsResi)
246
+ else path
247
+ else path
241
248
in
242
- if Sys. file_exists pathAsResi then (
243
- if debug then
244
- Printf. printf " preferring found resi file for impl: %s\n " pathAsResi;
245
- pathAsResi)
246
- else path
247
- else path
248
- in
249
- match Cmt. loadFullCmtFromPath ~path with
250
- | None ->
251
- Printf. eprintf
252
- " error: failed to generate doc for %s, try to build the project\n " path;
253
- exit 1
254
- | Some full ->
255
- let file = full.file in
256
- let structure = file.structure in
257
- let open SharedTypes in
258
- let env = QueryEnv. fromFile file in
259
- let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
260
- (structure : Module.structure ) =
261
- {
262
- id = modulePath |> List. rev |> ident;
263
- docstring = structure.docstring |> List. map String. trim;
264
- name = structure.name;
265
- deprecated = structure.deprecated;
266
- items =
267
- structure.items
268
- |> List. filter_map (fun (item : Module.item ) ->
269
- match item.kind with
270
- | Value typ ->
271
- Some
272
- (Value
273
- {
274
- id = modulePath |> makeId ~identifier: item.name;
275
- docstring = item.docstring |> List. map String. trim;
276
- signature =
277
- " let " ^ item.name ^ " : " ^ Shared. typeToString typ;
278
- name = item.name;
279
- deprecated = item.deprecated;
280
- })
281
- | Type (typ , _ ) ->
282
- Some
283
- (Type
284
- {
285
- id = modulePath |> makeId ~identifier: item.name;
286
- docstring = item.docstring |> List. map String. trim;
287
- signature = typ.decl |> Shared. declToString item.name;
288
- name = item.name;
289
- deprecated = item.deprecated;
290
- detail = typeDetail typ ~full ~env ;
291
- })
292
- | Module (Ident p ) ->
293
- (* module Whatever = OtherModule *)
294
- let aliasToModule = p |> pathIdentToString in
295
- let id =
296
- (modulePath |> List. rev |> List. hd) ^ " ." ^ item.name
297
- in
298
- let items, internalDocstrings =
299
- match
300
- ProcessCmt. fileForModule ~package: full.package
301
- aliasToModule
302
- with
303
- | None -> ([] , [] )
304
- | Some file ->
305
- let docs =
306
- extractDocsForModule ~module Path:[id] file.structure
249
+ match Cmt. loadFullCmtFromPath ~path with
250
+ | None ->
251
+ Error
252
+ (Printf. sprintf
253
+ " error: failed to generate doc for %s, try to build the project"
254
+ path)
255
+ | Some full ->
256
+ let file = full.file in
257
+ let structure = file.structure in
258
+ let open SharedTypes in
259
+ let env = QueryEnv. fromFile file in
260
+ let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
261
+ (structure : Module.structure ) =
262
+ {
263
+ id = modulePath |> List. rev |> ident;
264
+ docstring = structure.docstring |> List. map String. trim;
265
+ name = structure.name;
266
+ deprecated = structure.deprecated;
267
+ items =
268
+ structure.items
269
+ |> List. filter_map (fun (item : Module.item ) ->
270
+ match item.kind with
271
+ | Value typ ->
272
+ Some
273
+ (Value
274
+ {
275
+ id = modulePath |> makeId ~identifier: item.name;
276
+ docstring = item.docstring |> List. map String. trim;
277
+ signature =
278
+ " let " ^ item.name ^ " : "
279
+ ^ Shared. typeToString typ;
280
+ name = item.name;
281
+ deprecated = item.deprecated;
282
+ })
283
+ | Type (typ , _ ) ->
284
+ Some
285
+ (Type
286
+ {
287
+ id = modulePath |> makeId ~identifier: item.name;
288
+ docstring = item.docstring |> List. map String. trim;
289
+ signature =
290
+ typ.decl |> Shared. declToString item.name;
291
+ name = item.name;
292
+ deprecated = item.deprecated;
293
+ detail = typeDetail typ ~full ~env ;
294
+ })
295
+ | Module (Ident p ) ->
296
+ (* module Whatever = OtherModule *)
297
+ let aliasToModule = p |> pathIdentToString in
298
+ let id =
299
+ (modulePath |> List. rev |> List. hd) ^ " ." ^ item.name
300
+ in
301
+ let items, internalDocstrings =
302
+ match
303
+ ProcessCmt. fileForModule ~package: full.package
304
+ aliasToModule
305
+ with
306
+ | None -> ([] , [] )
307
+ | Some file ->
308
+ let docs =
309
+ extractDocsForModule ~module Path:[id]
310
+ file.structure
311
+ in
312
+ (docs.items, docs.docstring)
307
313
in
308
- (docs.items, docs.docstring)
309
- in
310
- Some
311
- (ModuleAlias
312
- {
313
- id;
314
- name = item.name;
315
- items;
316
- docstring =
317
- item.docstring @ internalDocstrings
318
- |> List. map String. trim;
319
- })
320
- | Module (Structure m ) ->
321
- (* module Whatever = {} in res or module Whatever: {} in resi. *)
322
- Some
323
- (Module
324
- (extractDocsForModule ~module Path:(m.name :: modulePath)
325
- m))
326
- | Module (Constraint (Structure _impl , Structure interface )) ->
327
- (* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
328
- Some
329
- (Module
330
- (extractDocsForModule
331
- ~module Path:(interface.name :: modulePath)
332
- interface))
333
- | _ -> None );
334
- }
335
- in
336
- let docs = extractDocsForModule structure in
337
- print_endline (stringifyDocsForModule ~original Env:env docs)
314
+ Some
315
+ (ModuleAlias
316
+ {
317
+ id;
318
+ name = item.name;
319
+ items;
320
+ docstring =
321
+ item.docstring @ internalDocstrings
322
+ |> List. map String. trim;
323
+ })
324
+ | Module (Structure m ) ->
325
+ (* module Whatever = {} in res or module Whatever: {} in resi. *)
326
+ Some
327
+ (Module
328
+ (extractDocsForModule
329
+ ~module Path:(m.name :: modulePath) m))
330
+ | Module
331
+ (Constraint (Structure _impl, Structure interface)) ->
332
+ (* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
333
+ Some
334
+ (Module
335
+ (extractDocsForModule
336
+ ~module Path:(interface.name :: modulePath)
337
+ interface))
338
+ | _ -> None );
339
+ }
340
+ in
341
+ let docs = extractDocsForModule structure in
342
+ Ok (stringifyDocsForModule ~original Env:env docs))
343
+ | true ->
344
+ Error
345
+ (Printf. sprintf
346
+ " error: failed to read %s, expected an .res or .resi file" path)
347
+ in
348
+
349
+ result
0 commit comments