• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ocaml / odoc / 3053

19 Feb 2026 10:47AM UTC coverage: 71.712% (-1.2%) from 72.946%
3053

Pull #1399

github

web-flow
Merge bf200aeef into c3f0f46ee
Pull Request #1399: Upstream OxCaml

20 of 280 new or added lines in 21 files covered. (7.14%)

162 existing lines in 11 files now uncovered.

10399 of 14501 relevant lines covered (71.71%)

7008.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.19
/src/document/comment.ml
1
(*
2
 * Copyright (c) 2016, 2017 Thomas Refis <trefis@janestreet.com>
3
 *
4
 * Permission to use, copy, modify, and distribute this software for any
5
 * purpose with or without fee is hereby granted, provided that the above
6
 * copyright notice and this permission notice appear in all copies.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 *)
16

17
open Odoc_utils
18
open Types
19
module Comment = Odoc_model.Comment
20
open Odoc_model.Names
21

22
let default_lang_tag = "ocaml"
23

24
let source_of_code s =
25
  if s = "" then [] else [ Source.Elt [ inline @@ Inline.Text s ] ]
×
26

27
module Reference = struct
28
  open Odoc_model.Paths
29

30
  let rec render_resolved : Reference.Resolved.t -> string =
31
   fun r ->
32
    let open Reference.Resolved in
3,942✔
33
    match r with
34
    | `Identifier id -> Identifier.name id
3,197✔
35
    | `Alias (_, r) -> render_resolved (r :> t)
77✔
36
    | `AliasModuleType (_, r) -> render_resolved (r :> t)
2✔
37
    | `Module (r, s) -> render_resolved (r :> t) ^ "." ^ ModuleName.to_string s
160✔
38
    | `Hidden p -> render_resolved (p :> t)
×
39
    | `ModuleType (r, s) ->
78✔
40
        render_resolved (r :> t) ^ "." ^ ModuleTypeName.to_string s
78✔
41
    | `Type (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
194✔
42
    | `Constructor (r, s) ->
5✔
43
        render_resolved (r :> t) ^ "." ^ ConstructorName.to_string s
5✔
44
    | `PolyConstructor (r, s) ->
8✔
45
        render_resolved (r :> t) ^ ".`" ^ ConstructorName.to_string s
8✔
46
    | `Field (r, s) -> render_resolved (r :> t) ^ "." ^ FieldName.to_string s
×
NEW
47
    | `UnboxedField (r, s) -> render_resolved (r :> t) ^ "." ^ UnboxedFieldName.to_string s
×
48
    | `Extension (r, s) ->
3✔
49
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
3✔
50
    | `ExtensionDecl (r, _, s) ->
2✔
51
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
2✔
52
    | `Exception (r, s) ->
126✔
53
        render_resolved (r :> t) ^ "." ^ ExceptionName.to_string s
126✔
54
    | `Value (r, s) -> render_resolved (r :> t) ^ "." ^ ValueName.to_string s
