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

Router: add missing single-line policy for .select #4250

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -1622,7 +1622,7 @@ class Router(formatOps: FormatOps) {
case _ => Some(false)
}.isDefined => Seq(Split(NoSplit, 0))

case FormatToken(left, _: T.Dot, GetSelectLike.OnRight(thisSelect)) =>
case FormatToken(left, r: T.Dot, GetSelectLike.OnRight(thisSelect)) =>
val enclosed = style.encloseSelectChains
val (expireTree, nextSelect) =
findLastApplyAndNextSelect(rightOwner, enclosed)
Expand Down Expand Up @@ -1670,6 +1670,18 @@ class Router(formatOps: FormatOps) {
}
}
}

def getSlbEnd() = {
val nft = nextNonCommentSameLineAfter(ft)
val eft = if (nft.noBreak) nextNonCommentSameLineAfter(nft) else nft
rhsOptimalToken(eft)
}
def shouldKillOnFail() = !(prevSelect.isEmpty && nextSelect.isEmpty) &&
(style.binPack.callSite ne BinPack.Site.Never) &&
findTreeWithParentSimple(rightOwner) {
_.isAny[Term.Interpolate, Pat.Interpolate]
}.isEmpty

val ftAfterRight = tokens(ft, 2)
val baseSplits = style.newlines.getSelectChains match {
case Newlines.classic =>
Expand Down Expand Up @@ -1759,16 +1771,25 @@ class Router(formatOps: FormatOps) {
case _ if left.is[T.Comment] => Seq(Split(Space.orNL(noBreak), 0))

case Newlines.keep =>
if (noBreak) Seq(
Split(NoSplit, 0),
if (hasBreak()) Seq(Split(Newline, 0))
else if (hasBreakAfterRightBeforeNonComment(ft))
Seq(Split(NoSplit, 0).withPolicy(
decideNewlinesOnlyAfterClose(Split(Newline, 0))(r),
ignore = next(ft).right.is[T.Comment],
))
else Seq(
Split(NoSplit, 0)
.withSingleLine(getSlbEnd(), killOnFail = shouldKillOnFail()),
Split(Newline, Constants.ExceedColumnPenalty * 3),
)
else Seq(Split(Newline, 0))

case Newlines.unfold =>
val nlCost = if (nlOnly) 0 else 1
if (prevSelect.isEmpty && nextSelect.isEmpty)
Seq(Split(nlOnly, 0)(NoSplit), Split(Newline, nlCost))
if (prevSelect.isEmpty && nextSelect.isEmpty) Seq(
Split(nlOnly, 0)(NoSplit)
.withSingleLine(getSlbEnd(), killOnFail = shouldKillOnFail()),
Split(Newline, nlCost),
)
else Seq(
Split(nlOnly, 0)(NoSplit)
.withSingleLine(expire, noSyntaxNL = true),
Expand All @@ -1778,21 +1799,22 @@ class Router(formatOps: FormatOps) {

case Newlines.fold =>
val nlCost = if (nlOnly) 0 else 1
val noSplitBase = Split(nlOnly, 0)(NoSplit)
val nlSplitBase = Split(NewlineT(alt = Some(NoSplit)), nlCost)
def nlSplitBase(implicit fileLine: FileLine) =
Split(NewlineT(alt = Some(NoSplit)), nlCost)
if (nextDotIfSig.isEmpty) {
val noSplit =
if (nlOnly) noSplitBase
if (nlOnly) Split.ignored
else {
val end = nextSelect
.fold(expire)(x => getLastNonTrivialToken(x.qual))
def exclude = insideBracesBlock(ft, end, true)
noSplitBase.withSingleLine(end, exclude)
val exclude = insideBracesBlock(ft, end, parensToo = true)
Split(NoSplit, 0).withSingleLine(end, exclude)
}
Seq(noSplit, nlSplitBase)
} else {
val policy: Policy = forcedBreakOnNextDotPolicy
Seq(noSplitBase.withPolicy(policy), nlSplitBase.withPolicy(policy))
val noSplit = Split(nlOnly, 0)(NoSplit).withPolicy(policy)
Seq(noSplit, nlSplitBase.withPolicy(policy))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ case class Split(
fexpire: => Token,
exclude: => TokenRanges = TokenRanges.empty,
noSyntaxNL: Boolean = false,
killOnFail: Boolean = false,
killOnFail: => Boolean = false,
rank: Int = 0,
extend: Boolean = false,
ignore: Boolean = false,
Expand Down
6 changes: 3 additions & 3 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6169,9 +6169,9 @@ object a {
// bridge is needed to compile these projects.
dependsOn(
`scala3-compiler` % Provided
).settings(
commonJavaSettings
).settings(foo)
).
settings(commonJavaSettings).
settings(foo)
}
<<< try with match expr
object a {
Expand Down
5 changes: 3 additions & 2 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7289,8 +7289,9 @@ object a {
}
>>>
object a {
val pebblesAndBammBammsParents = Person
.findAll(("childId" -> ("$in" -> List(pebblesId, bammbammId))))
val pebblesAndBammBammsParents = Person.findAll(
("childId" -> ("$in" -> List(pebblesId, bammbammId)))
)
}
<<< SM 4.7.0: 29.1
maxColumn = 75
Expand Down
60 changes: 26 additions & 34 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7053,8 +7053,8 @@ object a {
>>>
object a {
@threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol =
requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue(
"SOURCE")
requiredClass("java.lang.annotation.RetentionPolicy").linkedClass
.requiredValue("SOURCE")
}
<<< #4133 overflow apply with binpack, dangling
maxColumn = 70
Expand Down Expand Up @@ -7169,9 +7169,8 @@ object a:
insertClasspathInArgs(
args1,
List(dottyCompiler, dottyInterfaces, asm, dottyStaging,
dottyTastyInspector, tastyCore, compilerInterface).mkString(
File.pathSeparator).mkString(
File.pathSeparator)
dottyTastyInspector, tastyCore, compilerInterface)
.mkString(File.pathSeparator).mkString(File.pathSeparator)
))
} else run(args)
<<< #4133 overflow select with binpack, dangling
Expand All @@ -7183,17 +7182,11 @@ object a:
val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(month).resolve(day).resolve(name)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
val foo = map { postFile =>
- val destPath =
- ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(
- month
- ).resolve(day).resolve(name)
+ val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(
+ month
+ ).resolve(day).resolve(name)
}
object a:
val foo = map { postFile =>
val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year)
.resolve(month).resolve(day).resolve(name)
}
<<< #4133 overflow select with binpack, !dangling
binPack.preset = always
danglingParentheses.preset = false
Expand All @@ -7203,15 +7196,11 @@ object a:
val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(month).resolve(day).resolve(name)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
val foo = map { postFile =>
- val destPath =
- ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(
- month).resolve(day).resolve(name)
+ val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year).resolve(
+ month).resolve(day).resolve(name)
}
object a:
val foo = map { postFile =>
val destPath = ctx_docsPath_resolve_defaultDirectory_resolve(year)
.resolve(month).resolve(day).resolve(name)
}
<<< #4133 overflow selects/applies, nobreak-dot-break, binpack + !dangling
maxColumn = 70
binPack.preset = always
Expand All @@ -7226,12 +7215,14 @@ object a:
)
>>>
object a:
lazy val `stdlib-bootstrapped` =
project.in(file("stdlib-bootstrapped")).withCommonSettings(
Bootstrapped).dependsOn(dottyCompiler(
Bootstrapped) % "provided; compile->runtime; test->test").settings(
moduleName := "scala-library"
)
lazy val `stdlib-bootstrapped` = project.in(
file("stdlib-bootstrapped")).
withCommonSettings(Bootstrapped).
dependsOn(dottyCompiler(
Bootstrapped) % "provided; compile->runtime; test->test").
settings(
moduleName := "scala-library"
)
<<< #4133 overflow selects/applies, nobreak-dot-nobreak, binpack + !dangling
maxColumn = 70
binPack.preset = always
Expand All @@ -7246,9 +7237,10 @@ object a:
lazy val `stdlib-bootstrapped` =
project.in(file("stdlib-bootstrapped")).withCommonSettings(
Bootstrapped).dependsOn(dottyCompiler(
Bootstrapped) % "provided; compile->runtime; test->test").settings(
moduleName := "scala-library"
)
Bootstrapped) % "provided; compile->runtime; test->test")
.settings(
moduleName := "scala-library"
)
<<< #4133 overflow selects/applies, break-dot-nobreak, binpack + !dangling
maxColumn = 70
binPack.preset = always
Expand Down
36 changes: 24 additions & 12 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,12 @@ object Foo:
)

