Skip to content

fixes #26691; fixes #26692; nimpretty keeps the space after a binary operator when it joins lines - #26878

Open
konaito wants to merge 1 commit into
nim-lang:develfrom
konaito:fix-26691-nimpretty-and
Open

konaito wants to merge 1 commit into
nim-lang:develfrom
konaito:fix-26691-nimpretty-and

Conversation

@konaito

@konaito konaito commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

fixes #26691
fixes #26692

When nimpretty joins two lines that have the same indentation, optionalIsGood replaces the indentation of the second line with a separator. The separator was " " only when the line ended in ,; after every other split point it was "". The split points (rememberSplit) are ,/;, an opening bracket, and/or/in/notin, and binary operators, and only the opening bracket is written without a space after it. So a line ending in and was joined as andnode[...] (#26691) and a line ending in & as name &" ...", which is then a prefix & (#26692). Both outputs pass finalCheck because it only parses the result.

Fix: use "" only when the token before the split point is an opening bracket (ltSomeParLe), and " " otherwise.

Test: nimpretty/tests/tjoinlines.nim with the two examples from the issues.

Verification, on macOS arm64:

  • Before the fix, nim r nimpretty/tester.nim fails on tjoinlines (andnode[, name &"); 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). 44 files differ, in 33 lines; in each, the old output had a joined and/or/; without a space (for example nkSym andn[genericParamsPos].kind, zephyr) ordefined(nuttx)), and the new output differs only by that space.
  • The uneven indentation of cert & in nimpretty drops the space after a binary & when it joins lines, so & becomes a prefix operator #26692 is not changed by this PR.

… after a binary operator when it joins lines
Araq pushed a commit that referenced this pull request Dec 5, 2026
… a joined line (#26880)

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 #26878 but not the
same lines; the two merge without conflicts in either order and the
tester passes on the merged tree.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant