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

Improve: allow both values of boolean options #1062

Merged
merged 6 commits into from
Oct 9, 2019
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### (master)

+ Improve: allow both values of boolean options (#1062) (Jules Aguillon)
+ Improve: auto mode for break-string-literals instead of {wrap,newlines,newlines-and-wrap} (#1057) (Guillaume Petiot)
+ Improve: remove utility functions from Fmt_ast (#1059) (Guillaume Petiot)
+ Fix newlines and indentation in toplevel extension points (#1054) (Guillaume Petiot)
Expand Down
119 changes: 95 additions & 24 deletions ocamlformat-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ OPTIONS (CODE FORMATTING STYLE)
are ignored and can be used as comments.

--align-cases
Align match/try cases horizontally.
Align match/try cases horizontally. The flag is unset by default.

--align-constructors-decl
Align type declarations horizontally.
Align type declarations horizontally. The flag is unset by
default.

--align-variants-decl
Align type variants declarations horizontally.
Align type variants declarations horizontally. The flag is unset
by default.

--assignment-operator={end-line|begin-line}
Position of the assignment operator. end-line positions assignment
Expand Down Expand Up @@ -105,14 +107,21 @@ OPTIONS (CODE FORMATTING STYLE)
fit-or-vertical vertically breaks expressions if they do not fit
on a single line. The default value is wrap.

--break-infix-before-func
Break infix operators whose right arguments are anonymous
functions specially: do not break after the operator so that the
first line of the function appears docked at the end of line after
the operator. The flag is set by default.

--break-separators={before|after}
Break before or after separators such as `;` in list or record
expressions. before breaks the expressions before the separator.
after breaks the expressions after the separator. The default
value is before.

--break-sequences
Force sequence expressions to break irrespective of margin.
Force sequence expressions to break irrespective of margin. The
flag is unset by default.

--break-string-literals={auto|never}
Break string literals. auto mode breaks lines at newlines and
Expand Down Expand Up @@ -140,11 +149,12 @@ OPTIONS (CODE FORMATTING STYLE)
--disable
Disable ocamlformat. This is used in attributes to locally disable
automatic code formatting. One can also use [@@@ocamlformat
"enable"] instead of [@@@ocamlformat "disable=false"]
"enable"] instead of [@@@ocamlformat "disable=false"]. The flag is
unset by default.

--disambiguate-non-breaking-match
Add parentheses around matching constructs that fit on a single
line.
line. The flag is unset by default.

--doc-comments={after|before}
Doc comments position. after puts doc comments after the
Expand All @@ -166,7 +176,8 @@ OPTIONS (CODE FORMATTING STYLE)
--dock-collection-brackets
Dock the brackets of lists, arrays and records, so that when the
collection does not fit on a single line the brackets are opened
on the preceding line and closed on the following line.
on the preceding line and closed on the following line. The flag
is unset by default.

--escape-chars={preserve|decimal|hexadecimal}
Escape encoding for character literals. preserve escapes ASCII
Expand Down Expand Up @@ -247,7 +258,8 @@ OPTIONS (CODE FORMATTING STYLE)
precedences of infix operators. The default value is indent.

--leading-nested-match-parens
Nested match parens formatting. Cannot be set in attributes.
Nested match parens formatting. The flag is unset by default.
Cannot be set in attributes.

--let-and={compact|sparse}
Style of let_and. compact will try to format `let p = e and p = e`
Expand Down Expand Up @@ -313,22 +325,67 @@ OPTIONS (CODE FORMATTING STYLE)
nested pattern-matching under the encompassing pattern-matching.
The default value is wrap.

--no-align-cases
Unset align-cases.

--no-align-constructors-decl
Unset align-constructors-decl.

--no-align-variants-decl
Unset align-variants-decl.

--no-break-infix-before-func
Break infix operators whose right arguments are anonymous
functions specially: do not break after the operator so that the
first line of the function appears docked at the end of line after
the operator.
Unset break-infix-before-func.

--no-break-sequences
Unset break-sequences.

--no-disable
Unset disable.

--no-disambiguate-non-breaking-match
Unset disambiguate-non-breaking-match.

--no-dock-collection-brackets
gpetiot marked this conversation as resolved.
Show resolved Hide resolved
Unset dock-collection-brackets.

--no-leading-nested-match-parens
Unset leading-nested-match-parens.

--no-ocp-indent-compat
Unset ocp-indent-compat.

--no-parens-ite
Unset parens-ite.

--no-parse-docstrings
Unset parse-docstrings.

--no-space-around-arrays
Unset space-around-arrays.

--no-space-around-lists
Unset space-around-lists.

--no-space-around-records
Unset space-around-records.

--no-space-around-variants
Unset space-around-variants.

--no-wrap-comments
Unset wrap-comments.

--no-wrap-fun-args
Style for function call.
Unset wrap-fun-args.

--ocp-indent-compat
Attempt to generate output which does not change (much) when
post-processing with ocp-indent.
post-processing with ocp-indent. The flag is unset by default.

--parens-ite
Uses parentheses around if-then-else branches that spread across
multiple lines.
multiple lines. The flag is unset by default.

--parens-tuple={always|multi-line-only}
Parens tuple expressions. always always uses parentheses around
Expand All @@ -342,7 +399,7 @@ OPTIONS (CODE FORMATTING STYLE)
multi-line-only.

--parse-docstrings
Parse and format docstrings.
Parse and format docstrings. The flag is unset by default.

--sequence-blank-line={compact|preserve-one}
Blank line between expressions of a sequence. compact will not
Expand All @@ -363,16 +420,20 @@ OPTIONS (CODE FORMATTING STYLE)
is compact.

--space-around-arrays
Add a space inside the delimiters of arrays.
Add a space inside the delimiters of arrays. The flag is unset by
default.

--space-around-lists
Add a space inside the delimiters of lists.
Add a space inside the delimiters of lists. The flag is unset by
default.

--space-around-records
Add a space inside the delimiters of records.
Add a space inside the delimiters of records. The flag is unset by
default.

--space-around-variants
Add a space inside the delimiters of variants.
Add a space inside the delimiters of variants. The flag is unset
by default.

--stritem-extension-indent=COLS
Indentation of structure items inside extension nodes (COLS
Expand All @@ -395,7 +456,10 @@ OPTIONS (CODE FORMATTING STYLE)
and each paragraph is wrapped at the margin. Multi-line comments
with vertically-aligned asterisks on the left margin are not
wrapped. Consecutive comments with both left and right margin
aligned are not wrapped either.
aligned are not wrapped either. The flag is unset by default.

--wrap-fun-args
Style for function call. The flag is set by default.

OPTIONS
-c VAL, --config=VAL (absent OCAMLFORMAT env)
Expand All @@ -406,6 +470,11 @@ OPTIONS
Check whether the input files already are formatted. Mutually
exclusive with --inplace and --output.

--comment-check
Control whether to check comments and documentation comments.
Unsafe to turn off. May be set in .ocamlformat. The flag is set by
default.

--disable-conf-attrs
Disable configuration in attributes.

Expand Down Expand Up @@ -454,8 +523,10 @@ OPTIONS
NAME, see documentation of other options for details.

--no-comment-check
UNSAFE: Control whether to check comments and documentation
comments. May be set in .ocamlformat.
Unset comment-check.

--no-quiet
Unset quiet.

--no-version-check
Do no check version matches the one specified in .ocamlformat.
Expand Down Expand Up @@ -508,7 +579,7 @@ OPTIONS
specified, or for the current working directory otherwise.

-q, --quiet
Quiet. May be set in .ocamlformat.
Quiet. May be set in .ocamlformat. The flag is unset by default.

--root=DIR
Root of the project. If specified, only take into account
Expand Down
6 changes: 3 additions & 3 deletions src/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ module Formatting = struct
let doc =
"Disable ocamlformat. This is used in attributes to locally disable \
automatic code formatting. One can also use $(b,[@@@ocamlformat \
\"enable\"]) instead of $(b,[@@@ocamlformat \"disable=false\"])"
\"enable\"]) instead of $(b,[@@@ocamlformat \"disable=false\"])."
in
C.flag ~names:["disable"] ~default:false ~doc ~section
(fun conf x -> {conf with disable= x})
Expand Down Expand Up @@ -1122,8 +1122,8 @@ let docs = C.section_name section
let comment_check =
let default = true in
let doc =
"UNSAFE: Control whether to check comments and documentation comments. \
May be set in $(b,.ocamlformat)."
"Control whether to check comments and documentation comments. Unsafe \
to turn off. May be set in $(b,.ocamlformat)."
in
C.flag ~default ~names:["comment-check"] ~doc ~section
(fun conf x -> {conf with comment_check= x})
Expand Down
33 changes: 20 additions & 13 deletions src/Config_option.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ module Make (C : CONFIG) = struct
let deprecated_doc ~deprecated =
if deprecated then " Warning: This option is deprecated." else ""

let generated_flag_doc ~allow_inline ~doc ~section ~deprecated =
Format.sprintf "%s%s%s" doc
let generated_flag_doc ~allow_inline ~doc ~section ~default ~deprecated =
let default = if default then "set" else "unset" in
Format.sprintf "%s The flag is $(b,%s) by default.%s%s" doc default
(in_attributes ~section allow_inline)
(deprecated_doc ~deprecated)

Expand All @@ -84,20 +85,26 @@ module Make (C : CONFIG) = struct
?(allow_inline = Poly.(section = `Formatting)) ?(deprecated = false)
update get_value =
let open Cmdliner in
let invert_flag = default in
let names_for_cmdline =
if invert_flag then
List.filter_map names ~f:(fun n ->
if String.length n = 1 then None else Some ("no-" ^ n))
gpetiot marked this conversation as resolved.
Show resolved Hide resolved
else names
in
let doc = generated_flag_doc ~allow_inline ~doc ~section ~deprecated in
let invert_names =
List.filter_map names ~f:(fun n ->
if String.length n = 1 then None else Some ("no-" ^ n))
in
let doc =
generated_flag_doc ~allow_inline ~doc ~section ~default ~deprecated
in
let invert_doc = "Unset $(b," ^ List.last_exn names ^ ")." in
let docs = section_name section in
let term = Arg.(value & flag & info names_for_cmdline ~doc ~docs) in
let term =
Arg.(
value
& vflag None
[ (Some true, info names ~doc ~docs)
; (Some false, info invert_names ~doc:invert_doc ~docs) ])
in
let parse = Arg.(conv_parser bool) in
let r = mk ~default term in
let r = mk ~default:None term in
let to_string = Bool.to_string in
let cmdline_get () = if !r then Some (not invert_flag) else None in
let cmdline_get () = !r in
let opt =
{ names
; parse
Expand Down