diff options
Diffstat (limited to 'nimpretty')
-rw-r--r-- | nimpretty/nimpretty.nim | 128 | ||||
-rw-r--r-- | nimpretty/tester.nim | 30 | ||||
-rw-r--r-- | nimpretty/tests/exhaustive.nim | 22 | ||||
-rw-r--r-- | nimpretty/tests/expected/exhaustive.nim | 23 | ||||
-rw-r--r-- | nimpretty/tests/expected/simple.nim | 16 | ||||
-rw-r--r-- | nimpretty/tests/simple.nim | 16 |
6 files changed, 184 insertions, 51 deletions
diff --git a/nimpretty/nimpretty.nim b/nimpretty/nimpretty.nim index 9b5cf556b..e5abf0d2d 100644 --- a/nimpretty/nimpretty.nim +++ b/nimpretty/nimpretty.nim @@ -12,9 +12,9 @@ when not defined(nimpretty): {.error: "This needs to be compiled with --define:nimPretty".} -import ../compiler / [idents, msgs, syntaxes, options, pathutils, layouter] +import ../compiler / [idents, llstream, ast, msgs, syntaxes, options, pathutils, layouter] -import parseopt, strutils, os +import parseopt, strutils, os, sequtils const Version = "0.2" @@ -22,9 +22,10 @@ const (c) 2017 Andreas Rumpf Usage: - nimpretty [options] file.nim + nimpretty [options] nimfiles... Options: --out:file set the output file (default: overwrite the input file) + --outDir:dir set the output dir (default: overwrite the input files) --indent:N[=0] set the number of spaces that is used for indentation --indent:0 means autodetection (default behaviour) --maxLineLen:N set the desired maximum line length (default: 80) @@ -43,55 +44,124 @@ proc writeVersion() = quit(0) type - PrettyOptions = object - indWidth: Natural - maxLineLen: Positive + PrettyOptions* = object + indWidth*: Natural + maxLineLen*: Positive -proc prettyPrint(infile, outfile: string, opt: PrettyOptions) = +proc goodEnough(a, b: PNode): bool = + if a.kind == b.kind and a.safeLen == b.safeLen: + case a.kind + of nkNone, nkEmpty, nkNilLit: result = true + of nkIdent: result = a.ident.id == b.ident.id + of nkSym: result = a.sym == b.sym + of nkType: result = true + of nkCharLit, nkIntLit..nkInt64Lit, nkUIntLit..nkUInt64Lit: + result = a.intVal == b.intVal + of nkFloatLit..nkFloat128Lit: + result = a.floatVal == b.floatVal + of nkStrLit, nkRStrLit, nkTripleStrLit: + result = a.strVal == b.strVal + else: + for i in 0 ..< a.len: + if not goodEnough(a[i], b[i]): return false + return true + elif a.kind == nkStmtList and a.len == 1: + result = goodEnough(a[0], b) + elif b.kind == nkStmtList and b.len == 1: + result = goodEnough(a, b[0]) + else: + result = false + +proc finalCheck(content: string; origAst: PNode): bool {.nimcall.} = + var conf = newConfigRef() + let oldErrors = conf.errorCounter + var parser: Parser + parser.em.indWidth = 2 + let fileIdx = fileInfoIdx(conf, AbsoluteFile "nimpretty_bug.nim") + + openParser(parser, fileIdx, llStreamOpen(content), newIdentCache(), conf) + let newAst = parseAll(parser) + closeParser(parser) + result = conf.errorCounter == oldErrors # and goodEnough(newAst, origAst) + +proc prettyPrint*(infile, outfile: string, opt: PrettyOptions) = var conf = newConfigRef() let fileIdx = fileInfoIdx(conf, AbsoluteFile infile) let f = splitFile(outfile.expandTilde) conf.outFile = RelativeFile f.name & f.ext conf.outDir = toAbsoluteDir f.dir - var p: TParsers - p.parser.em.indWidth = opt.indWidth - if setupParsers(p, fileIdx, newIdentCache(), conf): - p.parser.em.maxLineLen = opt.maxLineLen - discard parseAll(p) - closeParsers(p) + var parser: Parser + parser.em.indWidth = opt.indWidth + if setupParser(parser, fileIdx, newIdentCache(), conf): + parser.em.maxLineLen = opt.maxLineLen + let fullAst = parseAll(parser) + closeParser(parser) + when defined(nimpretty): + closeEmitter(parser.em, fullAst, finalCheck) proc main = - var infile, outfile: string + var outfile, outdir: string + + var infiles = newSeq[string]() + var outfiles = newSeq[string]() + var backup = false # when `on`, create a backup file of input in case - # `prettyPrint` could over-write it (note that the backup may happen even - # if input is not actually over-written, when nimpretty is a noop). + # `prettyPrint` could overwrite it (note that the backup may happen even + # if input is not actually overwritten, when nimpretty is a noop). # --backup was un-documented (rely on git instead). var opt = PrettyOptions(indWidth: 0, maxLineLen: 80) + + for kind, key, val in getopt(): case kind of cmdArgument: - infile = key.addFileExt(".nim") + if dirExists(key): + for file in walkDirRec(key, skipSpecial = true): + if file.endsWith(".nim") or file.endsWith(".nimble"): + infiles.add(file) + else: + infiles.add(key.addFileExt(".nim")) + of cmdLongOption, cmdShortOption: case normalize(key) of "help", "h": writeHelp() of "version", "v": writeVersion() of "backup": backup = parseBool(val) of "output", "o", "out": outfile = val + of "outDir", "outdir": outdir = val of "indent": opt.indWidth = parseInt(val) of "maxlinelen": opt.maxLineLen = parseInt(val) else: writeHelp() of cmdEnd: assert(false) # cannot happen - if infile.len == 0: + if infiles.len == 0: quit "[Error] no input file." - if outfile.len == 0: - outfile = infile - if not fileExists(outfile) or not sameFile(infile, outfile): - backup = false # no backup needed since won't be over-written - if backup: - let infileBackup = infile & ".backup" # works with .nim or .nims - echo "writing backup " & infile & " > " & infileBackup - os.copyFile(source = infile, dest = infileBackup) - prettyPrint(infile, outfile, opt) - -main() + + if outfile.len != 0 and outdir.len != 0: + quit "[Error] out and outDir cannot both be specified" + + if outfile.len == 0 and outdir.len == 0: + outfiles = infiles + elif outfile.len != 0 and infiles.len > 1: + # Take the last file to maintain backwards compatibility + let infile = infiles[^1] + infiles = @[infile] + outfiles = @[outfile] + elif outfile.len != 0: + outfiles = @[outfile] + elif outdir.len != 0: + outfiles = infiles.mapIt($(joinPath(outdir, it))) + + for (infile, outfile) in zip(infiles, outfiles): + let (dir, _, _) = splitFile(outfile) + createDir(dir) + if not fileExists(outfile) or not sameFile(infile, outfile): + backup = false # no backup needed since won't be over-written + if backup: + let infileBackup = infile & ".backup" # works with .nim or .nims + echo "writing backup " & infile & " > " & infileBackup + os.copyFile(source = infile, dest = infileBackup) + prettyPrint(infile, outfile, opt) + +when isMainModule: + main() diff --git a/nimpretty/tester.nim b/nimpretty/tester.nim index 041e7edd8..b1f15aee6 100644 --- a/nimpretty/tester.nim +++ b/nimpretty/tester.nim @@ -1,9 +1,11 @@ # Small program that runs the test cases -import strutils, os +import strutils, os, sequtils +from std/private/gitutils import diffFiles const - dir = "nimpretty/tests/" + dir = "nimpretty/tests" + outputdir = dir / "outputdir" var failures = 0 @@ -25,12 +27,30 @@ proc test(infile, ext: string) = let produced = dir / nimFile.changeFileExt(ext) if readFile(expected) != readFile(produced): echo "FAILURE: files differ: ", nimFile - discard execShellCmd("diff -uNdr " & expected & " " & produced) + echo diffFiles(expected, produced).output failures += 1 else: echo "SUCCESS: files identical: ", nimFile -for t in walkFiles(dir / "*.nim"): +proc testTogether(infiles: seq[string]) = + if execShellCmd("$# --outDir:$# --backup:off $#" % [nimp, outputdir, infiles.join(" ")]) != 0: + echo "FAILURE: nimpretty cannot prettify files: ", $infiles + failures += 1 + return + + for infile in infiles: + let nimFile = splitFile(infile).name + let expected = dir / "expected" / nimFile & ".nim" + let produced = dir / "outputdir" / infile + if readFile(expected) != readFile(produced): + echo "FAILURE: files differ: ", nimFile + echo diffFiles(expected, produced).output + failures += 1 + else: + echo "SUCCESS: files identical: ", nimFile + +let allFiles = toSeq(walkFiles(dir / "*.nim")) +for t in allFiles: test(t, "pretty") # also test that pretty(pretty(x)) == pretty(x) test(t.changeFileExt("pretty"), "pretty2") @@ -38,5 +58,7 @@ for t in walkFiles(dir / "*.nim"): removeFile(t.changeFileExt("pretty")) removeFile(t.changeFileExt("pretty2")) +testTogether(allFiles) +removeDir(outputdir) if failures > 0: quit($failures & " failures occurred.") diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim index 2ba885d9a..e5a73305b 100644 --- a/nimpretty/tests/exhaustive.nim +++ b/nimpretty/tests/exhaustive.nim @@ -267,7 +267,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = if not em.endsInWhite: wr(" ") wr(tok.ident.s) template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA > 0 + tok.spacing == {tsLeading} if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: wr(" ") @@ -418,9 +418,9 @@ proc isValid1*[A](s: HashSet[A]): bool {.deprecated: result = s.data.len > 0 # bug #11468 -assert $type(a) == "Option[system.int]" -foo(a, $type(b), c) -foo(type(b), c) # this is ok +assert $typeof(a) == "Option[system.int]" +foo(a, $typeof(b), c) +foo(typeof(b), c) # this is ok proc `<`*[A](s, t: A): bool = discard proc `==`*[A](s, t: HashSet[A]): bool = discard @@ -693,11 +693,11 @@ proc newRecordGen(ctx: Context; typ: TypRef): PNode = String `interpolation`:idx: / `format`:idx: inspired by Python's ``f``-strings. -.. code-block:: nim - - import strformat - let msg = "hello" - doAssert fmt"{msg}\n" == "hello\\n" + ```nim + import strformat + let msg = "hello" + doAssert fmt"{msg}\n" == "hello\\n" + ``` Because the literal is a raw string literal, the ``\n`` is not interpreted as an escape sequence. @@ -843,3 +843,7 @@ type SpinnyEvent2 = tuple kind: EventKind payload: string + + +proc hid_open*(vendor_id: cushort; product_id: cushort; serial_number: cstring): ptr HidDevice {. + importc: "hid_open", dynlib: hidapi.} diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim index cfe9a43fa..7f78b7e56 100644 --- a/nimpretty/tests/expected/exhaustive.nim +++ b/nimpretty/tests/expected/exhaustive.nim @@ -272,7 +272,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = if not em.endsInWhite: wr(" ") wr(tok.ident.s) template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA > 0 + tok.spacing == {tsLeading} if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: wr(" ") @@ -423,9 +423,9 @@ proc isValid1*[A](s: HashSet[A]): bool {.deprecated: result = s.data.len > 0 # bug #11468 -assert $type(a) == "Option[system.int]" -foo(a, $type(b), c) -foo(type(b), c) # this is ok +assert $typeof(a) == "Option[system.int]" +foo(a, $typeof(b), c) +foo(typeof(b), c) # this is ok proc `<`*[A](s, t: A): bool = discard proc `==`*[A](s, t: HashSet[A]): bool = discard @@ -699,11 +699,11 @@ proc newRecordGen(ctx: Context; typ: TypRef): PNode = String `interpolation`:idx: / `format`:idx: inspired by Python's ``f``-strings. -.. code-block:: nim - - import strformat - let msg = "hello" - doAssert fmt"{msg}\n" == "hello\\n" + ```nim + import strformat + let msg = "hello" + doAssert fmt"{msg}\n" == "hello\\n" + ``` Because the literal is a raw string literal, the ``\n`` is not interpreted as an escape sequence. @@ -856,3 +856,8 @@ type SpinnyEvent2 = tuple kind: EventKind payload: string + + +proc hid_open*(vendor_id: cushort; product_id: cushort; + serial_number: cstring): ptr HidDevice {. + importc: "hid_open", dynlib: hidapi.} diff --git a/nimpretty/tests/expected/simple.nim b/nimpretty/tests/expected/simple.nim index 5126658ea..e711eb3b6 100644 --- a/nimpretty/tests/expected/simple.nim +++ b/nimpretty/tests/expected/simple.nim @@ -11,3 +11,19 @@ proc a() = # comment 2 discard + +# bug #15596 +discard ## comment 3 + +discard # comment 4 + + +# bug #20553 + +let `'hello` = 12 +echo `'hello` + + +proc `'u4`(n: string) = + # The leading ' is required. + discard diff --git a/nimpretty/tests/simple.nim b/nimpretty/tests/simple.nim index 5126658ea..2a01a176e 100644 --- a/nimpretty/tests/simple.nim +++ b/nimpretty/tests/simple.nim @@ -11,3 +11,19 @@ proc a() = # comment 2 discard + +# bug #15596 +discard## comment 3 + +discard # comment 4 + + +# bug #20553 + +let `'hello` = 12 +echo `'hello` + + +proc `'u4`(n: string) = + # The leading ' is required. + discard |