Skip to content

Make WithLineWidth(0) disable line wrapping - #528

Open
antoninbas wants to merge 1 commit into
yaml:mainfrom
antoninbas:fix-line-width-zero
Open

antoninbas wants to merge 1 commit into
yaml:mainfrom
antoninbas:fix-line-width-zero

Conversation

@antoninbas

Copy link
Copy Markdown

The WithLineWidth docs say -1 or 0 means unlimited width, but only negative values were normalized to -1. A width of 0 reached the emitter unchanged, and at stream start the emitter replaces any width from 0 to twice the indent with 80. So WithLineWidth(0), and the line-width: 0 option in OptsYAML and the CLI, wrapped at 80 columns.

Store any non-positive width as -1 so 0 behaves as documented, as suggested in #526, and mention 0 in the CLI help for line-width. Reword the comment in isSimpleCollection, since only -1 now reaches its non-positive branch.

The options fixture that expected 0 to be stored as-is now expects -1. New Dump fixtures check that widths 0 and -1 both leave long lines unwrapped, and a new CLI test covers -o width=0.

Fixes #526

Notes for reviewers

  • This changes output compared with v4.0.0 for anyone who passes a width of 0 (through WithLineWidth, OptsYAML or the CLI): long lines are no longer wrapped at 80. That is the documented behavior, but it may be worth a mention in the next release notes.
  • Emitter.SetWidth is unchanged, so the emitter still follows libyaml (0 means the default of 80). The public API can no longer pass it 0.
  • Not addressed here, possible follow-ups:
    • Widths from 1 to twice the indent are still replaced with 80 by the emitter (for example -o line-width=4 wraps at 80 with the default indent of 2). This is not documented.
    • With WithFlowSimpleCollections, an unlimited width (-1, and now 0) still limits flow collections to 80 characters (isSimpleCollection in internal/libyaml/serializer.go), while the option's docs say collections use flow style "if they fit within the line width". This predates this PR. The code could follow the docs, or the docs could mention the 80-character limit; I've only reworded the code comment to describe the current behavior.

The WithLineWidth docs say -1 or 0 means unlimited width, but only
negative values were normalized to -1. A width of 0 reached the
emitter unchanged, and at stream start the emitter replaces any width
from 0 to twice the indent with 80. So WithLineWidth(0), and the
line-width: 0 option in OptsYAML and the CLI, wrapped at 80 columns.

Store any non-positive width as -1 so 0 behaves as documented, and
mention 0 in the CLI help for line-width. Reword the comment in
isSimpleCollection, since only -1 now reaches its non-positive branch.

The options fixture that expected 0 to be stored as-is now expects -1.
New Dump fixtures check that widths 0 and -1 both leave long lines
unwrapped, and a new CLI test covers -o width=0.

Fixes yaml#526

Signed-off-by: Antonin Bas <antonin.bas@gmail.com>
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.

v4: WithLineWidth(0) wraps at 80 columns instead of disabling wrapping

2 participants