Skip to content

Commit 586a79b

Browse files
committed
count occurrences: untangle feature from render source code
Signed-off-by: Paul-Elliot <peada@free.fr>
1 parent d1b2481 commit 586a79b

File tree

13 files changed

+87
-83
lines changed

13 files changed

+87
-83
lines changed

src/document/generator.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,8 +1810,8 @@ module Make (Syntax : SYNTAX) = struct
18101810
in
18111811
let source_anchor =
18121812
match t.source_info with
1813-
| Some { id; _ } -> Some (Source_page.url id)
1814-
| None -> None
1813+
| Some { id = Some id; _ } -> Some (Source_page.url id)
1814+
| _ -> None
18151815
in
18161816
let page = make_expansion_page ~source_anchor url [ unit_doc ] items in
18171817
Document.Page page

src/loader/implementation.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,22 +503,30 @@ let of_cmt (source_id : Odoc_model.Paths.Identifier.SourcePage.t)
503503

504504
(uid_to_id, postprocess_poses source_id vs uid_to_id uid_to_loc)
505505

506-
let read_cmt_infos source_id_opt id cmt_info =
507-
let occ_infos = Occurrences.of_cmt cmt_info in
506+
let read_cmt_infos source_id_opt id cmt_info ~count_occurrences =
508507
match Odoc_model.Compat.shape_of_cmt_infos cmt_info with
509508
| Some shape -> (
510509
let uid_to_loc = cmt_info.cmt_uid_to_loc in
511-
match (source_id_opt, cmt_info.cmt_annots) with
512-
| Some source_id, Implementation impl ->
510+
match (source_id_opt, count_occurrences, cmt_info.cmt_annots) with
511+
| Some source_id, _, Implementation impl ->
513512
let map, source_infos = of_cmt source_id id impl uid_to_loc in
513+
let occ_infos = Occurrences.of_cmt impl in
514514
let source_infos = List.rev_append source_infos occ_infos in
515515
( Some (shape, map),
516516
Some
517517
{
518-
Odoc_model.Lang.Source_info.id = source_id;
518+
Odoc_model.Lang.Source_info.id = Some source_id;
519519
infos = source_infos;
520520
} )
521-
| _, _ -> (Some (shape, Odoc_model.Compat.empty_map), None))
521+
| None, true, Implementation impl ->
522+
let occ_infos = Occurrences.of_cmt impl in
523+
( None,
524+
Some
525+
{
526+
Odoc_model.Lang.Source_info.id = None;
527+
infos = occ_infos;
528+
} )
529+
| _, _, _ -> (Some (shape, Odoc_model.Compat.empty_map), None))
522530
| None -> (None, None)
523531

524532

src/loader/implementation.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ val read_cmt_infos :
22
Odoc_model.Paths.Identifier.Id.source_page option ->
33
Odoc_model.Paths.Identifier.Id.root_module ->
44
Cmt_format.cmt_infos ->
5+
count_occurrences:bool ->
56
(Odoc_model.Compat.shape
67
* Odoc_model.Paths.Identifier.Id.source_location
78
Odoc_model.Compat.shape_uid_map)

src/loader/occurrences.ml

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,46 +106,42 @@ module Global_analysis = struct
106106
| _ -> ()
107107
end
108108

109-
let of_cmt (cmt : Cmt_format.cmt_infos) =
110-
let ttree = cmt.cmt_annots in
111-
match ttree with
112-
| Cmt_format.Implementation structure ->
113-
let poses = ref [] in
114-
let module_expr iterator mod_expr =
115-
Global_analysis.module_expr poses mod_expr;
116-
Compat.Tast_iterator.default_iterator.module_expr iterator mod_expr
117-
in
118-
let expr iterator e =
119-
Global_analysis.expr poses e;
120-
Compat.Tast_iterator.default_iterator.expr iterator e
121-
in
122-
let pat iterator e =
123-
Global_analysis.pat poses e;
124-
Compat.Tast_iterator.default_iterator.pat iterator e
125-
in
126-
let typ iterator ctyp_expr =
127-
Global_analysis.core_type poses ctyp_expr;
128-
Compat.Tast_iterator.default_iterator.typ iterator ctyp_expr
129-
in
130-
let module_type iterator mty =
131-
Global_analysis.module_type poses mty;
132-
Compat.Tast_iterator.default_iterator.module_type iterator mty
133-
in
134-
let class_type iterator cl_type =
135-
Global_analysis.class_type poses cl_type;
136-
Compat.Tast_iterator.default_iterator.class_type iterator cl_type
137-
in
138-
let iterator =
139-
{
140-
Compat.Tast_iterator.default_iterator with
141-
expr;
142-
pat;
143-
module_expr;
144-
typ;
145-
module_type;
146-
class_type;
147-
}
148-
in
149-
iterator.structure iterator structure;
150-
!poses
151-
| _ -> []
109+
let of_cmt structure =
110+
let poses = ref [] in
111+
let module_expr iterator mod_expr =
112+
Global_analysis.module_expr poses mod_expr;
113+
Compat.Tast_iterator.default_iterator.module_expr iterator mod_expr
114+
in
115+
let expr iterator e =
116+
Global_analysis.expr poses e;
117+
Compat.Tast_iterator.default_iterator.expr iterator e
118+
in
119+
let pat iterator e =
120+
Global_analysis.pat poses e;
121+
Compat.Tast_iterator.default_iterator.pat iterator e
122+
in
123+
let typ iterator ctyp_expr =
124+
Global_analysis.core_type poses ctyp_expr;
125+
Compat.Tast_iterator.default_iterator.typ iterator ctyp_expr
126+
in
127+
let module_type iterator mty =
128+
Global_analysis.module_type poses mty;
129+
Compat.Tast_iterator.default_iterator.module_type iterator mty
130+
in
131+
let class_type iterator cl_type =
132+
Global_analysis.class_type poses cl_type;
133+
Compat.Tast_iterator.default_iterator.class_type iterator cl_type
134+
in
135+
let iterator =
136+
{
137+
Compat.Tast_iterator.default_iterator with
138+
expr;
139+
pat;
140+
module_expr;
141+
typ;
142+
module_type;
143+
class_type;
144+
}
145+
in
146+
iterator.structure iterator structure;
147+
!poses

