nimpretty.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, sequtils
  14. const
  15. Version = "0.2"
  16. Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
  17. (c) 2017 Andreas Rumpf
  18. Usage:
  19. nimpretty [options] nimfiles...
  20. Options:
  21. --out:file set the output file (default: overwrite the input file)
  22. --outDir:dir set the output dir (default: overwrite the input files)
  23. --indent:N[=0] set the number of spaces that is used for indentation
  24. --indent:0 means autodetection (default behaviour)
  25. --maxLineLen:N set the desired maximum line length (default: 80)
  26. --version show the version
  27. --help show this help
  28. """
  29. proc writeHelp() =
  30. stdout.write(Usage)
  31. stdout.flushFile()
  32. quit(0)
  33. proc writeVersion() =
  34. stdout.write(Version & "\n")
  35. stdout.flushFile()
  36. quit(0)
  37. type
  38. PrettyOptions* = object
  39. indWidth*: Natural
  40. maxLineLen*: Positive
  41. proc prettyPrint*(infile, outfile: string, opt: PrettyOptions) =
  42. var conf = newConfigRef()
  43. let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
  44. let f = splitFile(outfile.expandTilde)
  45. conf.outFile = RelativeFile f.name & f.ext
  46. conf.outDir = toAbsoluteDir f.dir
  47. var parser: Parser
  48. parser.em.indWidth = opt.indWidth
  49. if setupParser(parser, fileIdx, newIdentCache(), conf):
  50. parser.em.maxLineLen = opt.maxLineLen
  51. discard parseAll(parser)
  52. closeParser(parser)
  53. proc main =
  54. var outfile, outdir: string
  55. var infiles = newSeq[string]()
  56. var outfiles = newSeq[string]()
  57. var backup = false
  58. # when `on`, create a backup file of input in case
  59. # `prettyPrint` could overwrite it (note that the backup may happen even
  60. # if input is not actually overwritten, when nimpretty is a noop).
  61. # --backup was un-documented (rely on git instead).
  62. var opt = PrettyOptions(indWidth: 0, maxLineLen: 80)
  63. for kind, key, val in getopt():
  64. case kind
  65. of cmdArgument:
  66. infiles.add(key.addFileExt(".nim"))
  67. of cmdLongOption, cmdShortOption:
  68. case normalize(key)
  69. of "help", "h": writeHelp()
  70. of "version", "v": writeVersion()
  71. of "backup": backup = parseBool(val)
  72. of "output", "o", "out": outfile = val
  73. of "outDir", "outdir": outdir = val
  74. of "indent": opt.indWidth = parseInt(val)
  75. of "maxlinelen": opt.maxLineLen = parseInt(val)
  76. else: writeHelp()
  77. of cmdEnd: assert(false) # cannot happen
  78. if infiles.len == 0:
  79. quit "[Error] no input file."
  80. if outfile.len != 0 and outdir.len != 0:
  81. quit "[Error] out and outDir cannot both be specified"
  82. if outfile.len == 0 and outdir.len == 0:
  83. outfiles = infiles
  84. elif outfile.len != 0 and infiles.len > 1:
  85. # Take the last file to maintain backwards compatibility
  86. let infile = infiles[^1]
  87. infiles = @[infile]
  88. outfiles = @[outfile]
  89. elif outfile.len != 0:
  90. outfiles = @[outfile]
  91. elif outdir.len != 0:
  92. outfiles = infiles.mapIt($(joinPath(outdir, it)))
  93. for (infile, outfile) in zip(infiles, outfiles):
  94. let (dir, _, _) = splitFile(outfile)
  95. createDir(dir)
  96. if not fileExists(outfile) or not sameFile(infile, outfile):
  97. backup = false # no backup needed since won't be over-written
  98. if backup:
  99. let infileBackup = infile & ".backup" # works with .nim or .nims
  100. echo "writing backup " & infile & " > " & infileBackup
  101. os.copyFile(source = infile, dest = infileBackup)
  102. prettyPrint(infile, outfile, opt)
  103. when isMainModule:
  104. main()