Skip to content

Commit

Permalink
Improve: remove utility functions from Fmt_ast (ocaml-ppx#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot authored and bogdan2412 committed Mar 28, 2020
1 parent ccb1697 commit 09a368e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 138 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### (master)

+ Improve: remove utility functions from Fmt_ast (#1059) (Guillaume Petiot)
+ Fix newlines and indentation in toplevel extension points (#1054) (Guillaume Petiot)
+ Fix placement of doc comments around extensions (#1052) (Jules Aguillon)
+ Improve: inline extensions that do not break (#1050) (Guillaume Petiot)
Expand Down
38 changes: 37 additions & 1 deletion src/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2078,4 +2078,40 @@ and debug = !debug

and check = !check

let parse_line_in_attribute = parse_line ~from:`Attribute
open Migrate_ast.Parsetree

let update ?(quiet = false) c {attr_name= {txt; loc}; attr_payload; _} =
let result =
match txt with
| "ocamlformat" -> (
match attr_payload with
| PStr
[ { pstr_desc=
Pstr_eval
( { pexp_desc= Pexp_constant (Pconst_string (str, None))
; pexp_attributes= []
; _ }
, [] )
; _ } ] ->
parse_line ~from:`Attribute c str
| _ -> Error (`Malformed "string expected") )
| _ when String.is_prefix ~prefix:"ocamlformat." txt ->
Error
(`Malformed
(Format.sprintf "unknown suffix %S"
(String.chop_prefix_exn ~prefix:"ocamlformat." txt)))
| _ -> Ok c
in
match result with
| Ok conf -> conf
| Error error ->
let reason = function
| `Malformed line -> Format.sprintf "Invalid format %S" line
| `Misplaced (name, _) -> Format.sprintf "%s not allowed here" name
| `Unknown (name, _) -> Format.sprintf "Unknown option %s" name
| `Bad_value (name, value) ->
Format.sprintf "Invalid value for %s: %S" name value
in
let w = Warnings.Attribute_payload (txt, reason error) in
if (not c.quiet) && not quiet then Compat.print_warning loc w ;
c
12 changes: 3 additions & 9 deletions src/Conf.mli
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ val debug : bool
val check : bool
(** Check whether the input files already are formatted. *)

val parse_line_in_attribute :
t
-> string
-> ( t
, [ `Unknown of string * string
| `Bad_value of string * string
| `Malformed of string
| `Misplaced of string * string ] )
Result.t
val update : ?quiet:bool -> t -> Migrate_ast.Parsetree.attribute -> t
(** [update ?quiet c a] updates configuration [c] after reading attribute
[a]. [quiet] is false by default. *)
Loading

0 comments on commit 09a368e

Please sign in to comment.