src/loader/odoc_loader.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ exception Not_an_interface
4242

4343
exception Make_root_error of string
4444

45-
let read_cmt_infos source_id_opt id ~filename () =
45+
let read_cmt_infos source_id_opt id ~filename ~count_occurrences () =
4646
match Cmt_format.read_cmt filename with
4747
| exception Cmi_format.Error _ -> raise Corrupted
4848
| cmt_info -> (
4949
match cmt_info.cmt_annots with
50-
| Implementation _ -> Implementation.read_cmt_infos source_id_opt id cmt_info
50+
| Implementation _ -> Implementation.read_cmt_infos source_id_opt id cmt_info ~count_occurrences
5151
| _ -> raise Not_an_implementation)
5252

5353

@@ -99,7 +99,7 @@ let compilation_unit_of_sig ~make_root ~imports ~interface ?sourcefile ~name ~id
9999
make_compilation_unit ~make_root ~imports ~interface ?sourcefile ~name ~id
100100
?canonical ?shape_info content
101101

102-
let read_cmti ~make_root ~parent ~filename ~cmt_filename_opt ~source_id_opt () =
102+
let read_cmti ~make_root ~parent ~filename ~cmt_filename_opt ~source_id_opt ~count_occurrences () =
103103
let cmt_info = Cmt_format.read_cmt filename in
104104
match cmt_info.cmt_annots with
105105
| Interface intf -> (
@@ -116,15 +116,16 @@ let read_cmti ~make_root ~parent ~filename ~cmt_filename_opt ~source_id_opt () =
116116
let shape_info, source_info =
117117
match cmt_filename_opt with
118118
| Some cmt_filename ->
119-
read_cmt_infos source_id_opt id ~filename:cmt_filename ()
120-
| None -> (None, None)
119+
read_cmt_infos source_id_opt id ~filename:cmt_filename ~count_occurrences ()
120+
| None ->
121+
(None, None)
121122
in
122123
compilation_unit_of_sig ~make_root ~imports:cmt_info.cmt_imports
123124
~interface ~sourcefile ~name ~id ?shape_info ~source_info
124125
?canonical sg)
125126
| _ -> raise Not_an_interface
126127

127-
let read_cmt ~make_root ~parent ~filename ~source_id_opt () =
128+
let read_cmt ~make_root ~parent ~filename ~source_id_opt ~count_occurrences () =
128129
match Cmt_format.read_cmt filename with
129130
| exception Cmi_format.Error (Not_an_interface _) ->
130131
raise Not_an_implementation
@@ -168,7 +169,7 @@ let read_cmt ~make_root ~parent ~filename ~source_id_opt () =
168169
| Implementation impl ->
169170
let id, sg, canonical = Cmt.read_implementation parent name impl in
170171
let shape_info, source_info =
171-
read_cmt_infos source_id_opt id ~filename ()
172+
read_cmt_infos source_id_opt id ~filename ~count_occurrences ()
172173
in
173174
compilation_unit_of_sig ~make_root ~imports ~interface ~sourcefile
174175
~name ~id ?canonical ?shape_info ~source_info sg
@@ -199,12 +200,12 @@ let wrap_errors ~filename f =
199200
| Not_an_interface -> not_an_interface filename
200201
| Make_root_error m -> error_msg filename m)
201202

