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

Define a diff-friendly profile #2020

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ profile. This started with version 0.26.0.
- Fixed bug with attributes on sub-expressions of infix operators (#2459, @tdelvecchio-jsc)
- \* Fix cinaps comment formatting to not change multiline string contents (#2463, @tdelvecchio-jsc)

### Added

- New profile: `diff-friendly`, aiming to minimize the vertical diff (#2020, @gpetiot)

## 0.26.1 (2023-09-15)

### Changed
Expand Down
70 changes: 70 additions & 0 deletions lib/Conf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,72 @@ let ocamlformat_profile from =
; wrap_docstrings= elt true
; wrap_fun_args= elt true }

let diff_friendly_profile from =
let elt content = Elt.make content from in
{ align_symbol_open_paren= elt false
; assignment_operator= elt `End_line
; break_before_in= elt `Fit_or_vertical
; break_cases= elt `Vertical
; break_collection_expressions= elt `Fit_or_vertical
; break_colon= elt `After
; break_infix= elt `Fit_or_vertical
; break_infix_before_func= elt true
; break_fun_decl= elt `Fit_or_vertical
; break_fun_sig= elt `Fit_or_vertical
; break_separators= elt `After
; break_sequences= elt true
; break_string_literals= elt `Auto
; break_struct= elt true
; cases_exp_indent= elt 4
; cases_matching_exp_indent= elt `Compact
; disambiguate_non_breaking_match= elt false
; doc_comments= elt `Before_except_val
; doc_comments_padding= elt 2
; doc_comments_tag_only= elt `Default
; dock_collection_brackets= elt true
; exp_grouping= elt `Parens
; extension_indent= elt 2
; field_space= elt `Tight
; function_indent= elt 2
; function_indent_nested= elt `Never
; if_then_else= elt `Vertical
; indent_after_in= elt 0
; indicate_multiline_delimiters= elt `Closing_on_separate_line
; indicate_nested_or_patterns= elt `Unsafe_no
; infix_precedence= elt `Indent
; leading_nested_match_parens= elt false
; let_and= elt `Sparse
; let_binding_indent= elt 2
; let_binding_deindent_fun= elt true
; let_binding_spacing= elt `Compact
; let_module= elt `Sparse
; line_endings= elt `Lf
; margin= elt 80
; match_indent= elt 0
; match_indent_nested= elt `Never
; max_indent= elt None
; module_item_spacing= elt `Sparse
; nested_match= elt `Wrap
; ocp_indent_compat= elt false
; parens_ite= elt false
; parens_tuple= elt `Always
; parens_tuple_patterns= elt `Multi_line_only
; parse_docstrings= elt false
; parse_toplevel_phrases= elt false
; sequence_blank_line= elt `Compact
; sequence_style= elt `Separator
; single_case= elt `Sparse
; space_around_arrays= elt false
; space_around_lists= elt false
; space_around_records= elt false
; space_around_variants= elt false
; stritem_extension_indent= elt 0
; type_decl= elt `Sparse
; type_decl_indent= elt 2
; wrap_comments= elt false
; wrap_docstrings= elt true
; wrap_fun_args= elt false }

let janestreet_profile from =
let elt content = Elt.make content from in
{ align_symbol_open_paren= elt false
Expand Down Expand Up @@ -294,6 +360,8 @@ let profile =
; Decl.Value.make ~name:"conventional" `conventional
"The $(b,conventional) profile aims to be as familiar and \
\"conventional\" appearing as the available options allow."
; Decl.Value.make ~name:"diff-friendly" `diff_friendly
"The $(b,diff_friendly) profile aims to minimize the vertical diff."
; Decl.Value.make ~name:"ocamlformat" `ocamlformat
"The $(b,ocamlformat) profile aims to take advantage of the \
strengths of a parsetree-based auto-formatter, and to limit the \
Expand Down Expand Up @@ -324,6 +392,7 @@ let profile =
match p with
| `default -> "default"
| `conventional -> "conventional"
| `diff_friendly -> "diff-friendly"
| `ocamlformat -> "ocamlformat"
| `janestreet -> "janestreet"
in
Expand All @@ -339,6 +408,7 @@ let profile =
( match p with
| `default -> default_profile
| `conventional -> conventional_profile
| `diff_friendly -> diff_friendly_profile
| `ocamlformat -> ocamlformat_profile
| `janestreet -> janestreet_profile )
from_p
Expand Down
4 changes: 3 additions & 1 deletion lib/Conf_t.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,6 @@ type opr_opts =
type t =
{ fmt_opts: fmt_opts
; opr_opts: opr_opts
; profile: [`default | `conventional | `ocamlformat | `janestreet] elt }
; profile:
[`default | `conventional | `diff_friendly | `ocamlformat | `janestreet]
elt }
4 changes: 3 additions & 1 deletion lib/Conf_t.mli
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ type opr_opts =
type t =
{ fmt_opts: fmt_opts
; opr_opts: opr_opts
; profile: [`default | `conventional | `ocamlformat | `janestreet] elt }
; profile:
[`default | `conventional | `diff_friendly | `ocamlformat | `janestreet]
elt }
18 changes: 18 additions & 0 deletions test/passing/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4876,6 +4876,24 @@
(package ocamlformat)
(action (diff tests/skip.ml.err skip.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
(action
(with-stdout-to source-diff-friendly.ml.stdout
(with-stderr-to source-diff-friendly.ml.stderr
(run %{bin:ocamlformat} --margin-check --profile=diff-friendly --max-iters=3 %{dep:tests/source.ml})))))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/source-diff-friendly.ml.ref source-diff-friendly.ml.stdout)))

(rule
(alias runtest)
(package ocamlformat)
(action (diff tests/source-diff-friendly.ml.err source-diff-friendly.ml.stderr)))

(rule
(deps tests/.ocamlformat )
(package ocamlformat)
Expand Down
3 changes: 3 additions & 0 deletions test/passing/tests/source-diff-friendly.ml.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Warning: tests/source.ml:5402 exceeds the margin
Warning: tests/source.ml:7184 exceeds the margin
Warning: tests/source.ml:8105 exceeds the margin
2 changes: 2 additions & 0 deletions test/passing/tests/source-diff-friendly.ml.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--profile=diff-friendly
--max-iters=3
Loading
Loading