Skip to content

Commit

Permalink
Rewrite: remove one NL even if found many
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 6, 2024
1 parent e8a741a commit 33fdb31
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,8 @@ object Imports extends RewriteFactory {

protected final def getCommentAfter(tok: Token): Option[Token] = ctx
.tokenTraverser.findAtOrAfter(ctx.getIndex(tok) + 1) {
case _: Token.AtEOL => Some(false)
case t: Token.Comment if TokenOps.isSingleLineIfComment(t) => Some(true)
case _: Token.Whitespace | _: Token.Comma => None
case _: Token.HSpace | _: Token.Comma => None
case t: Token.Comment => Some(TokenOps.isSingleLineIfComment(t))
case _ => Some(false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@ case class RewriteCtx(style: ScalafmtConfig, input: Input, tree: Tree) {
def removeLFToAvoidEmptyLine(beg: Int, end: Int)(implicit
builder: Rewrite.PatchBuilder,
): Unit = if (onlyWhitespaceBefore(beg)) tokenTraverser.findAtOrAfter(end + 1) {
case _: T.EOL => Some(true)
case t: T.AtEOL =>
if (t.newlines > 1) builder += TokenPatch.Replace(t, t.text.stripLineEnd)
else builder += TokenPatch.Remove(t)
Some(false)
case _: T.HSpace => None
case _ => Some(false)
}.map(TokenPatch.Remove).foreach(builder += _)
}

// special case for Select which might contain a space instead of dot
def isPrefixExpr(expr: Tree): Boolean = RewriteCtx
Expand Down
12 changes: 7 additions & 5 deletions scalafmt-tests/shared/src/test/resources/rewrite/Imports.source
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,10 @@ import scala.concurrent.Future
// format: on
class Test {}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
-
// format: off
import scala.concurrent.Future

// format: off
/** Something
*/
// format: on
class Test {}

0 comments on commit 33fdb31

Please sign in to comment.