Skip to content

Commit

Permalink
fix nim-lang#9502 --indentSpaces:n
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Oct 25, 2018
1 parent 87d60b2 commit c28231d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
7 changes: 5 additions & 2 deletions compiler/layouter.nim
Expand Up @@ -41,9 +41,12 @@ 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),
if config.nimprettyOpt.indentSpaces > 0:
em.indWidth = config.nimprettyOpt.indentSpaces
else:
em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead),
cache, config)
if em.indWidth == 0: em.indWidth = 2
if em.indWidth == 0: em.indWidth = 2
em.config = config
em.fid = fileIdx
em.lastTok = tkInvalid
Expand Down
6 changes: 6 additions & 0 deletions compiler/options.nim
Expand Up @@ -161,6 +161,9 @@ type
version*: int
Suggestions* = seq[Suggest]

NimprettyOpt* = object
indentSpaces*: int ## number of spaces for indents; 0=infer from 1st indent

ConfigRef* = ref object ## every global configuration
## fields marked with '*' are subject to
## the incremental compilation mechanisms
Expand Down Expand Up @@ -244,6 +247,9 @@ type
structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string;
severity: Severity) {.closure.}
cppCustomNamespace*: string

when defined(nimpretty):
nimprettyOpt*: NimprettyOpt

template depConfigFields*(fn) {.dirty.} =
fn(target)
Expand Down
9 changes: 6 additions & 3 deletions nimpretty/nimpretty.nim
Expand Up @@ -26,6 +26,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)
--indentSpaces:n use `n` spaces for indends (default: 0=infer from 1st indent)
--version show the version
--help show this help
"""
Expand All @@ -40,8 +41,7 @@ proc writeVersion() =
stdout.flushFile()
quit(0)

proc prettyPrint(infile, outfile: string) =
var conf = newConfigRef()
proc prettyPrint(infile, outfile: string, conf: ConfigRef) =
let fileIdx = fileInfoIdx(conf, AbsoluteFile infile)
conf.outFile = AbsoluteFile outfile
when defined(nimpretty2):
Expand All @@ -51,8 +51,10 @@ proc prettyPrint(infile, outfile: string) =
renderModule(tree, infile, outfile, {}, fileIdx, conf)

proc main =
var conf = newConfigRef()
var infile, outfile: string
var backup = true
var indentSpaces = 0
for kind, key, val in getopt():
case kind
of cmdArgument:
Expand All @@ -62,6 +64,7 @@ proc main =
of "help", "h": writeHelp()
of "version", "v": writeVersion()
of "backup": backup = parseBool(val)
of "indentspaces": conf.nimprettyOpt.indentSpaces = parseInt(val)
of "output", "o": outfile = val
else: writeHelp()
of cmdEnd: assert(false) # cannot happen
Expand All @@ -70,6 +73,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, conf)

main()
6 changes: 6 additions & 0 deletions nimpretty/tests/expected/simple4.nim
@@ -0,0 +1,6 @@
proc fun() =
echo "ok1"

proc fun2() =
for i in 0..<2:
echo "ok2"
6 changes: 6 additions & 0 deletions nimpretty/tests/simple4.nim
@@ -0,0 +1,6 @@
proc fun() =
echo "ok1"

proc fun2() =
for i in 0..<2:
echo "ok2"

0 comments on commit c28231d

Please sign in to comment.