solved some TODOs about Tgeneric type arguments (2)
[hiphop-php.git] / hphp / hack / src / ide_rpc / nuclide_rpc_message_printer.ml
blob6f8f1c781490b0233c1abfa02b8f9129a63cff61
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 Core_kernel
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 identify_symbol_response_to_json results =
36 let get_definition_data = function
37 | Some x ->
38 SymbolDefinition.(
39 let pos = Pos.json x.pos in
40 let span = Pos.multiline_json x.span in
41 let id = opt_string_to_json x.id in
42 (pos, span, id))
43 | None -> (JSON_Null, JSON_Null, JSON_Null)
45 let result_type x =
46 SymbolOccurrence.(
47 match x.type_ with
48 | Class -> "class"
49 | Method _ -> "method"
50 | Record -> "record"
51 | Function -> "function"
52 | LocalVar -> "local"
53 | Property _ -> "property"
54 | ClassConst _ -> "class_const"
55 | Typeconst _ -> "typeconst"
56 | GConst -> "global_const"
57 | Attribute _ -> "attribute")
59 let symbol_to_json (occurrence, definition) =
60 let (definition_pos, definition_span, definition_id) =
61 get_definition_data definition
63 SymbolOccurrence.(
64 JSON_Object
66 ("name", JSON_String occurrence.name);
67 ("result_type", JSON_String (result_type occurrence));
68 ("pos", Pos.json occurrence.pos);
69 ("definition_pos", definition_pos);
70 ("definition_span", definition_span);
71 ("definition_id", definition_id);
74 JSON_Array (List.map results ~f:symbol_to_json)
76 let rec definition_to_json def =
77 SymbolDefinition.(
78 let modifiers =
79 JSON_Array
80 (List.map def.modifiers ~f:(fun x -> JSON_String (string_of_modifier x)))
82 let children = opt_field def.children "children" outline_response_to_json in
83 let params = opt_field def.params "params" outline_response_to_json in
84 let docblock = opt_field def.docblock "docblock" (fun x -> JSON_String x) in
85 JSON_Object
86 ( [
87 ("kind", JSON_String (string_of_kind def.kind));
88 ("name", JSON_String def.name);
89 ("id", opt_string_to_json def.id);
90 ("position", Pos.json def.pos);
91 ("span", Pos.multiline_json def.span);
92 ("modifiers", modifiers);
94 @ children
95 @ params
96 @ docblock ))
98 and outline_response_to_json x =
99 Hh_json.JSON_Array (List.map x ~f:definition_to_json)
101 let coverage_levels_response_to_json spans =
102 let opt_coverage_level_to_string =
103 Option.value_map ~f:Coverage_level.string_of_level ~default:"default"
105 let span_to_json (color, text) =
106 JSON_Object
108 ("color", JSON_String (opt_coverage_level_to_string color));
109 ("text", JSON_String text);
112 JSON_Array (List.map spans ~f:span_to_json)
114 let symbol_by_id_response_to_json = function
115 | Some def -> definition_to_json def
116 | None -> JSON_Null
118 let find_references_response_to_json = function
119 | None -> JSON_Array []
120 | Some (symbol_name, references) ->
121 let entries =
122 List.map references (fun x ->
123 Ide_api_types.(
124 Hh_json.JSON_Object
126 ("name", Hh_json.JSON_String symbol_name);
127 ("filename", Hh_json.JSON_String x.range_filename);
128 ("line", Hh_json.int_ x.file_range.st.line);
129 ("char_start", Hh_json.int_ x.file_range.st.column);
130 ("char_end", Hh_json.int_ (x.file_range.ed.column - 1));
133 Hh_json.JSON_Array entries
135 let highlight_references_response_to_json l =
136 JSON_Array
137 (List.map l ~f:(fun x ->
138 Ide_api_types.(
139 Hh_json.JSON_Object
141 ("line", Hh_json.int_ x.st.line);
142 ("char_start", Hh_json.int_ x.st.column);
143 ("char_end", Hh_json.int_ (x.ed.column - 1));
144 ])))
146 let print_json json = Hh_json.json_to_string json |> print_endline