Skip to content

Commit d790c41

Browse files
committed
Add a function to determine if an expression is extractable inside a region.
1 parent 1595bf4 commit d790c41

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/analysis/refactor_extract_region.ml

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,25 +366,35 @@ let find_associated_toplevel_item expr structure =
366366
| _ -> None)
367367
structure.Typedtree.str_items
368368

369+
let extract_region ~start ~stop enclosing structure =
370+
let open Option.Infix in
371+
most_inclusive_expr ~start ~stop enclosing >>= fun (expr, expr_env) ->
372+
find_associated_toplevel_item expr structure >>| fun toplevel_item ->
373+
(expr, expr_env, toplevel_item)
374+
375+
let is_region_extractable ~start ~stop enclosing structure =
376+
match extract_region ~start ~stop enclosing structure with
377+
| None -> false
378+
| Some _ -> true
379+
369380
let substitute ~start ~stop ?extract_name mconfig buffer typedtree =
370381
match typedtree with
371382
| `Interface _ -> raise Not_allowed_in_interface_file
372-
| `Implementation structure -> (
383+
| `Implementation structure -> begin
373384
let enclosing =
374385
Mbrowse.enclosing start [ Mbrowse.of_structure structure ]
375386
in
376-
match most_inclusive_expr ~start ~stop enclosing with
387+
match extract_region ~start ~stop enclosing structure with
377388
| None -> raise Nothing_to_do
378-
| Some (expr, expr_env) -> (
379-
match find_associated_toplevel_item expr structure with
380-
| None -> raise Nothing_to_do
381-
| Some toplevel_item -> (
382-
match expr.exp_desc with
383-
| Texp_constant _ ->
384-
(* Special case for constant. They can't produce side effect so it's not
389+
| Some (expr, expr_env, toplevel_item) -> begin
390+
match expr.exp_desc with
391+
| Texp_constant _ ->
392+
(* Special case for constant. They can't produce side effect so it's not
385393
necessary to add a trailing unit parameter to the let binding. *)
386-
extract_const_to_toplevel ?extract_name expr ~expr_env buffer
387-
~toplevel_item
388-
| _ ->
389-
extract_expr_to_toplevel ?extract_name expr buffer ~expr_env
390-
~toplevel_item ~local_defs:typedtree ~mconfig)))
394+
extract_const_to_toplevel ?extract_name expr ~expr_env buffer
395+
~toplevel_item
396+
| _ ->
397+
extract_expr_to_toplevel ?extract_name expr buffer ~expr_env
398+
~toplevel_item ~local_defs:typedtree ~mconfig
399+
end
400+
end

src/analysis/refactor_extract_region.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ exception Nothing_to_do
8585
(** Raised when extraction is called inside an interface file. *)
8686
exception Not_allowed_in_interface_file
8787

88+
(** Is an expression is extractable in the given region?. *)
89+
val is_region_extractable :
90+
start:Lexing.position ->
91+
stop:Lexing.position ->
92+
(Env.t * Browse_raw.node) list ->
93+
Typedtree.structure ->
94+
bool
95+
8896
(** [substitute ~start ~stop ~extract_name config buffer typedtree] tries to
8997
extract the most inclusive expression located in interval [start-stop] into
9098
a fresh toplevel generated let binding.

0 commit comments

Comments
 (0)