Skip to content

Commit

Permalink
Merge code for readable printing of symbols in tests and in client
Browse files Browse the repository at this point in the history
Summary:
We print them in hh_single_type_check and clientGetDefinition in different
ways for no particular reason.

Making:
   - ClientGetDefinition.print_readable the canonical method to print SymbolOccurence
   - FileOutline.print_def a canonical method to print SymbolDefinition
   - FileOutline.print a canonical method to print a collection of SymbolDefinitions

The tests are updated, and the things printed by the client are not intended to
be machine readable (as opposed to the --json version), so the change should be
safe.

Differential Revision: D4408138

fbshipit-source-id: e11089624559fb141199467430d5fea9a93a0f8b
  • Loading branch information
dabek authored and hhvm-bot committed Jan 19, 2017
1 parent 7b9b7a1 commit cc343ad
Show file tree
Hide file tree
Showing 55 changed files with 523 additions and 373 deletions.
35 changes: 12 additions & 23 deletions hphp/hack/src/client/clientGetDefinition.ml
Expand Up @@ -11,18 +11,6 @@
open Core
open Hh_json

let get_result_type res =
let open SymbolOccurrence in
match res.type_ with
| Class -> "class"
| Method _ -> "method"
| Function -> "function"
| LocalVar -> "local"
| Property _ -> "property"
| ClassConst _ -> "class_const"
| Typeconst _ -> "typeconst"
| GConst -> "global_const"

