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

ocaml / odoc / 3015

15 Jul 2025 04:41PM UTC coverage: 72.941% (-0.1%) from 73.057%
3015

push

github

jonludlam
Promote tests

10386 of 14239 relevant lines covered (72.94%)

7134.98 hits per line

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

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

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

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

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

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

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

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

166
let link_content = non_link_inline_element_list
167

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

191
and inline_element_list elements =
192
  List.concat
14,084✔
193
  @@ List.map
14,084✔
194
       (fun elt -> inline_element elt.Odoc_model.Location_.value)
287,610✔
195
       elements
196

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

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

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

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

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

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

383
(* TODO collaesce tags *)
384

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

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

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

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

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

420
let standalone docs =
421
  List.concat_map item_element
6,572✔
422
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
994✔
423

424
let to_ir (docs : Comment.elements) =
425
  List.concat_map block_element
10,236✔
426
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
9,057✔
427

428
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