Skip to content

Commit

Permalink
Implement completion request handler logic
Browse files Browse the repository at this point in the history
Summary:
# Overview
Refer to D47564289

This stack of diff will be split into the following steps:
1. Setup `CompletionItem` and helper signatures
2. Implement attribute completion core logic given file and position and test
3. Update symbol traversal logic to be right inclusive if necessary (to handle cursor at end of line case)
4. Implement prefix filtering and test
5. Implement localBasedLookup completion public endpoint
6. **Extend Pyre `requestHandler.ml` with handler for completion request**

Reviewed By: kinto0

Differential Revision: D47636309

fbshipit-source-id: 8f65a2b4e45e6095786a410b1e2a613df9e547d9
  • Loading branch information
NgaiJustin authored and facebook-github-bot committed Jul 28, 2023
1 parent 4247cb7 commit f00ad98
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions source/code_navigation_server/requestHandler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,46 @@ let handle_location_of_definition
>>| fun definitions -> Response.(LocationOfDefinition { definitions })


let get_completion_for_module ~overlay ~position module_reference =
let open Option in
let type_environment = ErrorsEnvironment.ReadOnly.type_environment overlay in
LocationBasedLookup.completion_info_for_position ~type_environment ~module_reference position
>>| Ast.Identifier.SerializableMap.bindings
>>| List.map ~f:(fun (identifier, attribute) ->
let attribute_kind =
match Ast.Node.value attribute with
| { ClassSummary.Attribute.kind = Simple _; _ } ->
Response.CompletionItem.CompletionItemKind.Simple
| { ClassSummary.Attribute.kind = Method _; _ } ->
Response.CompletionItem.CompletionItemKind.Method
| { ClassSummary.Attribute.kind = Property _; _ } ->
Response.CompletionItem.CompletionItemKind.Property
in
{ Response.CompletionItem.label = identifier; kind = attribute_kind })


let get_completion_in_overlay ~overlay ~build_system ~position path =
let open Result in
let module_tracker = ErrorsEnvironment.ReadOnly.module_tracker overlay in
get_modules ~module_tracker ~build_system path
>>| List.filter_map ~f:(get_completion_for_module ~overlay ~position)


let handle_completion
~path
~position
~client_id
{ State.environment; build_system; client_states; _ }
=
let open Result in
get_overlay_id ~path ~client_id client_states
>>= fun overlay_id ->
let overlay = get_overlay ~environment overlay_id in
get_completion_in_overlay ~overlay ~build_system ~position path
>>| List.concat
>>| fun completions -> Response.(Completion { completions })


let handle_superclasses
~class_:{ Request.ClassExpression.module_; qualified_name }
{ State.environment; _ }
Expand Down Expand Up @@ -468,11 +508,9 @@ let handle_query
Lwt.return response
in
Server.ExclusiveLock.read state ~f
| Request.Query.Completion _ ->
let f _state =
let response =
Response.Error (Response.ErrorKind.InvalidRequest "Autocomplete not yet implemented")
in
| Request.Query.Completion { path; position; client_id } ->
let f state =
let response = handle_completion ~path ~position ~client_id state |> response_from_result in
Lwt.return response
in
Server.ExclusiveLock.read state ~f
Expand Down

0 comments on commit f00ad98

Please sign in to comment.