Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide ocamlformat-rpc through the ocamlformat package #2035

Merged
merged 7 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

+ Qtest comments are not re-formatted (#2034, @gpetiot)

+ ocamlformat-rpc is now distributed through the ocamlformat package (#2035, @Julow)

#### New features

+ New syntax `(*= ... *)` for verbatim comments (#2028, @gpetiot)
Expand Down
9 changes: 3 additions & 6 deletions bin/ocamlformat-rpc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
(executable
(name main)
(public_name ocamlformat-rpc)
(package ocamlformat-rpc)
(package ocamlformat)
(flags
(:standard -open Ocamlformat_stdlib))
(instrumentation
(backend bisect_ppx))
(libraries ocamlformat-rpc))
(libraries ocamlformat.rpc))

(rule
(with-stdout-to
Expand All @@ -27,7 +27,4 @@
(install
(section man)
(files ocamlformat-rpc.1)
(package ocamlformat-rpc))

(documentation
(package ocamlformat-rpc))
(package ocamlformat))
1 change: 1 addition & 0 deletions doc/index.mld
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ OCamlFormat is a tool to format OCaml code.
- {{!page-howtos}How-To's}
- {{!page-faq}Frequently asked questions}
- {{!page-doc_comments}Doc-comments language reference}
- {{!page-rpc}RPC}
File renamed without changes.
4 changes: 2 additions & 2 deletions dune
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
(with-stdout-to
ocamlformat-rpc-help.actual
(run %{bin:ocamlformat-rpc} --help=plain)))
(package ocamlformat-rpc))
(package ocamlformat))

(rule
(alias runtest)
(package ocamlformat-rpc)
(package ocamlformat)
(action
(diff ocamlformat-rpc-help.txt ocamlformat-rpc-help.actual)))

Expand Down
20 changes: 3 additions & 17 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
(uuseg
(>= 10.0.0))
(uutf
(>= 1.0.1))))
(>= 1.0.1))
(csexp
(>= 1.4.0))))

(package
(name ocamlformat-bench)
Expand All @@ -93,22 +95,6 @@
(< v0.15))
yojson))

(package
(name ocamlformat-rpc)
(synopsis "Auto-formatter for OCaml code (RPC mode)")
(description
"OCamlFormat is a tool to automatically format OCaml code in a uniform style. This package defines a RPC interface to OCamlFormat")
(depends
(ocaml
(and
(>= 4.08)
(< 4.15)))
(alcotest :with-test)
(ocamlformat
(= :version))
(ocamlformat-rpc-lib
(= :version))))

(package
(name ocamlformat-rpc-lib)
(synopsis "Auto-formatter for OCaml code (RPC mode)")
Expand Down
4 changes: 2 additions & 2 deletions lib-rpc-server/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name ocamlformat_rpc)
(public_name ocamlformat-rpc)
(libraries ocamlformat ocamlformat-rpc-lib))
(public_name ocamlformat.rpc)
(libraries ocamlformat ocamlformat.rpc_lib_protocol))
56 changes: 25 additions & 31 deletions lib-rpc-server/ocamlformat_rpc.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Ocamlformat
open Ocamlformat_rpc_lib
open Ocamlformat_stdlib

module IO = struct
Expand All @@ -24,16 +23,19 @@ module IO = struct
return ()
end

module Rpc = Ocamlformat_rpc_lib_protocol.Protocol
module Protocol = Rpc.Make (IO)

