Skip to content

Commit

Permalink
attach postfix comments to command instead of param (#25)
Browse files Browse the repository at this point in the history
100% nim std lib formatting compatibility!
  • Loading branch information
arnetheduck authored Dec 27, 2023
1 parent 1e58fd9 commit 16749f6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 31 deletions.
6 changes: 3 additions & 3 deletions nph.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ proc commitProject(
cd "../.."

const projects = [
("Nim", "https://github.com/arnetheduck/Nim.git", "version-2-0", ["compiler"]),
("nim-results", "https://github.com/arnetheduck/nim-results.git", "master", [""]),
("nimbus-eth2", "https://github.com/status-im/nimbus-eth2.git", "unstable", [""]),
("Nim", "https://github.com/arnetheduck/Nim.git", "version-2-0", @["compiler", "lib"]),
("nim-results", "https://github.com/arnetheduck/nim-results.git", "master", @[""]),
("nimbus-eth2", "https://github.com/status-im/nimbus-eth2.git", "unstable", @[""]),
]

task play, "Format several popular projects":
Expand Down
4 changes: 0 additions & 4 deletions src/phparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1764,10 +1764,6 @@ proc parseExprStmt(p: var Parser): PNode =
getTok(p)
splitLookahead(p, result[^1], clPostfix)
optInd(p, result)
# This attaches any eol comments to the last argument instead of the
# command which may or may not be a good idea, specially if those comments
# are doc comments
splitLookahead(p, result[^1], baseIndent, clPostfix)
else:
result = a
result = postExprBlocks(p, result)
Expand Down
11 changes: 10 additions & 1 deletion src/phrenderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type
SubFlags = set[SubFlag]
TOutput = TSrcGen | TSrcLen

proc isDocComment(s: string): bool =
s.startsWith("##")

proc flagIndent(flags: SubFlags): int =
if sfNoIndent in flags:
0
Expand Down Expand Up @@ -693,6 +696,10 @@ proc gextras(g: var TSrcGen, toks: openArray[Token], indented, firstSticky: bool
if indented:
optIndent(g)

## We must place doc comments on the same line as certain nodes or the
## optInd parsing breaks
let firstSticky = firstSticky and isDocComment($toks[0])

if long and not firstSticky:
optNL(g)

Expand Down Expand Up @@ -2192,7 +2199,9 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
internalError(g.config, n.info, "renderer.gsub(" & $n.kind & ')')

if sfSkipPostfix notin flags:
gpostfixes(g, n)
const stickyPostfix = {nkCommand}

gpostfixes(g, n, n.kind in stickyPostfix)

if n.kind in blankAfterComplex and currLine < g.line:
g.blankLine()
Expand Down
8 changes: 7 additions & 1 deletion tests/after/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ const # const eol
v # const ident after symbol
:
# const ident after colon
int = # const ident after type
int =
# const ident after type
# const ident after equals
42 # const ident after value

Expand Down Expand Up @@ -329,6 +330,11 @@ proc x() =
## indented doc comment for proc
## that is long

proc x() =
echo a.b
## indented doc comment for proc
## with a dotexpr and a command

proc f(): bool =
## Comment here
## another
Expand Down
50 changes: 38 additions & 12 deletions tests/after/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1075,8 +1075,6 @@ sons:
- "# const eol"
sons:
- kind: "nkConstDef"
mid:
- "# const ident after type"
sons:
- kind: "nkIdent"
ident: "v"
Expand All @@ -1088,6 +1086,7 @@ sons:
ident: "int"
- kind: "nkIntLit"
prefix:
- "# const ident after type"
- "# const ident after equals"
intVal: 42
postfix:
Expand Down Expand Up @@ -1227,6 +1226,33 @@ sons:
"comment": "## indented doc comment for proc"
- kind: "nkCommentStmt"
"comment": "## that is long"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
ident: "x"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkCommand"
sons:
- kind: "nkIdent"
ident: "echo"
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "a"
- kind: "nkIdent"
ident: "b"
- kind: "nkCommentStmt"
"comment": "## indented doc comment for proc"
- kind: "nkCommentStmt"
"comment": "## with a dotexpr and a command"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
Expand Down Expand Up @@ -1386,8 +1412,8 @@ sons:
strVal: "b"
- kind: "nkStrLit"
strVal: "c"
postfix:
- "# command eol comment"
postfix:
- "# command eol comment"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand All @@ -1402,8 +1428,8 @@ sons:
- "# second arg comment"
- kind: "nkStrLit"
strVal: "third arg"
postfix:
- "# third arg comment"
postfix:
- "# third arg comment"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand All @@ -1418,8 +1444,8 @@ sons:
ident: "command"
- kind: "nkIntLit"
intVal: 234
postfix:
- "# command after ind"
postfix:
- "# command after ind"
- kind: "nkWhenStmt"
sons:
- kind: "nkElifBranch"
Expand Down Expand Up @@ -1472,8 +1498,8 @@ sons:
ident: "dotexpr"
- kind: "nkIdent"
ident: "dot"
postfix:
- "# after dotexpr in command"
postfix:
- "# after dotexpr in command"
- kind: "nkCommentStmt"
"comment": "# between two dotepxrs"
- kind: "nkDotExpr"
Expand Down Expand Up @@ -1502,8 +1528,8 @@ sons:
ident: "dotexpr"
- kind: "nkIdent"
ident: "dot"
postfix:
- "# after dotexpr in command ind"
postfix:
- "# after dotexpr in command ind"
- kind: "nkCommentStmt"
"comment": "# between two dotepxrs ind"
- kind: "nkDotExpr"
Expand Down
4 changes: 4 additions & 0 deletions tests/before/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ proc x = discard
## indented doc comment for proc
## that is long

proc x = echo a.b
## indented doc comment for proc
## with a dotexpr and a command

proc f: bool =
## Comment here
## another
Expand Down
46 changes: 36 additions & 10 deletions tests/before/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,32 @@ sons:
postfix:
- "## indented doc comment for proc"
- "## that is long"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
ident: "x"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkCommand"
sons:
- kind: "nkIdent"
ident: "echo"
- kind: "nkDotExpr"
sons:
- kind: "nkIdent"
ident: "a"
- kind: "nkIdent"
ident: "b"
postfix:
- "## indented doc comment for proc"
- "## with a dotexpr and a command"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
Expand Down Expand Up @@ -1385,8 +1411,8 @@ sons:
strVal: "b"
- kind: "nkStrLit"
strVal: "c"
postfix:
- "# command eol comment"
postfix:
- "# command eol comment"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand All @@ -1401,8 +1427,8 @@ sons:
- "# second arg comment"
- kind: "nkStrLit"
strVal: "third arg"
postfix:
- "# third arg comment"
postfix:
- "# third arg comment"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand All @@ -1417,8 +1443,8 @@ sons:
ident: "command"
- kind: "nkIntLit"
intVal: 234
postfix:
- "# command after ind"
postfix:
- "# command after ind"
- kind: "nkWhenStmt"
sons:
- kind: "nkElifBranch"
Expand Down Expand Up @@ -1471,8 +1497,8 @@ sons:
ident: "dotexpr"
- kind: "nkIdent"
ident: "dot"
postfix:
- "# after dotexpr in command"
postfix:
- "# after dotexpr in command"
- kind: "nkCommentStmt"
"comment": "# between two dotepxrs"
- kind: "nkDotExpr"
Expand Down Expand Up @@ -1501,8 +1527,8 @@ sons:
ident: "dotexpr"
- kind: "nkIdent"
ident: "dot"
postfix:
- "# after dotexpr in command ind"
postfix:
- "# after dotexpr in command ind"
- kind: "nkCommentStmt"
"comment": "# between two dotepxrs ind"
- kind: "nkDotExpr"
Expand Down

0 comments on commit 16749f6

Please sign in to comment.