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

Possible bug in Format #7862

Open
vicuna opened this issue Oct 9, 2018 · 8 comments

Comments

Projects
None yet
1 participant
@vicuna
Copy link

commented Oct 9, 2018

Original bug ID: 7862
Reporter: @nojb
Status: new
Resolution: open
Priority: normal
Severity: minor
Category: standard library
Related to: #7807
Monitored by: @nojb jmeber @alainfrisch

Bug description

The second call to "print_space" in "Steps To Reproduce" generates a trailing space even though the line is broken. Moreover, removing either

print_string "qyyyyyyyy";
print_space ();

or

open_box 0;
close_box ();

results in "correct" behavior (the line is not broken).

Steps to reproduce

open Format
let () =
open_box 0;
print_string "qyyyyyyyy";
print_space ();
print_string "qzzzzzzzzzzzzzzzzzzzqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzfffaaq";
print_space ();
open_box 0;
close_box ();
print_string "q";
close_box ()

Additional information

Output (there is a trailing space in the second line):

qyyyyyyyy
qzzzzzzzzzzzzzzzzzzzqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzfffaaq
q

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 9, 2018

Comment author: @alainfrisch

Note that the doc says:

  [pp_print_space ppf ()] emits a 'space' break hint:
  the pretty-printer may split the line at this point,
  otherwise it prints one space.

which indeed suggests that the expected behavior is that the space should not be printed when the pretty-printer does split the line (and this is what usually happens).

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 9, 2018

Comment author: bvaugon

It seems that there is two problems: an "unwanted trailing space" and a "suprising line break".

Concerning the "surprising line break" that is automatically inserted by the open_box once the current line size exceed 68 characters (68 = default_margin - default_min_space_left), it is configurable through the "min_space_left" property:

(* Minimal space left before margin, when opening a box. *)
mutable pp_min_space_left : int;

using the Format.set_max_indent function :

set_max_indent 77;

where max_indent = margin - space_left.

Remarks that calling pp_set_max_indent with an argument greater or equal than margin has no effect.

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 9, 2018

Comment author: @alainfrisch

I think the only problem here is the unwanted trailing space.

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 9, 2018

Comment author: @nojb

I am not sure about that, because if I remove the code that prints the first line, then there is no break between second and third line, which tells me that there is enough space and a break is not warranted there.

In other words: maybe the trailing space is correct and the problem is the break that shouldn't happen.

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 9, 2018

Comment author: bvaugon

In fact, when you remove the code that prints the first line, there is no break but it should be (or the documentation should be fixed). Indeed, opening a box after the 68th character should not be allowed following the comment "pp_min_space_left: minimal space left before margin when opening a box".

Furthermore (I don't know if there is a link with the other problems), opening a box at this positions seems to break vertical alignement like in the text printed by the following code:

open Format;
let () =
open_box 0;
print_string "q";
print_space ();
print_string "qzzzzzzzzzzzzzzzzzzzqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzfffaaa";
print_space ();
open_vbox 0;
print_string "A";
print_space ();
print_string "B";
close_box ();
print_space ();
print_string "q";
close_box ();

q qzzzzzzzzzzzzzzzzzzzqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzfffaaa A
B q

A and B should be aligned.

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 10, 2018

Comment author: @Octachron

This is due to the current behavior of max_indent: it rejects to the left boxes opened beyond max_indent (if the enclosing box does not fit on the line). In particular, it does not try to understand if the opened box could fit on the line, see #7807 .

bvaugon, for your second example, this is due to the fact that max_indent limits the maximum indentation of the value.

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 9, 2018

Comment author: @alainfrisch

Guys, do you think the only "problem" is the overly preventive heuristics mentioned in #7807, and that using a more clever approach there would avoid all cases where pp_print_space currently prints a space even though the line is broken?

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 9, 2018

Comment author: @Octachron

I am not sure for all cases, but a better integration of the max_indent limit or a way to disable it would solve this specific issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.