Skip to content

Commit

Permalink
[flow][refactor] Lift loc_of_aloc function from find-ref into comma…
Browse files Browse the repository at this point in the history
…nd handler

Summary:
Makes find ref code no longer depend on `reader`, which can help enable find-ref on try-flow. Also makes future refactors on `loc_of_aloc` easier, by limiting the construction of the function to fewer places.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D56160508

fbshipit-source-id: 5aca5cce5da316ed7e768e7f838200724e8fcdc4
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 16, 2024
1 parent a9e0acb commit 358d3be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
35 changes: 18 additions & 17 deletions src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2573,15 +2573,15 @@ let get_file_artifacts ~options ~client ~profiling ~env pos :
(Error err_str, Some (Hh_json.JSON_Object json_props))
| Ok file_artifacts -> (Ok (Some (file_artifacts, file_key)), None))

let find_local_references ~reader ~file_artifacts ~kind file_key pos :
let find_local_references ~loc_of_aloc ~file_artifacts ~kind file_key pos :
((Get_def_types.def_info * FindRefsTypes.find_refs_ok, string) result * Hh_json.json option)
Lwt.t =
let (parse_artifacts, typecheck_artifacts) = file_artifacts in
let (line, col) = Flow_lsp_conversions.position_of_document_position pos in
let%lwt refs_results =
let results =
FindRefs_js.find_local_refs
~reader
~loc_of_aloc
~file_key
~parse_artifacts
~typecheck_artifacts
Expand Down Expand Up @@ -2613,16 +2613,16 @@ let find_local_references ~reader ~file_artifacts ~kind file_key pos :
in
Lwt.return (refs_results, extra_data)

let map_local_find_references_results ~reader ~options ~client ~profiling ~env ~f text_doc_position
=
let map_local_find_references_results
~loc_of_aloc ~options ~client ~profiling ~env ~f text_doc_position =
let (file_artifacts_opt, extra_parse_data) =
get_file_artifacts ~options ~client ~profiling ~env text_doc_position
in
match file_artifacts_opt with
| Ok (Some (file_artifacts, file_key)) ->
let%lwt (local_refs, extra_data) =
find_local_references
~reader
~loc_of_aloc
~file_artifacts
~kind:FindRefsTypes.FindReferences
file_key
Expand All @@ -2639,7 +2639,7 @@ let map_local_find_references_results ~reader ~options ~client ~profiling ~env ~
| Error _ as err -> Lwt.return (err, extra_parse_data)

let handle_global_find_references
~reader
~loc_of_aloc
~options
~id
~metadata
Expand Down Expand Up @@ -2681,7 +2681,7 @@ let handle_global_find_references
let (line, col) = Flow_lsp_conversions.position_of_document_position text_doc_position in
(match
FindRefs_js.find_local_refs
~reader
~loc_of_aloc
~file_key
~parse_artifacts
~typecheck_artifacts
Expand Down Expand Up @@ -2760,13 +2760,13 @@ let handle_global_find_references
def_locs;
Lwt.return (env, LspProt.LspFromServer None, LspProt.empty_metadata))

let handle_persistent_find_references ~reader ~options ~id ~params ~metadata ~client ~profiling ~env
=
let handle_persistent_find_references
~loc_of_aloc ~options ~id ~params ~metadata ~client ~profiling ~env =
let text_doc_position = params.FindReferences.loc in
let ref_to_location (_, loc) = Flow_lsp_conversions.loc_to_lsp loc |> Base.Result.ok in
if Options.global_find_ref options then
handle_global_find_references
~reader
~loc_of_aloc
~options
~id
~metadata
Expand All @@ -2787,7 +2787,7 @@ let handle_persistent_find_references ~reader ~options ~id ~params ~metadata ~cl
else
let%lwt (result, extra_data) =
map_local_find_references_results
~reader
~loc_of_aloc
~options
~client
~profiling
Expand All @@ -2806,14 +2806,14 @@ let handle_persistent_find_references ~reader ~options ~id ~params ~metadata ~cl
Lwt.return (env, resp, metadata)

