Skip to content

Commit 4aeb105

Browse files
committed
Make extraction works in submodule.
1 parent 195ed6d commit 4aeb105

File tree

4 files changed

+108
-27
lines changed

4 files changed

+108
-27
lines changed

src/analysis/refactor_extract_region.ml

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ let most_inclusive_expr ~start ~stop nodes =
370370
select_among_child node.Browse_tree.t_env node.t_node)
371371
in
372372
let node_loc = Mbrowse.node_loc node in
373+
373374
match node with
374375
| Expression expr
375376
when node_loc.loc_ghost = false && is_inside_region node_loc ->
@@ -387,38 +388,42 @@ let most_inclusive_expr ~start ~stop nodes =
387388
the body of a method. *)
388389
(remove_poly expr, env))
389390

390-
let find_associated_toplevel_item expr structure =
391+
let find_associated_toplevel_item expr enclosing =
391392
Stdlib.List.find_map
392-
(fun { Typedtree.str_desc; str_loc; str_env } ->
393-
match str_desc with
394-
| Tstr_value (rec_flag, vb)
395-
when Location_aux.included expr.Typedtree.exp_loc ~into:str_loc ->
396-
Some { rec_flag; env = str_env; loc = str_loc; kind = Let vb }
397-
| Tstr_class cs ->
398-
Stdlib.List.find_map
399-
(fun (class_decl, _) ->
400-
let loc = class_decl.Typedtree.ci_loc in
401-
if Location_aux.included expr.exp_loc ~into:loc then
402-
Some
403-
{ rec_flag = Nonrecursive;
404-
env = str_env;
405-
loc;
406-
kind = Class_decl
407-
}
408-
else None)
409-
cs
393+
(fun (_, item) ->
394+
match item with
395+
| Browse_raw.Structure_item ({ str_desc; str_loc; str_env }, _) -> begin
396+
match str_desc with
397+
| Tstr_value (rec_flag, vb)
398+
when Location_aux.included expr.Typedtree.exp_loc ~into:str_loc ->
399+
Some { rec_flag; env = str_env; loc = str_loc; kind = Let vb }
400+
| Tstr_class cs ->
401+
Stdlib.List.find_map
402+
(fun (class_decl, _) ->
403+
let loc = class_decl.Typedtree.ci_loc in
404+
if Location_aux.included expr.exp_loc ~into:loc then
405+
Some
406+
{ rec_flag = Nonrecursive;
407+
env = str_env;
408+
loc;
409+
kind = Class_decl
410+
}
411+
else None)
412+
cs
413+
| _ -> None
414+
end
410415
| _ -> None)
411-
structure.Typedtree.str_items
416+
enclosing
412417

413-
let extract_region ~start ~stop enclosing structure =
418+
let extract_region ~start ~stop enclosing =
414419
let open Option.Infix in
415420
most_inclusive_expr ~start ~stop enclosing >>= fun (expr, expr_env) ->
416421
(* si contenu de l'expr contient une expression local alors inextrayable *)
417-
find_associated_toplevel_item expr structure >>| fun toplevel_item ->
422+
find_associated_toplevel_item expr enclosing >>| fun toplevel_item ->
418423
(expr, expr_env, toplevel_item)
419424

420-
let is_region_extractable ~start ~stop enclosing structure =
421-
match extract_region ~start ~stop enclosing structure with
425+
let is_region_extractable ~start ~stop enclosing =
426+
match extract_region ~start ~stop enclosing with
422427
| None -> false
423428
| Some _ -> true
424429

@@ -429,7 +434,7 @@ let substitute ~start ~stop ?extract_name buffer typedtree =
429434
let enclosing =
430435
Mbrowse.enclosing start [ Mbrowse.of_structure structure ]
431436
in
432-
match extract_region ~start ~stop enclosing structure with
437+
match extract_region ~start ~stop enclosing with
433438
| None -> raise Nothing_to_do
434439
| Some (expr, expr_env, toplevel_item) -> begin
435440
match expr.exp_desc with

src/analysis/refactor_extract_region.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ val is_region_extractable :
9090
start:Lexing.position ->
9191
stop:Lexing.position ->
9292
(Env.t * Browse_raw.node) list ->
93-
Typedtree.structure ->
9493
bool
9594

9695
(** [substitute ~start ~stop ~extract_name buffer typedtree] tries to

tests/test-dirs/refactor-extract-region/func-extraction.t/func.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,16 @@ let f x =
143143
end
144144
let z = 0
145145
end in
146-
x * M.z * M.MM.y + A.a
146+
(x * M.z * M.MM.y) + A.a
147+
148+
module T = struct
149+
let on_list x = x + 1
150+
let k : (int, int) Result.t = Ok 10
151+
let r = Ok 10
152+
153+
let x =
154+
let a_list = List.map on_list [ 1; 2; 3 ] in
155+
let open Format in
156+
let printer = pp_print_list pp_print_int in
157+
printf "%a\n" printer a_list
158+
end

tests/test-dirs/refactor-extract-region/func-extraction.t/run.t

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,3 +763,68 @@ TODO: This extraction shouldn't be allowed.
763763
},
764764
"notifications": []
765765
}
766+
767+
$ $MERLIN single refactoring-extract-region -start 154:17 -end 154:45 < func.ml
768+
{
769+
"class": "return",
770+
"value": {
771+
"start": {
772+
"line": 153,
773+
"col": 2
774+
},
775+
"end": {
776+
"line": 157,
777+
"col": 32
778+
},
779+
"content": "let fun_name2 () = List.map on_list [1; 2; 3]
780+
let x =
781+
let a_list = (fun_name2 ()) in
782+
let open Format in
783+
let printer = pp_print_list pp_print_int in
784+
printf \"%a\
785+
\" printer a_list",
786+
"selection-range": {
787+
"start": {
788+
"line": 153,
789+
"col": 4
790+
},
791+
"end": {
792+
"line": 153,
793+
"col": 13
794+
}
795+
}
796+
},
797+
"notifications": []
798+
}
799+
$ $MERLIN single refactoring-extract-region -start 154:18 -end 154:44 < func.ml
800+
{
801+
"class": "return",
802+
"value": {
803+
"start": {
804+
"line": 153,
805+
"col": 2
806+
},
807+
"end": {
808+
"line": 157,
809+
"col": 32
810+
},
811+
"content": "let fun_name2 () = on_list
812+
let x =
813+
let a_list = List.map (fun_name2 ()) [ 1; 2; 3 ] in
814+
let open Format in
815+
let printer = pp_print_list pp_print_int in
816+
printf \"%a\
817+
\" printer a_list",
818+
"selection-range": {
819+
"start": {
820+
"line": 153,
821+
"col": 4
822+
},
823+
"end": {
824+
"line": 153,
825+
"col": 13
826+
}
827+
}
828+
},
829+
"notifications": []
830+
}

0 commit comments

Comments
 (0)