Skip to content

Commit

Permalink
[flow][refactor] Remove autocompleteService's dependence on Export_se…
Browse files Browse the repository at this point in the history
…arch

Summary:
The goal is to make autocomplete work in try-flow. Auto imports search, which depends on some C++ code, absolutely won't work in browser.

This diff abstracts the code that runs the searching of autoimport results behind functions `search_exported_values` and `search_exported_types`. The implementation remains the same under the current LSP, but for flow.js, these functions can always return an empty list.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D55548906

fbshipit-source-id: e9fb0f3ea631071d6289d8f53260fe2bce6cb7f3
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 1, 2024
1 parent 9f3a9de commit 61bb5c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
18 changes: 17 additions & 1 deletion src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ let get_status ~options env =
in
(status_response, lazy_stats)

let autoimport_options ~ac_options =
let open Export_search in
{
default_options with
max_results = 100;
num_threads = Base.Int.max 1 (Sys_utils.nbr_procs - 2);
weighted = ac_options.AutocompleteService_js.imports_ranked_usage;
}

let search_exported_values ~exports ~ac_options before =
Export_search.search_values ~options:(autoimport_options ~ac_options) before exports

let search_exported_types ~exports ~ac_options before =
Export_search.search_types ~options:(autoimport_options ~ac_options) before exports

let autocomplete
~trigger_character
~reader
Expand Down Expand Up @@ -354,11 +369,12 @@ let autocomplete
~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
~file_sig
~ast
~available_ast
~exports:env.ServerEnv.exports
in
autocomplete_get_results typing ac_options trigger_character cursor_loc
)
Expand Down
40 changes: 11 additions & 29 deletions src/services/autocomplete/autocompleteService_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ open Loc_collections
module Ast = Flow_ast
module Statement = Fix_statement.Statement_

let max_autoimport_suggestions = 100

let default_autoimport_options =
let open Export_search in
{
default_options with
max_results = max_autoimport_suggestions;
num_threads = Base.Int.max 1 (Sys_utils.nbr_procs - 2);
}

module AcCompletion = struct
type completion_item = {
kind: Lsp.Completion.completionItemKind option;
Expand Down Expand Up @@ -358,11 +348,12 @@ type typing = {
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;
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;
file_sig: File_sig.t;
ast: (Loc.t, Loc.t) Flow_ast.Program.t;
available_ast: Typed_ast_utils.available_ast;
exports: Export_search.t;
norm_genv: Ty_normalizer_env.genv;
}

Expand All @@ -373,11 +364,12 @@ let mk_typing_artifacts
~get_haste_name
~get_package_info
~is_package_file
~search_exported_values
~search_exported_types
~cx
~file_sig
~ast
~available_ast
~exports =
~available_ast =
let norm_genv =
Ty_normalizer_flow.mk_genv
~options:ty_normalizer_options
Expand All @@ -392,11 +384,12 @@ let mk_typing_artifacts
get_haste_name;
get_package_info;
is_package_file;
search_exported_values;
search_exported_types;
cx;
file_sig;
ast;
available_ast;
exports;
norm_genv;
}

Expand Down Expand Up @@ -982,13 +975,7 @@ let autocomplete_id
else
let locals = set_of_locals ~f:(fun ((name, _docs_and_tags), _ty) -> name) identifiers in
let { Export_search_types.results = auto_imports; is_incomplete } =
let options =
{
default_autoimport_options with
Export_search.weighted = ac_options.imports_ranked_usage;
}
in
Export_search.search_values ~options before typing.exports
typing.search_exported_values ~ac_options before
in
let items_rev =
append_completion_items_of_autoimports
Expand Down Expand Up @@ -1343,11 +1330,12 @@ let autocomplete_unqualified_type
get_haste_name = _;
get_package_info = _;
is_package_file = _;
search_exported_values = _;
search_exported_types;
cx;
file_sig;
ast;
available_ast;
exports;
norm_genv = genv;
} =
typing
Expand Down Expand Up @@ -1464,13 +1452,7 @@ let autocomplete_unqualified_type
in
let { Export_search_types.results = auto_imports; is_incomplete } =
let (before, _after) = Autocomplete_sigil.remove token in
let options =
{
default_autoimport_options with
Export_search.weighted = ac_options.imports_ranked_usage;
}
in
Export_search.search_types ~options before exports
search_exported_types ~ac_options before
in
let items_rev =
append_completion_items_of_autoimports
Expand Down
3 changes: 2 additions & 1 deletion src/services/autocomplete/autocompleteService_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ val mk_typing_artifacts :
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) ->
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 ->
file_sig:File_sig.t ->
ast:(Loc.t, Loc.t) Flow_ast.Program.t ->
available_ast:Typed_ast_utils.available_ast ->
exports:Export_search.t ->
typing

type 'r autocomplete_service_result_generic =
Expand Down
2 changes: 1 addition & 1 deletion src/services/autocomplete/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(wrapped false)
(libraries
fuzzy_score
flow_search
flow_search_types
flow_server_protocol
flow_service_type_info
flow_typing
Expand Down

0 comments on commit 61bb5c6

Please sign in to comment.