let handle_persistent_document_highlight
~reader ~options ~id ~params ~metadata ~client ~profiling ~env =
~loc_of_aloc ~options ~id ~params ~metadata ~client ~profiling ~env =
(* All the locs are implicitly in the same file *)
let ref_to_highlight (_, loc) =
Some { DocumentHighlight.range = Lsp.loc_to_lsp_range loc; kind = Some DocumentHighlight.Text }
in
let%lwt (result, extra_data) =
map_local_find_references_results
~reader
~loc_of_aloc
~options
~client
~profiling
Expand All @@ -2834,7 +2834,7 @@ let handle_persistent_rename ~reader ~options ~id ~params ~metadata ~client ~pro
let text_doc_position = TextDocumentPositionParams.{ textDocument; position } in
if Options.global_rename options then
handle_global_find_references
~reader
~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader)
~options
~id
~metadata
Expand Down Expand Up @@ -2903,7 +2903,7 @@ let handle_persistent_rename ~reader ~options ~id ~params ~metadata ~client ~pro
let global = Options.global_rename options in
let%lwt (all_refs, extra_data) =
find_local_references
~reader
~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader)
~file_artifacts
~kind:FindRefsTypes.Rename
file_key
Expand Down Expand Up @@ -3428,6 +3428,7 @@ let get_persistent_handler ~genv ~client_id ~request:(request, metadata) :
let open LspProt in
let options = genv.ServerEnv.options in
let reader = State_reader.create () in
let loc_of_aloc = Parsing_heaps.Reader.loc_of_aloc ~reader in
match request with
| LspToServer (RequestMessage (id, _))
when IdSet.mem id !ServerMonitorListenerState.cancellation_requests ->
Expand Down Expand Up @@ -3530,10 +3531,10 @@ let get_persistent_handler ~genv ~client_id ~request:(request, metadata) :
| LspToServer (RequestMessage (id, DocumentHighlightRequest params)) ->
mk_parallelizable_persistent
~options
(handle_persistent_document_highlight ~reader ~options ~id ~params ~metadata)
(handle_persistent_document_highlight ~loc_of_aloc ~options ~id ~params ~metadata)
| LspToServer (RequestMessage (id, FindReferencesRequest params)) ->
Handle_nonparallelizable_persistent
(handle_persistent_find_references ~reader ~options ~id ~params ~metadata)
(handle_persistent_find_references ~loc_of_aloc ~options ~id ~params ~metadata)
| LspToServer (RequestMessage (id, RenameRequest params)) ->
Handle_nonparallelizable_persistent
(handle_persistent_rename ~reader ~options ~id ~params ~metadata)
Expand Down
6 changes: 3 additions & 3 deletions src/services/references/findRefs_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ let local_refs_of_find_ref_request
merge_with_var_refs prop_refs
| Get_def_types.NoDefinition no_def_reason -> Ok (FindRefsTypes.NoDefinition no_def_reason)

let find_local_refs ~reader ~file_key ~parse_artifacts ~typecheck_artifacts ~kind ~line ~col =
let find_local_refs ~loc_of_aloc ~file_key ~parse_artifacts ~typecheck_artifacts ~kind ~line ~col =
let open Base.Result.Let_syntax in
let ast_info =
match parse_artifacts with
| Types_js_types.Parse_artifacts { ast; file_sig; docblock; _ } -> (ast, file_sig, docblock)
in
let%bind def_info =
GetDefUtils.get_def_info
~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader)
~loc_of_aloc
~purpose:Get_def_types.Purpose.FindReferences
ast_info
typecheck_artifacts
(Loc.cursor (Some file_key) line col)
in
let%bind result =
local_refs_of_find_ref_request
~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader)
~loc_of_aloc
ast_info
typecheck_artifacts
file_key
Expand Down
2 changes: 1 addition & 1 deletion src/services/references/findRefs_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*)

val find_local_refs :
reader:State_reader.t ->
loc_of_aloc:(ALoc.t -> Loc.t) ->
file_key:File_key.t ->
parse_artifacts:Types_js_types.parse_artifacts ->
typecheck_artifacts:Types_js_types.typecheck_artifacts ->
Expand Down
3 changes: 1 addition & 2 deletions src/services/references/propertyFindRefs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ let property_find_refs_in_file ~loc_of_aloc ast_info type_info file_key = functi
:: Base.List.map references ~f:(fun l -> (FindRefsTypes.PropertyAccess, l))
)

let find_local_refs ~reader file_key ast_info type_info loc =
let loc_of_aloc = Parsing_heaps.Reader.loc_of_aloc ~reader in
let find_local_refs ~loc_of_aloc file_key ast_info type_info loc =
match get_property_def_info ~loc_of_aloc type_info loc with
| Error _ as err -> err
| Ok None -> Ok None
Expand Down
2 changes: 1 addition & 1 deletion src/services/references/propertyFindRefs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ val property_find_refs_in_file :
(FindRefsTypes.single_ref list, string) result

val find_local_refs :
reader:State_reader.t ->
loc_of_aloc:(ALoc.t -> Loc.t) ->
File_key.t ->
FindRefsUtils.ast_info ->
Types_js_types.typecheck_artifacts ->
Expand Down

0 comments on commit 358d3be

Please sign in to comment.