From f072543abf0af225af06b9ac2045482ae2620749 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:40:03 +0300 Subject: [PATCH 1/2] Test Imports with comment + blank + import --- .../src/test/resources/rewrite/Imports.source | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scalafmt-tests/src/test/resources/rewrite/Imports.source b/scalafmt-tests/src/test/resources/rewrite/Imports.source index 160a4d34b2..87f263eaa2 100644 --- a/scalafmt-tests/src/test/resources/rewrite/Imports.source +++ b/scalafmt-tests/src/test/resources/rewrite/Imports.source @@ -973,3 +973,16 @@ 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 +} From 88c478a6beab4615e7bb66fda1f1a837c4494188 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:30:08 +0300 Subject: [PATCH 2/2] Imports: improve blank line detection --- .../shared/src/main/scala/org/scalafmt/rewrite/Imports.scala | 3 ++- scalafmt-tests/src/test/resources/rewrite/Imports.source | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) 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 87f263eaa2..b22ac9957a 100644 --- a/scalafmt-tests/src/test/resources/rewrite/Imports.source +++ b/scalafmt-tests/src/test/resources/rewrite/Imports.source @@ -984,5 +984,6 @@ package foo { >>> package foo { // c1 + import bar.baz }