Skip to content

Commit

Permalink
feature: switch to ocamlformat-rpc V2
Browse files Browse the repository at this point in the history
Signed-off-by: Paul-Elliot <[email protected]>

ps-id: 274F2716-3909-46FA-BBA9-806D21BE5B17
  • Loading branch information
panglesd authored and rgrinberg committed Apr 6, 2022
1 parent 4fa149c commit 51f1879
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
57 changes: 39 additions & 18 deletions ocaml-lsp-server/src/ocamlformat_rpc.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
open Import
open Fiber.O

let type_option = [ ("module-item-spacing", "compact"); ("margin", "63") ]

module Ocamlformat_rpc = Ocamlformat_rpc_lib.Make (struct
type 'a t = 'a Fiber.t

Expand Down Expand Up @@ -42,7 +44,7 @@ end = struct

let client t = t.client

let supported_versions = [ "v1" ]
let supported_versions = [ "v2"; "v1" ]

let pick_client ~pid session =
Ocamlformat_rpc.pick_client ~pid session session supported_versions
Expand All @@ -51,18 +53,18 @@ end = struct
(* We ask for 64 columns formatting as this appear to be the maximum size of
VScode popups. TODO We should probably allow some flexibility for other
editors that use the server. *)
let* res =
Ocamlformat_rpc.config
[ ("module-item-spacing", "compact"); ("margin", "63") ]
client
in
match res with
| Ok () -> Fiber.return ()
| Error (`Msg msg) ->
let message =
Printf.sprintf "An error occured while configuring ocamlformat: %s" msg
in
logger ~type_:MessageType.Warning ~message
match client with
| `V2 _ -> Fiber.return ()
| `V1 client -> (
let* res = Ocamlformat_rpc.V1.Client.config type_option client in
match res with
| Ok () -> Fiber.return ()
| Error (`Msg msg) ->
let message =
Printf.sprintf "An error occured while configuring ocamlformat: %s"
msg
in
logger ~type_:MessageType.Warning ~message)

let create ~logger ~bin () =
let bin = Fpath.to_string bin in
Expand Down Expand Up @@ -157,20 +159,39 @@ let format_type t ~typ =
let* p = get_process t in
match p with
| Error `No_process -> Fiber.return @@ Error `No_process
| Ok p -> Ocamlformat_rpc.format typ (Process.client p)
| Ok p -> (
match Process.client p with
| `V1 p -> Ocamlformat_rpc.V1.Client.format typ p
| `V2 p ->
let config = Some type_option in
Ocamlformat_rpc.V2.Client.format
~format_args:{ Ocamlformat_rpc_lib.empty_args with config }
typ p)

let format_doc t doc =
let txt = Document.source doc |> Msource.text in
let+ res = format_type t ~typ:txt in
Result.map res ~f:(fun to_ -> Diff.edit ~from:txt ~to_)
let path = Some (Document.uri doc |> Uri.to_path) in
let* p = get_process t in
match p with
| Error `No_process -> Fiber.return @@ Error `No_process
| Ok p -> (
match Process.client p with
| `V2 p ->
let+ res =
Ocamlformat_rpc.V2.Client.format
~format_args:Ocamlformat_rpc_lib.{ empty_args with path }
txt p
in
Result.map res ~f:(fun to_ -> Diff.edit ~from:txt ~to_)
| `V1 _ -> Fiber.return @@ Error `No_V2)

let create_state () =
Waiting_for_init
{ ask_init = Fiber.Ivar.create (); wait_init = Fiber.Ivar.create () }

let _create () = ref (create_state ())
let create () = ref (create_state ())

let create () = ref Disabled
let _create () = ref Disabled

let maybe_fill ivar x =
let* v = Fiber.Ivar.peek ivar in
Expand Down
3 changes: 2 additions & 1 deletion ocaml-lsp-server/src/ocamlformat_rpc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ val format_type :
val format_doc :
t
-> Document.t
-> (TextEdit.t list, [> `Msg of string | `No_process ]) result Fiber.t
-> (TextEdit.t list, [> `Msg of string | `No_process | `No_V2 ]) result
Fiber.t

val run :
logger:(type_:MessageType.t -> message:string -> unit Fiber.t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ let f (x : t) = x
Object {
"edits": Array [
Object {
"newText": "match x with | Foo _ -> _ | Bar _ -> _",
"newText": "match x with Foo _ -> _ | Bar _ -> _
",
"range": Object {
"end": Object {
"character": 17,
Expand Down Expand Up @@ -204,6 +205,7 @@ let f (x : t) = x
"edits": Array [
Object {
"newText": "type t = Foo of int | Bar of bool
val f : t -> t
",
"range": Object {
Expand Down
37 changes: 37 additions & 0 deletions ocaml-lsp-server/test/e2e/__tests__/textDocument-hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ describe("textDocument/hover", () => {
});
});

it("returns type inferred under cursor in a formatted way", async () => {
languageServer = await LanguageServer.startAndInitialize();
await languageServer.sendNotification("textDocument/didOpen", {
textDocument: Types.TextDocumentItem.create(
"file:///test.ml",
"ocaml",
0,
"let f a b c d e f g h i = 1 + a + b + c + d + e + f + g + h + i\n",
),
});

let result = await languageServer.sendRequest("textDocument/hover", {
textDocument: Types.TextDocumentIdentifier.create("file:///test.ml"),
position: Types.Position.create(0, 4),
});

expect(result).toMatchObject({
contents: {
kind: "plaintext",
value: outdent`int ->
int ->
int ->
int ->
int ->
int ->
int ->
int ->
int ->
int`,
},
range: {
end: { character: 5, line: 0 },
start: { character: 4, line: 0 },
},
});
});

it("returns type inferred under cursor (markdown formatting)", async () => {
languageServer = await LanguageServer.startAndInitialize({
capabilities: {
Expand Down

0 comments on commit 51f1879

Please sign in to comment.