Skip to content

Commit

Permalink
reorder proc postfix doc comments with statement (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck authored Jan 4, 2024
1 parent bfc7db5 commit 9506b29
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/phrenderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,14 @@ proc gcase(g: var TOutput, n: PNode) =
optNL(g)
gsonsNL(g, n, start = 1)

proc reorderPostfix(n: PNode): bool =
# Because we move the body to a new line, we must also reorder postfix doc
# comments or they end up becoming the last statement in the proc and
# thus breaking it
# TODO enable AST checking for nkCommentStmt to catch this :/
n.kind in procDefs and n.postfix.len > 0 and isDocComment(n.postfix[0].literal) and
n[bodyPos].kind != nkEmpty

proc gproc(g: var TOutput, n: PNode) =
gsub(g, n[namePos])

Expand All @@ -1219,9 +1227,15 @@ proc gproc(g: var TOutput, n: PNode) =
withIndent(g):
g.optNL()
gmids(g, n)

if reorderPostfix(n):
gpostfixes(g, n)
else:
gmids(g, n, true)

if reorderPostfix(n):
gpostfixes(g, n)

gstmts(g, n[bodyPos], flags)
else:
withIndent(g):
Expand Down Expand Up @@ -2186,9 +2200,8 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
#nkNone, nkExplicitTypeListCall:
internalError(g.config, n.info, "renderer.gsub(" & $n.kind & ')')

if sfSkipPostfix notin flags:
if sfSkipPostfix notin flags and not reorderPostfix(n):
const stickyPostfix = {nkCommand}

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

if n.kind in blankAfterComplex and currLine < g.line:
Expand Down
9 changes: 7 additions & 2 deletions tests/after/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ proc x() =
discard

proc x() =
discard
## indented doc comment for proc
## that is long
discard

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

proc f(): bool =
## Comment here
Expand Down Expand Up @@ -372,6 +372,11 @@ proc a(
abc: int,
)

proc a(): int =
## Doc comment that needs body reordering
## even two lines again
42

command "a", "b", "c" # command eol comment

command "first arg", # first arg comment
Expand Down
34 changes: 27 additions & 7 deletions tests/after/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1221,13 +1221,13 @@ sons:
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkCommentStmt"
"comment": "## indented doc comment for proc"
- kind: "nkCommentStmt"
"comment": "## that is long"
- kind: "nkDiscardStmt"
sons:
- kind: "nkEmpty"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
Expand All @@ -1241,6 +1241,10 @@ sons:
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkCommentStmt"
"comment": "## indented doc comment for proc"
- kind: "nkCommentStmt"
"comment": "## with a dotexpr and a command"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand All @@ -1251,10 +1255,6 @@ sons:
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 @@ -1404,6 +1404,26 @@ sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
ident: "a"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkIdent"
ident: "int"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkCommentStmt"
"comment": "## Doc comment that needs body reordering"
- kind: "nkCommentStmt"
"comment": "## even two lines again"
- kind: "nkIntLit"
intVal: 42
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand Down
4 changes: 4 additions & 0 deletions tests/before/comments.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ proc f =

proc a(v#[block]#: int, abc: int)

proc a(): int = 42
## Doc comment that needs body reordering
## even two lines again

command "a", "b", "c" # command eol comment

command "first arg", # first arg comment
Expand Down
19 changes: 19 additions & 0 deletions tests/before/comments.nim.nph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,25 @@ sons:
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkProcDef"
sons:
- kind: "nkIdent"
ident: "a"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkFormalParams"
sons:
- kind: "nkIdent"
ident: "int"
- kind: "nkEmpty"
- kind: "nkEmpty"
- kind: "nkStmtList"
sons:
- kind: "nkIntLit"
intVal: 42
postfix:
- "## Doc comment that needs body reordering"
- "## even two lines again"
- kind: "nkCommand"
sons:
- kind: "nkIdent"
Expand Down

0 comments on commit 9506b29

Please sign in to comment.