Skip to content

Remove trailing space after expression when followed by an attribute and break before attributes attached to multi-line phrases#1382

Merged
gpetiot merged 6 commits intoocaml-ppx:masterfrom
gpetiot:remove-trailing-space-attr
Jun 30, 2020
Merged

Remove trailing space after expression when followed by an attribute and break before attributes attached to multi-line phrases#1382
gpetiot merged 6 commits intoocaml-ppx:masterfrom
gpetiot:remove-trailing-space-attr

Conversation

@gpetiot
Copy link
Copy Markdown
Collaborator

@gpetiot gpetiot commented May 27, 2020

fix #1356
I had to change the kind of boxes in a lot of places to maintain the current behavior of wrapping the attribute at the end of the previous line when possible.
But fmt_attributes c ~pre:(str " ") is still used in a lot of places so there must be more issues like this, @jberdine was this pre intended? Should we preserve the current behavior or is it okay to put the attribute in the next line instead of wrapping at the end of the previous line?

@jberdine
Copy link
Copy Markdown
Collaborator

jberdine commented Jun 9, 2020

Just for the record: I do not consider trailing spaces to be a "bug". It's nice not to generate them, but it is really not something that I would want to complicate the code too much over. But that is just my opinion.

I do not recall any particular need to place attributes on the same line instead of letting a newline appear before them. But IIRC @hhugo added most of the handling of attributes since early on attributes were just dropped. Any recollection Hugo?

@hhugo
Copy link
Copy Markdown
Collaborator

hhugo commented Jun 10, 2020

I do not recall any reason

@gpetiot gpetiot marked this pull request as ready for review June 15, 2020 18:15
@gpetiot
Copy link
Copy Markdown
Collaborator Author

gpetiot commented Jun 15, 2020

I extended the change to more constructs to have an idea of what it will look like. Most of the time having the attributes at the beginning of the line make it easier to read and see to what it is attached to.

@gpetiot gpetiot requested review from Julow, hhugo and jberdine June 15, 2020 18:20
Comment on lines +224 to +225
@ b________________________________________________________________ )
[@a]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this attribute should fit. If not, this test isn't checking the property it was intended for anymore and should be modified.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test still wants either modified or removed. I introduced it as part of #1306 to guard against a bug where the attribute was split across multiple lines on wrap.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I don't have a strong position either way. @gpetiot feel free to include a change to this test in this PR as you see fit.

Copy link
Copy Markdown
Collaborator

@jberdine jberdine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the interaction with ocp-indent is important, but the rest of this PR is good.

@gpetiot gpetiot changed the title Remove trailing space after expression when followed by an attribute Remove trailing space after expression when followed by an attribute and break before attributes attached to multi-line phrases Jun 27, 2020
@gpetiot
Copy link
Copy Markdown
Collaborator Author

gpetiot commented Jun 27, 2020

This change is now only applied if ocp-indent-compat is not set so there won't be regressions on JaneStreet codebase.

@gpetiot gpetiot requested a review from jberdine June 27, 2020 13:31
lib/Fmt_ast.ml Outdated

(* Breaking before an attribute can confuse ocp-indent that will produce a
suboptimal indentation. *)
let pre_attributes c = fmt_or c.conf.ocp_indent_compat " " "@ "
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry for not thinking of this suggestion before, but I am inclined to try a different implementation, where instead of adding the pre_attributes function, fmt_attributes is changed to test if pre is "@ " and if so change it to " ". That will more tightly localize the mess made by ocp-indent compatibility.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it looks better indeed. No regression with test_branch.

@gpetiot gpetiot requested a review from jberdine June 30, 2020 12:24
Copy link
Copy Markdown
Collaborator

@jberdine jberdine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good.

@gpetiot gpetiot merged commit 111754f into ocaml-ppx:master Jun 30, 2020
@gpetiot gpetiot deleted the remove-trailing-space-attr branch June 30, 2020 14:22
emillon added a commit to emillon/opam-repository that referenced this pull request Aug 6, 2020
CHANGES:

#### Changes

  + Do not break inline elements such as `{i blah}` in docstrings (ocaml-ppx/ocamlformat#1346, @jberdine)

  + Distinguish hash-getter from hash-comparison infix operators. Operators of the form `#**#` or `#**.` where `**` can be 0 or more operator chars are considered getter operators and are not surrounded by spaces, as opposed to regular infix operators (ocaml-ppx/ocamlformat#1376, @gpetiot)

  + Type constraint on return type of functions is now always printed before the function body (ocaml-ppx/ocamlformat#1381, ocaml-ppx/ocamlformat#1397, @gpetiot)

#### Bug fixes

  + Restore previous functionality for pre-post extension points (ocaml-ppx/ocamlformat#1342, @jberdine)

  + Fix extra break before `function` body of a `fun` (ocaml-ppx/ocamlformat#1343, @jberdine)
    Indent further args of anonymous functions (ocaml-ppx/ocamlformat#1440, @gpetiot)

  + Do not clear the emacs `*compilation*` buffer on successful reformat (ocaml-ppx/ocamlformat#1350, @jberdine)

  + Fix disabling with attributes on OCaml < 4.08 (ocaml-ppx/ocamlformat#1322, @emillon)

  + Preserve unwrapped comments by not adding artificial breaks when `wrap-comments=false` and `ocp-indent-compat=true` are set to avoid interfering with ocp-indent indentation. (ocaml-ppx/ocamlformat#1352, @gpetiot)

  + Break long literal strings at the margin (ocaml-ppx/ocamlformat#1367, @gpetiot)

  + Break after a multiline argument in an argument list (ocaml-ppx/ocamlformat#1360, @gpetiot)

  + Remove unnecessary parens around object (ocaml-ppx/ocamlformat#1379, @gpetiot)

  + Fix placement of comments on constants (ocaml-ppx/ocamlformat#1383, @gpetiot)

  + Do not escape arguments of some Odoc tags (ocaml-ppx/ocamlformat#1391, 1408, @gpetiot, @Julow)
    The characters `[]{}` must not be escaped in the arguments of `@raise`, `@author`, `@version` and others.

  + Fix missing open line between multi-line let-binding with poly-typexpr (ocaml-ppx/ocamlformat#1372, @jberdine)

  + Remove trailing space after expression when followed by an attribute and break before attributes attached to multi-line phrases (ocaml-ppx/ocamlformat#1382, @gpetiot)

  + Do not add a space to minimal comments `(* *)`, `(** *)` and `(*$ *)` (ocaml-ppx/ocamlformat#1407, @gpetiot)

  + Fix attributes position in labelled arguments type (ocaml-ppx/ocamlformat#1434, @gpetiot)

  + Add missing parens around type annotation in anonymous function (ocaml-ppx/ocamlformat#1433, @gpetiot)

  + Fix alignment of 'then' keyword in parenthesised expression (ocaml-ppx/ocamlformat#1421, @gpetiot)

#### New features

  + Support quoted extensions (added in ocaml 4.11) (ocaml-ppx/ocamlformat#1405, @gpetiot)

  + Recognise eliom file extensions (ocaml-ppx/ocamlformat#1430, @jrochel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: trailing spaces introduced for expressions with attributes

6 participants