Skip to content

Commit

Permalink
[flow] make autocompleteService_js return non-lazy result
Browse files Browse the repository at this point in the history
Summary:
The only difference between
* `ServerProt.Response.Completion.completion_item`
* `AutocompleteService_js.AcCompletion.completion_item`

is that the former computes `documentation_and_tags` lazily. By the time `autocomplete_get_results` is done, we should have determined that final list of items that will be included in the response. It is safe at that point to force documentation and tags into evaluation without doing redundant work. In other words, the perf fix of D52670618 still applies.

Motivation for this change is to allow be able to compare the result of completion between "typed-AST" and "on-demand" modes. To do so, I plan to use `eq` which cannot be expressed over lazy values. This also allows us to hide `AcCompletion` as an implementation detail of `AutocompleteService_js`.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D55231178

fbshipit-source-id: 7c70c139a513a6645600c808c5998dfa1b267917
  • Loading branch information
panagosg7 authored and facebook-github-bot committed Mar 22, 2024
1 parent bc4291c commit 2488b95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ let autocomplete
let open Hh_json in
match results_res with
| AcResult { result; errors_to_log } ->
let result = AcCompletion.to_server_prot_completion_t result in
let { ServerProt.Response.Completion.items; is_incomplete = _ } = result in
let result_string =
match (items, errors_to_log) with
Expand Down
20 changes: 15 additions & 5 deletions src/services/autocomplete/autocompleteService_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,19 @@ let mk_typing_artifacts ~options ~reader ~cx ~file_sig ~ast ~available_ast ~expo
in
{ options; reader; cx; file_sig; ast; available_ast; exports; norm_genv }

type ac_result = {
result: AcCompletion.t;
type 'r ac_result = {
result: 'r;
errors_to_log: string list;
}

type autocomplete_service_result =
| AcResult of ac_result
type 'r autocomplete_service_result_generic =
| AcResult of 'r ac_result
| AcEmpty of string
| AcFatalError of string

type autocomplete_service_result =
ServerProt.Response.Completion.t autocomplete_service_result_generic

let jsdoc_of_def_loc { reader; ast; _ } def_loc =
loc_of_aloc ~reader def_loc |> Find_documentation.jsdoc_of_getdef_loc ~ast ~reader

Expand Down Expand Up @@ -2109,7 +2112,7 @@ let autocomplete_get_results typing ac_options trigger_character cursor =
match process_location cx ~trigger_character ~cursor ~available_ast with
| Error err -> (None, None, "None", AcFatalError err)
| Ok None ->
let result = { AcCompletion.items = []; is_incomplete = false } in
let result = { ServerProt.Response.Completion.items = []; is_incomplete = false } in
( None,
None,
"None",
Expand Down Expand Up @@ -2268,4 +2271,11 @@ let autocomplete_get_results typing ac_options trigger_character cursor =
| Ac_qualified_type qtype ->
autocomplete_module_exports ~typing ~edit_locs ~token ~kind:`Type qtype
in
let result =
match result with
| AcResult { result; errors_to_log } ->
AcResult { result = AcCompletion.to_server_prot_completion_t result; errors_to_log }
| AcEmpty s -> AcEmpty s
| AcFatalError e -> AcFatalError e
in
(Some token, Some ac_loc, string_of_autocomplete_type autocomplete_type, result)
35 changes: 7 additions & 28 deletions src/services/autocomplete/autocompleteService_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@
* LICENSE file in the root directory of this source tree.
*)

module AcCompletion : sig
type completion_item = {
kind: Lsp.Completion.completionItemKind option;
name: string;
labelDetail: string option; (** LSP's CompletionItemLabelDetails.detail *)
description: string option; (** LSP's CompletionItemLabelDetails.description *)
itemDetail: string option; (** LSP's CompletionItem.detail *)
text_edit: ServerProt.Response.insert_replace_edit option;
additional_text_edits: ServerProt.Response.textedit list;
sort_text: string option;
preselect: bool;
documentation_and_tags: (string option * Lsp.CompletionItemTag.t list option) Lazy.t;
log_info: string;
insert_text_format: Lsp.Completion.insertTextFormat;
}

type t = {
items: completion_item list;
is_incomplete: bool;
}

val to_server_prot_completion_t : t -> ServerProt.Response.Completion.t
end

type ac_options = {
imports: bool;
imports_min_characters: int;
Expand All @@ -37,8 +13,8 @@ type ac_options = {
show_ranking_info: bool;
}

type ac_result = {
result: AcCompletion.t;
type 'r ac_result = {
result: 'r;
errors_to_log: string list;
}

Expand All @@ -54,11 +30,14 @@ val mk_typing_artifacts :
exports:Export_search.t ->
typing

type autocomplete_service_result =
| AcResult of ac_result
type 'r autocomplete_service_result_generic =
| AcResult of 'r ac_result
| AcEmpty of string
| AcFatalError of string

type autocomplete_service_result =
ServerProt.Response.Completion.t autocomplete_service_result_generic

val autocomplete_get_results :
typing ->
ac_options ->
Expand Down

0 comments on commit 2488b95

Please sign in to comment.