nimpretty.nim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Standard tool for pretty printing.
  10. when not defined(nimpretty):
  11. {.error: "This needs to be compiled with --define:nimPretty".}
  12. import ../compiler / [idents, msgs, ast, syntaxes, renderer, options,
  13. pathutils, layouter]
  14. import parseopt, strutils, os
  15. const
  16. Version = "0.1"
  17. Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
  18. (c) 2017 Andreas Rumpf
  19. Usage:
  20. nimpretty [options] file.nim
  21. Options:
  22. --output:file set the output file (default: overwrite the input file)
  23. --indent:N[=0] set the number of spaces that is used for indentation
  24. --indent:0 means autodetection (default behaviour)
  25. --version show the version
  26. --help show this help
  27. """
  28. proc writeHelp() =
  29. stdout.write(Usage)
  30. stdout.flushFile()
  31. quit(0)
  32. proc writeVersion() =
  33. stdout.write(Version & "\n")
  34. stdout.flushFile()
  35. quit(0)
  36. type
  37. PrettyOptions = object
  38. indWidth: int
  39. proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
  40. var conf = newConfigRef()
  41. let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
  42. let f = splitFile(outfile.expandTilde)
  43. conf.outFile = RelativeFile f.name & f.ext
  44. conf.outDir = toAbsoluteDir f.dir
  45. var p: TParsers
  46. p.parser.em.indWidth = opt.indWidth
  47. if setupParsers(p, fileIdx, newIdentCache(), conf):
  48. discard parseAll(p)
  49. closeParsers(p)
  50. proc main =
  51. var infile, outfile: string
  52. var backup = false
  53. # when `on`, create a backup file of input in case
  54. # `prettyPrint` could over-write it (note that the backup may happen even
  55. # if input is not actually over-written, when nimpretty is a noop).
  56. # --backup was un-documented (rely on git instead).
  57. var opt: PrettyOptions
  58. for kind, key, val in getopt():
  59. case kind
  60. of cmdArgument:
  61. infile = key.addFileExt(".nim")
  62. of cmdLongoption, cmdShortOption:
  63. case normalize(key)
  64. of "help", "h": writeHelp()
  65. of "version", "v": writeVersion()
  66. of "backup": backup = parseBool(val)
  67. of "output", "o": outfile = val
  68. of "indent": opt.indWidth = parseInt(val)
  69. else: writeHelp()
  70. of cmdEnd: assert(false) # cannot happen
  71. if infile.len == 0:
  72. quit "[Error] no input file."
  73. if outfile.len == 0:
  74. outfile = infile
  75. if not existsFile(outfile) or not sameFile(infile, outfile):
  76. backup = false # no backup needed since won't be over-written
  77. if backup:
  78. let infileBackup = infile & ".backup" # works with .nim or .nims
  79. echo "writing backup " & infile & " > " & infileBackup
  80. os.copyFile(source = infile, dest = infileBackup)
  81. prettyPrint(infile, outfile, opt)
  82. main()