Skip to content

Commit

Permalink
avoid spurious single-line commas
Browse files Browse the repository at this point in the history
Somewhat defensively, nph was putting a comma on a new line after
certain complex constructs, in particular when using a complex
expression in a parameter list - it turns out that this comma can stay
on the same line most of the time, as long as an `nkCommand` is not
involved
  • Loading branch information
arnetheduck committed Jul 18, 2024
1 parent 86fc4cd commit 2d36d7d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 21 deletions.
5 changes: 2 additions & 3 deletions format-git-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ git ls-files | grep ".nim$" | xargs -n1 nph

! git diff --exit-code > /dev/null || { echo "This repository is already formatted" ; exit 0 ; }

git commit -am "Formatted with nph $(nph --version)"
git commit -am "Formatted with nph $(nph --version)" --author "nph <[email protected]>"

echo "# Formatted with nph $(nph --version)" >> .git-blame-ignore-revs
echo "$(git rev-parse HEAD)" >> .git-blame-ignore-revs

git add .git-blame-ignore-revs
git commit -m "Add $(git rev-parse HEAD) to .git-blame-ignore-revs"
git commit -m "Add $(git rev-parse HEAD) to .git-blame-ignore-revs" --author "nph <[email protected]>"

echo "The repo has been updated with two commits recording the reformatting and blame information."
echo "You can review the changes with 'git diff HEAD^^' before pushing to a public repository."
echo "If you don't want blame information, remove the last commit with 'git reset --hard HEAD^'."

4 changes: 2 additions & 2 deletions nph.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ proc commitProject(

try:
exec "git checkout -b nph"
exec "git commit -am \"Formatted with nph $(nph --version)\""
exec "git commit -am \"Formatted with nph $(nph --version)\" --author \"nph <[email protected]>\""
except:
exec "git checkout nph"
exec "git commit -am \"Formatted with nph $(nph --version)\" --amend"
exec "git commit -am \"Formatted with nph $(nph --version)\" --amend --author \"nph <[email protected]>\""

cd "../.."

Expand Down
9 changes: 3 additions & 6 deletions src/phrenderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,8 @@ proc gsonsNL(
) =
for i in start .. n.len + theEnd:
gsub(g, n[i], flags)
g.optNL()
if i != n.len + theEnd:
g.optNL()

proc glist(
g: var TOutput,
Expand Down Expand Up @@ -977,10 +978,6 @@ proc gstmts(g: var TOutput, n: PNode, flags: SubFlags = {}, doIndent = true) =

dedent(g, ind)

if not trivial:
optNL(g)
# if not trivial:
# optNL(g)
if needsPar:
if not trivial:
optNL(g)
Expand Down Expand Up @@ -1095,7 +1092,7 @@ proc gif(g: var TOutput, n: PNode, flags: SubFlags) =
if oneline:
# We end up here when rendering things that were parsed as expressions but
# don't match the "trivial" rule above
# TODO treat such constructs as trivail for better determinism?
# TODO treat such constructs as trivial for better determinism?
gsons(g, n, 1)
else:
optNL(g)
Expand Down
3 changes: 1 addition & 2 deletions tests/after/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,7 @@ discard
54 # discard value

proc x() =
discard
# proc, impl and comment on one line
discard # proc, impl and comment on one line

proc xxx() = # after proc before indented name
discard
Expand Down
4 changes: 2 additions & 2 deletions tests/after/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,8 @@ sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkCommentStmt"
"comment": "# proc, impl and comment on one line"
postfix:
- "# proc, impl and comment on one line"
- kind: "nkProcDef"
mid:
- "# after proc before indented name"
Expand Down
3 changes: 1 addition & 2 deletions tests/after/postexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,4 @@ else:

discard (
aaa.bbb.exec do(res: int64):
size = res
).ccc()
size = res).ccc()
3 changes: 1 addition & 2 deletions tests/after/procs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ type Ep = proc {.nimcall.}
type Fp = proc(
aaaaaaaaaaaaaaaaa: int,
bbbbbbbbbbbbbbb = proc() =
discard
,
discard,
cccccccccccccccccc = 30,
)

Expand Down
3 changes: 1 addition & 2 deletions tests/after/style.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ functionCall(10000000, 2000000000, 3000000000, 40000000, 5000000000)
proc f(
myParameter = 0,
callback: SomeCallback = proc() =
discard
,
discard,
nextParameter = 1,
)

Expand Down

0 comments on commit 2d36d7d

Please sign in to comment.