Skip to content

Commit fbb69e2

Browse files
committed
Render source code: tidy up info extraction 2
Signed-off-by: Paul-Elliot <[email protected]>
1 parent f8302a9 commit fbb69e2

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

src/loader/implementation.ml

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ module IdentHashtbl = Hashtbl.Make (struct
144144
let hash = Hashtbl.hash
145145
end)
146146

147-
(* populate a [loc_to_id] and an [ident_to_id] map with local informations. Also
148-
removes the definitions from the list, since their information has been taken
149-
into account *)
150-
let process_local_defs source_id poses loc_tbl ident_to_id =
151-
List.filter_map
147+
module UidHashtbl = Shape.Uid.Tbl
148+
149+
(* Adds the local definitions found in traverse infos to the [loc_to_id] and
150+
[ident_to_id] tables. *)
151+
let populate_local_defs source_id poses loc_to_id ident_to_id =
152+
List.iter
152153
(function
153154
| Typedtree_traverse.Analysis.Definition id, loc ->
154155
let name =
@@ -159,9 +160,8 @@ let process_local_defs source_id poses loc_tbl ident_to_id =
159160
Odoc_model.Paths.Identifier.Mk.source_location_int (source_id, name)
160161
in
161162
IdentHashtbl.add ident_to_id id identifier;
162-
LocHashtbl.add loc_tbl loc identifier;
163-
None
164-
| x -> Some x)
163+
LocHashtbl.add loc_to_id loc identifier
164+
| _ -> ())
165165
poses
166166

167167
(* In order to turn an identifier into a source identifier, we need to generate
@@ -243,44 +243,44 @@ let anchor_of_identifier id =
243243
in
244244
anchor_of_identifier [] id |> String.concat "."
245245

246-
(* Adds the global defs from the odoc environment to [loc_to_id] table *)
247-
let process_global_defs env source_id loc_to_id =
246+
(* Adds the global definitions, found in the [uid_to_loc], to the [loc_to_id]
247+
and [uid_to_id] tables. *)
248+
let populate_global_defs env source_id loc_to_id uid_to_loc uid_to_id =
248249
let mk_src_id id =
249250
let name = Odoc_model.Names.DefName.make_std (anchor_of_identifier id) in
250251
(Odoc_model.Paths.Identifier.Mk.source_location (source_id, name)
251252
:> Odoc_model.Paths.Identifier.SourceLocation.t)
252253
in
253-
Ident_env.iter_located_identifier env @@ fun loc id ->
254-
LocHashtbl.add loc_to_id loc (mk_src_id id)
255-
256-
(* The [uid_to_id] is useful as when resolving a shape, we get the uid. *)
257-
let build_uid_to_id source_id uid_to_loc loc_to_id =
258-
let uid_to_loc_map = Shape.Uid.Tbl.to_map uid_to_loc in
254+
let () =
255+
Ident_env.iter_located_identifier env @@ fun loc id ->
256+
LocHashtbl.add loc_to_id loc (mk_src_id id)
257+
in
259258
let mk_src_id () =
260259
let name =
261260
Odoc_model.Names.DefName.make_std (Printf.sprintf "def_%d" (counter ()))
262261
in
263262
(Odoc_model.Paths.Identifier.Mk.source_location (source_id, name)
264263
:> Odoc_model.Paths.Identifier.SourceLocation.t)
265264
in
266-
Shape.Uid.Map.filter_map
265+
Shape.Uid.Tbl.iter
267266
(fun uid loc ->
268-
if loc.Location.loc_ghost then None
267+
if loc.Location.loc_ghost then ()
269268
else
270269
match LocHashtbl.find_opt loc_to_id loc with
271-
| Some id -> Some id
270+
| Some id -> UidHashtbl.add uid_to_id uid id
272271
| None -> (
273272
(* In case there is no entry for the location of the uid, we add one. *)
274273
match uid with
275274
| Item _ ->
276275
let id = mk_src_id () in
277276
LocHashtbl.add loc_to_id loc id;
278-
Some id
279-
| Compilation_unit _ -> None
280-
| _ -> None))
281-
uid_to_loc_map
277+
UidHashtbl.add uid_to_id uid id
278+
| Compilation_unit _ -> ()
279+
| _ -> ()))
280+
uid_to_loc
282281

283-
(* Turns [Typedtree_traverse] occurrence information into proper source infos *)
282+
(* Extract [Typedtree_traverse] occurrence information and turn them into proper
283+
source infos *)
284284
let process_occurrences poses uid_to_id ident_to_id =
285285
List.filter_map
286286
(function
@@ -290,14 +290,14 @@ let process_occurrences poses uid_to_id ident_to_id =
290290
Some (Odoc_model.Lang.Source_info.Value anchor, pos_of_loc loc)
291291
| None -> None)
292292
| Value (DefJmp x), loc -> (
293-
match Shape.Uid.Map.find_opt x uid_to_id with
293+
match UidHashtbl.find_opt uid_to_id x with
294294
| Some id -> Some (Value id, pos_of_loc loc)
295295
| None -> None)
296296
| Definition _, _ -> None)
297297
poses
298298

299299
(* Add definition source info from the [loc_to_id] table *)
300-
let add_definitions occurrences loc_to_id =
300+
let add_definitions loc_to_id occurrences =
301301
LocHashtbl.fold
302302
(fun loc id acc ->
303303
(Odoc_model.Lang.Source_info.Definition id, pos_of_loc loc) :: acc)
@@ -310,25 +310,26 @@ let read_cmt_infos source_id_opt id cmt_info =
310310
match (source_id_opt, cmt_info.cmt_annots) with
311311
| Some source_id, Implementation impl ->
312312
let env = Env.of_structure id impl in
313-
let occ_infos =
313+
let traverse_infos =
314314
Typedtree_traverse.of_cmt env uid_to_loc impl |> List.rev
315315
(* Information are accumulated in a list. We need to have the
316316
first info first in the list, to assign anchors with increasing
317317
numbers, so that adding some content at the end of a file does
318318
not modify the anchors for existing anchors. *)
319319
in
320320
let loc_to_id = LocHashtbl.create 10
321-
and ident_to_id = IdentHashtbl.create 10 in
322-
let occurrences =
323-
process_local_defs source_id occ_infos loc_to_id ident_to_id
321+
and ident_to_id = IdentHashtbl.create 10
322+
and uid_to_id = UidHashtbl.create 10 in
323+
let () =
324+
(* populate [loc_to_id], [ident_to_id] and [uid_to_id] *)
325+
populate_local_defs source_id traverse_infos loc_to_id ident_to_id;
326+
populate_global_defs env source_id loc_to_id uid_to_loc uid_to_id
324327
in
325-
let () = process_global_defs env source_id loc_to_id in
326-
let uid_to_id = build_uid_to_id source_id uid_to_loc loc_to_id in
327-
let occurrences =
328-
process_occurrences occurrences uid_to_id ident_to_id
328+
let source_infos =
329+
process_occurrences traverse_infos uid_to_id ident_to_id
330+
|> add_definitions loc_to_id
329331
in
330-
let source_infos = add_definitions occurrences loc_to_id in
331-
( Some (shape, uid_to_id),
332+
( Some (shape, Shape.Uid.Tbl.to_map uid_to_id),
332333
Some
333334
{
334335
Odoc_model.Lang.Source_info.id = source_id;

0 commit comments

Comments
 (0)