From 7cf8cbff957c918f8095b047c6979303bef3fa4d Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Tue, 2 Apr 2024 10:46:35 -0700 Subject: [PATCH] [flow][cleanup] Organize common lambdas used by Lsp_import_edits into `lsp_module_system_info.t` Summary: Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D55609740 fbshipit-source-id: 54259aca24f39425bf1d92c40e67a4fc16c9bfda --- src/flow_dot_js.ml | 16 +++-- src/server/command_handler/commandHandler.ml | 34 ++++++----- .../autocomplete/autocompleteService_js.ml | 59 +++---------------- .../autocomplete/autocompleteService_js.mli | 6 +- .../code_action/code_action_service.ml | 52 +++++----------- .../code_action/code_action_service.mli | 8 +-- src/services/code_action/lsp_import_edits.ml | 56 ++++++------------ src/services/code_action/lsp_import_edits.mli | 12 +--- .../code_action/lsp_module_system_info.ml | 14 +++++ 9 files changed, 85 insertions(+), 172 deletions(-) create mode 100644 src/services/code_action/lsp_module_system_info.ml diff --git a/src/flow_dot_js.ml b/src/flow_dot_js.ml index a00218b3f73..970792f5a8f 100644 --- a/src/flow_dot_js.ml +++ b/src/flow_dot_js.ml @@ -380,14 +380,18 @@ let autocomplete filename content line col js_config_object : { Export_search_types.results = []; is_incomplete = false } in mk_typing_artifacts - ~file_options:Files.default_options ~layout_options:Js_layout_generator.default_opts - ~haste_module_system:false - ~loc_of_aloc ~get_ast_from_shared_mem:(fun _ -> None) - ~get_haste_name:(fun _ -> None) - ~get_package_info:(fun _ -> None) - ~is_package_file:(fun _ -> false) + ~module_system_info: + Lsp_module_system_info. + { + file_options = Files.default_options; + haste_module_system = false; + get_haste_name = (fun _ -> None); + get_package_info = (fun _ -> None); + is_package_file = (fun _ -> false); + } + ~loc_of_aloc ~search_exported_values:(fun ~ac_options:_ _ -> empty_exports_search_result) ~search_exported_types:(fun ~ac_options:_ _ -> empty_exports_search_result) ~cx diff --git a/src/server/command_handler/commandHandler.ml b/src/server/command_handler/commandHandler.ml index 19b2e2e7874..e2d1f81d112 100644 --- a/src/server/command_handler/commandHandler.ml +++ b/src/server/command_handler/commandHandler.ml @@ -249,11 +249,21 @@ let of_file_input ~options ~env file_input = let get_haste_name ~reader f = Parsing_heaps.get_file_addr f |> Base.Option.bind ~f:(Parsing_heaps.Reader.get_haste_name ~reader) -let is_package_file ~reader module_name = - let dependency = Parsing_heaps.get_dependency (Modulename.String module_name) in - match Option.bind dependency (Parsing_heaps.Reader.get_provider ~reader) with - | Some addr -> Parsing_heaps.Reader.is_package_file ~reader addr - | None -> false +let mk_module_system_info = + let is_package_file ~reader module_name = + let dependency = Parsing_heaps.get_dependency (Modulename.String module_name) in + match Option.bind dependency (Parsing_heaps.Reader.get_provider ~reader) with + | Some addr -> Parsing_heaps.Reader.is_package_file ~reader addr + | None -> false + in + fun ~options ~reader -> + { + Lsp_module_system_info.file_options = Options.file_options options; + haste_module_system = Options.(module_system options = Haste); + get_haste_name = get_haste_name ~reader; + get_package_info = Parsing_heaps.Reader.get_package_info ~reader; + is_package_file = is_package_file ~reader; + } let get_status ~options env = let lazy_stats = Rechecker.get_lazy_stats ~options env in @@ -385,14 +395,10 @@ let autocomplete Profiling_js.with_timer profiling ~timer:"GetResults" ~f:(fun () -> let typing = AutocompleteService_js.mk_typing_artifacts - ~file_options:(Options.file_options options) ~layout_options:(Code_action_utils.layout_options options) - ~haste_module_system:Options.(module_system options = Haste) + ~module_system_info:(mk_module_system_info ~options ~reader) ~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader) ~get_ast_from_shared_mem:(Parsing_heaps.Reader.get_ast ~reader) - ~get_haste_name:(get_haste_name ~reader) - ~get_package_info:(Parsing_heaps.Reader.get_package_info ~reader) - ~is_package_file:(is_package_file ~reader) ~search_exported_values:(search_exported_values ~exports:env.ServerEnv.exports) ~search_exported_types:(search_exported_types ~exports:env.ServerEnv.exports) ~cx @@ -1422,10 +1428,8 @@ let find_code_actions ~reader ~options ~env ~profiling ~params ~client = ~env ~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader) ~get_ast_from_shared_mem:(Parsing_heaps.Reader.get_ast ~reader) - ~get_haste_name:(get_haste_name ~reader) ~get_type_sig:(Parsing_heaps.Reader.get_type_sig ~reader) - ~get_package_info:(Parsing_heaps.Reader.get_package_info ~reader) - ~is_package_file:(is_package_file ~reader) + ~module_system_info:(mk_module_system_info ~options ~reader) ~cx ~file_sig ~tolerable_errors @@ -1487,9 +1491,7 @@ let add_missing_imports ~reader ~options ~env ~profiling ~client textDocument = ~options ~env ~loc_of_aloc - ~get_haste_name:(get_haste_name ~reader) - ~get_package_info:(Parsing_heaps.Reader.get_package_info ~reader) - ~is_package_file:(is_package_file ~reader) + ~module_system_info:(mk_module_system_info ~options ~reader) ~cx ~ast ~uri diff --git a/src/services/autocomplete/autocompleteService_js.ml b/src/services/autocomplete/autocompleteService_js.ml index 4d027b187b9..c64aaac9647 100644 --- a/src/services/autocomplete/autocompleteService_js.ml +++ b/src/services/autocomplete/autocompleteService_js.ml @@ -307,14 +307,10 @@ let autocomplete_create_result_elt let ty_normalizer_options = Ty_normalizer_env.{ default_options with expand_internal_types = true } type typing = { - file_options: Files.options; layout_options: Js_layout_generator.opts; - haste_module_system: bool; loc_of_aloc: ALoc.t -> Loc.t; get_ast_from_shared_mem: File_key.t -> (Loc.t, Loc.t) Flow_ast.Program.t option; - get_haste_name: File_key.t -> string option; - get_package_info: File_key.t -> (Package_json.t, unit) result option; - is_package_file: string -> bool; + module_system_info: Lsp_module_system_info.t; search_exported_values: ac_options:ac_options -> string -> Export_search_types.search_results; search_exported_types: ac_options:ac_options -> string -> Export_search_types.search_results; cx: Context.t; @@ -325,14 +321,10 @@ type typing = { } let mk_typing_artifacts - ~file_options ~layout_options - ~haste_module_system ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name - ~get_package_info - ~is_package_file + ~module_system_info ~search_exported_values ~search_exported_types ~cx @@ -347,14 +339,10 @@ let mk_typing_artifacts ~file_sig in { - file_options; layout_options; - haste_module_system; loc_of_aloc; get_ast_from_shared_mem; - get_haste_name; - get_package_info; - is_package_file; + module_system_info; search_exported_values; search_exported_types; cx; @@ -650,26 +638,11 @@ let flow_text_edit_of_lsp_text_edit { Lsp.TextEdit.range; newText } = let completion_item_of_autoimport ~typing ~src_dir ~edit_locs ~ranking_info { Export_search_types.name; source; kind } rank = - let { - file_options; - layout_options; - haste_module_system; - get_haste_name; - get_package_info; - is_package_file; - ast; - _; - } = - typing - in + let { layout_options; module_system_info; ast; _ } = typing in match Lsp_import_edits.text_edits_of_import - ~file_options ~layout_options - ~haste_module_system - ~get_haste_name - ~get_package_info - ~is_package_file + ~module_system_info ~src_dir ~ast kind @@ -1744,21 +1717,7 @@ let autocomplete_jsx_intrinsic ~typing ~ac_loc ~edit_locs = { result = { AcCompletion.items; is_incomplete = false }; errors_to_log } let autocomplete_jsx_element ~typing ~ac_loc ~ac_options ~edit_locs ~token ~type_ = - let { - file_options; - layout_options; - haste_module_system; - cx; - loc_of_aloc; - get_haste_name; - get_package_info; - is_package_file; - file_sig; - ast; - _; - } = - typing - in + let { layout_options; module_system_info; cx; loc_of_aloc; file_sig; ast; _ } = typing in let results_id = autocomplete_id ~typing @@ -1803,12 +1762,8 @@ let autocomplete_jsx_element ~typing ~ac_loc ~ac_options ~edit_locs ~token ~type let name = "React" in let source = Export_index.Builtin "react" in Lsp_import_edits.text_edits_of_import - ~file_options ~layout_options - ~haste_module_system - ~get_haste_name - ~get_package_info - ~is_package_file + ~module_system_info ~src_dir ~ast kind diff --git a/src/services/autocomplete/autocompleteService_js.mli b/src/services/autocomplete/autocompleteService_js.mli index 987aa7d3ff1..5a6b0c5a43e 100644 --- a/src/services/autocomplete/autocompleteService_js.mli +++ b/src/services/autocomplete/autocompleteService_js.mli @@ -49,14 +49,10 @@ type 'r ac_result = { type typing val mk_typing_artifacts : - file_options:Files.options -> layout_options:Js_layout_generator.opts -> - haste_module_system:bool -> loc_of_aloc:(ALoc.t -> Loc.t) -> get_ast_from_shared_mem:(File_key.t -> (Loc.t, Loc.t) Flow_ast.Program.t option) -> - get_haste_name:(File_key.t -> string option) -> - get_package_info:(File_key.t -> (Package_json.t, unit) result option) -> - is_package_file:(string -> bool) -> + module_system_info:Lsp_module_system_info.t -> search_exported_values:(ac_options:ac_options -> string -> Export_search_types.search_results) -> search_exported_types:(ac_options:ac_options -> string -> Export_search_types.search_results) -> cx:Context.t -> diff --git a/src/services/code_action/code_action_service.ml b/src/services/code_action/code_action_service.ml index 5eb7ba9ce50..ed4296fe788 100644 --- a/src/services/code_action/code_action_service.ml +++ b/src/services/code_action/code_action_service.ml @@ -273,10 +273,8 @@ let maybe_sort_by_usage ~imports_ranked_usage imports = imports let suggest_imports - ~options - ~get_haste_name - ~get_package_info - ~is_package_file + ~layout_options + ~module_system_info ~ast ~diagnostics ~imports_ranked_usage @@ -309,12 +307,8 @@ let suggest_imports |> Base.List.fold ~init:[] ~f:(fun acc ((source, export_kind), _num) -> match Lsp_import_edits.text_edits_of_import - ~file_options:(Options.file_options options) - ~layout_options:(Code_action_utils.layout_options options) - ~haste_module_system:Options.(module_system options = Haste) - ~get_haste_name - ~get_package_info - ~is_package_file + ~layout_options + ~module_system_info ~src_dir ~ast export_kind @@ -831,9 +825,7 @@ let code_actions_of_errors ~options ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name - ~get_package_info - ~is_package_file + ~module_system_info ~cx ~file_sig ~env @@ -861,10 +853,8 @@ let code_actions_of_errors if include_quick_fixes && Loc.intersects error_loc loc then let { ServerEnv.exports; _ } = env in suggest_imports - ~options - ~get_haste_name - ~get_package_info - ~is_package_file + ~layout_options:(Code_action_utils.layout_options options) + ~module_system_info ~ast ~diagnostics ~imports_ranked_usage @@ -1050,10 +1040,8 @@ let code_actions_at_loc ~env ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name ~get_type_sig - ~get_package_info - ~is_package_file + ~module_system_info ~cx ~file_sig ~tolerable_errors @@ -1072,7 +1060,7 @@ let code_actions_at_loc ~cx ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name + ~get_haste_name:module_system_info.Lsp_module_system_info.get_haste_name ~get_type_sig ~ast ~file_sig @@ -1092,7 +1080,7 @@ let code_actions_at_loc ~typed_ast ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name + ~get_haste_name:module_system_info.Lsp_module_system_info.get_haste_name ~get_type_sig ~only uri @@ -1102,7 +1090,7 @@ let code_actions_at_loc ~cx ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name + ~get_haste_name:module_system_info.Lsp_module_system_info.get_haste_name ~get_type_sig ~ast ~file_sig @@ -1119,9 +1107,7 @@ let code_actions_at_loc ~options ~loc_of_aloc ~get_ast_from_shared_mem - ~get_haste_name - ~get_package_info - ~is_package_file + ~module_system_info ~cx ~file_sig ~env @@ -1157,8 +1143,7 @@ module ExportKindMap = WrappedMap.Make (struct end) (** insert imports for all undefined-variable errors that have only one suggestion *) -let autofix_imports - ~options ~env ~loc_of_aloc ~get_haste_name ~get_package_info ~is_package_file ~cx ~ast ~uri = +let autofix_imports ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~uri = let errors = Context.errors cx in let { ServerEnv.exports; _ } = env in let src_dir = Lsp_helpers.lsp_uri_to_path uri |> Filename.dirname |> Base.Option.return in @@ -1194,16 +1179,7 @@ let autofix_imports let added_imports = ExportSourceMap.fold (fun source names_of_kinds added_imports -> - let from = - Lsp_import_edits.from_of_source - ~file_options:(Options.file_options options) - ~haste_module_system:Options.(module_system options = Haste) - ~get_haste_name - ~get_package_info - ~is_package_file - ~src_dir - source - in + let from = Lsp_import_edits.from_of_source ~module_system_info ~src_dir source in match from with | None -> added_imports | Some from -> diff --git a/src/services/code_action/code_action_service.mli b/src/services/code_action/code_action_service.mli index 0ee428cfaec..9f21b9fae20 100644 --- a/src/services/code_action/code_action_service.mli +++ b/src/services/code_action/code_action_service.mli @@ -38,10 +38,8 @@ val code_actions_at_loc : env:ServerEnv.env -> loc_of_aloc:(ALoc.t -> Loc.t) -> get_ast_from_shared_mem:(File_key.t -> (Loc.t, Loc.t) Flow_ast.Program.t option) -> - get_haste_name:(File_key.t -> string option) -> get_type_sig:(File_key.t -> Type_sig_collections.Locs.index Packed_type_sig.Module.t option) -> - get_package_info:(File_key.t -> (Package_json.t, unit) result option) -> - is_package_file:(string -> bool) -> + module_system_info:Lsp_module_system_info.t -> cx:Context.t -> file_sig:File_sig.t -> tolerable_errors:File_sig.tolerable_error list -> @@ -60,9 +58,7 @@ val autofix_imports : options:Options.t -> env:ServerEnv.env -> loc_of_aloc:(ALoc.t -> Loc.t) -> - get_haste_name:(File_key.t -> string option) -> - get_package_info:(File_key.t -> (Package_json.t, unit) result option) -> - is_package_file:(string -> bool) -> + module_system_info:Lsp_module_system_info.t -> cx:Context.t -> ast:(Loc.t, Loc.t) Flow_ast.Program.t -> uri:Lsp.DocumentUri.t -> diff --git a/src/services/code_action/lsp_import_edits.ml b/src/services/code_action/lsp_import_edits.ml index fbbf00009ce..07c2631dfe5 100644 --- a/src/services/code_action/lsp_import_edits.ml +++ b/src/services/code_action/lsp_import_edits.ml @@ -6,6 +6,7 @@ *) open Code_action_text_edits +open Lsp_module_system_info let main_of_package ~get_package_info package_dir = let file_key = File_key.JsonFile (package_dir ^ Filename.dir_sep ^ "package.json") in @@ -91,7 +92,7 @@ let path_of_modulename ~node_resolver_dirnames ~get_package_info src_dir file_ke node_path ~node_resolver_dirnames ~get_package_info ~src_dir path) src_dir -let haste_package_path ~get_package_info ~is_package_file ~src_dir require_path = +let haste_package_path ~module_system_info ~src_dir require_path = match Files.split_path require_path |> Base.List.rev with | [] -> None | base :: parent_dir_names -> @@ -100,7 +101,7 @@ let haste_package_path ~get_package_info ~is_package_file ~src_dir require_path match remaining with | [] -> None | package_name_candidate :: parent_dir_names -> - if is_package_file package_name_candidate then + if module_system_info.is_package_file package_name_candidate then let package_path_parts = List.rev (package_name_candidate :: parent_dir_names) in let within_package = match find_ancestor_rev package_path_parts src_parts with @@ -115,7 +116,7 @@ let haste_package_path ~get_package_info ~is_package_file ~src_dir require_path Some (match main_of_package - ~get_package_info + ~get_package_info:module_system_info.get_package_info (String.concat Filename.dir_sep package_path_parts) with | Some main when path_matches (String.concat "/" acc) main -> package_name_candidate @@ -125,56 +126,33 @@ let haste_package_path ~get_package_info ~is_package_file ~src_dir require_path in f [base] parent_dir_names -let from_of_source - ~file_options - ~haste_module_system - ~get_haste_name - ~get_package_info - ~is_package_file - ~src_dir - source = +let from_of_source ~module_system_info ~src_dir source = match source with | Export_index.Global -> None | Export_index.Builtin from -> Some from | Export_index.File_key from -> let module_name = - match get_haste_name from with + match module_system_info.get_haste_name from with | Some module_name -> Some module_name - | None when haste_module_system -> + | None when module_system_info.haste_module_system -> Base.Option.bind src_dir ~f:(fun src_dir -> haste_package_path - ~get_package_info - ~is_package_file + ~module_system_info ~src_dir (File_key.to_string (Files.chop_flow_ext from)) ) | None -> None in - let node_resolver_dirnames = Files.node_resolver_dirnames file_options in - path_of_modulename ~node_resolver_dirnames ~get_package_info src_dir from module_name + let node_resolver_dirnames = Files.node_resolver_dirnames module_system_info.file_options in + path_of_modulename + ~node_resolver_dirnames + ~get_package_info:module_system_info.get_package_info + src_dir + from + module_name -let text_edits_of_import - ~file_options - ~layout_options - ~haste_module_system - ~get_haste_name - ~get_package_info - ~is_package_file - ~src_dir - ~ast - kind - name - source = - let from = - from_of_source - ~file_options - ~haste_module_system - ~get_haste_name - ~get_package_info - ~is_package_file - ~src_dir - source - in +let text_edits_of_import ~layout_options ~module_system_info ~src_dir ~ast kind name source = + let from = from_of_source ~module_system_info ~src_dir source in match from with | None -> None | Some from -> diff --git a/src/services/code_action/lsp_import_edits.mli b/src/services/code_action/lsp_import_edits.mli index 89abae0bd7a..d1d8fbcaa30 100644 --- a/src/services/code_action/lsp_import_edits.mli +++ b/src/services/code_action/lsp_import_edits.mli @@ -8,22 +8,14 @@ (** Generates the 'from' part of 'import ... from ...' required to import [source] from a file in [src_dir] *) val from_of_source : - file_options:Files.options -> - haste_module_system:bool -> - get_haste_name:(File_key.t -> string option) -> - get_package_info:(File_key.t -> (Package_json.t, 'a) result option) -> - is_package_file:(string -> bool) -> + module_system_info:Lsp_module_system_info.t -> src_dir:string option -> Export_index.source -> string option val text_edits_of_import : - file_options:Files.options -> layout_options:Js_layout_generator.opts -> - haste_module_system:bool -> - get_haste_name:(File_key.t -> string option) -> - get_package_info:(File_key.t -> (Package_json.t, unit) result option) -> - is_package_file:(string -> bool) -> + module_system_info:Lsp_module_system_info.t -> src_dir:string option -> ast:(Loc.t, Loc.t) Flow_ast.Program.t -> Export_index.kind -> diff --git a/src/services/code_action/lsp_module_system_info.ml b/src/services/code_action/lsp_module_system_info.ml new file mode 100644 index 00000000000..423f330744a --- /dev/null +++ b/src/services/code_action/lsp_module_system_info.ml @@ -0,0 +1,14 @@ +(* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + *) + +type t = { + file_options: Files.options; + haste_module_system: bool; + get_haste_name: File_key.t -> string option; + get_package_info: File_key.t -> (Package_json.t, unit) result option; + is_package_file: string -> bool; +}