From de17ac6f0fddd2ae006f79ef007f43c0a7072f3a Mon Sep 17 00:00:00 2001 From: Maksim Ochenashko Date: Sat, 7 Dec 2019 10:57:01 +0200 Subject: [PATCH 1/3] Do not add a new line in multilevel chained package clause --- .../org/scalafmt/internal/FormatWriter.scala | 15 +++++++++++++-- .../alwaysBeforeTopLevelStatements.source | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index a5d969f3cd..a8b829ff01 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -259,24 +259,35 @@ class FormatWriter(formatOps: FormatOps) { } initStyle.newlines.alwaysBeforeTopLevelStatements && { val formatToken = toks(i).formatToken + def checkPackage: Option[Boolean] = if (!initStyle.activeForEdition_2019_11) None else Some(owners(formatToken.left)) .collect { case term: Term.Name => term.parent } .flatten - .collect { case pkg: Pkg => pkg.stats.headOption } + .collect { + // package a + case pkg: Pkg => + pkg.stats.headOption + + // package a.b.c + case select: Term.Select => + select.parent.collect { case pkg: Pkg => pkg.stats.headOption }.flatten + } .flatten .map { case pkg: Pkg => next(pkg.ref.tokens.last).is[T.LeftBrace] case _ => true } - def checkTopLevelStatement(): Boolean = + + def checkTopLevelStatement: Boolean = topLevelTokens.contains(hash(formatToken.right)) && { val (distance, FormatToken(_, nextNonComment, _)) = nextNonCommentWithCount(formatToken) isMultiline(actualOwner(nextNonComment).tokens.last, i + distance + 1) } + checkPackage.getOrElse(checkTopLevelStatement) } } diff --git a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source index f5ba1b864a..329a4bd76a 100644 --- a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source +++ b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source @@ -36,3 +36,16 @@ package b { import a.b.c class d {} } +<<< #mytest +package a.b.c +package d.e +package f +import g.h.i.j +package k {} +>>> +package a.b.c +package d.e +package f + +import g.h.i.j +package k {} \ No newline at end of file From 38e99ed3a43cc72bb05cf8173bd7bf774737aabf Mon Sep 17 00:00:00 2001 From: Maksim Ochenashko Date: Sat, 7 Dec 2019 15:22:34 +0200 Subject: [PATCH 2/3] Change test name --- .../resources/newlines/alwaysBeforeTopLevelStatements.source | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source index 329a4bd76a..d01b2cd9ef 100644 --- a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source +++ b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source @@ -36,7 +36,7 @@ package b { import a.b.c class d {} } -<<< #mytest +<<< #1594 package a.b.c package d.e package f From 638c2b04569181ab748d84144353a48c0f335f2f Mon Sep 17 00:00:00 2001 From: Maksim Ochenashko Date: Sat, 7 Dec 2019 15:50:03 +0200 Subject: [PATCH 3/3] Add additional test --- .../alwaysBeforeTopLevelStatements.source | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source index d01b2cd9ef..23bc0b23f8 100644 --- a/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source +++ b/scalafmt-tests/src/test/resources/newlines/alwaysBeforeTopLevelStatements.source @@ -36,7 +36,7 @@ package b { import a.b.c class d {} } -<<< #1594 +<<< #1593 1 package a.b.c package d.e package f @@ -48,4 +48,25 @@ package d.e package f import g.h.i.j -package k {} \ No newline at end of file +package k {} +<<< #1593 2 +package a.b.c +package d.e +package f +import g.h.i.j + +package k.l.m { + import n.o.p + class q {} +} +>>> +package a.b.c +package d.e +package f + +import g.h.i.j + +package k.l.m { + import n.o.p + class q {} +} \ No newline at end of file