summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-11-11 10:38:41 +0100
committerAndreas Rumpf <rumpf_a@web.de>2018-11-11 10:39:20 +0100
commitd1fe195dcc4026ee3f732bb38a0642ff6c4e0d91 (patch)
treef9d126664559a2df4435116b3c9bc7f2536a0ddf
parent532647171e013ed9f8847a4f97ebca0fc6dfc57f (diff)
downloadNim-d1fe195dcc4026ee3f732bb38a0642ff6c4e0d91.tar.gz
nimpretty: explicit --indent option; fixes #9502; refs #9510 [backport]
-rw-r--r--compiler/layouter.nim9
-rw-r--r--compiler/parser.nim2
-rw-r--r--compiler/syntaxes.nim18
-rw-r--r--nimpretty/nimpretty.nim20
4 files changed, 33 insertions, 16 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index ad4eb7488..8605ade45 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -30,7 +30,7 @@ type
     lastTok: TTokType
     inquote, lastTokWasTerse: bool
     semicolons: SemicolonKind
-    col, lastLineNumber, lineSpan, indentLevel, indWidth: int
+    col, lastLineNumber, lineSpan, indentLevel, indWidth*: int
     keepIndents*: int
     doIndentMore*: int
     content: string
@@ -41,9 +41,10 @@ type
 proc openEmitter*(em: var Emitter, cache: IdentCache;
                   config: ConfigRef, fileIdx: FileIndex) =
   let fullPath = Absolutefile config.toFullPath(fileIdx)
-  em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead),
-                               cache, config)
-  if em.indWidth == 0: em.indWidth = 2
+  if em.indWidth == 0:
+    em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead),
+                                cache, config)
+    if em.indWidth == 0: em.indWidth = 2
   em.config = config
   em.fid = fileIdx
   em.lastTok = tkInvalid
diff --git a/compiler/parser.nim b/compiler/parser.nim
index 0b7eee32e..54b360a24 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -45,7 +45,7 @@ type
     inSemiStmtList*: int
     emptyNode: PNode
     when defined(nimpretty2):
-      em: Emitter
+      em*: Emitter
 
   SymbolMode = enum
     smNormal, smAllowNil, smAfterDot
diff --git a/compiler/syntaxes.nim b/compiler/syntaxes.nim
index 7a89452c6..60246b9bf 100644
--- a/compiler/syntaxes.nim
+++ b/compiler/syntaxes.nim
@@ -158,14 +158,18 @@ proc openParsers*(p: var TParsers, fileIdx: FileIndex, inputstream: PLLStream;
 proc closeParsers*(p: var TParsers) =
   parser.closeParser(p.parser)
 
-proc parseFile*(fileIdx: FileIndex; cache: IdentCache; config: ConfigRef): PNode {.procvar.} =
-  var
-    p: TParsers
-    f: File
+proc setupParsers*(p: var TParsers; fileIdx: FileIndex; cache: IdentCache;
+                   config: ConfigRef): bool =
+  var f: File
   let filename = toFullPathConsiderDirty(config, fileIdx)
   if not open(f, filename.string):
     rawMessage(config, errGenerated, "cannot open file: " & filename.string)
-    return
+    return false
   openParsers(p, fileIdx, llStreamOpen(f), cache, config)
-  result = parseAll(p)
-  closeParsers(p)
+  result = true
+
+proc parseFile*(fileIdx: FileIndex; cache: IdentCache; config: ConfigRef): PNode {.procvar.} =
+  var p: TParsers
+  if setupParsers(p, fileIdx, cache, config):
+    result = parseAll(p)
+    closeParsers(p)
diff --git a/nimpretty/nimpretty.nim b/nimpretty/nimpretty.nim
index fb2b8637f..628bc163e 100644
--- a/nimpretty/nimpretty.nim
+++ b/nimpretty/nimpretty.nim
@@ -12,7 +12,8 @@
 when not defined(nimpretty):
   {.error: "This needs to be compiled with --define:nimPretty".}
 
-import ../compiler / [idents, msgs, ast, syntaxes, renderer, options, pathutils]
+import ../compiler / [idents, msgs, ast, syntaxes, renderer, options,
+  pathutils, layouter]
 
 import parseopt, strutils, os
 
@@ -26,6 +27,7 @@ Usage:
 Options:
   --backup:on|off     create a backup file before overwritting (default: ON)
   --output:file       set the output file (default: overwrite the .nim file)
+  --indent:N          set the number of spaces that is used for indentation
   --version           show the version
   --help              show this help
 """
@@ -40,12 +42,20 @@ proc writeVersion() =
   stdout.flushFile()
   quit(0)
 
-proc prettyPrint(infile, outfile: string) =
+type
+  PrettyOptions = object
+    indWidth: int
+
+proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
   var conf = newConfigRef()
   let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
   conf.outFile = AbsoluteFile outfile
   when defined(nimpretty2):
-    discard parseFile(fileIdx, newIdentCache(), conf)
+    var p: TParsers
+    p.parser.em.indWidth = opt.indWidth
+    if setupParsers(p, fileIdx, newIdentCache(), conf):
+      discard parseAll(p)
+      closeParsers(p)
   else:
     let tree = parseFile(fileIdx, newIdentCache(), conf)
     renderModule(tree, infile, outfile, {}, fileIdx, conf)
@@ -53,6 +63,7 @@ proc prettyPrint(infile, outfile: string) =
 proc main =
   var infile, outfile: string
   var backup = true
+  var opt: PrettyOptions
   for kind, key, val in getopt():
     case kind
     of cmdArgument:
@@ -63,6 +74,7 @@ proc main =
       of "version", "v": writeVersion()
       of "backup": backup = parseBool(val)
       of "output", "o": outfile = val
+      of "indent": opt.indWidth = parseInt(val)
       else: writeHelp()
     of cmdEnd: assert(false) # cannot happen
   if infile.len == 0:
@@ -70,6 +82,6 @@ proc main =
   if backup:
     os.copyFile(source=infile, dest=changeFileExt(infile, ".nim.backup"))
   if outfile.len == 0: outfile = infile
-  prettyPrint(infile, outfile)
+  prettyPrint(infile, outfile, opt)
 
 main()