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

ocaml / odoc / 2168

10 Jul 2024 02:38PM UTC coverage: 71.437% (-0.4%) from 71.864%
2168

Pull #1142

github

web-flow
Merge 9bc2c3b35 into de54ed266
Pull Request #1142: Parsing of path-references to pages and modules

68 of 127 new or added lines in 6 files covered. (53.54%)

700 existing lines in 17 files now uncovered.

9794 of 13710 relevant lines covered (71.44%)

3534.91 hits per line

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

77.03
/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 Types
18
module Comment = Odoc_model.Comment
19
open Odoc_model.Names
20

21
let default_lang_tag = "ocaml"
22

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

26
module Reference = struct
27
  open Odoc_model.Paths
28

29
  let rec render_resolved : Reference.Resolved.t -> string =
30
   fun r ->
31
    let open Reference.Resolved in
865✔
32
    match r with
33
    | `Identifier id -> Identifier.name id
593✔
34
    | `Alias (_, r) -> render_resolved (r :> t)
36✔
35
    | `AliasModuleType (_, r) -> render_resolved (r :> t)
2✔
36
    | `Module (r, s) -> render_resolved (r :> t) ^ "." ^ ModuleName.to_string s
78✔
37
    | `Hidden p -> render_resolved (p :> t)
×
38
    | `ModuleType (r, s) ->
18✔
39
        render_resolved (r :> t) ^ "." ^ ModuleTypeName.to_string s
18✔
40
    | `Type (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
66✔
41
    | `Constructor (r, s) ->
5✔
42
        render_resolved (r :> t) ^ "." ^ ConstructorName.to_string s
5✔
43
    | `PolyConstructor (r, s) ->
8✔
44
        render_resolved (r :> t) ^ ".`" ^ ConstructorName.to_string s
8✔
45
    | `Field (r, s) -> render_resolved (r :> t) ^ "." ^ FieldName.to_string s
×
46
    | `Extension (r, s) ->
3✔
47
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
3✔
48
    | `ExtensionDecl (r, _, s) ->
2✔
49
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
2✔
50
    | `Exception (r, s) ->
×
51
        render_resolved (r :> t) ^ "." ^ ExceptionName.to_string s
×
52
    | `Value (r, s) -> render_resolved (r :> t) ^ "." ^ ValueName.to_string s
12✔
53
    | `Class (r, s) -> render_resolved (r :> t) ^ "." ^ ClassName.to_string s
×
54
    | `ClassType (r, s) ->
×
55
        render_resolved (r :> t) ^ "." ^ ClassTypeName.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
42✔
63

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

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

106
  (* This is the entry point. *)
107
  let to_ir : ?text:Inline.t -> Reference.t -> Inline.t =
108
   fun ?text ref ->
109
    match ref with
683✔
110
    | `Resolved r -> (
635✔
111
        (* IDENTIFIER MUST BE RENAMED TO DEFINITION. *)
112
        let id = Reference.Resolved.identifier r in
113
        let rendered = render_resolved r in
635✔
114
        let content =
635✔
115
          match text with
116
          | None -> [ inline @@ Inline.Source (source_of_code rendered) ]
406✔
117
          | Some s -> s
229✔
118
        and tooltip =
119
          (* Add a tooltip if the content is not the rendered reference. *)
120
          match text with None -> None | Some _ -> Some rendered
229✔
121
        in
122
        match Url.from_identifier ~stop_before:false id with
123
        | Ok url ->
635✔
124
            let target = InternalLink.Resolved url in
125
            let link = { InternalLink.target; content; tooltip } in
126
            [ inline @@ Inline.InternalLink link ]
635✔
127
        | Error (Not_linkable _) -> content
×
128
        | Error exn ->
×
129
            (* FIXME: better error message *)
130
            Printf.eprintf "Id.href failed: %S\n%!" (Url.Error.to_string exn);
×
131
            content)
×
132
    | _ -> (
48✔
133
        let s = render_unresolved ref in
134
        match text with
48✔
135
        | None ->
47✔
136
            let s = source_of_code s in
137
            [ inline @@ Inline.Source s ]
47✔
138
        | Some content ->
1✔
139
            let link =
140
              { InternalLink.target = Unresolved; content; tooltip = Some s }
141
            in
142
            [ inline @@ Inline.InternalLink link ])
1✔
143
end
144

145
let leaf_inline_element : Comment.leaf_inline_element -> Inline.one = function
146
  | `Space -> inline @@ Text " "
17,361✔
147
  | `Word s -> inline @@ Text s
21,242✔
148
  | `Code_span s -> inline @@ Source (source_of_code s)
1,529✔
149
  | `Math_span s -> inline @@ Math s
10✔
150
  | `Raw_markup (target, s) -> inline @@ Raw_markup (target, s)
21✔
151

152
let rec non_link_inline_element : Comment.non_link_inline_element -> Inline.one
153
    = function
154
  | #Comment.leaf_inline_element as e -> leaf_inline_element e
1,209✔
155
  | `Styled (style, content) ->
72✔
156
      inline @@ Styled (style, non_link_inline_element_list content)
