diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/Imports.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/Imports.scala index 94134926cc..200f15960b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/Imports.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/Imports.scala @@ -386,7 +386,8 @@ object Imports extends RewriteFactory { var hadLf = false val slc = new ListBuffer[Token] ctx.tokenTraverser.findAtOrBefore(ctx.getIndex(tok) - 1) { - case _: Token.AtEOL => if (hadLf) Some(true) else { hadLf = true; None } + case t: Token.AtEOL => + if (hadLf || t.newlines > 1) Some(true) else { hadLf = true; None } case t: Token.Comment if TokenOps.isSingleLineIfComment(t) => slc.prepend(t); hadLf = false; None case _: Token.Whitespace => None diff --git a/scalafmt-tests/src/test/resources/rewrite/Imports.source b/scalafmt-tests/src/test/resources/rewrite/Imports.source index 160a4d34b2..b22ac9957a 100644 --- a/scalafmt-tests/src/test/resources/rewrite/Imports.source +++ b/scalafmt-tests/src/test/resources/rewrite/Imports.source @@ -973,3 +973,17 @@ import j.collections as collections >>> import java as j import j.collections as collections +<<< comment and blank before import +rewrite.imports.groups = [["qux"]] +=== +package foo { + // c1 + + import bar.baz +} +>>> +package foo { + // c1 + + import bar.baz +}