let to_json x =
JSON_Array (List.map x begin function (symbol, definition) ->
let definition_pos, definition_span, definition_id =
Expand All @@ -38,7 +26,7 @@ let to_json x =
JSON_Object [
"name", JSON_String symbol.SymbolOccurrence.name;
"result_type", JSON_String
(get_result_type symbol);
(SymbolOccurrence.(kind_to_string symbol.type_));
"pos", Pos.json (symbol.SymbolOccurrence.pos);
"definition_pos", definition_pos;
"definition_span", definition_span;
Expand All @@ -49,16 +37,17 @@ let to_json x =
let print_json res =
print_endline (Hh_json.json_to_string (to_json res))

let print_readable x =
List.iter x begin function (symbol, definition) ->
Printf.printf "Name: %s, type: %s, position: %s"
symbol.SymbolOccurrence.name
(get_result_type symbol)
(Pos.string_no_file symbol.SymbolOccurrence.pos);
Option.iter definition begin fun x ->
Printf.printf ", defined: %s" (Pos.string_no_file x.SymbolDefinition.pos);
Printf.printf ", definition span: %s"
(Pos.multiline_string_no_file x.SymbolDefinition.pos)
let print_readable ?short_pos:(short_pos=false) x =
List.iter x begin function (occurrence, definition) ->
let open SymbolOccurrence in
let {name; type_; pos;} = occurrence in
Printf.printf "name: %s, kind: %s, span: %s, definition: "
name (kind_to_string type_) (Pos.string_no_file pos);
begin match definition with
| None -> Printf.printf "None\n"
| Some definition ->
print_newline ();
FileOutline.print_def ~short_pos " " definition
end;
print_newline ()
end
Expand Down
33 changes: 5 additions & 28 deletions hphp/hack/src/hh_single_type_check.ml
Expand Up @@ -406,30 +406,6 @@ let print_coverage fn type_acc =
let counts = ServerCoverageMetric.count_exprs fn type_acc in
ClientCoverageMetric.go ~json:false (Some (Leaf counts))

let print_symbol (symbol, definition) =
let open SymbolOccurrence in
Printf.printf "%s\n%s\n%s\n"
symbol.name
begin match symbol.type_ with
| Class -> "Class"
| Function -> "Function"
| Method _ -> "Method"
| LocalVar -> "LocalVar"
| Property _ -> "Property"
| ClassConst _ -> "ClassConst"
| Typeconst _ -> "Typeconst"
| GConst -> "GlobalConst"
end
(Pos.string_no_file symbol.pos);
Printf.printf "defined: %s\n"
(Option.value_map definition
~f:(fun x -> Pos.string_no_file x.SymbolDefinition.pos)
~default:"None");
Printf.printf "definition span: %s\n"
(Option.value_map definition
~f:(fun x -> Pos.multiline_string_no_file x.SymbolDefinition.span)
~default:"None")

let check_errors opts errors files_info =
Relative_path.Map.fold files_info ~f:begin fun fn fileinfo errors ->
errors @ Errors.get_error_list
Expand Down Expand Up @@ -548,16 +524,17 @@ let handle_mode mode filename opts popt files_contents files_info errors =
end;
| Identify_symbol (line, column) ->
let file = cat (Relative_path.to_absolute filename) in
let result = ServerIdentifyFunction.go file line column opts in
let result = ServerIdentifyFunction.go_absolute file line column opts in
begin match result with
| [] -> print_endline "None"
| _ -> List.iter result print_symbol
| result -> ClientGetDefinition.print_readable ~short_pos:true result
end
| Symbol_definition_by_id id ->
let result = ServerSymbolDefinition.from_symbol_id opts id in
begin match result with
| None -> print_endline "None"
| Some s -> FileOutline.print [SymbolDefinition.to_absolute s]
| Some s ->
FileOutline.print ~short_pos:true [SymbolDefinition.to_absolute s]
end
| Find_local (line, column) ->
let file = cat (Relative_path.to_absolute filename) in
Expand All @@ -567,7 +544,7 @@ let handle_mode mode filename opts popt files_contents files_info errors =
| Outline ->
let file = cat (Relative_path.to_absolute filename) in
let results = FileOutline.outline popt file in
FileOutline.print results;
FileOutline.print ~short_pos:true results
| Find_refs (line, column) ->
Typing_deps.update_files files_info;
let genv = ServerEnvBuild.default_genv in
Expand Down
21 changes: 13 additions & 8 deletions hphp/hack/src/server/fileOutline.ml
Expand Up @@ -334,33 +334,38 @@ and to_json outline =
List.map outline ~f:definition_to_json
end

let rec print_def indent def =
let rec print_def ~short_pos indent def =
let
{name; kind; id; pos; span; modifiers; children; params; docblock} = def
in
let print_pos, print_span = if short_pos
then Pos.string_no_file, Pos.multiline_string_no_file
else Pos.string, Pos.multiline_string
in
Printf.printf "%s%s\n" indent name;
Printf.printf "%s kind: %s\n" indent (string_of_kind kind);
Option.iter id (fun id -> Printf.printf "%s id: %s\n" indent id);
Printf.printf "%s position: %s\n" indent (Pos.string pos);
Printf.printf "%s span: %s\n" indent (Pos.multiline_string span);
Printf.printf "%s position: %s\n" indent (print_pos pos);
Printf.printf "%s span: %s\n" indent (print_span span);
Printf.printf "%s modifiers: " indent;
List.iter modifiers
(fun x -> Printf.printf "%s " (string_of_modifier x));
Printf.printf "\n";
Option.iter params (fun x ->
Printf.printf "%s params:\n" indent;
print (indent ^ " ") x;
print ~short_pos (indent ^ " ") x;
);
Option.iter docblock (fun x ->
Printf.printf "%s docblock:\n" indent;
Printf.printf "%s\n" x;
);
Printf.printf "\n";
Option.iter children (fun x ->
print (indent ^ " ") x
print ~short_pos (indent ^ " ") x
);

and print indent defs =
List.iter defs ~f:(print_def indent)
and print ~short_pos indent defs =
List.iter defs ~f:(print_def ~short_pos indent)

let print = print ""
let print_def ?short_pos:(short_pos = false) = print_def ~short_pos
let print ?short_pos:(short_pos = false) = print ~short_pos ""
10 changes: 10 additions & 0 deletions hphp/hack/src/utils/symbolOccurrence.ml
Expand Up @@ -28,3 +28,13 @@ type 'a t = {
let to_absolute x = { x with
pos = Pos.to_absolute x.pos;
}

let kind_to_string = function
| Class -> "class"
| Method _ -> "method"
| Function -> "function"
| LocalVar -> "local"
| Property _ -> "property"
| ClassConst _ -> "class_const"
| Typeconst _ -> "typeconst"
| GConst -> "global_const"
14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/abstract_const.php.exp
@@ -1,5 +1,9 @@
\C::FOO
ClassConst
line 4, characters 25-27
defined: line 4, characters 25-27
definition span: line 4, character 25 - line 4, character 27
name: \C::FOO, kind: class_const, span: line 4, characters 25-27, definition:
FOO
kind: const
id: class_const::C::FOO
position: line 4, characters 25-27
span: line 4, character 25 - line 4, character 27
modifiers: abstract


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/catch.php.exp
@@ -1,5 +1,9 @@
\Foo
Class
line 8, characters 12-14
defined: line 3, characters 7-9
definition span: line 3, character 1 - line 3, character 30
name: \Foo, kind: class, span: line 8, characters 12-14, definition:
Foo
kind: class
id: type_id::Foo
position: line 3, characters 7-9
span: line 3, character 1 - line 3, character 30
modifiers:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/class.php.exp
@@ -1,5 +1,9 @@
\Foo
Class
line 5, characters 15-17
defined: line 3, characters 7-9
definition span: line 3, character 1 - line 3, character 12
name: \Foo, kind: class, span: line 5, characters 15-17, definition:
Foo
kind: class
id: type_id::Foo
position: line 3, characters 7-9
span: line 3, character 1 - line 3, character 12
modifiers:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/const.php.exp
@@ -1,5 +1,9 @@
\C::FOO
ClassConst
line 8, characters 6-8
defined: line 4, characters 9-11
definition span: line 4, character 9 - line 4, character 19
name: \C::FOO, kind: class_const, span: line 8, characters 6-8, definition:
FOO
kind: const
id: class_const::C::FOO
position: line 4, characters 9-11
span: line 4, character 9 - line 4, character 19
modifiers:


29 changes: 19 additions & 10 deletions hphp/hack/test/identify_symbol/constructor_inheritance.php.exp
@@ -1,10 +1,19 @@
\Foo
Class
line 10, characters 7-9
defined: line 7, characters 7-9
definition span: line 7, character 1 - line 7, character 22
\Foo::__construct
Method
line 10, characters 3-11
defined: line 4, characters 19-29
definition span: line 4, character 3 - line 4, character 34
name: \Foo, kind: class, span: line 10, characters 7-9, definition:
Foo
kind: class
id: type_id::Foo
position: line 7, characters 7-9
span: line 7, character 1 - line 7, character 22
modifiers:


name: \Foo::__construct, kind: method, span: line 10, characters 3-11, definition:
__construct
kind: method
id: method::C::__construct
position: line 4, characters 19-29
span: line 4, character 3 - line 4, character 34
modifiers: public
params:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/decl_const.php.exp
@@ -1,5 +1,9 @@
\C::FOO
ClassConst
line 4, characters 9-11
defined: line 4, characters 9-11
definition span: line 4, character 9 - line 4, character 19
name: \C::FOO, kind: class_const, span: line 4, characters 9-11, definition:
FOO
kind: const
id: class_const::C::FOO
position: line 4, characters 9-11
span: line 4, character 9 - line 4, character 19
modifiers:


15 changes: 10 additions & 5 deletions hphp/hack/test/identify_symbol/decl_method.php.exp
@@ -1,5 +1,10 @@
\C::foo
Method
line 4, characters 19-21
defined: line 4, characters 19-21
definition span: line 4, character 3 - line 4, character 26
name: \C::foo, kind: method, span: line 4, characters 19-21, definition:
foo
kind: method
id: method::C::foo
position: line 4, characters 19-21
span: line 4, character 3 - line 4, character 26
modifiers: public
params:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/decl_property.php.exp
@@ -1,5 +1,9 @@
\C::foo
Property
line 4, characters 15-18
defined: line 4, characters 15-18
definition span: line 4, character 15 - line 4, character 18
name: \C::foo, kind: property, span: line 4, characters 15-18, definition:
foo
kind: property
id: property::C::foo
position: line 4, characters 15-18
span: line 4, character 15 - line 4, character 18
modifiers: public


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/decl_typeconst.php.exp
@@ -1,5 +1,9 @@
\C::FOO
Typeconst
line 4, characters 14-16
defined: line 4, characters 14-16
definition span: line 4, character 3 - line 4, character 25
name: \C::FOO, kind: typeconst, span: line 4, characters 14-16, definition:
FOO
kind: typeconst
id: class_const::C::FOO
position: line 4, characters 14-16
span: line 4, character 3 - line 4, character 25
modifiers:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/enum_member.php.exp
@@ -1,5 +1,9 @@
\E::FOO
ClassConst
line 8, characters 6-8
defined: line 4, characters 3-5
definition span: line 4, character 3 - line 4, character 9
name: \E::FOO, kind: class_const, span: line 8, characters 6-8, definition:
FOO
kind: const
id: class_const::E::FOO
position: line 4, characters 3-5
span: line 4, character 3 - line 4, character 9
modifiers:


15 changes: 10 additions & 5 deletions hphp/hack/test/identify_symbol/function.php.exp
@@ -1,5 +1,10 @@
\foo
Function
line 6, characters 3-5
defined: line 3, characters 10-12
definition span: line 3, character 1 - line 3, character 17
name: \foo, kind: function, span: line 6, characters 3-5, definition:
foo
kind: function
id: function::foo
position: line 3, characters 10-12
span: line 3, character 1 - line 3, character 17
modifiers:
params:


13 changes: 8 additions & 5 deletions hphp/hack/test/identify_symbol/function_parameter.php.exp
@@ -1,5 +1,8 @@
$string
LocalVar
line 9, characters 9-15
defined: line 8, characters 3-9
definition span: line 8, character 3 - line 8, character 9
name: $string, kind: local, span: line 9, characters 9-15, definition:
$string
kind: local
position: line 8, characters 3-9
span: line 8, character 3 - line 8, character 9
modifiers:


14 changes: 9 additions & 5 deletions hphp/hack/test/identify_symbol/global_const.php.exp
@@ -1,5 +1,9 @@
\FOO
GlobalConst
line 6, characters 3-5
defined: line 3, characters 7-9
definition span: line 3, character 7 - line 3, character 20
name: \FOO, kind: global_const, span: line 6, characters 3-5, definition:
FOO
kind: const
id: class_const::FOO
position: line 3, characters 7-9
span: line 3, character 7 - line 3, character 20
modifiers:


0 comments on commit cc343ad

Please sign in to comment.