nimpretty: bugfix [bugfix]

(cherry picked from commit 284a8cb58a)
This commit is contained in:
Araq
2019-06-28 11:45:21 +02:00
committed by narimiran
parent 1503970c3b
commit 970c7616bc
4 changed files with 106 additions and 12 deletions

View File

@@ -143,14 +143,21 @@ proc closeEmitter*(em: var Emitter) =
maxLhs = computeMax(em, lineBegin)
of ltEndSection:
maxLhs = 0
lineBegin = i+1
of ltTab:
if maxLhs == 0 or computeRhs(em, i+1)+maxLhs > MaxLineLen:
if maxLhs == 0:
content.add em.tokens[i]
inc lineLen, em.tokens[i].len
else:
let spaces = maxLhs - lineLen #max(maxLhs - lineLen, 1)
for j in 1..spaces: content.add ' '
inc lineLen, spaces
# pick the shorter indentation token:
var spaces = maxLhs - lineLen
if spaces < em.tokens[i].len or computeRhs(em, i+1)+maxLhs <= MaxLineLen:
if spaces <= 0 and content[^1] notin {' ', '\L'}: spaces = 1
for j in 1..spaces: content.add ' '
inc lineLen, spaces
else:
content.add em.tokens[i]
inc lineLen, em.tokens[i].len
of ltNewline:
content.add em.tokens[i]
lineLen = 0
@@ -217,8 +224,16 @@ proc wrSpace(em: var Emitter) =
proc wrTab(em: var Emitter) =
wr(em, " ", ltTab)
proc beginSection*(em: var Emitter) = wr(em, "", ltBeginSection)
proc endSection*(em: var Emitter) = wr(em, "", ltEndSection)
proc beginSection*(em: var Emitter) =
em.tokens.insert "", em.tokens.len-2
em.kinds.insert ltBeginSection, em.kinds.len-2
#wr(em, "", ltBeginSection)
proc endSection*(em: var Emitter) =
em.tokens.insert "", em.tokens.len-2
em.kinds.insert ltEndSection, em.kinds.len-2
#wr(em, "", ltEndSection)
proc removeSpaces(em: var Emitter) =
while em.kinds.len > 0 and em.kinds[^1] == ltSpaces:
@@ -262,7 +277,8 @@ proc emitMultilineComment(em: var Emitter, lit: string, col: int) =
var a = 0
while a < commentLine.len and commentLine[a] == ' ': inc a
if i == 0:
wr(em, "", ltTab)
if em.kinds.len > 0 and em.kinds[^1] != ltTab:
wr(em, "", ltTab)
elif stripped.len == 0:
wrNewline em
else:

View File

@@ -160,11 +160,11 @@ type
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c
OptParser* =
object of RootObj ## Implementation of the command line parser.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
OptParser* = object of RootObj ## \
## Implementation of the command line parser.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
pos*: int
inShortState: bool
allowWhitespaceAfterColon: bool

View File

@@ -618,3 +618,42 @@ veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureV
veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureValue1,
futureValue2, node)
type
CmdLineKind* = enum ## The detected command line token.
cmdEnd, ## End of command line reached
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c
OptParser* = object of RootObj ## \
## Implementation of the command line parser. Here is even more text yad.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
pos*: int
inShortState: bool
allowWhitespaceAfterColon: bool
shortNoVal: set[char]
longNoVal: seq[string]
cmds: seq[string]
idx: int
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value
OptParserDifferently* = object of RootObj ## Implementation of the command line parser.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
pos*: int
inShortState: bool
allowWhitespaceAfterColon: bool
shortNoVal: set[char]
longNoVal: seq[string]
cmds: seq[string]
idx: int
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value

View File

@@ -624,3 +624,42 @@ veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1],
veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureValue1,
futureValue2, node)
type
CmdLineKind* = enum ## The detected command line token.
cmdEnd, ## End of command line reached
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c
OptParser* = object of RootObj ## \
## Implementation of the command line parser. Here is even more text yad.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
pos*: int
inShortState: bool
allowWhitespaceAfterColon: bool
shortNoVal: set[char]
longNoVal: seq[string]
cmds: seq[string]
idx: int
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value
OptParserDifferently* = object of RootObj ## Implementation of the command line parser.
##
## To initialize it, use the
## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
pos*: int
inShortState: bool
allowWhitespaceAfterColon: bool
shortNoVal: set[char]
longNoVal: seq[string]
cmds: seq[string]
idx: int
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value