Skip to content

Commit

Permalink
[flow][refactor] Split lsp into parts that are are pure and the parts…
Browse files Browse the repository at this point in the history
… that uses jsonrpc

Summary:
The goal is to make autocomplete work in try-flow.

Somewhere in autocomplete and code action, we will pull in the lsp target. This is understandable, since we want to produce these lsp response objects. However, the lsp target also includes some code that does some JSON-RPC stuff, which will not work in the browser.

This diff separates these two parts, so that when autocomplete and code actions pull in lsp, we only pull in these type definition and pure functions.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D55549369

fbshipit-source-id: 3839e47e95790110de158e1605a56f1e6a429c12
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Apr 2, 2024
1 parent 9ba8a69 commit f5420c9
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/commands/dune
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
flow_server_files
flow_server_protocol
flow_server_status
marshal_tools_lwt
socket ; hack
))

Expand Down
1 change: 0 additions & 1 deletion src/hack_forked/utils/lsp/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
file_content
file_url
hh_json
jsonrpc
flow_exit_status
utils_core)
(preprocess
Expand Down
38 changes: 0 additions & 38 deletions src/hack_forked/utils/lsp/lsp_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
(* A few helpful wrappers around LSP *)

open Lsp
open Lsp_fmt

(************************************************************************)
(* Conversions *)
Expand Down Expand Up @@ -313,40 +312,3 @@ let supports_hierarchical_document_symbol (p : Lsp.Initialize.params) : bool =

let supports_connectionStatus (p : Lsp.Initialize.params) : bool =
Lsp.Initialize.(p.client_capabilities.telemetry.connectionStatus)

(************************************************************************)
(* Wrappers for some LSP methods *)
(************************************************************************)

let telemetry (writer : Jsonrpc.writer) (level : MessageType.t) (message : string) : unit =
print_logMessage level message |> Jsonrpc.notify writer "telemetry/event"

let telemetry_error (writer : Jsonrpc.writer) = telemetry writer MessageType.ErrorMessage

let telemetry_log (writer : Jsonrpc.writer) = telemetry writer MessageType.LogMessage

let log (writer : Jsonrpc.writer) (level : MessageType.t) (message : string) : unit =
print_logMessage level message |> Jsonrpc.notify writer "window/logMessage"

let log_error (writer : Jsonrpc.writer) = log writer MessageType.ErrorMessage

let log_warning (writer : Jsonrpc.writer) = log writer MessageType.WarningMessage

let log_info (writer : Jsonrpc.writer) = log writer MessageType.InfoMessage

let dismiss_diagnostics (writer : Jsonrpc.writer) (diagnostic_uris : UriSet.t) : UriSet.t =
let dismiss_one (uri : DocumentUri.t) : unit =
let message = { Lsp.PublishDiagnostics.uri; diagnostics = [] } in
message |> print_diagnostics |> Jsonrpc.notify writer "textDocument/publishDiagnostics"
in
UriSet.iter dismiss_one diagnostic_uris;
UriSet.empty

let notify_connectionStatus
(p : Lsp.Initialize.params) (writer : Jsonrpc.writer) (wasConnected : bool) (isConnected : bool)
: bool =
( if supports_connectionStatus p && wasConnected <> isConnected then
let message = { Lsp.ConnectionStatus.isConnected } in
message |> print_connectionStatus |> Jsonrpc.notify writer "telemetry/connectionStatus"
);
isConnected
18 changes: 0 additions & 18 deletions src/hack_forked/utils/lsp/lsp_helpers.mli
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,3 @@ val supports_completion_item_label_details : Lsp.Initialize.params -> bool
val supports_connectionStatus : Lsp.Initialize.params -> bool

val supports_hierarchical_document_symbol : Lsp.Initialize.params -> bool

val telemetry : Jsonrpc.writer -> Lsp.MessageType.t -> string -> unit

val telemetry_error : Jsonrpc.writer -> string -> unit

val telemetry_log : Jsonrpc.writer -> string -> unit

val log : Jsonrpc.writer -> Lsp.MessageType.t -> string -> unit

val log_error : Jsonrpc.writer -> string -> unit

val log_warning : Jsonrpc.writer -> string -> unit

val log_info : Jsonrpc.writer -> string -> unit

val dismiss_diagnostics : Jsonrpc.writer -> Lsp.UriSet.t -> Lsp.UriSet.t

val notify_connectionStatus : Lsp.Initialize.params -> Jsonrpc.writer -> bool -> bool -> bool
11 changes: 11 additions & 0 deletions src/hack_forked/utils/lsp_writers/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(library
(name lsp_writers)
(wrapped false)
(libraries
lsp
jsonrpc)
(preprocess
(pps lwt_ppx ppx_deriving.show ppx_deriving.std ppx_deriving.enum ppx_deriving.eq)))

(dirs
(:standard __tests__))
46 changes: 46 additions & 0 deletions src/hack_forked/utils/lsp_writers/lsp_writers.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

open Lsp
open Lsp_fmt

(************************************************************************)
(* Wrappers for some LSP methods *)
(************************************************************************)

let telemetry (writer : Jsonrpc.writer) (level : MessageType.t) (message : string) : unit =
print_logMessage level message |> Jsonrpc.notify writer "telemetry/event"

let telemetry_error (writer : Jsonrpc.writer) = telemetry writer MessageType.ErrorMessage

let telemetry_log (writer : Jsonrpc.writer) = telemetry writer MessageType.LogMessage

let log (writer : Jsonrpc.writer) (level : MessageType.t) (message : string) : unit =
print_logMessage level message |> Jsonrpc.notify writer "window/logMessage"

let log_error (writer : Jsonrpc.writer) = log writer MessageType.ErrorMessage

let log_warning (writer : Jsonrpc.writer) = log writer MessageType.WarningMessage

let log_info (writer : Jsonrpc.writer) = log writer MessageType.InfoMessage

let dismiss_diagnostics (writer : Jsonrpc.writer) (diagnostic_uris : UriSet.t) : UriSet.t =
let dismiss_one (uri : DocumentUri.t) : unit =
let message = { Lsp.PublishDiagnostics.uri; diagnostics = [] } in
message |> print_diagnostics |> Jsonrpc.notify writer "textDocument/publishDiagnostics"
in
UriSet.iter dismiss_one diagnostic_uris;
UriSet.empty

let notify_connectionStatus
(p : Lsp.Initialize.params) (writer : Jsonrpc.writer) (wasConnected : bool) (isConnected : bool)
: bool =
( if Lsp_helpers.supports_connectionStatus p && wasConnected <> isConnected then
let message = { Lsp.ConnectionStatus.isConnected } in
message |> print_connectionStatus |> Jsonrpc.notify writer "telemetry/connectionStatus"
);
isConnected
24 changes: 24 additions & 0 deletions src/hack_forked/utils/lsp_writers/lsp_writers.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

val telemetry : Jsonrpc.writer -> Lsp.MessageType.t -> string -> unit

val telemetry_error : Jsonrpc.writer -> string -> unit

val telemetry_log : Jsonrpc.writer -> string -> unit

val log : Jsonrpc.writer -> Lsp.MessageType.t -> string -> unit

val log_error : Jsonrpc.writer -> string -> unit

val log_warning : Jsonrpc.writer -> string -> unit

val log_info : Jsonrpc.writer -> string -> unit

val dismiss_diagnostics : Jsonrpc.writer -> Lsp.UriSet.t -> Lsp.UriSet.t

val notify_connectionStatus : Lsp.Initialize.params -> Jsonrpc.writer -> bool -> bool -> bool
4 changes: 3 additions & 1 deletion src/lsp/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
flow_commands_utils
flow_common_lsp_conversions
flow_exit_status
logging_common_lwt)
jsonrpc
logging_common_lwt
lsp_writers)
(preprocess
(pps lwt_ppx)))
16 changes: 8 additions & 8 deletions src/lsp/flowLsp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ let show_connected_status (cenv : connected_env) : connected_env =
let show_connected (env : connected_env) : server_state =
(* report that we're connected to telemetry/connectionStatus *)
let i_isConnected =
Lsp_helpers.notify_connectionStatus
Lsp_writers.notify_connectionStatus
env.c_ienv.i_initialize_params
to_stdout
env.c_ienv.i_isConnected
Expand All @@ -897,7 +897,7 @@ let show_connected (env : connected_env) : server_state =

let show_connecting (reason : CommandConnectSimple.error) (env : disconnected_env) : server_state =
if reason = CommandConnectSimple.Server_missing then
Lsp_helpers.log_info to_stdout "Starting Flow server";
Lsp_writers.log_info to_stdout "Starting Flow server";

let (message, shortMessage, progress, total) =
match (reason, env.d_server_status) with
Expand Down Expand Up @@ -930,7 +930,7 @@ let show_disconnected (code : FlowExit.t option) (message : string option) (env
: server_state =
(* report that we're disconnected to telemetry/connectionStatus *)
let i_isConnected =
Lsp_helpers.notify_connectionStatus
Lsp_writers.notify_connectionStatus
env.d_ienv.i_initialize_params
to_stdout
env.d_ienv.i_isConnected
Expand Down Expand Up @@ -1750,7 +1750,7 @@ let try_connect ~version_mismatch_strategy flowconfig_name (env : disconnected_e
^ current_version_str
^ "\n"
in
Lsp_helpers.telemetry_log to_stdout message;
Lsp_writers.telemetry_log to_stdout message;
lsp_exit_bad ()
);
let start_env =
Expand Down Expand Up @@ -2672,7 +2672,7 @@ and main_handle_error (exn : Exception.t) (state : state) (event : event option)
match state with
| Initialized (Connected env) ->
let i_isConnected =
Lsp_helpers.notify_connectionStatus
Lsp_writers.notify_connectionStatus
env.c_ienv.i_initialize_params
to_stdout
env.c_ienv.i_isConnected
Expand All @@ -2690,7 +2690,7 @@ and main_handle_error (exn : Exception.t) (state : state) (event : event option)
in
let code = Base.Option.value_map code ~f:FlowExit.to_string ~default:"" in
let report = Printf.sprintf "Server fatal exception: [%s] %s\n%s" code message stack in
Lsp_helpers.telemetry_error to_stdout report;
Lsp_writers.telemetry_error to_stdout report;
let (d_autostart, d_ienv) =
match state with
| Initialized
Expand Down Expand Up @@ -2731,7 +2731,7 @@ and main_handle_error (exn : Exception.t) (state : state) (event : event option)
let stack = remote_stack ^ "---\n" ^ stack in
main_log_error ~expected:true ("[Client recoverable] " ^ message) stack event;
let report = Printf.sprintf "Client exception: %s\n%s" message stack in
Lsp_helpers.telemetry_error to_stdout report;
Lsp_writers.telemetry_error to_stdout report;
state
| Client_fatal_connection_exception { Marshal_tools.stack = remote_stack; message } ->
let stack = remote_stack ^ "---\n" ^ stack in
Expand All @@ -2751,6 +2751,6 @@ and main_handle_error (exn : Exception.t) (state : state) (event : event option)
let key = command_key_of_state state in
let json = Lsp_fmt.print_lsp_response ~key id (ErrorResult (e, stack)) in
to_stdout json
| _ -> Lsp_helpers.telemetry_error to_stdout text
| _ -> Lsp_writers.telemetry_error to_stdout text
in
state
1 change: 1 addition & 0 deletions src/monitor/rpc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
(wrapped false)
(libraries
flow_server_protocol
marshal_tools_lwt
sys_utils ; hack
))

0 comments on commit f5420c9

Please sign in to comment.