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

ocaml / odoc / 3054

19 Feb 2026 11:33AM UTC coverage: 71.709% (-1.2%) from 72.946%
3054

Pull #1399

github

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

20 of 281 new or added lines in 21 files covered. (7.12%)

162 existing lines in 11 files now uncovered.

10400 of 14503 relevant lines covered (71.71%)

7007.14 hits per line

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

82.46
/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) ->
×
NEW
48
        render_resolved (r :> t) ^ "." ^ UnboxedFieldName.to_string s
×
49
    | `Extension (r, s) ->
3✔
50
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
3✔
51
    | `ExtensionDecl (r, _, s) ->
2✔
52
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
2✔
53
    | `Exception (r, s) ->
126✔
54
        render_resolved (r :> t) ^ "." ^ ExceptionName.to_string s
126✔
55
    | `Value (r, s) -> render_resolved (r :> t) ^ "." ^ ValueName.to_string s
36✔
56
    | `Class (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
57
    | `ClassType (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
58
    | `Method (r, s) ->
×
59
        (* CR trefis: do we really want to print anything more than [s] here?  *)
60
        render_resolved (r :> t) ^ "." ^ MethodName.to_string s
×
61
    | `InstanceVariable (r, s) ->
×
62
        (* CR trefis: the following makes no sense to me... *)
63
        render_resolved (r :> t) ^ "." ^ InstanceVariableName.to_string s
×
64
    | `Label (_, s) -> LabelName.to_string s
54✔
65

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

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

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

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

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

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

170
let link_content = non_link_inline_element_list
171

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

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

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

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

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

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

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

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

387
(* TODO collaesce tags *)
388

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

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

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

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

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

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

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

432
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