case a: A if a.c() =>
val varName = a
.method(a.someField.anotherField.method().map(ObjectWithLongName.A))
val otherVarName = varName
.method(a.someField.method(ObjectWithLongName.B))
val varName = a.method(
a.someField.anotherField.method().map(ObjectWithLongName.A)
)
val otherVarName = varName.method(
a.someField.method(ObjectWithLongName.B)
)
otherVarName

case a: A
Expand Down Expand Up @@ -6104,8 +6106,11 @@ object a:
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
def slice(offset: Int, length: Int) = api.search(
query,
From(offset),
Size(length)
)
} mapFutureList pager.withChaptersAndLiking(me)
<<< new anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
Expand All @@ -6122,8 +6127,11 @@ object a:
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
def slice(offset: Int, length: Int) = api.search(
query,
From(offset),
Size(length)
)
} mapFutureList
<<< new anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
Expand All @@ -6140,8 +6148,11 @@ object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
def slice(offset: Int, length: Int) = api.search(
query,
From(offset),
Size(length)
)
.mapFutureList
<<< #3623 beforeCurlyLambdaParams
newlines.beforeCurlyLambdaParams = true
Expand Down Expand Up @@ -7093,8 +7104,9 @@ object a:
revision: Option[String],
projectRoot: Path = Paths.get("").toAbsolutePath
)(using CompilerContext): SourceLinks =
PathBased
.parse(config, projectRoot)(using SourceLinkParser(revision)) match {
PathBased.parse(config, projectRoot)(using
SourceLinkParser(revision)
) match {
case PathBased.ParsingResult(errors, sourceLinks) =>
if errors.nonEmpty then
report.warning(s"""Following templates has invalid format:
Expand Down
Loading