Skip to content

Commit

Permalink
[lsp][extract] Compute the union of all extracted statements once ins…
Browse files Browse the repository at this point in the history
…ide `provide_available_refactor`

Summary:
This diff extracts the code to compute the union of all statements to `provide_available_refactor`.

In the next diff where we compute the locations of other scopes to insert function definitions, we will use this computed location union

Reviewed By: vrama628

Differential Revision: D29050299

fbshipit-source-id: 14fcb4b198fd61f90fbbd21920f1496b399db697
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jun 11, 2021
1 parent 817baa1 commit 51af1ef
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/services/code_action/refactor_extract_function.ml
Expand Up @@ -49,18 +49,14 @@ let union_loc acc loc =
| None -> Some loc
| Some existing_loc -> Some (Loc.btwn existing_loc loc)

class new_function_call_replacer insert_new_function_call_loc rest_statements_loc_union =
class new_function_call_replacer
insert_new_function_call_loc rest_statements_loc_union statements_loc_union =
object (this)
inherit [Loc.t] Flow_ast_mapper.mapper as super

method private should_be_replaced_by_function_call loc =
Loc.equal insert_new_function_call_loc loc

method private inserted_function_call_loc () =
match rest_statements_loc_union with
| None -> insert_new_function_call_loc
| Some rest_union -> Loc.btwn insert_new_function_call_loc rest_union

method private should_be_replaced_by_empty loc =
match rest_statements_loc_union with
| Some rest_statements_loc_union -> Loc.contains rest_statements_loc_union loc
Expand All @@ -72,7 +68,7 @@ class new_function_call_replacer insert_new_function_call_loc rest_statements_lo
if this#should_be_replaced_by_function_call statement_loc then
[
Statements.expression
~loc:(this#inserted_function_call_loc ())
~loc:statements_loc_union
(Expressions.call (Expressions.identifier "newFunction"));
]
else if this#should_be_replaced_by_empty statement_loc then
Expand Down Expand Up @@ -120,7 +116,15 @@ let provide_available_refactor ast extract_range =
| [] -> None
| insert_new_function_call_loc :: rest_statements_locations ->
let rest_statements_loc_union = List.fold_left union_loc None rest_statements_locations in
let statements_loc_union =
match rest_statements_loc_union with
| None -> insert_new_function_call_loc
| Some loc -> Loc.btwn insert_new_function_call_loc loc
in
let replacer =
new new_function_call_replacer insert_new_function_call_loc rest_statements_loc_union
new new_function_call_replacer
insert_new_function_call_loc
rest_statements_loc_union
statements_loc_union
in
Some (insert_function_to_toplevel (replacer#program ast) extracted_statements))

0 comments on commit 51af1ef

Please sign in to comment.