Skip to content

Commit

Permalink
[flow][cleanup] Organize common lambdas used by Lsp_import_edits into…
Browse files Browse the repository at this point in the history
… `lsp_module_system_info.t`

Summary: Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D55609740

fbshipit-source-id: 54259aca24f39425bf1d92c40e67a4fc16c9bfda
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 2, 2024
1 parent e0eb545 commit 7cf8cbf
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 172 deletions.
16 changes: 10 additions & 6 deletions src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
59 changes: 7 additions & 52 deletions src/services/autocomplete/autocompleteService_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/services/autocomplete/autocompleteService_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
52 changes: 14 additions & 38 deletions src/services/code_action/code_action_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
8 changes: 2 additions & 6 deletions src/services/code_action/code_action_service.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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 ->
Expand Down
Loading

0 comments on commit 7cf8cbf

Please sign in to comment.