Skip to content

Commit 95569af

Browse files
panagosg7facebook-github-bot
authored andcommitted
[flow] only populate search index if autoimports is true
Summary: `Export_search` results are only used to service auto-imports. When auto-imports are disabled (e.g. through setting `autoimports=false` in the flowconfig) we don't need to worry about indexing files. This diff makes `ServerEnv.exports` optional and only populates when the option is true. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D69668537 ------------------------------------------------------------------------ (from 090ecacb3fea983e4ca4187aedab2bfcf92cff77) fbshipit-source-id: 5bcb35be85f055ebc4bb5941c4658836731b022a
1 parent fc43315 commit 95569af

File tree

8 files changed

+206
-31
lines changed

8 files changed

+206
-31
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"method": "textDocument/completion",
3+
"result": {
4+
"isIncomplete": false,
5+
"items": [
6+
{
7+
"label": "Test",
8+
"kind": 6,
9+
"detail": "type Test = any",
10+
"sortText": "00000000000000000000",
11+
"insertTextFormat": 1,
12+
"textEdit": {
13+
"range": {
14+
"start": {
15+
"line": 2,
16+
"character": 12
17+
},
18+
"end": {
19+
"line": 2,
20+
"character": 13
21+
}
22+
},
23+
"newText": "Test"
24+
},
25+
"command": {
26+
"title": "",
27+
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>",
28+
"arguments": [
29+
"textDocument/completion",
30+
"unqualified type: local type identifier",
31+
{
32+
"token": "TAUTO332",
33+
"index": 0,
34+
"session_requests": 1,
35+
"typed_length": 1,
36+
"completion": "Test",
37+
"ac_type": "Actype"
38+
}
39+
]
40+
}
41+
},
42+
{
43+
"label": "true",
44+
"kind": 6,
45+
"detail": "true",
46+
"sortText": "00000000000000000001",
47+
"insertTextFormat": 1,
48+
"textEdit": {
49+
"range": {
50+
"start": {
51+
"line": 2,
52+
"character": 12
53+
},
54+
"end": {
55+
"line": 2,
56+
"character": 13
57+
}
58+
},
59+
"newText": "true"
60+
},
61+
"command": {
62+
"title": "",
63+
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>",
64+
"arguments": [
65+
"textDocument/completion",
66+
"builtin type",
67+
{
68+
"token": "TAUTO332",
69+
"index": 1,
70+
"session_requests": 1,
71+
"typed_length": 1,
72+
"completion": "true",
73+
"ac_type": "Actype"
74+
}
75+
]
76+
}
77+
},
78+
{
79+
"label": "$NonMaybeType",
80+
"kind": 3,
81+
"detail": "$NonMaybeType",
82+
"sortText": "00000000000000000002",
83+
"insertTextFormat": 1,
84+
"textEdit": {
85+
"range": {
86+
"start": {
87+
"line": 2,
88+
"character": 12
89+
},
90+
"end": {
91+
"line": 2,
92+
"character": 13
93+
}
94+
},
95+
"newText": "$NonMaybeType"
96+
},
97+
"command": {
98+
"title": "",
99+
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>",
100+
"arguments": [
101+
"textDocument/completion",
102+
"builtin type",
103+
{
104+
"token": "TAUTO332",
105+
"index": 2,
106+
"session_requests": 1,
107+
"typed_length": 1,
108+
"completion": "$NonMaybeType",
109+
"ac_type": "Actype"
110+
}
111+
]
112+
}
113+
}
114+
]
115+
}
116+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[libs]
2+
lib/
3+
4+
[options]
5+
exact_by_default=false
6+
autoimports=false
7+
8+
; disabling the builtins prevents this test from failing every time they change
9+
no_flowlib=true

newtests/lsp/completion/autoimports/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,27 @@ module.exports = (suite(
344344
],
345345
),
346346
]),
347+
348+
test('textDocument/completion with autoimports=false', [
349+
addFiles('foo.js', 'types.js', 'lib/builtins.js'),
350+
addCode(`type Test = T`),
351+
lspStartAndConnect(),
352+
lspRequestAndWaitUntilResponse('textDocument/completion', {
353+
textDocument: {uri: '<PLACEHOLDER_PROJECT_URL>/test.js'},
354+
position: {line: 2, character: 13},
355+
context: {triggerKind: 1},
356+
}).verifyLSPMessageSnapshot(
357+
path.join(
358+
__dirname,
359+
'__snapshots__',
360+
'completion_with_no_autoimports.json',
361+
),
362+
[
363+
'textDocument/publishDiagnostics',
364+
'window/showStatus',
365+
'$/cancelRequest',
366+
],
367+
),
368+
]).flowConfig('_flowconfig_autoimports_false'),
347369
],
348370
): SuiteType);

