Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add a new line in multilevel chained package clause #1594

Merged
merged 3 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this pattern match package a.b.c { as well? there might an Apply there somewhere.

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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be an empty line here as well? perhaps it was this way before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, scalafmt does not change formatting between import and class and keeps an empty line (if it present) as is.