nimpretty.nim 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, syntaxes, options, pathutils, layouter]
  13. import parseopt, strutils, os
  14. const
  15. Version = "0.2"
  16. Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
  17. (c) 2017 Andreas Rumpf
  18. Usage:
  19. nimpretty [options] file.nim
  20. Options:
  21. --out:file set the output file (default: overwrite the input file)
  22. --indent:N[=0] set the number of spaces that is used for indentation
  23. --indent:0 means autodetection (default behaviour)
  24. --maxLineLen:N set the desired maximum line length (default: 80)
  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: Natural
  39. maxLineLen: Positive
  40. proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
  41. var conf = newConfigRef()
  42. let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
  43. let f = splitFile(outfile.expandTilde)
  44. conf.outFile = RelativeFile f.name & f.ext
  45. conf.outDir = toAbsoluteDir f.dir
  46. var p: TParsers
  47. p.parser.em.indWidth = opt.indWidth
  48. if setupParsers(p, fileIdx, newIdentCache(), conf):
  49. p.parser.em.maxLineLen = opt.maxLineLen
  50. discard parseAll(p)
  51. closeParsers(p)
  52. proc main =
  53. var infile, outfile: string
  54. var backup = false
  55. # when `on`, create a backup file of input in case
  56. # `prettyPrint` could over-write it (note that the backup may happen even
  57. # if input is not actually over-written, when nimpretty is a noop).
  58. # --backup was un-documented (rely on git instead).
  59. var opt = PrettyOptions(indWidth: 0, maxLineLen: 80)
  60. for kind, key, val in getopt():
  61. case kind
  62. of cmdArgument:
  63. infile = key.addFileExt(".nim")
  64. of cmdLongOption, cmdShortOption:
  65. case normalize(key)
  66. of "help", "h": writeHelp()
  67. of "version", "v": writeVersion()
  68. of "backup": backup = parseBool(val)
  69. of "output", "o", "out": outfile = val
  70. of "indent": opt.indWidth = parseInt(val)
  71. of "maxlinelen": opt.maxLineLen = parseInt(val)
  72. else: writeHelp()
  73. of cmdEnd: assert(false) # cannot happen
  74. if infile.len == 0:
  75. quit "[Error] no input file."
  76. if outfile.len == 0:
  77. outfile = infile
  78. if not existsFile(outfile) or not sameFile(infile, outfile):
  79. backup = false # no backup needed since won't be over-written
  80. if backup:
  81. let infileBackup = infile & ".backup" # works with .nim or .nims
  82. echo "writing backup " & infile & " > " & infileBackup
  83. os.copyFile(source = infile, dest = infileBackup)
  84. prettyPrint(infile, outfile, opt)
  85. main()