@@ -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+
369380let 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
0 commit comments