module V = struct
let handshake x =
match Version.of_string x with
match Rpc.Version.of_string x with
| Some v -> `Handled v
| None -> `Propose_another Version.V2
| None -> `Propose_another Rpc.Version.V2
end

type state = Waiting_for_version | Version_defined of (Version.t * Conf.t)

include Make (IO)
type state =
| Waiting_for_version
| Version_defined of (Rpc.Version.t * Conf.t)

let format fg conf source =
let input_name = "<rpc input>" in
Expand Down Expand Up @@ -80,20 +82,16 @@ let run_format conf x =
; format Expression
; format Use_file ]

let run_format_with_args {path; config} conf x =
let run_format_with_args {Rpc.path; config} conf x =
let open Result in
Option.value_map path ~default:(Ok conf) ~f:run_path
>>= fun conf ->
Option.value_map config ~default:(Ok conf) ~f:(fun c -> run_config conf c)
>>= fun conf -> run_format conf x

let handle_format_error e output =
output stdout (`Error e) ;
Out_channel.flush stdout
let handle_format_error e output = output stdout (`Error e)

let handle_path_error e output =
output stdout (`Error e) ;
Out_channel.flush stdout
let handle_path_error e output = output stdout (`Error e)

let handle_config_error (e : Config_option.Error.t) output =
let msg =
Expand All @@ -105,8 +103,7 @@ let handle_config_error (e : Config_option.Error.t) output =
Format.sprintf "Misplaced configuration value (%s, %s)" x y
| Unknown (x, _) -> Format.sprintf "Unknown configuration option %s" x
in
output stdout (`Error msg) ;
Out_channel.flush stdout
output stdout (`Error msg)

let handle_error e output =
match e with
Expand All @@ -116,58 +113,55 @@ let handle_error e output =

let rec rpc_main = function
| Waiting_for_version -> (
match Init.read_input stdin with
match Protocol.Init.read_input stdin with
| `Halt -> Ok ()
| `Unknown -> Ok ()
| `Version vstr -> (
match V.handshake vstr with
| `Handled v ->
Init.output stdout (`Version vstr) ;
Out_channel.flush stdout ;
Protocol.Init.output stdout (`Version vstr) ;
rpc_main (Version_defined (v, Conf.default))
| `Propose_another v ->
let vstr = Version.to_string v in
Init.output stdout (`Version vstr) ;
Out_channel.flush stdout ;
let vstr = Rpc.Version.to_string v in
Protocol.Init.output stdout (`Version vstr) ;
rpc_main Waiting_for_version ) )
| Version_defined (v, conf) as state -> (
match v with
| V1 -> (
match V1.Command.read_input stdin with
match Protocol.V1.read_input stdin with
| `Halt -> Ok ()
| `Unknown | `Error _ -> rpc_main state
| `Format x ->
let conf =
match run_format_with_args empty_args conf x with
match run_format_with_args Rpc.empty_args conf x with
| Ok (`Format formatted) ->
V1.Command.output stdout (`Format formatted) ;
Protocol.V1.output stdout (`Format formatted) ;
conf
| Error e ->
handle_error e V1.Command.output ;
handle_error e Protocol.V1.output ;
conf
in
rpc_main (Version_defined (v, conf))
| `Config c -> (
match run_config conf c with
| Ok conf ->
V1.Command.output stdout (`Config c) ;
Out_channel.flush stdout ;
Protocol.V1.output stdout (`Config c) ;
rpc_main (Version_defined (v, conf))
| Error (`Config_error e) ->
handle_config_error e V1.Command.output ;
handle_config_error e Protocol.V1.output ;
rpc_main state ) )
| V2 -> (
match V2.Command.read_input stdin with
match Protocol.V2.read_input stdin with
| `Halt -> Ok ()
| `Unknown | `Error _ -> rpc_main state
| `Format (x, format_args) ->
let conf =
match run_format_with_args format_args conf x with
| Ok (`Format formatted) ->
V2.Command.output stdout (`Format (formatted, format_args)) ;
Protocol.V2.output stdout (`Format (formatted, format_args)) ;
conf
| Error e ->
handle_error e V2.Command.output ;
handle_error e Protocol.V2.output ;
conf
in
rpc_main (Version_defined (v, conf)) ) )
Expand Down
21 changes: 21 additions & 0 deletions lib-rpc/lib-protocol/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; OCamlFormat ;
; ;
; Copyright (c) Facebook, Inc. and its affiliates. ;
; ;
; This source code is licensed under the MIT license found in ;
; the LICENSE file in the root directory of this source tree. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Duplicate these two modules in order to share types and
; serialization/deserialization code between two independent packages.

(library
(name ocamlformat_rpc_lib_protocol)
(public_name ocamlformat.rpc_lib_protocol)
(libraries csexp))

(copy_files#
(files ../{IO,protocol}.ml{,i}))
Loading