72✔
157

158
and non_link_inline_element_list : _ -> Inline.t =
159
 fun elements ->
160
  List.map
365✔
161
    (fun elt -> non_link_inline_element elt.Odoc_model.Location_.value)
1,281✔
162
    elements
163

164
let link_content = non_link_inline_element_list
165

166
let rec inline_element : Comment.inline_element -> Inline.t = function
167
  | #Comment.leaf_inline_element as e -> [ leaf_inline_element e ]
38,954✔
168
  | `Styled (style, content) ->
341✔
169
      [ inline @@ Styled (style, inline_element_list content) ]
341✔
170
  | `Reference (path, content) ->
623✔
171
      (* TODO Rework that ugly function. *)
172
      (* TODO References should be set in code style, if they are to code
173
              elements. *)
174
      let content =
175
        match content with
176
        | [] -> None
393✔
177
        | _ -> Some (non_link_inline_element_list content)
230✔
178
        (* XXX Span *)
179
      in
180
      Reference.to_ir ?text:content path
181
  | `Link (target, content) ->
88✔
182
      let content =
183
        match content with
184
        | [] -> [ inline @@ Text target ]
25✔
185
        | _ -> non_link_inline_element_list content
63✔
186
      in
187
      [ inline @@ Link (target, content) ]
88✔
188

189
and inline_element_list elements =
190
  List.concat
4,530✔
191
  @@ List.map
4,530✔
192
       (fun elt -> inline_element elt.Odoc_model.Location_.value)
39,982✔
193
       elements
194

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

212
let rec nestable_block_element :
213
    Comment.nestable_block_element -> Block.one list =
214
 fun content ->
215
  match content with
2,779✔
216
  | `Paragraph p -> [ paragraph p ]
2,535✔
217
  | `Code_block (lang_tag, code, outputs) ->
19✔
218
      let lang_tag =
219
        match lang_tag with None -> default_lang_tag | Some t -> t
×
220
      in
221
      let rest =
222
        match outputs with
223
        | Some xs -> nestable_block_element_list xs
×
224
        | None -> []
19✔
225
      in
226
      [
227
        block
19✔
228
        @@ Source (lang_tag, source_of_code (Odoc_model.Location_.value code));
19✔
229
      ]
230
      @ rest
231
  | `Math_block s -> [ block @@ Math s ]
6✔
232
  | `Verbatim s -> [ block @@ Verbatim s ]
29✔
233
  | `Modules ms -> [ module_references ms ]
30✔
234
  | `List (kind, items) ->
130✔
235
      let kind =
236
        match kind with
237
        | `Unordered -> Block.Unordered
106✔
238
        | `Ordered -> Block.Ordered
24✔
239
      in
240
      let f = function
241
        | [ { Odoc_model.Location_.value = `Paragraph content; _ } ] ->
396✔
242
            [ block @@ Block.Inline (inline_element_list content) ]
396✔
243
        | item -> nestable_block_element_list item
18✔
244
      in
245
      let items = List.map f items in
246
      [ block @@ Block.List (kind, items) ]
130✔
247
  | `Table { data; align } ->
30✔
248
      let data =
249
        List.map
250
          (List.map (fun (cell, cell_type) ->
30✔
251
               (nestable_block_element_list cell, cell_type)))
192✔
252
          data
253
      in
254
      let generate_align data =
30✔
255
        let max (a : int) b = if a < b then b else a in
18✔
256
        (* Length of the longest line of the table *)
257
        let max_length =
258
          List.fold_left (fun m l -> max m (List.length l)) 0 data
36✔
259
        in
260
        let rec list_init i =
18✔
261
          if i <= 0 then [] else Table.Default :: list_init (i - 1)
18✔
262
        in
263
        list_init max_length
264
      in
265
      let align =
266
        match align with
267
        | None -> generate_align data
18✔
268
        | Some align ->
12✔
269
            List.map
12✔
270
              (function
271
                | None -> Table.Default
12✔
272
                | Some `Right -> Right
12✔
273
                | Some `Left -> Left
12✔
274
                | Some `Center -> Center)
12✔
275
              align
276
        (* We should also check wellness of number of table cells vs alignment,
277
           and raise warnings *)
278
      in
279
      [ block @@ Table { data; align } ]
30✔
280