202-
let read_cmti ~make_root ~parent ~filename ~source_id_opt ~cmt_filename_opt =
203+
let read_cmti ~make_root ~parent ~filename ~source_id_opt ~cmt_filename_opt ~count_occurrences =
203204
wrap_errors ~filename
204-
(read_cmti ~make_root ~parent ~filename ~source_id_opt ~cmt_filename_opt)
205+
(read_cmti ~make_root ~parent ~filename ~source_id_opt ~cmt_filename_opt ~count_occurrences)
205206

206-
let read_cmt ~make_root ~parent ~filename ~source_id_opt =
207-
wrap_errors ~filename (read_cmt ~make_root ~parent ~filename ~source_id_opt)
207+
let read_cmt ~make_root ~parent ~filename ~source_id_opt ~count_occurrences =
208+
wrap_errors ~filename (read_cmt ~make_root ~parent ~filename ~source_id_opt ~count_occurrences)
208209

209210
let read_cmi ~make_root ~parent ~filename =
210211
wrap_errors ~filename (read_cmi ~make_root ~parent ~filename)

src/loader/odoc_loader.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ val read_cmti :
1919
filename:string ->
2020
source_id_opt:Identifier.SourcePage.t option ->
2121
cmt_filename_opt:string option ->
22+
count_occurrences:bool ->
2223
(Lang.Compilation_unit.t, Error.t) result Error.with_warnings
2324

2425
val read_cmt :
2526
make_root:make_root ->
2627
parent:Identifier.ContainerPage.t option ->
2728
filename:string ->
2829
source_id_opt:Identifier.SourcePage.t option ->
30+
count_occurrences:bool ->
2931
(Lang.Compilation_unit.t, Error.t) result Error.with_warnings
3032

3133
val read_cmi :

src/model/lang.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Source_info = struct
3232

3333
type infos = annotation with_pos list
3434

35-
type t = { id : Identifier.SourcePage.t; infos : infos }
35+
type t = { id : Identifier.SourcePage.t option; infos : infos }
3636
end
3737

3838
module rec Module : sig

src/model_desc/lang_desc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let inline_status =
1818

1919
let source_info =
2020
let open Lang.Source_info in
21-
Record [ F ("id", (fun t -> t.id), identifier) ]
21+
Record [ F ("id", (fun t -> t.id), Option identifier) ]
2222

2323
(** {3 Module} *)
2424

src/odoc/compile.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ let resolve_imports resolver imports =
9999
(** Raises warnings and errors. *)
100100
let resolve_and_substitute ~resolver ~make_root ~source_id_opt ~cmt_filename_opt
101101
~hidden (parent : Paths.Identifier.ContainerPage.t option) input_file
102-
input_type ~count_occurrences:_ =
102+
input_type ~count_occurrences =
103103
let filename = Fs.File.to_string input_file in
104104
let unit =
105105
match input_type with
106106
| `Cmti ->
107107
Odoc_loader.read_cmti ~make_root ~parent ~filename ~source_id_opt
108-
~cmt_filename_opt
108+
~cmt_filename_opt ~count_occurrences
109109
|> Error.raise_errors_and_warnings
110110
| `Cmt ->
111111
Odoc_loader.read_cmt ~make_root ~parent ~filename ~source_id_opt
112+
~count_occurrences
112113
|> Error.raise_errors_and_warnings
113114
| `Cmi ->
114115
Odoc_loader.read_cmi ~make_root ~parent ~filename

src/odoc/html_page.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let render { html_config; source = _; assets = _ } page =
3939

4040
let source_documents source_info source ~syntax =
4141
match (source_info, source) with
42-
| Some { Lang.Source_info.id; infos }, Some src -> (
42+
| Some { Lang.Source_info.id = Some id; infos }, Some src -> (
4343
let file =
4444
match src with
4545
| Source.File f -> f
@@ -68,7 +68,7 @@ let source_documents source_info source ~syntax =
6868
Odoc_document.Renderer.document_of_source ~syntax id syntax_info
6969
infos source_code;
7070
])
71-
| Some { id; _ }, None ->
71+
| Some { id = Some id; _ }, None ->
7272
let filename = Paths.Identifier.name id in
7373
Error.raise_warning
7474
(Error.filename_only
@@ -77,14 +77,14 @@ let source_documents source_info source ~syntax =
7777
--source-name"
7878
filename);
7979
[]
80-
| None, Some src ->
80+
| _, Some src ->
8181
Error.raise_warning
8282
(Error.filename_only
8383
"--source argument is invalid on compilation unit that were not \
8484
compiled with --source-parent and --source-name"
8585
(Source.to_string src));
8686
[]
87-
| None, None -> []
87+
| _, None -> []
8888

8989
let list_filter_map f lst =
9090
List.rev

0 commit comments

Comments
 (0)