Repository navigation
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
Open
fixes #26691; fixes #26692; nimpretty keeps the space after a binary operator when it joins lines#26878konaito wants to merge 1 commit into
konaito wants to merge 1 commit into
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #26691
fixes #26692
When nimpretty joins two lines that have the same indentation,
optionalIsGoodreplaces 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 inandwas joined asandnode[...](#26691) and a line ending in&asname &" ...", which is then a prefix&(#26692). Both outputs passfinalCheckbecause 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.nimwith the two examples from the issues.Verification, on macOS arm64:
nim r nimpretty/tester.nimfails ontjoinlines(andnode[,name &"); after the fix all files are identical, including the pretty(pretty(x)) runs.compiler/*.nim,lib/pure/*.nimandlib/std/*.nim(307 files). 44 files differ, in 33 lines; in each, the old output had a joinedand/or/;without a space (for examplenkSym andn[genericParamsPos].kind,zephyr) ordefined(nuttx)), and the new output differs only by that space.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.