From 96d49608c05647ed5ea22a6b4321b864034bd495 Mon Sep 17 00:00:00 2001 From: narimiran Date: Tue, 9 Jul 2019 13:15:35 +0200 Subject: [PATCH] nimpretty: more comment indentation fixes --- compiler/layouter.nim | 13 +++++++------ nimpretty/tests/expected/tevil_spaces.nim | 13 +++++++++++++ nimpretty/tests/tevil_spaces.nim | 13 +++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/compiler/layouter.nim b/compiler/layouter.nim index 5d65c012f7..221afc78ce 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -307,7 +307,7 @@ proc emitMultilineComment(em: var Emitter, lit: string, col: int; dontIndent: bo var lastIndent = if em.keepIndents > 0: em.indentLevel else: em.indentStack[^1] var b = 0 var dontIndent = dontIndent - var lastWasEmpty = false + var hasEmptyLine = false for commentLine in splitLines(lit): if i == 0 and (commentLine.endsWith("\\") or commentLine.endsWith("[")): dontIndent = true @@ -322,7 +322,7 @@ proc emitMultilineComment(em: var Emitter, lit: string, col: int; dontIndent: bo wr(em, "", ltTab) elif stripped.len == 0: wrNewline em - lastWasEmpty = true + hasEmptyLine = true else: var a = 0 while a < commentLine.len and commentLine[a] == ' ': inc a @@ -334,13 +334,13 @@ proc emitMultilineComment(em: var Emitter, lit: string, col: int; dontIndent: bo b -= em.indWidth lastIndent = a wrNewline em - #wrSpaces em, col + b - if not lastWasEmpty or col + b < 15: + if not hasEmptyLine or col + b < 15: if col + b > 0: wr(em, repeat(' ', col+b), ltTab) else: wr(em, "", ltTab) - #lastWasEmpty = stripped.len == 0 + else: + wr(em, repeat(' ', a), ltSpaces) wr em, stripped, ltComment inc i @@ -363,7 +363,7 @@ proc endsInAlpha(em: Emitter): bool = result = if i >= 0: em.tokens[i].lastChar in SymChars+{'_'} else: false proc emitComment(em: var Emitter; tok: TToken; dontIndent: bool) = - let col = em.col + var col = em.col let lit = strip fileSection(em.config, em.fid, tok.commentOffsetA, tok.commentOffsetB) em.lineSpan = countNewlines(lit) if em.lineSpan > 0: calcCol(em, lit) @@ -374,6 +374,7 @@ proc emitComment(em: var Emitter; tok: TToken; dontIndent: bool) = else: if not endsInWhite(em): wrTab em + inc col emitMultilineComment(em, lit, col, dontIndent) proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = diff --git a/nimpretty/tests/expected/tevil_spaces.nim b/nimpretty/tests/expected/tevil_spaces.nim index 9c6b08854d..7d3a72dfd7 100644 --- a/nimpretty/tests/expected/tevil_spaces.nim +++ b/nimpretty/tests/expected/tevil_spaces.nim @@ -43,3 +43,16 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = if tue: foo() # comment here # end if + +proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = + ## Splits and distributes a sequence `s` into `num` sub-sequences. + let num = int(num) # XXX probably only needed because of .. bug + # This is part of the above. + result = newSeq[seq[T]](num) + +proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = + ## Splits and distributes a sequence `s` into `num` sub-sequences. + let num = int(num) # XXX probably only needed because of .. bug + + # This belongs below. + result = newSeq[seq[T]](num) diff --git a/nimpretty/tests/tevil_spaces.nim b/nimpretty/tests/tevil_spaces.nim index 31591643fd..33a13e559b 100644 --- a/nimpretty/tests/tevil_spaces.nim +++ b/nimpretty/tests/tevil_spaces.nim @@ -43,3 +43,16 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = if tue: foo() # comment here # end if + +proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = + ## Splits and distributes a sequence `s` into `num` sub-sequences. + let num = int(num) # XXX probably only needed because of .. bug + # This is part of the above. + result = newSeq[seq[T]](num) + +proc distribute*[T](s: seq[T], num: Positive, spread = true): seq[seq[T]] = + ## Splits and distributes a sequence `s` into `num` sub-sequences. + let num = int(num) # XXX probably only needed because of .. bug + + # This belongs below. + result = newSeq[seq[T]](num)