@@ -14,117 +14,99 @@ let filter_by_cursor cursor (loc : Warnings.loc) : bool =
1414 in
1515 line_in && col_in
1616
17- type filter = Cursor of (int * int ) | Loc of Loc .t
18-
19- let dump ~state ?filter rescript_json cmt_path =
20- let uri = Uri. from_path (Filename. remove_extension cmt_path ^ " .res" ) in
21- let package =
22- let uri = Uri. from_path rescript_json in
23- Packages. get_package ~state ~uri |> Option. get
24- in
25- let module_name =
26- Build_system. namespaced_name package.namespace
27- (Find_files. get_name cmt_path)
17+ let dump ~(filter_for_position : (int * int) option ) ~full =
18+ let open Shared_types in
19+ let open Shared_types.Stamps in
20+ let buffer = Buffer. create 4096 in
21+ let printf fmt = Printf. bprintf buffer fmt in
22+ let apply_filter =
23+ match filter_for_position with
24+ | None -> fun _ -> true
25+ | Some cursor -> Loc. has_pos ~pos: cursor
2826 in
29- match Cmt. full_for_cmt ~module_name ~package ~uri cmt_path with
30- | None -> failwith (Format. sprintf " Could not load cmt for %s" cmt_path)
31- | Some full ->
32- let open Shared_types in
33- let open Shared_types.Stamps in
34- let apply_filter =
35- match filter with
36- | None -> fun _ -> true
37- | Some (Cursor cursor ) -> Loc. has_pos ~pos: cursor
38- | Some (Loc loc ) -> Loc. is_inside loc
39- in
40- (match filter with
41- | None -> ()
42- | Some (Cursor (line , col )) ->
43- Printf. printf " Filtering by cursor %d,%d\n " line col
44- | Some (Loc loc ) ->
45- Printf. printf " Filtering by loc %s\n " (Loc. to_string loc));
27+ (match filter_for_position with
28+ | None -> ()
29+ | Some (line , col ) -> printf " Filtering by cursor %d,%d\n " line col);
4630
47- Printf. printf " file moduleName: %s\n\n " full.file.module_name;
31+ printf " file moduleName: %s\n\n " full.file.module_name;
4832
49- let stamps =
50- full.file.stamps |> get_entries
51- |> List. filter (fun (_ , stamp ) -> apply_filter (loc_of_kind stamp))
52- in
33+ let stamps =
34+ full.file.stamps |> get_entries
35+ |> List. filter (fun (_ , stamp ) -> apply_filter (loc_of_kind stamp))
36+ in
5337
54- let total_stamps = List. length stamps in
55- Printf. printf " Found %d stamps:\n %s" total_stamps
56- (if total_stamps > 0 then " \n " else " " );
38+ let total_stamps = List. length stamps in
39+ printf " Found %d stamps:\n %s" total_stamps
40+ (if total_stamps > 0 then " \n " else " " );
5741
58- stamps
59- |> List. sort (fun (_ , a ) (_ , b ) ->
60- let a_loc = loc_of_kind a in
61- let b_loc = loc_of_kind b in
62- match compare a_loc.loc_start.pos_lnum b_loc.loc_start.pos_lnum with
63- | 0 -> compare a_loc.loc_start.pos_cnum b_loc.loc_start.pos_cnum
64- | c -> c)
65- |> List. iter (fun (stamp , kind ) ->
66- match kind with
67- | KType t ->
68- Printf. printf " %d ktype %s\n " stamp
69- (Warnings. loc_to_string t.extent_loc)
70- | KValue t ->
71- Printf. printf " %d kvalue %s\n " stamp
72- (Warnings. loc_to_string t.extent_loc)
73- | KModule t ->
74- Printf. printf " %d kmodule %s\n " stamp
75- (Warnings. loc_to_string t.extent_loc)
76- | KConstructor t ->
77- Printf. printf " %d kconstructor %s\n " stamp
78- (Warnings. loc_to_string t.extent_loc));
42+ stamps
43+ |> List. sort (fun (_ , a ) (_ , b ) ->
44+ let a_loc = loc_of_kind a in
45+ let b_loc = loc_of_kind b in
46+ match compare a_loc.loc_start.pos_lnum b_loc.loc_start.pos_lnum with
47+ | 0 -> compare a_loc.loc_start.pos_cnum b_loc.loc_start.pos_cnum
48+ | c -> c)
49+ |> List. iter (fun (stamp , kind ) ->
50+ match kind with
51+ | KType t ->
52+ printf " %d ktype %s\n " stamp
53+ (Warnings. loc_to_string t.extent_loc)
54+ | KValue t ->
55+ printf " %d kvalue %s\n " stamp
56+ (Warnings. loc_to_string t.extent_loc)
57+ | KModule t ->
58+ printf " %d kmodule %s\n " stamp
59+ (Warnings. loc_to_string t.extent_loc)
60+ | KConstructor t ->
61+ printf " %d kconstructor %s\n " stamp
62+ (Warnings. loc_to_string t.extent_loc));
7963
80- (* dump the structure *)
81- let rec dump_structure indent (structure : Module.structure ) =
82- if indent > 0 then Printf. printf " %s" (String. make indent ' ' );
83- Printf. printf " Structure %s:\n " structure.name;
84- structure.items |> List. iter (dump_structure_item (indent + 2 ))
85- and dump_structure_item indent item =
86- if indent > 0 then Printf. printf " %s" (String. make indent ' ' );
87- let open Module in
88- match item.kind with
89- | Value _typedExpr ->
90- Printf. printf " Value %s %s\n " item.name
91- (Warnings. loc_to_string item.loc)
92- | Type _ ->
93- Printf. printf " Type %s %s\n " item.name (Warnings. loc_to_string item.loc)
94- | Module {type_ = m } ->
95- Printf. printf " Module %s %s\n " item.name
96- (Warnings. loc_to_string item.loc);
97- dump_module indent m
98- and dump_module indent (module_ : Module.t ) =
99- match module_ with
100- | Ident path -> Printf. printf " Module (Ident) %s\n " (Path. name path)
101- | Structure structure -> dump_structure indent structure
102- | Constraint (m1 , m2 ) ->
103- dump_module indent m1;
104- dump_module indent m2
105- in
64+ (* dump the structure *)
65+ let rec dump_structure indent (structure : Module.structure ) =
66+ if indent > 0 then printf " %s" (String. make indent ' ' );
67+ printf " Structure %s:\n " structure.name;
68+ structure.items |> List. iter (dump_structure_item (indent + 2 ))
69+ and dump_structure_item indent item =
70+ if indent > 0 then printf " %s" (String. make indent ' ' );
71+ let open Module in
72+ match item.kind with
73+ | Value _typedExpr ->
74+ printf " Value %s %s\n " item.name (Warnings. loc_to_string item.loc)
75+ | Type _ ->
76+ printf " Type %s %s\n " item.name (Warnings. loc_to_string item.loc)
77+ | Module {type_ = m } ->
78+ printf " Module %s %s\n " item.name (Warnings. loc_to_string item.loc);
79+ dump_module indent m
80+ and dump_module indent (module_ : Module.t ) =
81+ match module_ with
82+ | Ident path -> printf " Module (Ident) %s\n " (Path. name path)
83+ | Structure structure -> dump_structure indent structure
84+ | Constraint (m1 , m2 ) ->
85+ dump_module indent m1;
86+ dump_module indent m2
87+ in
10688
107- print_newline () ;
108- dump_structure 0 full.file.structure;
89+ printf " \n " ;
90+ dump_structure 0 full.file.structure;
10991
110- (* Dump all locItems (typed nodes) *)
111- let loc_items =
112- match full.extra with
113- | {loc_items} ->
114- loc_items |> List. filter (fun loc_item -> apply_filter loc_item.loc)
115- in
92+ (* Dump all locItems (typed nodes) *)
93+ let loc_items =
94+ match full.extra with
95+ | {loc_items} ->
96+ loc_items |> List. filter (fun loc_item -> apply_filter loc_item.loc)
97+ in
11698
117- Printf. printf " \n Found %d locItems (typed nodes):\n\n "
118- (List. length loc_items);
99+ printf " \n Found %d locItems (typed nodes):\n\n " (List. length loc_items);
119100
120- loc_items
121- |> List. sort (fun a b ->
122- let a_loc = a.loc.Location. loc_start in
123- let b_loc = b.loc.Location. loc_start in
124- match compare a_loc.pos_lnum b_loc.pos_lnum with
125- | 0 -> compare a_loc.pos_cnum b_loc.pos_cnum
126- | c -> c)
127- |> List. iter (fun {loc; loc_type} ->
128- let loc_str = Warnings. loc_to_string loc in
129- let kind_str = Shared_types. loc_type_to_string loc_type in
130- Printf. printf " %s %s\n " loc_str kind_str)
101+ loc_items
102+ |> List. sort (fun a b ->
103+ let a_loc = a.loc.Location. loc_start in
104+ let b_loc = b.loc.Location. loc_start in
105+ match compare a_loc.pos_lnum b_loc.pos_lnum with
106+ | 0 -> compare a_loc.pos_cnum b_loc.pos_cnum
107+ | c -> c)
108+ |> List. iter (fun {loc; loc_type} ->
109+ let loc_str = Warnings. loc_to_string loc in
110+ let kind_str = Shared_types. loc_type_to_string loc_type in
111+ printf " %s %s\n " loc_str kind_str);
112+ Buffer. contents buffer
0 commit comments