2 * Copyright (c) 2016, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
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
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
) =
29 ( [("type", opt_string_to_json type_string
); ("pos", deprecated_pos_field)]
32 | Some json
-> [("full_type", json_of_string json
)]
35 let identify_symbol_response_to_json results
=
36 let get_definition_data = function
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
43 | None
-> (JSON_Null
, JSON_Null
, JSON_Null
)
49 | Method _
-> "method"
51 | Function
-> "function"
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
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
=
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
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);
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
) =
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
118 let find_references_response_to_json = function
119 | None
-> JSON_Array
[]
120 | Some
(symbol_name
, references
) ->
122 List.map references
(fun x
->
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
=
137 (List.map l ~f
:(fun x
->
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));
146 let print_json json
= Hh_json.json_to_string json
|> print_endline