summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/layouter.nim20
-rw-r--r--compiler/lexer.nim4
-rw-r--r--compiler/parser.nim4
-rw-r--r--nimpretty/tests/exhaustive.nim12
-rw-r--r--nimpretty/tests/expected/exhaustive.nim12
5 files changed, 43 insertions, 9 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index d03105eb2..560e8eec7 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -31,7 +31,7 @@ type
     inquote: bool
     semicolons: SemicolonKind
     col, lastLineNumber, lineSpan, indentLevel, indWidth: int
-    inParamList*: int
+    keepIndents*: int
     doIndentMore*: int
     content: string
     indentStack: seq[int]
@@ -134,6 +134,22 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
         for i in 1 .. LineCommentColumn - em.col: wr(" ")
     wr lit
 
+  if tok.tokType == tkComment and tok.literal.startsWith("#!nimpretty"):
+    case tok.literal
+    of "#!nimpretty off":
+      inc em.keepIndents
+      wr("\L")
+      em.lastLineNumber = tok.line + 1
+    of "#!nimpretty on":
+      dec em.keepIndents
+      em.lastLineNumber = tok.line
+    wr("\L")
+    #for i in 1 .. tok.indent: wr " "
+    wr tok.literal
+    em.col = 0
+    em.lineSpan = 0
+    return
+
   var preventComment = false
   if tok.tokType == tkComment and tok.line == em.lastLineNumber and tok.indent >= 0:
     # we have an inline comment so handle it before the indentation token:
@@ -142,7 +158,7 @@ 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.inParamList > 0:
+    if em.lastTok in (splitters + oprSet) or em.keepIndents > 0:
       em.indentLevel = tok.indent
     else:
       if tok.indent > em.indentStack[^1]:
diff --git a/compiler/lexer.nim b/compiler/lexer.nim
index d9051bd7f..90ee98c9d 100644
--- a/compiler/lexer.nim
+++ b/compiler/lexer.nim
@@ -1114,7 +1114,9 @@ proc skip(L: var TLexer, tok: var TToken) =
         buf = L.buf
       else:
         tokenBegin(tok, pos)
-        while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}: inc(pos)
+        while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}:
+          when defined(nimpretty): tok.literal.add buf[pos]
+          inc(pos)
         tokenEndIgnore(tok, pos+1)
         when defined(nimpretty):
           tok.commentOffsetB = L.offsetBase + pos + 1
diff --git a/compiler/parser.nim b/compiler/parser.nim
index cf64ad555..61736c923 100644
--- a/compiler/parser.nim
+++ b/compiler/parser.nim
@@ -1037,7 +1037,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
   addSon(result, p.emptyNode) # return type
   when defined(nimpretty2):
     inc p.em.doIndentMore
-    inc p.em.inParamList
+    inc p.em.keepIndents
   let hasParLe = p.tok.tokType == tkParLe and p.tok.indent < 0
   if hasParLe:
     getTok(p)
@@ -1074,7 +1074,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
     result = p.emptyNode
   when defined(nimpretty2):
     dec p.em.doIndentMore
-    dec p.em.inParamList
+    dec p.em.keepIndents
 
 proc optPragmas(p: var TParser): PNode =
   if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)):
diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim
index cfb42e7a9..ab58b7da4 100644
--- a/nimpretty/tests/exhaustive.nim
+++ b/nimpretty/tests/exhaustive.nim
@@ -315,9 +315,17 @@ proc f() =
     # escape char
     str &= c
 
-const test = r"C:\Users\-\Desktop\test.txt"
-
 proc getKeyAndData(cursor: int, op: int):
     tuple[key, data: string, success: bool] {.noInit.} =
   var keyVal: string
   var dataVal: string
+
+#!nimpretty off
+  when stuff:
+    echo "so nice"
+    echo "more"
+  else:
+     echo "misaligned"
+#!nimpretty on
+
+const test = r"C:\Users\-\Desktop\test.txt"
diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim
index b9cb2e053..b114e9b8c 100644
--- a/nimpretty/tests/expected/exhaustive.nim
+++ b/nimpretty/tests/expected/exhaustive.nim
@@ -324,9 +324,17 @@ proc f() =
     # escape char
     str &= c
 
-const test = r"C:\Users\-\Desktop\test.txt"
-
 proc getKeyAndData(cursor: int; op: int):
     tuple[key, data: string; success: bool] {.noInit.} =
   var keyVal: string
   var dataVal: string
+
+#!nimpretty off
+  when stuff:
+    echo "so nice"
+    echo "more"
+  else:
+     echo "misaligned"
+#!nimpretty on
+
+const test = r"C:\Users\-\Desktop\test.txt"