src/server/command_handler/commandHandler.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,21 @@ let autocomplete_on_parsed
499499
let open AutocompleteService_js in
500500
let (token_opt, ac_loc, ac_type_string, results_res) =
501501
Profiling_js.with_timer profiling ~timer:"GetResults" ~f:(fun () ->
502+
let (search_exported_values, search_exported_types) =
503+
match env.ServerEnv.exports with
504+
| Some exports -> (search_exported_values ~exports, search_exported_types ~exports)
505+
| None ->
506+
let empty ~ac_options:_ _ = Export_search_types.empty_search_results in
507+
(empty, empty)
508+
in
502509
let typing =
503510
AutocompleteService_js.mk_typing_artifacts
504511
~layout_options:(Code_action_utils.layout_options options)
505512
~module_system_info:(mk_module_system_info ~options ~reader)
506513
~loc_of_aloc:(Parsing_heaps.Reader.loc_of_aloc ~reader)
507514
~get_ast_from_shared_mem:(Parsing_heaps.Reader.get_ast ~reader)
508-
~search_exported_values:(search_exported_values ~exports:env.ServerEnv.exports)
509-
~search_exported_types:(search_exported_types ~exports:env.ServerEnv.exports)
515+
~search_exported_values
516+
~search_exported_types
510517
~cx
511518
~file_sig
512519
~ast

src/server/env/serverEnv.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ type env = {
5050
coverage: Coverage.file_coverage Utils_js.FilenameMap.t;
5151
collated_errors: Collated_errors.t;
5252
connections: Persistent_connection.t;
53-
exports: Export_search.t;
53+
exports: Export_search.t option; (** None means auto-imports are not enabled *)
5454
master_cx: Context.master_context;
5555
}

src/services/code_action/code_action_service.ml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,13 @@ let code_actions_of_errors
11461146
Flow_error.msg_of_error error |> Error_message.map_loc_of_error_message loc_of_aloc
11471147
in
11481148
let (suggest_imports_actions, has_missing_import) =
1149-
match error_message with
1150-
| Error_message.EBuiltinNameLookupFailed { loc = error_loc; name }
1149+
match (error_message, env) with
1150+
| ( Error_message.EBuiltinNameLookupFailed { loc = error_loc; name },
1151+
{ ServerEnv.exports = Some exports; _ }
1152+
)
11511153
when Options.autoimports options ->
11521154
let actions =
11531155
if include_quick_fixes && Loc.intersects error_loc loc then
1154-
let { ServerEnv.exports; _ } = env in
11551156
suggest_imports
11561157
~layout_options:(Code_action_utils.layout_options options)
11571158
~module_system_info
@@ -1512,15 +1513,16 @@ end)
15121513
(** insert imports for all undefined-variable errors that have only one suggestion *)
15131514
let autofix_imports ~options ~env ~loc_of_aloc ~module_system_info ~cx ~ast ~src_dir =
15141515
let errors = Context.errors cx in
1515-
let { ServerEnv.exports; _ } = env in
15161516
(* collect imports for all of the undefined variables in the file *)
15171517
let (imports, _) =
15181518
Flow_error.ErrorSet.fold
15191519
(fun error (imports, unbound_names) ->
15201520
match
1521-
Flow_error.msg_of_error error |> Error_message.map_loc_of_error_message loc_of_aloc
1521+
(Flow_error.msg_of_error error |> Error_message.map_loc_of_error_message loc_of_aloc, env)
15221522
with
1523-
| Error_message.EBuiltinNameLookupFailed { loc = error_loc; name }
1523+
| ( Error_message.EBuiltinNameLookupFailed { loc = error_loc; name },
1524+
{ ServerEnv.exports = Some exports; _ }
1525+
)
15241526
when Options.autoimports options ->
15251527
(match preferred_import ~ast ~exports name error_loc with
15261528
| Some (source, export_kind) when not (SSet.mem name unbound_names) ->
@@ -1616,14 +1618,17 @@ let suggest_imports_cli
16161618
let uri = File_key.to_string file_key |> Lsp_helpers.path_to_lsp_uri ~default_path:"" in
16171619
let get_edits ~cx ~ast =
16181620
let errors = Context.errors cx in
1619-
let { ServerEnv.exports; _ } = env in
16201621
let (imports, _) =
16211622
Flow_error.ErrorSet.fold
16221623
(fun error (imports, unbound_names) ->
16231624
match
1624-
Flow_error.msg_of_error error |> Error_message.map_loc_of_error_message loc_of_aloc
1625+
( Flow_error.msg_of_error error |> Error_message.map_loc_of_error_message loc_of_aloc,
1626+
env
1627+
)
16251628
with
1626-
| Error_message.EBuiltinNameLookupFailed { loc = error_loc; name }
1629+
| ( Error_message.EBuiltinNameLookupFailed { loc = error_loc; name },
1630+
{ ServerEnv.exports = Some exports; _ }
1631+
)
16271632
when Options.autoimports options ->
16281633
let ranked_imports =
16291634
suggest_imports

src/services/export/search/types/export_search_types.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ type search_results = {
2424
is_incomplete: bool;
2525
}
2626
[@@deriving show]
27+
28+
let empty_search_results = { results = []; is_incomplete = false }

src/services/inference/types_js.ml

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,17 +1408,19 @@ end = struct
14081408
~sig_dependency_graph
14091409
in
14101410

1411-
Hh_logger.info "Updating index";
14121411
let%lwt exports =
1413-
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
1414-
Export_service.update
1415-
~workers
1416-
~reader
1417-
~dirty_files:sig_new_or_changed
1418-
env.ServerEnv.exports
1419-
)
1412+
match env.ServerEnv.exports with
1413+
| None -> Lwt.return None
1414+
| Some exports ->
1415+
Hh_logger.info "Updating index";
1416+
let%lwt exports =
1417+
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
1418+
Export_service.update ~workers ~reader ~dirty_files:sig_new_or_changed exports
1419+
)
1420+
in
1421+
Hh_logger.info "Done updating index";
1422+
Lwt.return (Some exports)
14201423
in
1421-
Hh_logger.info "Done updating index";
14221424

14231425
let%lwt ( errors,
14241426
coverage,
@@ -1822,13 +1824,19 @@ let init_with_initial_state
18221824

18231825
let%lwt dependency_info = restore_dependency_info () in
18241826

1825-
Hh_logger.info "Indexing files";
18261827
let%lwt exports =
1827-
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
1828-
Export_service.init ~workers ~reader ~libs:lib_exports parsed
1829-
)
1828+
if Options.autoimports options then (
1829+
Hh_logger.info "Indexing files";
1830+
let%lwt exports =
1831+
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
1832+
Export_service.init ~workers ~reader ~libs:lib_exports parsed
1833+
)
1834+
in
1835+
Hh_logger.info "Finished indexing files";
1836+
Lwt.return (Some exports)
1837+
) else
1838+
Lwt.return None
18301839
in
1831-
Hh_logger.info "Finished indexing files";
18321840

18331841
let connections =
18341842
Base.Option.value_map
@@ -2221,13 +2229,19 @@ let init_from_scratch ~profiling ~workers options =
22212229
)
22222230
in
22232231

2224-
Hh_logger.info "Indexing files";
22252232
let%lwt exports =
2226-
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
2227-
Export_service.init ~workers ~reader ~libs:lib_exports parsed_set
2228-
)
2233+
if Options.autoimports options then (
2234+
Hh_logger.info "Indexing files";
2235+
let%lwt exports =
2236+
with_memory_timer_lwt ~options "Indexing" profiling (fun () ->
2237+
Export_service.init ~workers ~reader ~libs:lib_exports parsed_set
2238+
)
2239+
in
2240+
Hh_logger.info "Finished indexing files";
2241+
Lwt.return (Some exports)
2242+
) else
2243+
Lwt.return None
22292244
in
2230-
Hh_logger.info "Finished indexing files";
22312245

22322246
let (collated_errors, _) =
22332247
ErrorCollator.update_local_collated_errors

0 commit comments

Comments
 (0)