36✔
55
    | `Class (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
56
    | `ClassType (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
57
    | `Method (r, s) ->
×
58
        (* CR trefis: do we really want to print anything more than [s] here?  *)
59
        render_resolved (r :> t) ^ "." ^ MethodName.to_string s
×
60
    | `InstanceVariable (r, s) ->
×
61
        (* CR trefis: the following makes no sense to me... *)
62
        render_resolved (r :> t) ^ "." ^ InstanceVariableName.to_string s
×
63
    | `Label (_, s) -> LabelName.to_string s
54✔
64

65
  let render_path (tag, cs) =
66
    let tag =
8✔
67
      match tag with
68
      | `TRelativePath -> "./"
8✔
69
      | `TAbsolutePath -> "/"
×
70
      | `TCurrentPackage -> "//"
×
71
    in
72
    tag ^ String.concat ~sep:"/" cs
8✔
73

74
  let rec render_unresolved : Reference.t -> string =
75
    let open Reference in
76
    function
77
    | `Resolved r -> render_resolved r
6✔
78
    | `Root (n, _) -> n
381✔
79
    | `Dot (p, f) -> render_unresolved (p :> t) ^ "." ^ f
505✔
80
    | `Page_path p -> render_path p
×
81
    | `Asset_path p -> render_path p
7✔
82
    | `Module_path p -> render_path p
×
83
    | `Any_path p -> render_path p
1✔
84
    | `Module (p, f) ->
×
85
        render_unresolved (p :> t) ^ "." ^ ModuleName.to_string f
×
86
    | `ModuleType (p, f) ->
×
87
        render_unresolved (p :> t) ^ "." ^ ModuleTypeName.to_string f
×
88
    | `Type (p, f) -> render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
89
    | `Constructor (p, f) ->
1✔
90
        render_unresolved (p :> t) ^ "." ^ ConstructorName.to_string f
1✔
91
    | `Field (p, f) -> render_unresolved (p :> t) ^ "." ^ FieldName.to_string f
×
NEW
92
    | `UnboxedField (p, f) -> render_unresolved (p :> t) ^ "." ^ UnboxedFieldName.to_string f
×
93
    | `Extension (p, f) ->
×
94
        render_unresolved (p :> t) ^ "." ^ ExtensionName.to_string f
×
95
    | `ExtensionDecl (p, f) ->
×
96
        render_unresolved (p :> t) ^ "." ^ ExtensionName.to_string f
×
97
    | `Exception (p, f) ->
×
98
        render_unresolved (p :> t) ^ "." ^ ExceptionName.to_string f
×
99
    | `Value (p, f) -> render_unresolved (p :> t) ^ "." ^ ValueName.to_string f
×
100
    | `Class (p, f) -> render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
101
    | `ClassType (p, f) ->
×
102
        render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
103
    | `Method (p, f) ->
×
104
        render_unresolved (p :> t) ^ "." ^ MethodName.to_string f
×
105
    | `InstanceVariable (p, f) ->
×
106
        render_unresolved (p :> t) ^ "." ^ InstanceVariableName.to_string f
×
107
    | `Label (p, f) -> render_unresolved (p :> t) ^ "." ^ LabelName.to_string f
×
108

109
  (* This is the entry point. *)
110
  let to_ir : ?text:Inline.t -> Reference.t -> Inline.t =
111
   fun ?text ref ->
112
    match ref with
3,627✔
113
    | `Resolved r ->
3,245✔
114
        (* IDENTIFIER MUST BE RENAMED TO DEFINITION. *)
115
        let id = Reference.Resolved.identifier r in
116
        let rendered = render_resolved r in
3,245✔
117
        let content =
3,245✔
118
          match text with
119
          | None -> [ inline @@ Inline.Source (source_of_code rendered) ]
1,950✔
120
          | Some s -> s
1,295✔
121
        and tooltip =
122
          (* Add a tooltip if the content is not the rendered reference. *)
123
          match text with
124
          | None -> None
1,950✔
125
          | Some _ -> Some rendered
1,295✔
126
        in
127
        let target =
128
          match id with
129
          | Some id ->
3,245✔
130
              let url = Url.from_identifier ~stop_before:false id in
131
              Target.Internal (Resolved url)
3,245✔
132
          | None -> Internal Unresolved
×
133
        in
134
        let link = { Link.target; content; tooltip } in
135
        [ inline @@ Inline.Link link ]
3,245✔
136
    | _ -> (
382✔
137
        let s = render_unresolved ref in
138
        match text with
382✔
139
        | None ->
380✔
140
            let s = source_of_code s in
141
            [ inline @@ Inline.Source s ]
380✔
142
        | Some content ->
2✔
143
            let link =
144
              { Link.target = Internal Unresolved; content; tooltip = Some s }
145
            in
146
            [ inline @@ Inline.Link link ])
2✔
147
end
148

149
let leaf_inline_element : Comment.leaf_inline_element -> Inline.one = function
150
  | `Space -> inline @@ Text " "
135,147✔
151
  | `Word s -> inline @@ Text s
145,523✔
152
  | `Code_span s -> inline @@ Source (source_of_code s)
5,495✔
153
  | `Math_span s -> inline @@ Math s
15✔
154
  | `Raw_markup (target, s) -> inline @@ Raw_markup (target, s)
17✔
155

156
let rec non_link_inline_element : Comment.non_link_inline_element -> Inline.one
157
    = function
158
  | #Comment.leaf_inline_element as e -> leaf_inline_element e
2,722✔
159
  | `Styled (style, content) ->
96✔
160
      inline @@ Styled (style, non_link_inline_element_list content)
96✔
161

162
and non_link_inline_element_list : _ -> Inline.t =
163
 fun elements ->
164
  List.map
1,518✔
165
    (fun elt -> non_link_inline_element elt.Odoc_model.Location_.value)
2,818✔
166
    elements
167

168
let link_content = non_link_inline_element_list
169

170
let rec inline_element : Comment.inline_element -> Inline.t = function
171
  | #Comment.leaf_inline_element as e -> [ leaf_inline_element e ]
283,475✔
172
  | `Styled (style, content) ->
653✔
173
      [ inline @@ Styled (style, inline_element_list content) ]
653✔
174
  | `Reference (path, content) ->
3,547✔
175
      (* TODO Rework that ugly function. *)
176
      (* TODO References should be set in code style, if they are to code
177
              elements. *)
178
      let content =
179
        match content with
180
        | [] -> None
2,250✔
181
        | _ -> Some (non_link_inline_element_list content)
1,297✔
182
        (* XXX Span *)
183
      in
184
      Reference.to_ir ?text:content path
185
  | `Link (target, content) ->
129✔
186
      let content =
187
        match content with
188
        | [] -> [ inline @@ Text target ]
34✔
189
        | _ -> non_link_inline_element_list content
95✔
190
      in
191
      [ inline @@ Link { target = External target; content; tooltip = None } ]
129✔
192

193
and inline_element_list elements =
194
  List.concat
14,096✔
195
  @@ List.map
14,096✔
196
       (fun elt -> inline_element elt.Odoc_model.Location_.value)
287,646✔
197
       elements
198

199
let module_references ms =
200
  let module_reference (m : Comment.module_reference) =
40✔
201
    let reference =
80✔
202
      Reference.to_ir (m.module_reference :> Odoc_model.Paths.Reference.t)
80✔
203
    and synopsis =
204
      match m.module_synopsis with
205
      | Some synopsis ->
24✔
206
          [
207
            block ~attr:[ "synopsis" ] @@ Inline (inline_element_list synopsis);
24✔
208
          ]
209
      | None -> []
56✔
210
    in
211
    { Description.attr = []; key = reference; definition = synopsis }
212
  in
213
  let items = List.map module_reference ms in
214
  block ~attr:[ "modules" ] @@ Description items
40✔
215

216
let rec nestable_block_element :
217
    Comment.nestable_block_element -> Block.one list =
218
 fun content ->
219
  match content with
11,348✔
220
  | `Paragraph p -> [ paragraph p ]
10,817✔
221
  | `Code_block (lang_tag, code, outputs) ->
117✔
222
      let lang_tag =
223
        match lang_tag with None -> default_lang_tag | Some t -> t
2✔
224
      in
225
      let rest =
226
        match outputs with
227
        | Some xs -> nestable_block_element_list xs
×
228
        | None -> []
117✔
229
      in
230
      [
231
        block
117✔
232
        @@ Source (lang_tag, source_of_code (Odoc_model.Location_.value code));
117✔
233
      ]
234
      @ rest
235
  | `Math_block s -> [ block @@ Math s ]
9✔
236
  | `Verbatim s -> [ block @@ Verbatim s ]
94✔
237
  | `Modules ms -> [ module_references ms ]
40✔
238
  | `List (kind, items) ->
188✔
239
      let kind =
240
        match kind with
241
        | `Unordered -> Block.Unordered
153✔
242
        | `Ordered -> Block.Ordered
35✔
243
      in
244
      let f = function
245
        | [ { Odoc_model.Location_.value = `Paragraph content; _ } ] ->
791✔
246
            [ block @@ Block.Inline (inline_element_list content) ]
791✔
247
        | item -> nestable_block_element_list item
45✔
248
      in
249
      let items = List.map f items in
250
      [ block @@ Block.List (kind, items) ]
188✔
251
  | `Table { data; align } ->
42✔
252
      let data =
253
        List.map
254
          (List.map (fun (cell, cell_type) ->
42✔
255
               (nestable_block_element_list cell, cell_type)))
268✔
256
          data
257
      in
258
      let generate_align data =
42✔
259
        let max (a : int) b = if a < b then b else a in
25✔
260
        (* Length of the longest line of the table *)
261
        let max_length =
262
          List.fold_left (fun m l -> max m (List.length l)) 0 data
51✔
263
        in
264
        let rec list_init i =
25✔
265
          if i <= 0 then [] else Table.Default :: list_init (i - 1)
25✔
266
        in
267
        list_init max_length
268
      in
269
      let align =
270
        match align with
271
        | None -> generate_align data
25✔
272
        | Some align ->
17✔
273
            List.map
17✔
274
              (function
275
                | None -> Table.Default
18✔
276
                | Some `Right -> Right
16✔
277
                | Some `Left -> Left
16✔
278
                | Some `Center -> Center)
16✔
279
              align
280
        (* We should also check wellness of number of table cells vs alignment,
281
           and raise warnings *)
282
      in
283
      [ block @@ Table { data; align } ]
42✔
284
  | `Media (href, media, content) ->
41✔
285
      let content =
286
        match (content, href) with
287
        | "", `Reference path ->
13✔
288
            Reference.render_unresolved (path :> Comment.Reference.t)
13✔
289
        | "", `Link href -> href
13✔
290
        | _ -> content
15✔
291
      in
292
      let url =
293
        match href with
294
        | `Reference (`Resolved r) -> (
9✔
295
            let id =
296
              Odoc_model.Paths.Reference.Resolved.Asset.(identifier (r :> t))
9✔
297
            in
298
            match Url.from_asset_identifier id with
299
            | url -> Target.Internal (Resolved url))
9✔
300
        | `Reference _ -> Internal Unresolved
10✔
301
        | `Link href -> External href
22✔
302
      in
303
      let i =
304
        match media with
305
        | `Audio -> Block.Audio (url, content)
9✔
306
        | `Video -> Video (url, content)
9✔
307
        | `Image -> Image (url, content)
23✔
308
      in
309
      [ block i ]
41✔
310

311
and paragraph : Comment.paragraph -> Block.one = function
312
  | [ { value = `Raw_markup (target, s); _ } ] ->
9✔
313
      block @@ Block.Raw_markup (target, s)
314
  | p -> block @@ Block.Paragraph (inline_element_list p)
11,557✔
315

316
and nestable_block_element_list :
317
    Comment.nestable_block_element Comment.with_location list -> Block.one list
318
    =
319
 fun elements ->
320
  elements
1,054✔
321
  |> List.map Odoc_model.Location_.value
322
  |> List.map nestable_block_element
1,054✔
323
  |> List.concat
1,054✔
324

325
let tag : Comment.tag -> Description.one =
326
 fun t ->
327
  let sp = inline (Text " ") in
846✔
328
  let item ?value ~tag definition =
846✔
329
    let tag_name = inline ~attr:[ "at-tag" ] (Text tag) in
846✔
330
    let tag_value = match value with None -> [] | Some t -> sp :: t in
189✔
331
    let key = tag_name :: tag_value in
332
    { Description.attr = [ tag ]; key; definition }
333
  in
334
  let mk_value desc = [ inline ~attr:[ "value" ] desc ] in
499✔
335
  let text_def s = [ block (Block.Inline [ inline @@ Text s ]) ] in
49✔
336
  let content_to_inline ?(prefix = []) content =
40✔
337
    match content with
56✔
338
    | None -> []
24✔
339
    | Some content -> prefix @ [ inline @@ Text content ]
32✔
340
  in
341
  match t with
342
  | `Author s -> item ~tag:"author" (text_def s)
16✔
343
  | `Deprecated content ->
59✔
344
      item ~tag:"deprecated" (nestable_block_element_list content)
59✔
345
  | `Param (name, content) ->
24✔
346
      let value = mk_value (Inline.Text name) in
347
      item ~tag:"parameter" ~value (nestable_block_element_list content)
24✔
348
  | `Raise (kind, content) ->
158✔
349
      let value = inline_element (kind :> Comment.inline_element) in
350
      item ~tag:"raises" ~value (nestable_block_element_list content)
158✔
351
  | `Return content -> item ~tag:"returns" (nestable_block_element_list content)
25✔
352
  | `See (kind, target, content) ->
443✔
353
      let value =
354
        match kind with
355
        | `Url ->
403✔
356
            mk_value
403✔
357
              (Inline.Link
358
                 {
359
                   target = External target;
360
                   content = [ inline @@ Text target ];
403✔
361
                   tooltip = None;
362
                 })
363
        | `File -> mk_value (Inline.Source (source_of_code target))
16✔
364
        | `Document -> mk_value (Inline.Text target)
24✔
365
      in
366
      item ~tag:"see" ~value (nestable_block_element_list content)
443✔
367
  | `Since s -> item ~tag:"since" (text_def s)
17✔
368
  | `Before (version, content) ->
32✔
369
      let value = mk_value (Inline.Text version) in
370
      item ~tag:"before" ~value (nestable_block_element_list content)
32✔
371
  | `Version s -> item ~tag:"version" (text_def s)
16✔
372
  | `Alert ("deprecated", content) ->
40✔
373
      let content = content_to_inline content in
374
      item ~tag:"deprecated" [ block (Block.Inline content) ]
40✔
375
  | `Alert (tag, content) ->
16✔
376
      let content = content_to_inline ~prefix:[ sp ] content in
377
      item ~tag:"alert"
16✔
378
        [ block (Block.Inline ([ inline @@ Text tag ] @ content)) ]
16✔
379

380
let attached_block_element : Comment.attached_block_element -> Block.t =
381
  function
382
  | #Comment.nestable_block_element as e -> nestable_block_element e
10,257✔
383
  | `Tag t -> [ block ~attr:[ "at-tags" ] @@ Description [ tag t ] ]
846✔
384

385
(* TODO collaesce tags *)
386

387
let block_element : Comment.block_element -> Block.t = function
388
  | #Comment.attached_block_element as e -> attached_block_element e
8,861✔
389
  | `Heading (_, _, text) ->
202✔
390
      (* We are not supposed to receive Heading in this context.
391
         TODO: Remove heading in attached documentation in the model *)
392
      [ block @@ Paragraph (inline_element_list text) ]
202✔
393

394
let heading_level_to_int = function
395
  | `Title -> 0
70✔
396
  | `Section -> 1
522✔
397
  | `Subsection -> 2
126✔
398
  | `Subsubsection -> 3
124✔
399
  | `Paragraph -> 4
19✔
400
  | `Subparagraph -> 5
8✔
401

402
let heading
403
    (attrs, { Odoc_model.Paths.Identifier.iv = `Label (_, label); _ }, text) =
404
  let label = Odoc_model.Names.LabelName.to_string label in
869✔
405
  let title = inline_element_list text in
869✔
406
  let level = heading_level_to_int attrs.Comment.heading_level in
869✔
407
  let label = Some label in
869✔
408
  let source_anchor = None in
409
  Item.Heading { label; level; title; source_anchor }
410

411
let item_element : Comment.block_element -> Item.t list = function
412
  | #Comment.attached_block_element as e ->
968✔
413
      [ Item.Text (attached_block_element e) ]
968✔
414
  | `Heading h -> [ heading h ]
32✔
415

416
(** The documentation of the expansion is used if there is no comment attached
417
    to the declaration. *)
418
let synopsis ~decl_doc ~expansion_doc =
419
  let ([], Some docs | docs, _) = (decl_doc, expansion_doc) in
3,426✔
420
  match Comment.synopsis docs with Some p -> [ paragraph p ] | None -> []
749✔
421

422
let standalone docs =
423
  List.concat_map item_element
6,584✔
424
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
1,000✔
425

426
let to_ir (docs : Comment.elements) =
427
  List.concat_map block_element
10,242✔
428
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
9,063✔
429

430
let has_doc docs = docs <> []
590✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc