summary refs log tree commit diff stats
path: root/compiler
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 /compiler
parent2c03c9f42e882eaff4f8e8fa358b9c66ecdd803a (diff)
downloadNim-9f82e07b2dc2ea89e1e35d5ec470fae84eab66b4.tar.gz
nimpretty: fixes #10156 [bugfix]
Diffstat (limited to 'compiler')
-rw-r--r--compiler/layouter.nim31
1 files changed, 30 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