summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2019-06-08 00:00:00 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-06-08 00:00:00 +0200
commit9f82e07b2dc2ea89e1e35d5ec470fae84eab66b4 (patch)
tree0a9654fa2dbb34d9a58bd725d7d5d805279eed86
parent2c03c9f42e882eaff4f8e8fa358b9c66ecdd803a (diff)
downloadNim-9f82e07b2dc2ea89e1e35d5ec470fae84eab66b4.tar.gz
nimpretty: fixes #10156 [bugfix]
-rw-r--r--compiler/layouter.nim31
-rw-r--r--nimpretty/tests/expected/wrong_ind.nim49
-rw-r--r--nimpretty/tests/wrong_ind.nim49
3 files changed, 128 insertions, 1 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index 336b09fa7..0b88a4aea 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -156,6 +156,31 @@ proc softLinebreak(em: var Emitter, lit: string) =
           a = -1
           break
 
+proc emitMultilineComment(em: var Emitter, lit: string, col: int) =
+  # re-align every line in the multi-line comment:
+  var i = 0
+  var lastIndent = em.indentStack[^1]
+  var b = 0
+  for commentLine in splitLines(lit):
+    let stripped = commentLine.strip()
+    var a = 0
+    while a < commentLine.len and commentLine[a] == ' ': inc a
+    if i == 0:
+      discard
+    elif stripped.len == 0:
+      wr em, "\L", ltNewline
+    else:
+      if a > lastIndent:
+        b += em.indWidth
+        lastIndent = a
+      elif a < lastIndent:
+        b -= em.indWidth
+        lastIndent = a
+      wr em, "\L", ltNewline
+      for i in 1 .. col + b: wr(em, " ", ltSpaces)
+    wr em, stripped, ltComment
+    inc i
+
 proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
 
   template endsInWhite(em): bool =
@@ -167,6 +192,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
     em.kinds.len > 0 and em.kinds[^1] == ltExportMarker
 
   proc emitComment(em: var Emitter; tok: TToken) =
+    let col = em.col
     let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA, tok.commentOffsetB)
     em.lineSpan = countNewlines(lit)
     if em.lineSpan > 0: calcCol(em, lit)
@@ -174,7 +200,10 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
       wr(em, " ", ltSpaces)
       if em.lineSpan == 0 and max(em.col, LineCommentColumn) + lit.len <= MaxLineLen:
         for i in 1 .. LineCommentColumn - em.col: wr(em, " ", ltSpaces)
-    wr em, lit, ltComment
+    if em.lineSpan == 0:
+      wr em, lit, ltComment
+    else:
+      emitMultilineComment(em, lit, col)
 
   if tok.tokType == tkComment and tok.literal.startsWith("#!nimpretty"):
     case tok.literal
diff --git a/nimpretty/tests/expected/wrong_ind.nim b/nimpretty/tests/expected/wrong_ind.nim
index a21e94618..9855e0c4d 100644
--- a/nimpretty/tests/expected/wrong_ind.nim
+++ b/nimpretty/tests/expected/wrong_ind.nim
@@ -22,3 +22,52 @@ proc funB() =
       3
     ]
     discard
+
+
+# bug #10156
+proc foo =
+    ## Comment 1
+    ## Comment 2
+    discard
+
+proc bar =
+    ## Comment 3
+    ## Comment 4
+    ## More here.
+    discard
+
+
+proc barB =
+    # Comment 5
+    # Comment 6
+    discard
+
+
+var x: int = 2
+
+echo x
+# bug #9144
+
+proc a() =
+    if cond:
+        while true:
+            discard
+            # comment 1
+        # end while
+    #end if
+
+    # comment 2
+        #if
+            #case
+            #end case
+        #end if
+    discard
+
+
+proc a() =
+    while true:
+        discard
+        # comment 1
+
+    # comment 2
+    discard
diff --git a/nimpretty/tests/wrong_ind.nim b/nimpretty/tests/wrong_ind.nim
index 0559673f8..eaa561b51 100644
--- a/nimpretty/tests/wrong_ind.nim
+++ b/nimpretty/tests/wrong_ind.nim
@@ -22,3 +22,52 @@ proc funB() =
     3
   ]
   discard
+
+
+# bug #10156
+proc foo =
+    ## Comment 1
+    ## Comment 2
+    discard
+
+proc bar =
+  ## Comment 3
+  ## Comment 4
+  ## More here.
+  discard
+
+
+proc barB =
+  # Comment 5
+  # Comment 6
+  discard
+
+
+var x: int = 2
+
+echo x
+# bug #9144
+
+proc a() =
+  if cond:
+    while true:
+      discard
+      # comment 1
+    # end while
+  #end if
+
+  # comment 2
+    #if
+      #case
+      #end case
+    #end if
+  discard
+
+
+proc a() =
+  while true:
+    discard
+    # comment 1
+
+  # comment 2
+  discard