summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/lexer.nim11
-rw-r--r--nimpretty/tests/expected/simple2.nim1
-rw-r--r--nimpretty/tests/simple2.nim1
3 files changed, 8 insertions, 5 deletions
diff --git a/compiler/lexer.nim b/compiler/lexer.nim
index 8762bac49..c62671ee9 100644
--- a/compiler/lexer.nim
+++ b/compiler/lexer.nim
@@ -224,7 +224,7 @@ proc openLexer*(lex: var TLexer, fileIdx: FileIndex, inputstream: PLLStream;
                  cache: IdentCache; config: ConfigRef) =
   openBaseLexer(lex, inputstream)
   lex.fileIdx = fileidx
-  lex.indentAhead = - 1
+  lex.indentAhead = -1
   lex.currLineIndent = 0
   inc(lex.lineNumber, inputstream.lineOffset)
   lex.cache = cache
@@ -1120,6 +1120,7 @@ proc skip(L: var TLexer, tok: var TToken) =
     var hasComment = false
     tok.commentOffsetA = L.offsetBase + pos
     tok.commentOffsetB = tok.commentOffsetA
+    tok.line = -1
   while true:
     case buf[pos]
     of ' ':
@@ -1130,9 +1131,6 @@ proc skip(L: var TLexer, tok: var TToken) =
       inc(pos)
     of CR, LF:
       tokenEndPrevious(tok, pos)
-      when defined(nimpretty):
-        # we are not yet in a comment, so update the comment token's line information:
-        if not hasComment: inc tok.line
       pos = handleCRLF(L, pos)
       buf = L.buf
       var indent = 0
@@ -1155,7 +1153,10 @@ proc skip(L: var TLexer, tok: var TToken) =
     of '#':
       # do not skip documentation comment:
       if buf[pos+1] == '#': break
-      when defined(nimpretty): hasComment = true
+      when defined(nimpretty):
+        hasComment = true
+        if tok.line < 0: tok.line = L.lineNumber
+
       if buf[pos+1] == '[':
         skipMultiLineComment(L, tok, pos+2, false)
         pos = L.bufpos
diff --git a/nimpretty/tests/expected/simple2.nim b/nimpretty/tests/expected/simple2.nim
index 8d60c4067..e265aaff0 100644
--- a/nimpretty/tests/expected/simple2.nim
+++ b/nimpretty/tests/expected/simple2.nim
@@ -1,3 +1,4 @@
+# comment here
 var x: int = 2
 
 echo x
diff --git a/nimpretty/tests/simple2.nim b/nimpretty/tests/simple2.nim
index 8d60c4067..e265aaff0 100644
--- a/nimpretty/tests/simple2.nim
+++ b/nimpretty/tests/simple2.nim
@@ -1,3 +1,4 @@
+# comment here
 var x: int = 2
 
 echo x