Skip to content

Commit

Permalink
Change query to type_at_position
Browse files Browse the repository at this point in the history
Reviewed By: grievejia

Differential Revision: D59638913

fbshipit-source-id: 926c1da04c9db04e23404c329312fdd3a6ff3c17
  • Loading branch information
stroxler authored and facebook-github-bot committed Jul 12, 2024
1 parent e9e5e70 commit 09fcc73
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
31 changes: 20 additions & 11 deletions source/analysis/locationBasedLookup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1230,18 +1230,27 @@ let document_symbol_info ~source =
[]


let hover_info_for_position ~type_environment ~module_reference position =
let symbol_data = find_narrowest_spanning_symbol ~type_environment ~module_reference position in
let type_ =
Result.ok symbol_data
>>= resolve_type_for_symbol ~type_environment
>>| fun type_ -> show_type_for_hover type_
in
let docstring = Result.ok symbol_data >>= find_docstring_for_symbol ~type_environment in
let type_for_symbol_data ~type_environment ~module_reference ~symbol_data position =
let maybe_type = Result.ok symbol_data >>= resolve_type_for_symbol ~type_environment in
Log.log
~section:`Server
"Hover info for symbol at position `%s:%s`: %s"
"Type of narrowest symbol at position `%s:%s`: %s"
(Reference.show module_reference)
([%show: Location.position] position)
(Option.value type_ ~default:"<EMPTY>");
{ value = type_; docstring }
(Option.map maybe_type ~f:Type.show |> Option.value ~default:"<EMPTY>");
maybe_type


let type_at_position ~type_environment ~module_reference position =
let symbol_data = find_narrowest_spanning_symbol ~type_environment ~module_reference position in
type_for_symbol_data ~type_environment ~module_reference ~symbol_data position


let hover_info_for_position ~type_environment ~module_reference position =
let symbol_data = find_narrowest_spanning_symbol ~type_environment ~module_reference position in
let value =
type_for_symbol_data ~type_environment ~module_reference ~symbol_data position
>>| show_type_for_hover
in
let docstring = Result.ok symbol_data >>= find_docstring_for_symbol ~type_environment in
{ value; docstring }
6 changes: 6 additions & 0 deletions source/analysis/locationBasedLookup.mli
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ val coverage_gaps_in_module : coverage_data list -> coverage_gap list

val get_expression_level_coverage : coverage_data_lookup -> coverage_for_path

val type_at_position
: type_environment:TypeEnvironment.ReadOnly.t ->
module_reference:Reference.t ->
Location.position ->
Type.t option

val hover_info_for_position
: type_environment:TypeEnvironment.ReadOnly.t ->
module_reference:Reference.t ->
Expand Down
25 changes: 8 additions & 17 deletions source/server/query.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Request = struct
qualifiers: Reference.t list;
parse_errors: string list;
}
| HoverInfoForPosition of {
| TypeAtPosition of {
path: PyrePath.t;
position: Location.position;
}
Expand Down Expand Up @@ -93,12 +93,6 @@ module Response = struct
}
[@@deriving equal, to_yojson]

type hover_info = {
value: string option;
docstring: string option;
}
[@@deriving equal, to_yojson]

type coverage_at_path = {
path: string;
total_expressions: int;
Expand Down Expand Up @@ -199,7 +193,7 @@ module Response = struct
| FoundModules of Reference.t list
| FoundPath of string
| GlobalLeakErrors of global_leak_errors
| HoverInfoForPosition of hover_info
| TypeAtPosition of Type.t option
| ModelVerificationErrors of Taint.ModelVerificationError.t list
| ReferenceTypesInPath of types_at_path
| Success of string
Expand Down Expand Up @@ -297,7 +291,7 @@ module Response = struct
let reference_to_yojson reference = `String (Reference.show reference) in
`List (List.map references ~f:reference_to_yojson)
| FoundPath path -> `Assoc ["path", `String path]
| HoverInfoForPosition hover_info -> hover_info_to_yojson hover_info
| TypeAtPosition maybe_type -> [%to_yojson: Type.t option] maybe_type
| ReferenceTypesInPath referenceTypesInPath -> types_at_path_to_yojson referenceTypesInPath
| Success message -> `Assoc ["message", `String message]
| Superclasses class_to_superclasses_mapping ->
Expand Down Expand Up @@ -450,8 +444,8 @@ let rec parse_request_exn query =
List.map ~f:single_argument_to_reference arguments
|> List.partition_result
|> fun (qualifiers, parse_errors) -> Request.GlobalLeaks { qualifiers; parse_errors }
| "hover_info_for_position", [path; line; column] ->
Request.HoverInfoForPosition
| "type_at_position", [path; line; column] ->
Request.TypeAtPosition
{
path = PyrePath.create_absolute (string path);
position = { Location.line = integer line; column = integer column };
Expand Down Expand Up @@ -872,14 +866,11 @@ let rec process_request_exn
List.map ~f:find_leak_errors_for_qualifier qualifiers
|> List.partition_result
|> construct_result
| HoverInfoForPosition { path; position } ->
| TypeAtPosition { path; position } ->
qualifier_of_path path
>>| (fun module_reference ->
LocationBasedLookup.hover_info_for_position
~type_environment
~module_reference
position)
>>| (fun { value; docstring } -> Single (Base.HoverInfoForPosition { value; docstring }))
LocationBasedLookup.type_at_position ~type_environment ~module_reference position)
>>| (fun maybe_type -> Single (Base.TypeAtPosition maybe_type))
|> Option.value
~default:(Error (Format.sprintf "No module found for path `%s`" (PyrePath.show path)))
| ModelQuery { path; query_name } -> (
Expand Down
10 changes: 2 additions & 8 deletions source/server/query.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Request : sig
qualifiers: Reference.t list;
parse_errors: string list;
}
| HoverInfoForPosition of {
| TypeAtPosition of {
path: PyrePath.t;
position: Location.position;
}
Expand Down Expand Up @@ -73,12 +73,6 @@ module Response : sig
}
[@@deriving equal, to_yojson]

type hover_info = {
value: string option;
docstring: string option;
}
[@@deriving equal, to_yojson]

type coverage_at_path = {
path: string;
total_expressions: int;
Expand Down Expand Up @@ -171,7 +165,7 @@ module Response : sig
| FoundModules of Ast.Reference.t list
| FoundPath of string
| GlobalLeakErrors of global_leak_errors
| HoverInfoForPosition of hover_info
| TypeAtPosition of Type.t option
| ModelVerificationErrors of Taint.ModelVerificationError.t list
| ReferenceTypesInPath of types_at_path
| Success of string
Expand Down
22 changes: 11 additions & 11 deletions source/server/test/queryTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ let test_parse_query context =
assert_fails_to_parse "expression_level_coverage(a.py)";
assert_fails_to_parse "expression_level_coverage('a.py', 1, 2)";
assert_parses
"hover_info_for_position(path='/foo.py', line=42, column=10)"
(HoverInfoForPosition
"type_at_position(path='/foo.py', line=42, column=10)"
(TypeAtPosition
{ path = PyrePath.create_absolute "/foo.py"; position = Location.{ line = 42; column = 10 } });
assert_fails_to_parse "hover_info_for_position(path='/foo.py', line=42)";
assert_fails_to_parse "hover_info_for_position(path='/foo.py', column=10)";
assert_fails_to_parse "hover_info_for_position(path=99, line=42, column=10)";
assert_fails_to_parse "type_at_position(path='/foo.py', line=42)";
assert_fails_to_parse "type_at_position(path='/foo.py', column=10)";
assert_fails_to_parse "type_at_position(path=99, line=42, column=10)";
assert_parses
"global_leaks(path.to.my_function)"
(GlobalLeaks { qualifiers = [!&"path.to.my_function"]; parse_errors = [] });
Expand Down Expand Up @@ -2555,7 +2555,7 @@ let test_expression_level_coverage context =
|> fun json -> `List [`String "Query"; json] |> Yojson.Safe.to_string )))


let test_hover context =
let test_type_at_position context =
let sources =
["foo.py", {|
def foo(x: int) -> int:
Expand All @@ -2567,16 +2567,16 @@ let test_hover context =
in
let queries_and_expected_responses =
[
( "hover_info_for_position(path='foo.py', line=2, column=0)",
( "type_at_position(path='foo.py', line=2, column=0)",
{|
{
"response": { "value": null, "docstring": null}
"response": null
}
|} );
( "hover_info_for_position(path='foo.py', line=3, column=9)",
( "type_at_position(path='foo.py', line=3, column=9)",
{|
{
"response": { "value": "int", "docstring": null}
"response": "int"
}
|} );
]
Expand Down Expand Up @@ -2907,7 +2907,7 @@ let () =
>:: OUnitLwt.lwt_wrapper test_handle_query_with_build_system;
"handle_query_pysa" >:: OUnitLwt.lwt_wrapper test_handle_query_pysa;
"expression_level_coverage" >:: OUnitLwt.lwt_wrapper test_expression_level_coverage;
"hover" >:: OUnitLwt.lwt_wrapper test_hover;
"hover" >:: OUnitLwt.lwt_wrapper test_type_at_position;
"dump_call_graph" >:: OUnitLwt.lwt_wrapper test_dump_call_graph;
"global_leaks" >:: OUnitLwt.lwt_wrapper test_global_leaks;
"process_request" >:: test_process_request;
Expand Down

0 comments on commit 09fcc73

Please sign in to comment.