281
and paragraph : Comment.paragraph -> Block.one = function
282
  | [ { value = `Raw_markup (target, s); _ } ] ->
6✔
283
      block @@ Block.Raw_markup (target, s)
284
  | p -> block @@ Block.Paragraph (inline_element_list p)
3,105✔
285

286
and nestable_block_element_list :
287
    Comment.nestable_block_element Comment.with_location list -> Block.one list
288
    =
289
 fun elements ->
290
  elements
360✔
291
  |> List.map Odoc_model.Location_.value
292
  |> List.map nestable_block_element
360✔
293
  |> List.concat
360✔
294

295
let tag : Comment.tag -> Description.one =
296
 fun t ->
297
  let sp = inline (Text " ") in
234✔
298
  let item ?value ~tag definition =
234✔
299
    let tag_name = inline ~attr:[ "at-tag" ] (Text tag) in
234✔
300
    let tag_value = match value with None -> [] | Some t -> sp :: t in
108✔
301
    let key = tag_name :: tag_value in
302
    { Description.attr = [ tag ]; key; definition }
303
  in
304
  let mk_value desc = [ inline ~attr:[ "value" ] desc ] in
84✔
305
  let text_def s = [ block (Block.Inline [ inline @@ Text s ]) ] in
36✔
306
  let content_to_inline ?(prefix = []) content =
36✔
307
    match content with
48✔
308
    | None -> []
18✔
309
    | Some content -> prefix @ [ inline @@ Text content ]
30✔
310
  in
311
  match t with
312
  | `Author s -> item ~tag:"author" (text_def s)
12✔
313
  | `Deprecated content ->
24✔
314
      item ~tag:"deprecated" (nestable_block_element_list content)
24✔
315
  | `Param (name, content) ->
18✔
316
      let value = mk_value (Inline.Text name) in
317
      item ~tag:"parameter" ~value (nestable_block_element_list content)
18✔
318
  | `Raise (kind, content) ->
24✔
319
      let value = inline_element (kind :> Comment.inline_element) in
320
      item ~tag:"raises" ~value (nestable_block_element_list content)
24✔
321
  | `Return content -> item ~tag:"returns" (nestable_block_element_list content)
18✔
322
  | `See (kind, target, content) ->
42✔
323
      let value =
324
        match kind with
325
        | `Url -> mk_value (Inline.Link (target, [ inline @@ Text target ]))
12✔
326
        | `File -> mk_value (Inline.Source (source_of_code target))
12✔
327
        | `Document -> mk_value (Inline.Text target)
18✔
328
      in
329
      item ~tag:"see" ~value (nestable_block_element_list content)
42✔
330
  | `Since s -> item ~tag:"since" (text_def s)
12✔
331
  | `Before (version, content) ->
24✔
332
      let value = mk_value (Inline.Text version) in
333
      item ~tag:"before" ~value (nestable_block_element_list content)
24✔
334
  | `Version s -> item ~tag:"version" (text_def s)
12✔
335
  | `Alert ("deprecated", content) ->
36✔
336
      let content = content_to_inline content in
337
      item ~tag:"deprecated" [ block (Block.Inline content) ]
36✔
338
  | `Alert (tag, content) ->
12✔
339
      let content = content_to_inline ~prefix:[ sp ] content in
340
      item ~tag:"alert"
12✔
341
        [ block (Block.Inline ([ inline @@ Text tag ] @ content)) ]
12✔
342

343
let attached_block_element : Comment.attached_block_element -> Block.t =
344
  function
345
  | #Comment.nestable_block_element as e -> nestable_block_element e
2,407✔
346
  | `Tag t -> [ block ~attr:[ "at-tags" ] @@ Description [ tag t ] ]
234✔
347

348
(* TODO collaesce tags *)
349

350
let block_element : Comment.block_element -> Block.t = function
351
  | #Comment.attached_block_element as e -> attached_block_element e
999✔
352
  | `Heading (_, _, text) ->
56✔
353
      (* We are not supposed to receive Heading in this context.
354
         TODO: Remove heading in attached documentation in the model *)
355
      [ block @@ Paragraph (inline_element_list text) ]
56✔
356

357
let heading_level_to_int = function
358
  | `Title -> 0
42✔
359
  | `Section -> 1
391✔
360
  | `Subsection -> 2
79✔
361
  | `Subsubsection -> 3
84✔
362
  | `Paragraph -> 4
12✔
363
  | `Subparagraph -> 5
6✔
364

365
let heading
366
    (attrs, { Odoc_model.Paths.Identifier.iv = `Label (_, label); _ }, text) =
367
  let label = Odoc_model.Names.LabelName.to_string label in
614✔
368
  let title = inline_element_list text in
614✔
369
  let level = heading_level_to_int attrs.Comment.heading_level in
614✔
370
  let label = Some label in
614✔
371
  let source_anchor = None in
372
  Item.Heading { label; level; title; source_anchor }
373

374
let item_element : Comment.block_element -> Item.t list = function
375
  | #Comment.attached_block_element as e ->
723✔
376
      [ Item.Text (attached_block_element e) ]
723✔
377
  | `Heading h -> [ heading h ]
24✔
378

379
(** The documentation of the expansion is used if there is no comment attached
380
    to the declaration. *)
381
let synopsis ~decl_doc ~expansion_doc =
382
  let ([], Some docs | docs, _) = (decl_doc, expansion_doc) in
2,639✔
383
  match Comment.synopsis docs with Some p -> [ paragraph p ] | None -> []
576✔
384

385
let standalone docs =
386
  Utils.flatmap ~f:item_element
5,110✔
387
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
747✔
388

389
let to_ir (docs : Comment.docs) =
390
  Utils.flatmap ~f:block_element
4,067✔
391
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
1,055✔
392

393
let has_doc docs = docs <> []
505✔
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