Support of Arch Linux in `getdeps.py`
[hiphop-php.git] / hphp / hack / src / ide_rpc / nuclide_rpc_message_printer.ml
blob601ee68e617a5375e2fa60f334e3782f5b4deccf
1 (*
2 * Copyright (c) 2016, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
9 open Hh_prelude
10 open Hh_json
12 let opt_field ~v_opt ~label ~f =
13 Option.value_map v_opt ~f:(fun x -> [(label, f x)]) ~default:[]
15 (* There are fields that Nuclide doesn't use anymore, but the RPC framework
16 * still requires them in responses. Stub them with some default values in the
17 * meantime *)
18 let deprecated_pos_field = Pos.json (Pos.to_absolute Pos.none)
20 let deprecated_int_field = Hh_json.int_ 0
22 let deprecated_bool_field = JSON_Bool false
24 (* Instead of "assert false" *)
25 let should_not_happen = JSON_Object [("this_should", JSON_String "not_happen")]
27 let infer_type_response_to_json (type_string, type_json) =
28 Hh_json.JSON_Object
29 ([("type", opt_string_to_json type_string); ("pos", deprecated_pos_field)]
31 match type_json with
32 | Some json -> [("full_type", json_of_string json)]
33 | _ -> [])
35 let infer_type_error_response_to_json
36 ( actual_type_string,
37 actual_type_json,
38 expected_type_string,
39 expected_type_json ) =
40 Hh_json.JSON_Object
41 (List.filter_map
42 ~f:Fn.id
44 Some ("actual_type", opt_string_to_json actual_type_string);
45 Option.map
46 ~f:(fun ty -> ("full_actual_type", json_of_string ty))
47 actual_type_json;
48 Some ("expected_type", opt_string_to_json expected_type_string);
49 Option.map
50 ~f:(fun ty -> ("full_expected_type", json_of_string ty))
51 expected_type_json;
54 let tast_holes_response_to_json ~print_file holes =
55 let printer pos =
56 if print_file then
57 Pos.to_absolute pos |> Pos.multiline_json
58 else
59 Pos.multiline_json_no_filename pos
61 let f
62 ( actual_type_str,
63 actual_type_json,
64 expected_type_str,
65 expected_type_json,
66 pos ) =
67 Hh_json.JSON_Object
69 ("actual_type", Hh_json.string_ actual_type_str);
70 ("full_actual_type", json_of_string actual_type_json);
71 ("expected_type", Hh_json.string_ expected_type_str);
72 ("full_expected_type", json_of_string expected_type_json);
73 ("pos", printer pos);
76 Hh_json.JSON_Array (List.map ~f holes)
78 let identify_symbol_response_to_json results =
79 let get_definition_data = function
80 | Some x ->
81 SymbolDefinition.(
82 let pos = Pos.json x.pos in
83 let span = Pos.multiline_json x.span in
84 let id = opt_string_to_json x.id in
85 (pos, span, id))
86 | None -> (JSON_Null, JSON_Null, JSON_Null)
88 let symbol_to_json (occurrence, definition) =
89 let (definition_pos, definition_span, definition_id) =
90 get_definition_data definition
92 SymbolOccurrence.(
93 JSON_Object
95 ("name", JSON_String occurrence.name);
96 ("result_type", JSON_String (kind_to_string occurrence.type_));
97 ("pos", Pos.json occurrence.pos);
98 ("definition_pos", definition_pos);
99 ("definition_span", definition_span);
100 ("definition_id", definition_id);
103 JSON_Array (List.map results ~f:symbol_to_json)
105 let rec definition_to_json def =
106 SymbolDefinition.(
107 let modifiers =
108 JSON_Array
109 (List.map def.modifiers ~f:(fun x -> JSON_String (string_of_modifier x)))
111 let children =
112 opt_field
113 ~v_opt:def.children
114 ~label:"children"
115 ~f:outline_response_to_json
117 let params =
118 opt_field ~v_opt:def.params ~label:"params" ~f:outline_response_to_json
120 let docblock =
121 opt_field ~v_opt:def.docblock ~label:"docblock" ~f:(fun x ->
122 JSON_String x)
124 JSON_Object
126 ("kind", JSON_String (string_of_kind def.kind));
127 ("name", JSON_String def.name);
128 ("id", opt_string_to_json def.id);
129 ("position", Pos.json def.pos);
130 ("span", Pos.multiline_json def.span);
131 ("modifiers", modifiers);
133 @ children
134 @ params
135 @ docblock))
137 and outline_response_to_json x =
138 Hh_json.JSON_Array (List.map x ~f:definition_to_json)
140 let symbol_by_id_response_to_json = function
141 | Some def -> definition_to_json def
142 | None -> JSON_Null
144 let highlight_references_response_to_json l =
145 JSON_Array
146 (List.map l ~f:(fun x ->
147 Ide_api_types.(
148 Hh_json.JSON_Object
150 ("line", Hh_json.int_ x.st.line);
151 ("char_start", Hh_json.int_ x.st.column);
152 ("char_end", Hh_json.int_ x.ed.column);
153 ])))
155 let print_json json = Hh_json.json_to_string json |> print_endline