summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorMiran <narimiran@disroot.org>2019-07-06 00:21:21 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-07-06 00:21:21 +0200
commit34c09a98c7ffc5210861a9d67add681b16049c40 (patch)
tree9ee9046a8a7840aec49ba2878e935a97203b501c /compiler
parent7d5d9f7703901e092422d682f7ab21995cfa1122 (diff)
downloadNim-34c09a98c7ffc5210861a9d67add681b16049c40.tar.gz
nimpretty: improved detection of commas and semicolons (#11661)
* nimpretty: improved detection of commas and semicolons

* address the comments
Diffstat (limited to 'compiler')
-rw-r--r--compiler/layouter.nim15
1 files changed, 11 insertions, 4 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim
index b54fb93bf..a6891f564 100644
--- a/compiler/layouter.nim
+++ b/compiler/layouter.nim
@@ -516,6 +516,11 @@ proc endsWith(em: Emitter; k: varargs[string]): bool =
     if em.tokens[em.tokens.len - k.len + i] != k[i]: return false
   return true
 
+proc rfind(em: Emitter, t: string): int =
+  for i in 1 .. 5:
+    if em.tokens[^i] == t:
+      return i
+
 proc starWasExportMarker*(em: var Emitter) =
   if em.endsWith(" ", "*", " "):
     setLen(em.tokens, em.tokens.len-3)
@@ -526,11 +531,13 @@ proc starWasExportMarker*(em: var Emitter) =
 
 proc commaWasSemicolon*(em: var Emitter) =
   if em.semicolons == detectSemicolonKind:
-    em.semicolons = if em.endsWith(",", " "): dontTouch else: useSemicolon
-  if em.semicolons == useSemicolon and em.endsWith(",", " "):
-    em.tokens[em.tokens.len-2] = ";"
+    em.semicolons = if em.rfind(";") > 0: useSemicolon else: dontTouch
+  if em.semicolons == useSemicolon:
+    let commaPos = em.rfind(",")
+    if commaPos > 0:
+      em.tokens[^commaPos] = ";"
 
 proc curlyRiWasPragma*(em: var Emitter) =
   if em.endsWith("}"):
-    em.tokens[em.tokens.len-1] = ".}"
+    em.tokens[^1] = ".}"
     inc em.col