Skip to content

Commit

Permalink
don't add newline when already present
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck committed Dec 19, 2023
1 parent d49ca19 commit 3675b8f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ jobs:
- name: Check formatting
run: |
! ./nph --check tests/before
./nph --check tests/after
! ./nph --check tests/before || echo "Failed before check"
./nph --check tests/after || echo "Failed after check"
./nph src
git diff --exit-code
git diff --exit-code || echo "Failed diff"
4 changes: 3 additions & 1 deletion src/nph.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ proc prettyPrint(infile, outfile: string; debug, check, printTokens: bool): int
if conf.errorCounter > 0:
return ErrParseFailed

let output = renderTree(node, conf) & "\n"
var output = renderTree(node, conf)
if not output.endsWith("\n"):
output.add "\n"

if conf.errorCounter > 0:
return ErrParseFailed
Expand Down
28 changes: 20 additions & 8 deletions src/phlexer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ proc bufMatches(L: Lexer; pos: int; chars: string): bool =
true

proc scanMultiLineComment(
L: var Lexer; tok: var Token; start: int; starter, ender: string; endOptional = true
L: var Lexer; tok: var Token; start: int; starter, ender: string; endOptional = false
) =
var pos = start

Expand All @@ -1324,7 +1324,9 @@ proc scanMultiLineComment(

pos += 1

var nesting = 0
var
nesting = 0
ended = false
while true:
if L.buf[pos] == nimlexbase.EndOfFile:
if not endOptional:
Expand All @@ -1334,22 +1336,32 @@ proc scanMultiLineComment(
if L.bufMatches(pos, starter):
nesting += 1
elif L.bufMatches(pos, ender):
if nesting == 0:
tok.literal.add ender
if nesting <= 0:
if endOptional:
ended = true
else:
tok.literal.add ender

pos += ender.len
pos += ender.len

break
break

nesting -= 1

tok.literal.add L.buf[pos]
if L.buf[pos] in {CR, LF}:
if ended:
break

tok.literal.add L.buf[pos]
pos = handleCRLF(L, pos)
else:
tok.literal.add L.buf[pos]
pos += 1

tokenEnd(tok, pos - 1)
if ended:
tokenEnd(tok, pos)
else:
tokenEnd(tok, pos - 1)

L.bufpos = pos

Expand Down
1 change: 0 additions & 1 deletion tests/after/fmtofffile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
proc uglyFormat(hangingIndent: int,
isVery: bool,
ugly = true)

2 changes: 1 addition & 1 deletion tests/after/fmtofffile.nim.nph.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kind: "nkStmtList"
sons:
- kind: "nkCommentStmt"
"comment": "#!fmt: off\u000A\u000Aproc uglyFormat(hangingIndent: int,\u000A isVery: bool,\u000A ugly = true)\u000A\u000A"
"comment": "#!fmt: off\u000A\u000Aproc uglyFormat(hangingIndent: int,\u000A isVery: bool,\u000A ugly = true)\u000A"

0 comments on commit 3675b8f

Please sign in to comment.