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

Separator before closing? #11

Open
yunxing opened this issue Mar 12, 2016 · 3 comments
Open

Separator before closing? #11

yunxing opened this issue Mar 12, 2016 · 3 comments

Comments

@yunxing
Copy link

yunxing commented Mar 12, 2016

Is it possible to support adding a separator before closing so we can support trailing comma like this?

[
  1,
  2,
];

or

[ 1, 2, ]

?

@jordwalke
Copy link
Collaborator

This might be a dupe of this issue: #2

@mjambon
Copy link
Member

mjambon commented Mar 13, 2016

If the trailing comma should always be printed no matter how the code gets formatted, it's much simpler than issue #2.

Currently, one way of achieving this is:

#use "topfind";;
#require "easy-format";;

open Easy_format

let add_trailing_string x s =
  List (
    ("", "", "", { list with
                   space_after_opening = false;
                   space_after_separator = false;
                   space_before_closing = false;
                   wrap_body = `No_breaks }),
    [x; Atom (s, atom)]
  )

let () =
  let element = Atom (String.make 90 'a', atom) in
  let element_with_sticky_comma = add_trailing_string element "," in
  let elements =
    List (
      ("[", "", "]", list),
      [element_with_sticky_comma; element_with_sticky_comma]
    ) in
  Pretty.to_stdout elements

Output:

[
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
]

It looks like a good idea to add a trailing_separator option to list_param to avoid having to go through this.

@pieterv
Copy link

pieterv commented Dec 29, 2016

@mjambon for my usecase (printing JavaScript) we need to only print the trailing comma if we break. I tried to implement it with the above approach and using Format.pp_print_if_newline inside a Custom node but i was unable to get it to break correctly, it would never print if part of the list and if i moved it to after the list it would always print :/

This is what i was using:

let string_on_break s =>
    Custom (
      fun fmt => {
        Format.pp_print_if_newline fmt ();
        Format.pp_print_string fmt s
      }
    );

I did manage to implement with a hack (as suggested by @jordwalke, but not a good long term solution) where we insert a token in places we potental want trailing commas and then after printing from EasyFormat to string we find the instances of the token followed by a "\n" and replace with ",\n" otherwise strip it. It mostly works but does mess with the indentation a little since you remove chars after doing all the breaking.

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

No branches or pull requests

4 participants