summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/layouter.nim23
-rw-r--r--nimpretty/tests/exhaustive.nim25
-rw-r--r--nimpretty/tests/expected/exhaustive.nim30
3 files changed, 71 insertions, 7 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index 2bf0552ff..367d2aead 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -153,8 +153,15 @@ proc lenOfNextTokens(em: Emitter; pos: int): int =
     if em.kinds[pos+i] in {ltCrucialNewline, ltSplittingNewline, ltOptionalNewline}: break
     inc result, em.tokens[pos+i].len
 
-proc lacksGuidingInd(em: Emitter; pos: int): bool =
-  result = true
+proc guidingInd(em: Emitter; pos: int): int =
+  var i = pos - 1
+  while i >= 0 and em.kinds[i] != ltSomeParLe:
+    dec i
+  while i+1 <= em.kinds.high and em.kinds[i] != ltSomeParRi:
+    if em.kinds[i] == ltSplittingNewline and em.kinds[i+1] == ltSpaces:
+      return em.tokens[i+1].len
+    inc i
+  result = -1
 
 proc closeEmitter*(em: var Emitter) =
   template defaultCase() =
@@ -209,11 +216,13 @@ proc closeEmitter*(em: var Emitter) =
           let spaces = em.tokens[i-1].len
           content.setLen(content.len - spaces)
         content.add "\L"
-        content.add em.tokens[i]
-        lineLen = em.tokens[i].len
-        if false: # openPars == 0 or lacksGuidingInd(em, i):
-          content.add repeat(' ', em.indWidth*2)
-          lineLen += em.indWidth*2
+        let guide = if openPars > 0: guidingInd(em, i) else: -1
+        if guide >= 0:
+          content.add repeat(' ', guide)
+          lineLen = guide
+        else:
+          content.add em.tokens[i]
+          lineLen = em.tokens[i].len
         lineBegin = i+1
         if i+1 < em.kinds.len and em.kinds[i+1] == ltSpaces:
           # inhibit extra spaces at the start of a new line
diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim
index 7dde99ca5..40ffd4f94 100644
--- a/nimpretty/tests/exhaustive.nim
+++ b/nimpretty/tests/exhaustive.nim
@@ -761,3 +761,28 @@ var rows2 = await pool.rows(sql"""
     "BBBB"
   ]
 )
+
+
+# bug #11699
+
+const keywords = @[
+  "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar",
+  "zzz", "ggg", "ddd",
+]
+
+let keywords1 = @[
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5", "bar5", "foo6", "bar6", "foo7",
+  "zzz", "ggg", "ddd",
+]
+
+let keywords2 = @[
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5", "bar5", "foo6", "bar6", "foo7",
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5", "bar5", "foo6", "bar6", "foo7",
+  "zzz", "ggg", "ddd",
+]
+
+if true:
+  let keywords3 = @[
+    "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5", "bar5", "foo6", "bar6", "foo7",
+    "zzz", "ggg", "ddd",
+  ]
diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim
index 79f0ad05a..25521e970 100644
--- a/nimpretty/tests/expected/exhaustive.nim
+++ b/nimpretty/tests/expected/exhaustive.nim
@@ -769,3 +769,33 @@ var rows2 = await pool.rows(sql"""
     "BBBB"
   ]
 )
+
+
+# bug #11699
+
+const keywords = @[
+  "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo",
+  "bar", "foo", "bar",
+  "zzz", "ggg", "ddd",
+]
+
+let keywords1 = @[
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5",
+  "bar5", "foo6", "bar6", "foo7",
+  "zzz", "ggg", "ddd",
+]
+
+let keywords2 = @[
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5",
+  "bar5", "foo6", "bar6", "foo7",
+  "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5",
+  "bar5", "foo6", "bar6", "foo7",
+  "zzz", "ggg", "ddd",
+]
+
+if true:
+  let keywords3 = @[
+    "foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5",
+    "bar5", "foo6", "bar6", "foo7",
+    "zzz", "ggg", "ddd",
+  ]