nimpretty: fixes #10177 [bugfix]
This commit is contained in:
parent
5a37224102
commit
790bc5b07b
3 changed files with 85 additions and 40 deletions
|
|
@ -24,6 +24,10 @@ type
|
||||||
SemicolonKind = enum
|
SemicolonKind = enum
|
||||||
detectSemicolonKind, useSemicolon, dontTouch
|
detectSemicolonKind, useSemicolon, dontTouch
|
||||||
|
|
||||||
|
LayoutToken = enum
|
||||||
|
ltSpaces, ltNewline, ltComment, ltLit, ltKeyword, ltExportMarker, ltIdent,
|
||||||
|
ltOther, ltOpr
|
||||||
|
|
||||||
Emitter* = object
|
Emitter* = object
|
||||||
config: ConfigRef
|
config: ConfigRef
|
||||||
fid: FileIndex
|
fid: FileIndex
|
||||||
|
|
@ -34,6 +38,7 @@ type
|
||||||
keepIndents*: int
|
keepIndents*: int
|
||||||
doIndentMore*: int
|
doIndentMore*: int
|
||||||
content: string
|
content: string
|
||||||
|
kinds: seq[LayoutToken]
|
||||||
indentStack: seq[int]
|
indentStack: seq[int]
|
||||||
fixedUntil: int # marks where we must not go in the content
|
fixedUntil: int # marks where we must not go in the content
|
||||||
altSplitPos: array[SplitKind, int] # alternative split positions
|
altSplitPos: array[SplitKind, int] # alternative split positions
|
||||||
|
|
@ -79,10 +84,29 @@ proc calcCol(em: var Emitter; s: string) =
|
||||||
dec i
|
dec i
|
||||||
inc em.col
|
inc em.col
|
||||||
|
|
||||||
template wr(x) =
|
proc wr(em: var Emitter; x: string; lt: LayoutToken) =
|
||||||
em.content.add x
|
em.content.add x
|
||||||
|
em.kinds.add lt
|
||||||
inc em.col, x.len
|
inc em.col, x.len
|
||||||
|
|
||||||
|
when false:
|
||||||
|
proc wrNewline(em: var Emitter) =
|
||||||
|
em.tokens.add "\L"
|
||||||
|
em.kinds.add ltNewline
|
||||||
|
inc em.tokensSum, 1
|
||||||
|
em.col = 0
|
||||||
|
|
||||||
|
proc wrSpaces(em: var Emitter; spaces: Natural) =
|
||||||
|
wr(em, strutils.repeat(' ', spaces), ltSpaces)
|
||||||
|
|
||||||
|
proc removeSpaces(em: var Emitter) =
|
||||||
|
while em.kinds.len > 0 and em.kinds[^1] == ltSpaces:
|
||||||
|
let tokenLen = em.tokens[^1].len
|
||||||
|
setLen(em.tokens, em.tokens.len-1)
|
||||||
|
setLen(em.kinds, em.kinds.len-1)
|
||||||
|
dec em.tokensSum, tokenLen
|
||||||
|
dec em.col, tokenLen
|
||||||
|
|
||||||
template goodCol(col): bool = col in 40..MaxLineLen
|
template goodCol(col): bool = col in 40..MaxLineLen
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
@ -109,12 +133,11 @@ proc softLinebreak(em: var Emitter, lit: string) =
|
||||||
# bug #10295, check first if even more indentation would help:
|
# bug #10295, check first if even more indentation would help:
|
||||||
let spaces = em.indentLevel+moreIndent(em)
|
let spaces = em.indentLevel+moreIndent(em)
|
||||||
if spaces < em.col:
|
if spaces < em.col:
|
||||||
let oldPos = em.content.len # we can undo our changes if not benefitial
|
|
||||||
while em.content.len > 0 and em.content[em.content.high] == ' ':
|
while em.content.len > 0 and em.content[em.content.high] == ' ':
|
||||||
setLen(em.content, em.content.len-1)
|
setLen(em.content, em.content.len-1)
|
||||||
wr("\L")
|
wr(em, "\L", ltNewline)
|
||||||
em.col = 0
|
em.col = 0
|
||||||
for i in 1..spaces: wr(" ")
|
for i in 1..spaces: wr(em, " ", ltSpaces)
|
||||||
else:
|
else:
|
||||||
# search backwards for a good split position:
|
# search backwards for a good split position:
|
||||||
for a in mitems(em.altSplitPos):
|
for a in mitems(em.altSplitPos):
|
||||||
|
|
@ -136,28 +159,31 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
template endsInAlpha(em): bool =
|
template endsInAlpha(em): bool =
|
||||||
em.content.len > 0 and em.content[em.content.high] in SymChars+{'_'}
|
em.content.len > 0 and em.content[em.content.high] in SymChars+{'_'}
|
||||||
|
|
||||||
|
template wasExportMarker(em): bool =
|
||||||
|
em.kinds.len > 0 and em.kinds[^1] == ltExportMarker
|
||||||
|
|
||||||
proc emitComment(em: var Emitter; tok: TToken) =
|
proc emitComment(em: var Emitter; tok: TToken) =
|
||||||
let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA, tok.commentOffsetB)
|
let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA, tok.commentOffsetB)
|
||||||
em.lineSpan = countNewlines(lit)
|
em.lineSpan = countNewlines(lit)
|
||||||
if em.lineSpan > 0: calcCol(em, lit)
|
if em.lineSpan > 0: calcCol(em, lit)
|
||||||
if not endsInWhite(em):
|
if not endsInWhite(em):
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
if em.lineSpan == 0 and max(em.col, LineCommentColumn) + lit.len <= MaxLineLen:
|
if em.lineSpan == 0 and max(em.col, LineCommentColumn) + lit.len <= MaxLineLen:
|
||||||
for i in 1 .. LineCommentColumn - em.col: wr(" ")
|
for i in 1 .. LineCommentColumn - em.col: wr(em, " ", ltSpaces)
|
||||||
wr lit
|
wr em, lit, ltComment
|
||||||
|
|
||||||
if tok.tokType == tkComment and tok.literal.startsWith("#!nimpretty"):
|
if tok.tokType == tkComment and tok.literal.startsWith("#!nimpretty"):
|
||||||
case tok.literal
|
case tok.literal
|
||||||
of "#!nimpretty off":
|
of "#!nimpretty off":
|
||||||
inc em.keepIndents
|
inc em.keepIndents
|
||||||
wr("\L")
|
wr(em, "\L", ltNewline)
|
||||||
em.lastLineNumber = tok.line + 1
|
em.lastLineNumber = tok.line + 1
|
||||||
of "#!nimpretty on":
|
of "#!nimpretty on":
|
||||||
dec em.keepIndents
|
dec em.keepIndents
|
||||||
em.lastLineNumber = tok.line
|
em.lastLineNumber = tok.line
|
||||||
wr("\L")
|
wr(em, "\L", ltNewline)
|
||||||
#for i in 1 .. tok.indent: wr " "
|
#for i in 1 .. tok.indent: wr " "
|
||||||
wr tok.literal
|
wr em, tok.literal, ltComment
|
||||||
em.col = 0
|
em.col = 0
|
||||||
em.lineSpan = 0
|
em.lineSpan = 0
|
||||||
return
|
return
|
||||||
|
|
@ -192,49 +218,49 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
# remove trailing whitespace:
|
# remove trailing whitespace:
|
||||||
while em.content.len > 0 and em.content[em.content.high] == ' ':
|
while em.content.len > 0 and em.content[em.content.high] == ' ':
|
||||||
setLen(em.content, em.content.len-1)
|
setLen(em.content, em.content.len-1)
|
||||||
wr("\L")
|
wr(em, "\L", ltNewline)
|
||||||
for i in 2..tok.line - em.lastLineNumber: wr("\L")
|
for i in 2..tok.line - em.lastLineNumber: wr(em, "\L", ltNewline)
|
||||||
em.col = 0
|
em.col = 0
|
||||||
for i in 1..em.indentLevel:
|
for i in 1..em.indentLevel:
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
em.fixedUntil = em.content.high
|
em.fixedUntil = em.content.high
|
||||||
|
|
||||||
var lastTokWasTerse = false
|
var lastTokWasTerse = false
|
||||||
case tok.tokType
|
case tok.tokType
|
||||||
of tokKeywordLow..tokKeywordHigh:
|
of tokKeywordLow..tokKeywordHigh:
|
||||||
if endsInAlpha(em):
|
if endsInAlpha(em):
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
elif not em.inquote and not endsInWhite(em) and
|
elif not em.inquote and not endsInWhite(em) and
|
||||||
em.lastTok notin openPars and not em.lastTokWasTerse:
|
em.lastTok notin openPars and not em.lastTokWasTerse:
|
||||||
#and tok.tokType in oprSet
|
#and tok.tokType in oprSet
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
|
|
||||||
if not em.inquote:
|
if not em.inquote:
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltKeyword)
|
||||||
|
|
||||||
case tok.tokType
|
case tok.tokType
|
||||||
of tkAnd: rememberSplit(splitAnd)
|
of tkAnd: rememberSplit(splitAnd)
|
||||||
of tkOr: rememberSplit(splitOr)
|
of tkOr: rememberSplit(splitOr)
|
||||||
of tkIn, tkNotin:
|
of tkIn, tkNotin:
|
||||||
rememberSplit(splitIn)
|
rememberSplit(splitIn)
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
else: discard
|
else: discard
|
||||||
else:
|
else:
|
||||||
# keywords in backticks are not normalized:
|
# keywords in backticks are not normalized:
|
||||||
wr(tok.ident.s)
|
wr(em, tok.ident.s, ltIdent)
|
||||||
|
|
||||||
of tkColon:
|
of tkColon:
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
of tkSemicolon, tkComma:
|
of tkSemicolon, tkComma:
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
rememberSplit(splitComma)
|
rememberSplit(splitComma)
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
of tkParDotLe, tkParLe, tkBracketDotLe, tkBracketLe,
|
of tkParDotLe, tkParLe, tkBracketDotLe, tkBracketLe,
|
||||||
tkCurlyLe, tkCurlyDotLe, tkBracketLeColon:
|
tkCurlyLe, tkCurlyDotLe, tkBracketLeColon:
|
||||||
if tok.strongSpaceA > 0 and not em.endsInWhite:
|
if tok.strongSpaceA > 0 and not em.endsInWhite and not em.wasExportMarker:
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
rememberSplit(splitParLe)
|
rememberSplit(splitParLe)
|
||||||
of tkParRi,
|
of tkParRi,
|
||||||
tkBracketRi, tkCurlyRi,
|
tkBracketRi, tkCurlyRi,
|
||||||
|
|
@ -242,33 +268,33 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
tkCurlyDotRi,
|
tkCurlyDotRi,
|
||||||
tkParDotRi,
|
tkParDotRi,
|
||||||
tkColonColon:
|
tkColonColon:
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
of tkDot:
|
of tkDot:
|
||||||
lastTokWasTerse = true
|
lastTokWasTerse = true
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
of tkEquals:
|
of tkEquals:
|
||||||
if not em.inquote and not em.endsInWhite: wr(" ")
|
if not em.inquote and not em.endsInWhite: wr(em, " ", ltSpaces)
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
if not em.inquote: wr(" ")
|
if not em.inquote: wr(em, " ", ltSpaces)
|
||||||
of tkOpr, tkDotDot:
|
of tkOpr, tkDotDot:
|
||||||
if ((tok.strongSpaceA == 0 and tok.strongSpaceB == 0) or em.inquote) and
|
if ((tok.strongSpaceA == 0 and tok.strongSpaceB == 0) or em.inquote) and
|
||||||
tok.ident.s notin ["<", ">", "<=", ">=", "==", "!="]:
|
tok.ident.s notin ["<", ">", "<=", ">=", "==", "!="]:
|
||||||
# bug #9504: remember to not spacify a keyword:
|
# bug #9504: remember to not spacify a keyword:
|
||||||
lastTokWasTerse = true
|
lastTokWasTerse = true
|
||||||
# if not surrounded by whitespace, don't produce any whitespace either:
|
# if not surrounded by whitespace, don't produce any whitespace either:
|
||||||
wr(tok.ident.s)
|
wr(em, tok.ident.s, ltOpr)
|
||||||
else:
|
else:
|
||||||
if not em.endsInWhite: wr(" ")
|
if not em.endsInWhite: wr(em, " ", ltSpaces)
|
||||||
wr(tok.ident.s)
|
wr(em, tok.ident.s, ltOpr)
|
||||||
template isUnary(tok): bool =
|
template isUnary(tok): bool =
|
||||||
tok.strongSpaceB == 0 and tok.strongSpaceA > 0
|
tok.strongSpaceB == 0 and tok.strongSpaceA > 0
|
||||||
|
|
||||||
if not isUnary(tok):
|
if not isUnary(tok):
|
||||||
rememberSplit(splitBinary)
|
rememberSplit(splitBinary)
|
||||||
wr(" ")
|
wr(em, " ", ltSpaces)
|
||||||
of tkAccent:
|
of tkAccent:
|
||||||
if not em.inquote and endsInAlpha(em): wr(" ")
|
if not em.inquote and endsInAlpha(em): wr(em, " ", ltSpaces)
|
||||||
wr(TokTypeToStr[tok.tokType])
|
wr(em, TokTypeToStr[tok.tokType], ltOther)
|
||||||
em.inquote = not em.inquote
|
em.inquote = not em.inquote
|
||||||
of tkComment:
|
of tkComment:
|
||||||
if not preventComment:
|
if not preventComment:
|
||||||
|
|
@ -276,16 +302,16 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
of tkIntLit..tkStrLit, tkRStrLit, tkTripleStrLit, tkGStrLit, tkGTripleStrLit, tkCharLit:
|
of tkIntLit..tkStrLit, tkRStrLit, tkTripleStrLit, tkGStrLit, tkGTripleStrLit, tkCharLit:
|
||||||
let lit = fileSection(em.config, em.fid, tok.offsetA, tok.offsetB)
|
let lit = fileSection(em.config, em.fid, tok.offsetA, tok.offsetB)
|
||||||
softLinebreak(em, lit)
|
softLinebreak(em, lit)
|
||||||
if endsInAlpha(em) and tok.tokType notin {tkGStrLit, tkGTripleStrLit}: wr(" ")
|
if endsInAlpha(em) and tok.tokType notin {tkGStrLit, tkGTripleStrLit}: wr(em, " ", ltSpaces)
|
||||||
em.lineSpan = countNewlines(lit)
|
em.lineSpan = countNewlines(lit)
|
||||||
if em.lineSpan > 0: calcCol(em, lit)
|
if em.lineSpan > 0: calcCol(em, lit)
|
||||||
wr lit
|
wr em, lit, ltLit
|
||||||
of tkEof: discard
|
of tkEof: discard
|
||||||
else:
|
else:
|
||||||
let lit = if tok.ident != nil: tok.ident.s else: tok.literal
|
let lit = if tok.ident != nil: tok.ident.s else: tok.literal
|
||||||
softLinebreak(em, lit)
|
softLinebreak(em, lit)
|
||||||
if endsInAlpha(em): wr(" ")
|
if endsInAlpha(em): wr(em, " ", ltSpaces)
|
||||||
wr lit
|
wr em, lit, ltIdent
|
||||||
|
|
||||||
em.lastTok = tok.tokType
|
em.lastTok = tok.tokType
|
||||||
em.lastTokWasTerse = lastTokWasTerse
|
em.lastTokWasTerse = lastTokWasTerse
|
||||||
|
|
@ -293,6 +319,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
em.lineSpan = 0
|
em.lineSpan = 0
|
||||||
|
|
||||||
proc starWasExportMarker*(em: var Emitter) =
|
proc starWasExportMarker*(em: var Emitter) =
|
||||||
|
em.kinds[^1] = ltExportMarker
|
||||||
if em.content.endsWith(" * "):
|
if em.content.endsWith(" * "):
|
||||||
setLen(em.content, em.content.len-3)
|
setLen(em.content, em.content.len-3)
|
||||||
em.content.add("*")
|
em.content.add("*")
|
||||||
|
|
|
||||||
|
|
@ -375,3 +375,12 @@ let res = execProcess(
|
||||||
"echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
"echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
||||||
|
|
||||||
let res = execProcess("echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
let res = execProcess("echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
||||||
|
|
||||||
|
|
||||||
|
# bug #10177
|
||||||
|
|
||||||
|
proc foo * () =
|
||||||
|
discard
|
||||||
|
|
||||||
|
proc foo* [T]() =
|
||||||
|
discard
|
||||||
|
|
|
||||||
|
|
@ -385,3 +385,12 @@ let res = execProcess(
|
||||||
|
|
||||||
let res = execProcess(
|
let res = execProcess(
|
||||||
"echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
"echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates")
|
||||||
|
|
||||||
|
|
||||||
|
# bug #10177
|
||||||
|
|
||||||
|
proc foo*() =
|
||||||
|
discard
|
||||||
|
|
||||||
|
proc foo*[T]() =
|
||||||
|
discard
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue