Skip to content

Commit

Permalink
Router: allow breaks in any double-quote strings
Browse files Browse the repository at this point in the history
Previously, we would only allow that in triple-quote strings. However,
for classic/keep try to avoid break if one wasn't present.
  • Loading branch information
kitbellew committed Sep 12, 2024
1 parent 6d840ad commit 8bcdf70
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ class Router(formatOps: FormatOps) {
case FormatToken(_: T.Shebang, _, _) => Seq(Split(Newline2x(ft), 0))
case FormatToken(start: T.Interpolation.Start, _, m) =>
val end = matching(start)
val okNewlines = style.newlines.inInterpolation
.ne(Newlines.InInterpolation.avoid) && isTripleQuote(m.left.text)
def policy = PenalizeAllNewlines(end, BreakSingleLineInterpolatedString)
val split = Split(NoSplit, 0).withPolicy(policy, okNewlines)
val policy = {
val penalty = BreakSingleLineInterpolatedString
if (style.newlines.inInterpolation eq Newlines.InInterpolation.avoid)
PenalizeAllNewlines(end, penalty)
else Policy ?
(style.newlines.sourceIgnored || isTripleQuote(m.left.text)) ||
Policy.on(end, "INTERP-KEEP-NONL") {
case Decision(x, ss) if x.noBreak =>
ss.map(s => if (s.isNL) s.withPenalty(penalty) else s)
}
}
val split = Split(NoSplit, 0, policy = policy)
val alignIndents =
if (style.align.stripMargin) findInterpolate(leftOwner)
.flatMap { ti =>
Expand Down
5 changes: 4 additions & 1 deletion scalafmt-tests/src/test/resources/newlines/source.source
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ package a.b.c
import a.b.c
object a {
val a =
s"${a.b.c.d}"
s"${a
.b
.c
.d}"
val b =
foo[
a.b.c.d.e
Expand Down
14 changes: 10 additions & 4 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9092,7 +9092,10 @@ object a {
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}"
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE)
)}"
)
}
<<< #4187 !dangling
Expand All @@ -9110,9 +9113,12 @@ object a {
}
>>>
object a {
logWarning(log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
}
<<< #4133 overflow select within interpolate
object a {
Expand Down
22 changes: 16 additions & 6 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -8466,8 +8466,13 @@ object a {
object a {
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}"
log" Current value of " + log"'${MDC(
LogKeys.CONFIG,
Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key
)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE)
)}"
)
}
<<< #4187 !dangling
Expand All @@ -8485,9 +8490,13 @@ object a {
}
>>>
object a {
logWarning(log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " + log"'${MDC(
LogKeys.CONFIG,
Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
}
<<< #4133 overflow select within interpolate
object a {
Expand All @@ -8503,7 +8512,8 @@ object a {
canConstrain: Boolean = false,
isSoft: Boolean = true
): Type = /*>|>*/ trace(
s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
s"lub(${tp1.show}, ${tp2
.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
subtyping,
show = true
) /*<|<*/ {
Expand Down
17 changes: 12 additions & 5 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9179,7 +9179,10 @@ object a {
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}"
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE)
)}"
)
}
<<< #4187 !dangling
Expand All @@ -9197,9 +9200,12 @@ object a {
}
>>>
object a {
logWarning(log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(LogKeys.CACHE_SIZE, SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
logWarning(
log"Session plan cache is disabled due to non-positive cache size." +
log" Current value of " +
log"'${MDC(LogKeys.CONFIG, Connect.CONNECT_SESSION_PLAN_CACHE_SIZE.key)}' is ${MDC(
LogKeys.CACHE_SIZE,
SparkEnv.get.conf.get(Connect.CONNECT_SESSION_PLAN_CACHE_SIZE))}")
}
<<< #4133 overflow select within interpolate
object a {
Expand All @@ -9216,7 +9222,8 @@ object a {
isSoft: Boolean = true
): Type = /*>|>*/
trace(
s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
s"lub(${tp1.show}, ${tp2
.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
subtyping,
show = true
) /*<|<*/ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6984,7 +6984,8 @@ object a:
canConstrain: Boolean = false,
isSoft: Boolean = true
): Type = /*>|>*/ trace(
s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
s"lub(${tp1.show}, ${tp2
.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
subtyping,
show = true
) /*<|<*/ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7522,7 +7522,8 @@ object a:
isSoft: Boolean = true
): Type = /*>|>*/
trace(
s"lub(${tp1.show}, ${tp2.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
s"lub(${tp1.show}, ${tp2
.show}, canConstrain=$canConstrain, isSoft=$isSoft)",
subtyping,
show = true
) /*<|<*/ {
Expand Down

0 comments on commit 8bcdf70

Please sign in to comment.