22open Tlapm_lib
33module StepMap = Map. Make (String )
44
5- exception Found_the_step of string * int * Range. t list
5+ module StepInfo = struct
6+ type t = { name : string ; label_offset : int ; step_ranges : Range .t list }
7+
8+ let make level suffix step_loc =
9+ let prefix_str = Printf. sprintf " <%d>" level in
10+ let step_str = Printf. sprintf " <%d>%s" level suffix in
11+ let range = Range. of_locus_must step_loc in
12+ let range = Range. of_len range (String. length step_str) in
13+ {
14+ name = step_str;
15+ label_offset = String. length prefix_str;
16+ step_ranges = [ range ];
17+ }
18+
19+ let with_range si range = { si with step_ranges = range :: si .step_ranges }
20+
21+ let matching_range si range =
22+ List. find_opt
23+ (fun r -> Range. intersect range r || Range. touches range r)
24+ si.step_ranges
25+
26+ let step_label si =
27+ String. sub si.name si.label_offset (String. length si.name - si.label_offset)
28+
29+ let step_label_range si range = Range. crop_line_prefix range si.label_offset
30+ end
31+
32+ exception Found_the_step of StepInfo. t
633
734class step_rename_visitor (at_loc : Range.t ) =
835 object (self : 'self )
936 inherit Module.Visit. map as m_super
1037 inherit [unit ] Proof.Visit. iter as p_super
11- val mutable step_map : ( int * Range. t list ) StepMap. t = StepMap. empty
38+ val mutable step_map : StepInfo. t StepMap. t = StepMap. empty
1239
13- (* Leaf at the `Proof .Visit.iter as p_super `.
40+ (* Leaf at the `Module .Visit.map as m_super `.
1441 We will look at `orig_pf`, it is closer to the initial source code. *)
15- method! theorem cx name sq naxs _pf orig_pf summ =
16- let pf = orig_pf in
17- let ctx = Expr.Visit. empty () in
18- self#proof ctx pf;
42+ method! theorem cx name sq naxs pf orig_pf summ =
43+ (if Range. lines_intersect at_loc (Range. of_wrapped_must orig_pf) then
44+ (* Only look for steps in proofs that intersects with the user selection. *)
45+ let pf = orig_pf in
46+ let ctx = Expr.Visit. empty () in
47+ self#proof ctx pf);
1948 m_super#theorem cx name sq naxs pf orig_pf summ
2049
2150 (* After each proof drop new step names that were defined within the proof.
@@ -33,14 +62,13 @@ class step_rename_visitor (at_loc : Range.t) =
3362 | _ -> () );
3463 (* If we are removing some step from the context, all its references
3564 are collected already. Check maybe we found what we were looking for. *)
36- let matches range = Range. intersect range at_loc in
3765 step_map < -
3866 StepMap. filter
39- (fun step_str ( label_offset , step_ranges ) ->
40- if not (StepMap. mem step_str step_map_before) then (
41- if List. exists matches step_ranges then
42- raise ( Found_the_step (step_str, label_offset, step_ranges));
43- false )
67+ (fun step_str step_info ->
68+ if not (StepMap. mem step_str step_map_before) then
69+ match StepInfo. matching_range step_info at_loc with
70+ | None -> false
71+ | Some _ -> raise ( Found_the_step step_info )
4472 else true )
4573 step_map
4674
@@ -63,32 +91,26 @@ class step_rename_visitor (at_loc : Range.t) =
6391 method add_step_opt stepno_opt (step_loc : Loc.locus ) =
6492 match stepno_opt with
6593 | Proof.T. Named (level , suffix , false ) ->
66- let prefix_str = Printf. sprintf " <%d>" level in
67- let step_str = Printf. sprintf " <%d>%s" level suffix in
68- let range = Range. of_locus_must step_loc in
69- let range = Range. of_len range (String. length step_str) in
70- let step_info = (String. length prefix_str, [ range ]) in
71- step_map < - StepMap. add step_str step_info step_map
94+ let si = StepInfo. make level suffix step_loc in
95+ step_map < - StepMap. add si.name si step_map
7296 | Proof.T. Named (_level , _suffix , true ) -> ()
7397 | Proof.T. Unnamed (_ , _ ) -> ()
7498
7599 (* Private helper. *)
76100 method add_step_range_opt (step_name : string ) (locus : Loc.locus ) =
77101 match StepMap. find_opt step_name step_map with
78102 | None -> ()
79- | Some ( label_offset , ranges ) ->
80- let new_info = (label_offset, Range. of_locus_must locus :: ranges ) in
81- step_map < - StepMap. add step_name new_info step_map
103+ | Some si ->
104+ let si = StepInfo. with_range si ( Range. of_locus_must locus) in
105+ step_map < - StepMap. add step_name si step_map
82106 end
83107
84- let find_ranges (at_loc : Range.t ) (mule : Module.T.mule ) :
85- (string * int * Range. t list ) option =
108+ let find_ranges (at_loc : Range.t ) (mule : Module.T.mule ) : StepInfo.t option =
86109 let visitor = new step_rename_visitor at_loc in
87110 try
88111 let _ = visitor#tla_module_root mule in
89112 None
90- with Found_the_step (step_str , label_offset , step_ranges ) ->
91- Some (step_str, label_offset, step_ranges)
113+ with Found_the_step si -> Some si
92114
93115let % test_module " poc renames" =
94116 (module struct
@@ -111,47 +133,47 @@ let%test_module "poc renames" =
111133 let % test_unit " find <1>1 by step name" =
112134 match find_ranges (Range. of_ints ~lf: 3 ~cf: 6 ~lt: 3 ~ct: 6 ) mule with
113135 | None -> assert false
114- | Some (st_name , st_lb_o , st_ranges ) ->
115- assert (st_name = " <1>1" );
116- assert (st_lb_o = 3 );
117- assert (List. length st_ranges = 3 );
118- ()
136+ | Some si ->
137+ assert (si.name = " <1>1" );
138+ assert (si.label_offset = 3 );
139+ assert (List. length si.step_ranges = 3 )
119140
120141 let % test_unit " find <1>1 by step use" =
121142 match find_ranges (Range. of_ints ~lf: 5 ~cf: 18 ~lt: 5 ~ct: 18 ) mule with
122143 | None -> assert false
123- | Some (st_name , st_lb_o , st_ranges ) ->
124- assert (st_name = " <1>1" );
125- assert (st_lb_o = 3 );
126- assert (List. length st_ranges = 3 );
127- ()
144+ | Some si ->
145+ assert (si.name = " <1>1" );
146+ assert (si.label_offset = 3 );
147+ assert (List. length si.step_ranges = 3 )
128148
129149 let % test_unit " find <1>2" =
130150 match find_ranges (Range. of_ints ~lf: 4 ~cf: 6 ~lt: 4 ~ct: 6 ) mule with
131151 | None -> assert false
132- | Some (st_name , st_lb_o , st_ranges ) ->
133- assert (st_name = " <1>2" );
134- assert (st_lb_o = 3 );
135- assert (List. length st_ranges = 2 );
136- ()
152+ | Some si ->
153+ assert (si.name = " <1>2" );
154+ assert (si.label_offset = 3 );
155+ assert (List. length si.step_ranges = 2 )
156+
157+ let % test_unit " find <1>2 - start col" =
158+ match find_ranges (Range. of_ints ~lf: 4 ~cf: 5 ~lt: 4 ~ct: 5 ) mule with
159+ | None -> assert false
160+ | Some si -> assert (si.name = " <1>2" )
161+
162+ let % test_unit " find <1>2 - end col" =
163+ match find_ranges (Range. of_ints ~lf: 4 ~cf: 9 ~lt: 4 ~ct: 9 ) mule with
164+ | None -> assert false
165+ | Some si -> assert (si.name = " <1>2" )
137166
138167 let % test_unit " find <1>1 -- qed" =
139168 match find_ranges (Range. of_ints ~lf: 5 ~cf: 6 ~lt: 5 ~ct: 6 ) mule with
140169 | None -> assert false
141- | Some (st_name , st_lb_o , st_ranges ) ->
142- assert (st_name = " <1>q" );
143- assert (st_lb_o = 3 );
144- assert (List. length st_ranges = 1 );
145- ()
170+ | Some si ->
171+ assert (si.name = " <1>q" );
172+ assert (si.label_offset = 3 );
173+ assert (List. length si.step_ranges = 1 )
146174
147175 let % test_unit " not found" =
148176 match find_ranges (Range. of_ints ~lf: 2 ~cf: 6 ~lt: 2 ~ct: 6 ) mule with
149177 | None -> ()
150- | Some ( _st_name , _st_lb_o , _st_ranges ) -> assert false
178+ | Some _si -> assert false
151179 end )
152-
153- let step_label step_name label_offset =
154- String. sub step_name label_offset (String. length step_name - label_offset)
155-
156- let step_label_range range label_offset =
157- Range. crop_line_prefix range label_offset
0 commit comments