Skip to content

Commit

Permalink
Rearrange autofix-exports code to make room for other code actions
Browse files Browse the repository at this point in the history
Summary:
This diff just moves code around and shouldn't change any behavior.
The main change is that checking the `opt_autofix_exports` flowconfig flag is moved out of `commandHandler.ml` and pushed into `Type_info_service.code_actions_at_loc`, where it's closer to the logic which computes the codeActions. This will let us use the flag to guard only the "autofix exports" (instead of guarding all codeActions), so that we can provide "did you mean?"-related quickfixes even when the flag is turned off.

Reviewed By: gkz

Differential Revision: D19917799

fbshipit-source-id: cbb6f44f7703559b63a5e4e8e4f5cffbd836f8bc
  • Loading branch information
Vijay Ramamurthy authored and facebook-github-bot committed Feb 28, 2020
1 parent 0a23767 commit 922c145
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 59 deletions.
37 changes: 15 additions & 22 deletions src/server/command_handler/commandHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -878,28 +878,21 @@ let handle_save_state ~saved_state_filename ~genv ~profiling ~env =
Lwt.return (env, ServerProt.Response.SAVE_STATE result, None)

let find_code_actions ~options ~env ~profiling ~params ~client =
CodeActionRequest.(
Flow_lsp_conversions.(
let { textDocument; range; _ } = params in
(* The current ide-lsp-server/flow-lsp-client doesn't necisarrily get restart for every project.
* Checking the option here ensures the the flow server doesn't do too much work for code
* action requests on projects where code actions are not enabled in the `.flowconfig`.
*)
if not options.Options.opt_autofix_exports then
Lwt.return (Ok [])
else
let (file_key, file, loc) = lsp_textDocument_and_range_to_flow textDocument range client in
match File_input.content_of_file_input file with
| Error msg -> Lwt.return (Error msg)
| Ok file_contents ->
Type_info_service.code_actions_at_loc
~options
~env
~profiling
~params
~file_key
~file_contents
~loc))
let CodeActionRequest.{ textDocument; range; _ } = params in
let (file_key, file, loc) =
Flow_lsp_conversions.lsp_textDocument_and_range_to_flow textDocument range client
in
match File_input.content_of_file_input file with
| Error msg -> Lwt.return (Error msg)
| Ok file_contents ->
Type_info_service.code_actions_at_loc
~options
~env
~profiling
~params
~file_key
~file_contents
~loc

type command_handler =
(* A command can be handled immediately if it is super duper fast and doesn't require the env.
Expand Down
79 changes: 42 additions & 37 deletions src/services/type_info/type_info_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,40 +142,45 @@ let suggest ~options ~env ~profiling file_key file_content =
| (None, errors, _) -> Error errors

let code_actions_at_loc ~options ~env ~profiling ~params ~file_key ~file_contents ~loc =
Lsp.(
CodeAction.(
CodeActionRequest.(
CodeActionKind.(
let { textDocument; range = _; context } = params in
let uri = TextDocumentIdentifier.(textDocument.uri) |> Lsp.string_of_uri in
Types_js.typecheck_contents ~options ~env ~profiling file_contents file_key >|= function
| (Some (full_cx, ast, file_sig, typed_ast), _, _) ->
Autofix_exports.(
let fixable_locs = set_of_fixable_signature_verification_locations file_sig in
if
contains_kind_opt ~default:true quickfix context.only && LocSet.mem loc fixable_locs
then
match
fix_signature_verification_error_at_loc ~full_cx ~file_sig ~typed_ast ast loc
with
| new_ast ->
let diff = Insert_type.mk_diff ast new_ast in
let edits =
Replacement_printer.mk_loc_patch_ast_differ diff ast
|> Flow_lsp_conversions.flow_loc_patch_to_lsp_edits
in
Ok
[
Action
{
CodeAction.title = "insert type annotation";
kind = quickfix;
(* Handing back the diagnostics we were given is a placeholder for
eventually generating the diagnostics for the errors we are fixing *)
diagnostics = CodeActionRequest.(context.diagnostics);
action = EditOnly WorkspaceEdit.{ changes = SMap.of_list [(uri, edits)] };
};
]
else
Ok [])
| _ -> Ok []))))
let open Lsp in
let CodeActionRequest.{ textDocument; range = _; context } = params in
let uri = TextDocumentIdentifier.(textDocument.uri) |> string_of_uri in
let autofix_exports_code_actions ~full_cx ~ast ~file_sig ~typed_ast =
let open Autofix_exports in
let fixable_locs = set_of_fixable_signature_verification_locations file_sig in
if LocSet.mem loc fixable_locs then
match fix_signature_verification_error_at_loc ~full_cx ~file_sig ~typed_ast ast loc with
| new_ast ->
let diff = Insert_type.mk_diff ast new_ast in
let edits =
Replacement_printer.mk_loc_patch_ast_differ diff ast
|> Flow_lsp_conversions.flow_loc_patch_to_lsp_edits
in
[
CodeAction.Action
{
CodeAction.title = "insert type annotation";
kind = CodeActionKind.quickfix;
(* Handing back the diagnostics we were given is a placeholder for
eventually generating the diagnostics for the errors we are fixing *)
diagnostics = context.CodeActionRequest.diagnostics;
action = CodeAction.EditOnly WorkspaceEdit.{ changes = SMap.of_list [(uri, edits)] };
};
]
else
[]
in
Types_js.typecheck_contents ~options ~env ~profiling file_contents file_key >|= function
| (Some (full_cx, ast, file_sig, typed_ast), _, _)
when CodeActionKind.contains_kind_opt
~default:true
CodeActionKind.quickfix
context.CodeActionRequest.only ->
(* The current ide-lsp-server/flow-lsp-client doesn't necessarily get restarted for every project.
* Checking the option here ensures that the flow server doesn't do too much work for code
* action requests on projects where code actions are not enabled in the `.flowconfig`. *)
if options.Options.opt_autofix_exports then
Ok (autofix_exports_code_actions ~full_cx ~ast ~file_sig ~typed_ast)
else
Ok []
| _ -> Ok []

0 comments on commit 922c145

Please sign in to comment.