Skip to content

Commit

Permalink
nimpretty: fixes nim-lang#9504
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored and narimiran committed Oct 31, 2018
1 parent 56f376d commit 44415e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
13 changes: 10 additions & 3 deletions compiler/layouter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type
config: ConfigRef
fid: FileIndex
lastTok: TTokType
inquote: bool
inquote, lastTokWasTerse: bool
semicolons: SemicolonKind
col, lastLineNumber, lineSpan, indentLevel, indWidth: int
keepIndents*: int
Expand Down Expand Up @@ -192,12 +192,13 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
wr(" ")
em.fixedUntil = em.content.high

var lastTokWasTerse = false
case tok.tokType
of tokKeywordLow..tokKeywordHigh:
if endsInAlpha(em):
wr(" ")
elif not em.inquote and not endsInWhite(em) and
em.lastTok notin openPars:
em.lastTok notin openPars and not em.lastTokWasTerse:
#and tok.tokType in oprSet
wr(" ")

Expand Down Expand Up @@ -233,14 +234,19 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
tkBracketDotRi,
tkCurlyDotRi,
tkParDotRi,
tkColonColon, tkDot:
tkColonColon:
wr(TokTypeToStr[tok.tokType])
of tkDot:
lastTokWasTerse = true
wr(TokTypeToStr[tok.tokType])
of tkEquals:
if not em.inquote and not em.endsInWhite: wr(" ")
wr(TokTypeToStr[tok.tokType])
if not em.inquote: wr(" ")
of tkOpr, tkDotDot:
if tok.strongSpaceA == 0 and tok.strongSpaceB == 0:
# bug #9504: remember to not spacify a keyword:
lastTokWasTerse = true
# if not surrounded by whitespace, don't produce any whitespace either:
wr(tok.ident.s)
else:
Expand Down Expand Up @@ -274,6 +280,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
wr lit

em.lastTok = tok.tokType
em.lastTokWasTerse = lastTokWasTerse
em.lastLineNumber = tok.line + em.lineSpan
em.lineSpan = 0

Expand Down
5 changes: 5 additions & 0 deletions nimpretty/tests/exhaustive.nim
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,8 @@ proc getKeyAndData(cursor: int, op: int):
#!nimpretty on

const test = r"C:\Users\-\Desktop\test.txt"

proc abcdef*[T:not (tuple|object|string|cstring|char|ref|ptr|array|seq|distinct)]() =
# bug #9504
type T2 = a.type
discard
5 changes: 5 additions & 0 deletions nimpretty/tests/expected/exhaustive.nim
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,8 @@ proc getKeyAndData(cursor: int; op: int):
#!nimpretty on

const test = r"C:\Users\-\Desktop\test.txt"

proc abcdef*[T: not (tuple|object|string|cstring|char|ref|ptr|array|seq|distinct)]() =
# bug #9504
type T2 = a.type
discard

0 comments on commit 44415e5

Please sign in to comment.