summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/layouter.nim30
-rw-r--r--lib/pure/parseopt.nim10
-rw-r--r--nimpretty/tests/exhaustive.nim39
-rw-r--r--nimpretty/tests/expected/exhaustive.nim39
4 files changed, 106 insertions, 12 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index 112c57542..546d4766e 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -143,14 +143,21 @@ proc closeEmitter*(em: var Emitter) =
       maxLhs = computeMax(em, lineBegin)
     of ltEndSection:
       maxLhs = 0
+      lineBegin = i+1
     of ltTab:
-      if maxLhs == 0 or computeRhs(em, i+1)+maxLhs > MaxLineLen:
+      if maxLhs == 0:
         content.add em.tokens[i]
         inc lineLen, em.tokens[i].len
       else:
-        let spaces = maxLhs - lineLen #max(maxLhs - lineLen, 1)
-        for j in 1..spaces: content.add ' '
-        inc lineLen, spaces
+        # pick the shorter indentation token:
+        var spaces = maxLhs - lineLen
+        if spaces < em.tokens[i].len or computeRhs(em, i+1)+maxLhs <= MaxLineLen:
+          if spaces <= 0 and content[^1] notin {' ', '\L'}: spaces = 1
+          for j in 1..spaces: content.add ' '
+          inc lineLen, spaces
+        else:
+          content.add em.tokens[i]
+          inc lineLen, em.tokens[i].len
     of ltNewline:
       content.add em.tokens[i]
       lineLen = 0
@@ -217,8 +224,16 @@ proc wrSpace(em: var Emitter) =
 proc wrTab(em: var Emitter) =
   wr(em, " ", ltTab)
 
-proc beginSection*(em: var Emitter) = wr(em, "", ltBeginSection)
-proc endSection*(em: var Emitter) = wr(em, "", ltEndSection)
+proc beginSection*(em: var Emitter) =
+  em.tokens.insert "", em.tokens.len-2
+  em.kinds.insert ltBeginSection, em.kinds.len-2
+
+#wr(em, "", ltBeginSection)
+proc endSection*(em: var Emitter) =
+  em.tokens.insert "", em.tokens.len-2
+  em.kinds.insert ltEndSection, em.kinds.len-2
+
+#wr(em, "", ltEndSection)
 
 proc removeSpaces(em: var Emitter) =
   while em.kinds.len > 0 and em.kinds[^1] == ltSpaces:
@@ -262,7 +277,8 @@ proc emitMultilineComment(em: var Emitter, lit: string, col: int) =
     var a = 0
     while a < commentLine.len and commentLine[a] == ' ': inc a
     if i == 0:
-      wr(em, "", ltTab)
+      if em.kinds.len > 0 and em.kinds[^1] != ltTab:
+        wr(em, "", ltTab)
     elif stripped.len == 0:
       wrNewline em
     else:
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index 97b3fd51d..fab32060a 100644
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -160,11 +160,11 @@ type
     cmdArgument,              ## An argument such as a filename
     cmdLongOption,            ## A long option such as --option
     cmdShortOption            ## A short option such as -c
-  OptParser* =
-      object of RootObj ## Implementation of the command line parser.
-      ##
-      ## To initialize it, use the
-      ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
+  OptParser* = object of RootObj ## \
+    ## Implementation of the command line parser.
+    ##
+    ## To initialize it, use the
+    ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
     pos*: int
     inShortState: bool
     allowWhitespaceAfterColon: bool
diff --git a/nimpretty/tests/exhaustive.nim b/nimpretty/tests/exhaustive.nim
index 14c318495..ffdc95cca 100644
--- a/nimpretty/tests/exhaustive.nim
+++ b/nimpretty/tests/exhaustive.nim
@@ -618,3 +618,42 @@ veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureV
 
 veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureValue1,
                                futureValue2, node)
+
+type
+  CmdLineKind* = enum         ## The detected command line token.
+    cmdEnd,                   ## End of command line reached
+    cmdArgument,              ## An argument such as a filename
+    cmdLongOption,            ## A long option such as --option
+    cmdShortOption            ## A short option such as -c
+  OptParser* = object of RootObj ## \
+    ## Implementation of the command line parser. Here is even more text yad.
+    ##
+    ## To initialize it, use the
+    ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
+    pos*: int
+    inShortState: bool
+    allowWhitespaceAfterColon: bool
+    shortNoVal: set[char]
+    longNoVal: seq[string]
+    cmds: seq[string]
+    idx: int
+    kind*: CmdLineKind        ## The detected command line token
+    key*, val*: TaintedString ## Key and value pair; the key is the option
+                              ## or the argument, and the value is not "" if
+                              ## the option was given a value
+
+  OptParserDifferently* = object of RootObj ## Implementation of the command line parser.
+    ##
+    ## To initialize it, use the
+    ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
+    pos*: int
+    inShortState: bool
+    allowWhitespaceAfterColon: bool
+    shortNoVal: set[char]
+    longNoVal: seq[string]
+    cmds: seq[string]
+    idx: int
+    kind*: CmdLineKind        ## The detected command line token
+    key*, val*: TaintedString ## Key and value pair; the key is the option
+                              ## or the argument, and the value is not "" if
+                              ## the option was given a value
diff --git a/nimpretty/tests/expected/exhaustive.nim b/nimpretty/tests/expected/exhaustive.nim
index 21ff505dd..4d0366afb 100644
--- a/nimpretty/tests/expected/exhaustive.nim
+++ b/nimpretty/tests/expected/exhaustive.nim
@@ -624,3 +624,42 @@ veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1],
 
 veryLongVariableName.createVar("future" & $node[1][0].toStrLit, node[1], futureValue1,
                                futureValue2, node)
+
+type
+  CmdLineKind* = enum ## The detected command line token.
+    cmdEnd,           ## End of command line reached
+    cmdArgument,      ## An argument such as a filename
+    cmdLongOption,    ## A long option such as --option
+    cmdShortOption    ## A short option such as -c
+  OptParser* = object of RootObj ## \
+                                 ## Implementation of the command line parser. Here is even more text yad.
+                                 ##
+                                 ## To initialize it, use the
+                                 ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
+    pos*: int
+    inShortState: bool
+    allowWhitespaceAfterColon: bool
+    shortNoVal: set[char]
+    longNoVal: seq[string]
+    cmds: seq[string]
+    idx: int
+    kind*: CmdLineKind           ## The detected command line token
+    key*, val*: TaintedString    ## Key and value pair; the key is the option
+                                 ## or the argument, and the value is not "" if
+                                 ## the option was given a value
+
+  OptParserDifferently* = object of RootObj ## Implementation of the command line parser.
+                              ##
+                              ## To initialize it, use the
+                              ## `initOptParser proc<#initOptParser,string,set[char],seq[string]>`_.
+    pos*: int
+    inShortState: bool
+    allowWhitespaceAfterColon: bool
+    shortNoVal: set[char]
+    longNoVal: seq[string]
+    cmds: seq[string]
+    idx: int
+    kind*: CmdLineKind        ## The detected command line token
+    key*, val*: TaintedString ## Key and value pair; the key is the option
+                              ## or the argument, and the value is not "" if
+                              ## the option was given a value