nimpretty: explicit --indent option; fixes #9502; refs #9510 [backport]

This commit is contained in:
Andreas Rumpf 2018-11-11 10:38:41 +01:00
commit d1fe195dcc
4 changed files with 33 additions and 16 deletions

View file

@ -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

View file

@ -45,7 +45,7 @@ type
inSemiStmtList*: int
emptyNode: PNode
when defined(nimpretty2):
em: Emitter
em*: Emitter
SymbolMode = enum
smNormal, smAllowNil, smAfterDot

View file

@ -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)