From 667af87e66ed1ad4f3dbe455df72257d21fbc426 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 7 Jun 2019 12:59:31 +0200 Subject: [PATCH] nimpretty: fixes #9505 [bugfix] --- compiler/layouter.nim | 10 +++++++++- nimpretty/tests/expected/wrong_ind.nim | 24 ++++++++++++++++++++++++ nimpretty/tests/wrong_ind.nim | 24 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 nimpretty/tests/expected/wrong_ind.nim create mode 100644 nimpretty/tests/wrong_ind.nim diff --git a/compiler/layouter.nim b/compiler/layouter.nim index ddcb5a9762..336b09fa71 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -113,6 +113,10 @@ const openPars = {tkParLe, tkParDotLe, tkBracketLe, tkBracketLeColon, tkCurlyDotLe, tkCurlyLe} + closedPars = {tkParRi, tkParDotRi, + tkBracketRi, tkCurlyDotRi, + tkCurlyRi} + splitters = openPars + {tkComma, tkSemicolon} oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor} @@ -196,8 +200,12 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = em.fixedUntil = em.content.high elif tok.indent >= 0: - if em.lastTok in (splitters + oprSet) or em.keepIndents > 0: + if em.keepIndents > 0: em.indentLevel = tok.indent + elif (em.lastTok in (splitters + oprSet) and tok.tokType notin closedPars): + # aka: we are in an expression context: + let alignment = tok.indent - em.indentStack[^1] + em.indentLevel = alignment + em.indentStack.high * em.indWidth else: if tok.indent > em.indentStack[^1]: em.indentStack.add tok.indent diff --git a/nimpretty/tests/expected/wrong_ind.nim b/nimpretty/tests/expected/wrong_ind.nim new file mode 100644 index 0000000000..a21e94618d --- /dev/null +++ b/nimpretty/tests/expected/wrong_ind.nim @@ -0,0 +1,24 @@ + +# bug #9505 + +import std/[ + strutils, ospaths, os +] +import pkg/[ + regex +] + +proc fun() = + let a = [ + 1, + 2, + ] + discard + +proc funB() = + let a = [ + 1, + 2, + 3 + ] + discard diff --git a/nimpretty/tests/wrong_ind.nim b/nimpretty/tests/wrong_ind.nim new file mode 100644 index 0000000000..0559673f89 --- /dev/null +++ b/nimpretty/tests/wrong_ind.nim @@ -0,0 +1,24 @@ + +# bug #9505 + +import std/[ + strutils, ospaths, os +] +import pkg/[ + regex +] + +proc fun() = + let a = [ + 1, + 2, + ] + discard + +proc funB() = + let a = [ + 1, + 2, + 3 + ] + discard