summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-06-19 23:45:11 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-06-19 23:45:11 +0200
commitc3090fcb48b0323cb7be79d2fcd009e11360dab5 (patch)
tree55478fb6f3959d331e7bebb6eff3f220da0c79fa
parent837d0c7270a67ea632d492586843807075eefb88 (diff)
downloadNim-c3090fcb48b0323cb7be79d2fcd009e11360dab5.tar.gz
nimpretty: don't produce trailing whitespace; fixes the rendering of unary operators
-rw-r--r--compiler/layouter.nim13
-rw-r--r--nimpretty/tests/exhaustive.nim2
-rw-r--r--nimpretty/tests/expected/exhaustive.nim12
3 files changed, 16 insertions, 11 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index eb591b6e1..d0c9a77b9 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -93,6 +93,8 @@ proc softLinebreak(em: var Emitter, lit: string) =
   # +2 because we blindly assume a comma or ' &' might follow
   if not em.inquote and em.col+lit.len+2 >= MaxLineLen:
     if em.lastTok in splitters:
+      while em.content.len > 0 and em.content[em.content.high] == ' ':
+        setLen(em.content, em.content.len-1)
       wr("\L")
       em.col = 0
       for i in 1..em.indentLevel+moreIndent(em): wr(" ")
@@ -100,8 +102,11 @@ proc softLinebreak(em: var Emitter, lit: string) =
       # search backwards for a good split position:
       for a in em.altSplitPos:
         if a > em.fixedUntil:
-          let ws = "\L" & repeat(' ',em.indentLevel+moreIndent(em) -
-              ord(em.content[a] == ' '))
+          var spaces = 0
+          while a+spaces < em.content.len and em.content[a+spaces] == ' ':
+            inc spaces
+          if spaces > 0: delete(em.content, a, a+spaces-1)
+          let ws = "\L" & repeat(' ',em.indentLevel+moreIndent(em))
           em.col = em.content.len - a
           em.content.insert(ws, a)
           break
@@ -186,8 +191,8 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
     wr(" ")
   of tkSemicolon, tkComma:
     wr(TokTypeToStr[tok.tokType])
-    wr(" ")
     rememberSplit(splitComma)
+    wr(" ")
   of tkParDotLe, tkParLe, tkBracketDotLe, tkBracketLe,
      tkCurlyLe, tkCurlyDotLe, tkBracketLeColon:
     if tok.strongSpaceA > 0 and not em.endsInWhite:
@@ -215,7 +220,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
       template isUnary(tok): bool =
         tok.strongSpaceB == 0 and tok.strongSpaceA > 0
 
-      if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}:
+      if not isUnary(tok):
         wr(" ")
         rememberSplit(splitBinary)
   of tkAccent:
diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim
index 5d37b0882..1039e308b 100644
--- a/nimpretty/tests/exhaustive.nim
+++ b/nimpretty/tests/exhaustive.nim
@@ -16,7 +16,7 @@ type
 var x*: string
 var y: seq[string] #[ yay inline comments. So nice I have to care bout these. ]#
 
-echo "#", x, "##", y, "#"
+echo "#", x, "##", y, "#" & "string" & $test
 
 echo (tup, here)
 echo(argA, argB)
diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim
index b8c55033c..8f8a03b0b 100644
--- a/nimpretty/tests/expected/exhaustive.nim
+++ b/nimpretty/tests/expected/exhaustive.nim
@@ -3,7 +3,7 @@ discard """
   exitcode: "1"
 """
 
-import verylongnamehere, verylongnamehere, 
+import verylongnamehere, verylongnamehere,
   verylongnamehereverylongnamehereverylong, namehere, verylongnamehere
 
 type
@@ -17,7 +17,7 @@ type
 var x*: string
 var y: seq[string] #[ yay inline comments. So nice I have to care bout these. ]#
 
-echo "#", x, "##", y, "#"
+echo "#", x, "##", y, "#" & "string" & $test
 
 echo (tup, here)
 echo(argA, argB)
@@ -104,7 +104,7 @@ type
     fixedUntil: int           # marks where we must not go in the content
     altSplitPos: array[SplitKind, int] # alternative split positions
 
-proc openEmitter*[T, S](em: var Emitter; config: ConfigRef; 
+proc openEmitter*[T, S](em: var Emitter; config: ConfigRef;
     fileIdx: FileIndex) {.pragmaHereWrongCurlyEnd.} =
   let outfile = changeFileExt(config.toFullPath(fileIdx), ".pretty.nim")
   em.f = llStreamOpen(outfile, fmWrite)
@@ -173,13 +173,13 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
     em.content.len > 0 and em.content[em.content.high] in SymChars+{'_'}
 
   proc emitComment(em: var Emitter; tok: TToken) =
-    let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA, 
+    let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA,
         tok.commentOffsetB)
     em.lineSpan = countNewlines(lit)
     if em.lineSpan > 0: calcCol(em, lit)
     if not endsInWhite(em):
       wr(" ")
-      if em.lineSpan == 0 and max(em.col, 
+      if em.lineSpan == 0 and max(em.col,
           LineCommentColumn) + lit.len <= MaxLineLen:
         for i in 1 .. LineCommentColumn - em.col: wr(" ")
     wr lit
@@ -264,7 +264,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
   of tkComment:
     if not preventComment:
       emitComment(em, tok)
-  of tkIntLit..tkStrLit, tkRStrLit, tkTripleStrLit, tkGStrLit, 
+  of tkIntLit..tkStrLit, tkRStrLit, tkTripleStrLit, tkGStrLit,
       tkGTripleStrLit, tkCharLit:
     let lit = fileSection(em.config, em.fid, tok.offsetA, tok.offsetB)
     softLinebreak(em, lit)