Skip to content

fixes #26693; nimpretty no longer writes an empty line when it splits a joined line - #26880

Merged
Araq merged 1 commit into
nim-lang:develfrom
konaito:fix-26693-nimpretty-wrap
Dec 5, 2026
Merged

Araq merged 1 commit into
nim-lang:develfrom
konaito:fix-26693-nimpretty-wrap

Conversation

@konaito

@konaito konaito commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

fixes #26693

When optionalIsGood joins two lines that have the same indentation, it turns the newline between them into an ltOptionalNewline, but the token stays "\n". The token of an optional newline is the indentation that renderTokens writes when it splits the line there. So when the joined line was split at that position again, nimpretty wrote "\L" & "\n": an empty line, then the rest of the expression at column 0. Inside brackets the output still parses; in an indented block it fails finalCheck with invalid indentation.

Fix: when the newline becomes an optional newline, set its token to the indentation of the joined line.

Test: nimpretty/tests/twrappedexpr.nim with the two examples from the issue.

Verification, on macOS arm64:

  • Before the fix, nim r nimpretty/tester.nim fails on twrappedexpr (invalid indentation); after the fix all files are identical, including the pretty(pretty(x)) runs.
  • The expected output compiles and prints the same as the input.
  • I ran nimpretty before and after the fix over compiler/*.nim, lib/pure/*.nim and lib/std/*.nim (307 files). The 284 files that both versions format are byte-identical. 5 files that failed before with (line, 1) Error: invalid indentation now format (compiler/docgen2.nim, jsgen.nim, lexer.nim, main.nim, varpartitions.nim). 18 files fail with both versions; in 3 of them the column-1 error is gone and a later, different error remains.
  • The output is not exactly the "Expected Output" of the issue: the deeper indentation of the last continuation line is existing behaviour and is not changed by this PR.
  • This touches the same optionalIsGood block as fixes #26691; fixes #26692; nimpretty keeps the space after a binary operator when it joins lines #26878 but not the same lines; the two merge without conflicts in either order and the tester passes on the merged tree.

…t splits a joined line

When optionalIsGood joins two lines it turns the newline between them into an optional newline, but kept its token "\n". An optional newline's token is the indentation written when the line is split there, so splitting at that position wrote an empty line and put the rest at column 0. Now the token becomes the indentation of the joined line.
@Araq Araq added the merge_when_passes_CI mergeable once green label Dec 4, 2026
@Araq
Araq merged commit 420817d into nim-lang:devel Dec 5, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge_when_passes_CI mergeable once green

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nimpretty writes an empty line and moves part of a wrapped expression to column 0

2 participants