From 6b28a04882a770a97b0fa726d77ecdcbd1875b01 Mon Sep 17 00:00:00 2001 From: Kasper Kopec Date: Fri, 4 Mar 2016 23:12:42 -0800 Subject: [PATCH] Expose FindRefs commands in persistent connection mode Summary: Connecting the dots to make things work. Reviewed By: int3 Differential Revision: D3004874 fb-gh-sync-id: dc95cd0cdaa44ddc3e194e6f1c7c384ae101532a shipit-source-id: dc95cd0cdaa44ddc3e194e6f1c7c384ae101532a --- hphp/hack/src/client/clientFindRefs.ml | 16 +--------------- hphp/hack/src/server/ideJson.ml | 1 + hphp/hack/src/server/ideJsonUtils.ml | 12 ++++++++++++ hphp/hack/src/server/ideMain.ml | 12 ++++++++++-- hphp/hack/src/server/serverFindRefs.ml | 14 ++++++++++++++ hphp/hack/src/server/serverFindRefs.mli | 2 ++ hphp/hack/test/unit/ide/ide_json_test.ml | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 17 deletions(-) diff --git a/hphp/hack/src/client/clientFindRefs.ml b/hphp/hack/src/client/clientFindRefs.ml index 2e082ac137e..94e0d4af875 100644 --- a/hphp/hack/src/client/clientFindRefs.ml +++ b/hphp/hack/src/client/clientFindRefs.ml @@ -15,22 +15,8 @@ let print_result (name, pos) = print_endline (pos_str ^ " " ^ name); () -let to_json input = - let entries = List.map input begin fun (name, pos) -> - let filename = Pos.filename pos in - let line, start, end_ = Pos.info_pos pos in - Hh_json.JSON_Object [ - "name", Hh_json.JSON_String name; - "filename", Hh_json.JSON_String filename; - "line", Hh_json.int_ line; - "char_start", Hh_json.int_ start; - "char_end", Hh_json.int_ end_; - ] - end in - Hh_json.JSON_Array entries - let print_json res = - print_endline (Hh_json.json_to_string (to_json res)) + print_endline (Hh_json.json_to_string (ServerFindRefs.to_json res)) let print_readable res = List.iter res print_result; diff --git a/hphp/hack/src/server/ideJson.ml b/hphp/hack/src/server/ideJson.ml index c81523585cb..e21b707053b 100644 --- a/hphp/hack/src/server/ideJson.ml +++ b/hphp/hack/src/server/ideJson.ml @@ -24,6 +24,7 @@ type response_type = | IdentifyFunctionResponse of string | SearchCallResponse of Hh_json.json | StatusResponse of Hh_json.json + | FindRefsResponse of FindRefsService.result type parsing_result = (* ParsingError means that message was unrecoverably mangled (eg. no ID, or diff --git a/hphp/hack/src/server/ideJsonUtils.ml b/hphp/hack/src/server/ideJsonUtils.ml index 31e37052be8..8f771584024 100644 --- a/hphp/hack/src/server/ideJsonUtils.ml +++ b/hphp/hack/src/server/ideJsonUtils.ml @@ -48,6 +48,17 @@ let args_to_call = function IdentifyFunctionCall (content, line, char) | [JSON_String "--search"; JSON_String content] -> SearchCall content | [] -> StatusCall + | [JSON_String "--find-refs"; JSON_String s] -> + let open FindRefsService in + begin match Str.split (Str.regexp "::") s with + | class_name :: method_name :: _ -> + FindRefsCall (Method (class_name, method_name)) + | function_name :: _ -> + FindRefsCall (Function function_name) + | _ -> raise Not_found + end + | [JSON_String "--find-class-refs"; JSON_String s] -> + FindRefsCall (FindRefsService.Class s) | _ -> raise Not_found let call_of_string s = @@ -134,6 +145,7 @@ let json_string_of_response id response = | IdentifyFunctionResponse s -> JSON_String s | SearchCallResponse r -> r | StatusResponse r -> r + | FindRefsResponse r -> ServerFindRefs.to_json r in json_to_string (build_response_json id result_field) diff --git a/hphp/hack/src/server/ideMain.ml b/hphp/hack/src/server/ideMain.ml index ce33583dcca..ea08ad827d6 100644 --- a/hphp/hack/src/server/ideMain.ml +++ b/hphp/hack/src/server/ideMain.ml @@ -94,6 +94,13 @@ let send_call_to_typecheker env id = function let msg = IdeProcessMessage.FindRefsCall (id, action) in IdeProcessPipe.send env.typechecker msg +let send_typechecker_response_to_client env id response = + Option.iter env.client begin fun (_, oc) -> + let response = IdeJsonUtils.json_string_of_response id response in + Marshal.to_channel oc response []; + flush oc + end + (* Will return a response for the client, or None if the response is going to * be computed asynchronously *) let get_call_response env id call = @@ -157,8 +164,9 @@ let handle_typechecker_message typechecker_process env = | IdeProcessMessage.SyncErrorList errorl -> Hh_logger.log "Received error list update"; { env with errorl = errorl } - | IdeProcessMessage.FindRefsResponse (_id, _result) -> - (* TODO: output it to the client *) + | IdeProcessMessage.FindRefsResponse (id, response) -> + let response = IdeJson.FindRefsResponse response in + send_typechecker_response_to_client env id response; env let handle_server_idle env = diff --git a/hphp/hack/src/server/serverFindRefs.ml b/hphp/hack/src/server/serverFindRefs.ml index f1313933b9c..431d2b9234b 100644 --- a/hphp/hack/src/server/serverFindRefs.ml +++ b/hphp/hack/src/server/serverFindRefs.ml @@ -10,6 +10,20 @@ open Core +let to_json input = + let entries = List.map input begin fun (name, pos) -> + let filename = Pos.filename pos in + let line, start, end_ = Pos.info_pos pos in + Hh_json.JSON_Object [ + "name", Hh_json.JSON_String name; + "filename", Hh_json.JSON_String filename; + "line", Hh_json.int_ line; + "char_start", Hh_json.int_ start; + "char_end", Hh_json.int_ end_; + ] + end in + Hh_json.JSON_Array entries + let add_ns name = if name.[0] = '\\' then name else "\\" ^ name diff --git a/hphp/hack/src/server/serverFindRefs.mli b/hphp/hack/src/server/serverFindRefs.mli index 6ffe7e79767..932aa4aa9fe 100644 --- a/hphp/hack/src/server/serverFindRefs.mli +++ b/hphp/hack/src/server/serverFindRefs.mli @@ -10,6 +10,8 @@ open FindRefsService +val to_json: result -> Hh_json.json + val get_refs_with_defs : action -> ServerEnv.genv -> ServerEnv.env -> (Naming_heap.FunHeap.key * Pos.t) list diff --git a/hphp/hack/test/unit/ide/ide_json_test.ml b/hphp/hack/test/unit/ide/ide_json_test.ml index f39b856ff9b..a2feb54a0d9 100644 --- a/hphp/hack/test/unit/ide/ide_json_test.ml +++ b/hphp/hack/test/unit/ide/ide_json_test.ml @@ -124,6 +124,35 @@ let test_status_call () = | Call (4, StatusCall) -> true | _ -> false +let test_find_function_refs_call () = + let msg = "{\"type\" : \"call\", \ + \"id\" : 4, \ + \"args\" : [ \ + \"--find-refs\", \ + \"array_pull\" ]}" in + match call_of_string msg with + | Call (4, FindRefsCall (FindRefsService.Function "array_pull")) -> true + | _ -> false + +let test_find_method_refs_call () = + let msg = "{\"type\" : \"call\", \ + \"id\" : 4, \ + \"args\" : [ \ + \"--find-refs\", \ + \"C::getID\" ]}" in + match call_of_string msg with + | Call (4, FindRefsCall (FindRefsService.Method ("C", "getID"))) -> true + | _ -> false + +let test_find_class_refs_call () = + let msg = "{\"type\" : \"call\", \ + \"id\" : 4, \ + \"args\" : [ \ + \"--find-class-refs\", \ + \"C\" ]}" in + match call_of_string msg with + | Call (4, FindRefsCall (FindRefsService.Class "C")) -> true + | _ -> false let test_strip_json_arg () = let msg = "{\"type\" : \"call\", \ @@ -156,6 +185,9 @@ let tests = [ "test_identify_function_call", test_identify_function_call; "test_strip_json_arg", test_strip_json_arg; "test_status_call", test_status_call; + "test_find_function_refs_call", test_find_function_refs_call; + "test_find_method_refs_call", test_find_method_refs_call; + "test_find_class_refs_call", test_find_class_refs_call; ] let () = -- 2.11.4.GIT