diff --git a/nph.nimble b/nph.nimble index 59ddf6d..712246f 100644 --- a/nph.nimble +++ b/nph.nimble @@ -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": diff --git a/src/phparser.nim b/src/phparser.nim index 7443205..0c94c87 100644 --- a/src/phparser.nim +++ b/src/phparser.nim @@ -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) diff --git a/src/phrenderer.nim b/src/phrenderer.nim index 7328e6e..dc3e3eb 100644 --- a/src/phrenderer.nim +++ b/src/phrenderer.nim @@ -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 @@ -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) @@ -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() diff --git a/tests/after/comments.nim b/tests/after/comments.nim index 267bb53..b073b97 100644 --- a/tests/after/comments.nim +++ b/tests/after/comments.nim @@ -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 @@ -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 diff --git a/tests/after/comments.nim.nph.yaml b/tests/after/comments.nim.nph.yaml index 939da45..dcf8932 100644 --- a/tests/after/comments.nim.nph.yaml +++ b/tests/after/comments.nim.nph.yaml @@ -1075,8 +1075,6 @@ sons: - "# const eol" sons: - kind: "nkConstDef" - mid: - - "# const ident after type" sons: - kind: "nkIdent" ident: "v" @@ -1088,6 +1086,7 @@ sons: ident: "int" - kind: "nkIntLit" prefix: + - "# const ident after type" - "# const ident after equals" intVal: 42 postfix: @@ -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" @@ -1386,8 +1412,8 @@ sons: strVal: "b" - kind: "nkStrLit" strVal: "c" - postfix: - - "# command eol comment" + postfix: + - "# command eol comment" - kind: "nkCommand" sons: - kind: "nkIdent" @@ -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" @@ -1418,8 +1444,8 @@ sons: ident: "command" - kind: "nkIntLit" intVal: 234 - postfix: - - "# command after ind" + postfix: + - "# command after ind" - kind: "nkWhenStmt" sons: - kind: "nkElifBranch" @@ -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" @@ -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" diff --git a/tests/before/comments.nim b/tests/before/comments.nim index 8c95a6b..bafe6e4 100644 --- a/tests/before/comments.nim +++ b/tests/before/comments.nim @@ -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 diff --git a/tests/before/comments.nim.nph.yaml b/tests/before/comments.nim.nph.yaml index be8d11d..0ab70d9 100644 --- a/tests/before/comments.nim.nph.yaml +++ b/tests/before/comments.nim.nph.yaml @@ -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" @@ -1385,8 +1411,8 @@ sons: strVal: "b" - kind: "nkStrLit" strVal: "c" - postfix: - - "# command eol comment" + postfix: + - "# command eol comment" - kind: "nkCommand" sons: - kind: "nkIdent" @@ -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" @@ -1417,8 +1443,8 @@ sons: ident: "command" - kind: "nkIntLit" intVal: 234 - postfix: - - "# command after ind" + postfix: + - "# command after ind" - kind: "nkWhenStmt" sons: - kind: "nkElifBranch" @@ -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" @@ -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"