Skip to content

Commit

Permalink
PolicyOps: use newlines.ignoreInSyntax
Browse files Browse the repository at this point in the history
Specifically, for PenalizeAllNewlines and SingleLineBlock policies.
  • Loading branch information
kitbellew committed Aug 23, 2023
1 parent 7fb7657 commit 0c0dbfe
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ class FormatOps(
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent] = Seq.empty
): Seq[Split] = {
)(implicit style: ScalafmtConfig): Seq[Split] = {
def bheadFT = tokens.getHead(body)
val blastFT = tokens.getLastNonTrivial(body)
val blast = blastFT.left
Expand Down Expand Up @@ -1788,14 +1788,14 @@ class FormatOps(
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent]
): Seq[Split] =
)(implicit style: ScalafmtConfig): Seq[Split] =
if (body.tokens.isEmpty) Seq(Split(Space, 0))
else foldedNonEmptyNonComment(body, nlSplitFunc, isKeep, spaceIndents)

private def unfoldedSpaceNonEmptyNonComment(
body: Tree,
slbOnly: Boolean
): Split = {
)(implicit style: ScalafmtConfig): Split = {
val expire = nextNonCommentSameLine(tokens.getLastNonTrivial(body)).left
def slbSplit(end: T)(implicit fileLine: FileLine) =
Split(Space, 0).withSingleLine(end, noSyntaxNL = true)
Expand All @@ -1816,7 +1816,7 @@ class FormatOps(
nlSplitFunc: Int => Split,
spaceIndents: Seq[Indent],
slbOnly: Boolean
): Seq[Split] =
)(implicit style: ScalafmtConfig): Seq[Split] =
if (body.tokens.isEmpty) Seq(Split(Space, 0).withIndents(spaceIndents))
else {
val spaceSplit = unfoldedSpaceNonEmptyNonComment(body, slbOnly)
Expand Down Expand Up @@ -1848,7 +1848,7 @@ class FormatOps(
body: Tree,
isKeep: Boolean,
spaceIndents: Seq[Indent] = Seq.empty
)(nlSplitFunc: Int => Split): Seq[Split] =
)(nlSplitFunc: Int => Split)(implicit style: ScalafmtConfig): Seq[Split] =
checkComment(ft, nlSplitFunc) { _ =>
foldedNonComment(body, nlSplitFunc, isKeep, spaceIndents)
}
Expand All @@ -1857,7 +1857,7 @@ class FormatOps(
ft: FormatToken,
body: Tree,
spaceIndents: Seq[Indent] = Seq.empty
)(nlSplitFunc: Int => Split): Seq[Split] =
)(nlSplitFunc: Int => Split)(implicit style: ScalafmtConfig): Seq[Split] =
checkComment(ft, nlSplitFunc) { _ =>
unfoldedNonComment(body, nlSplitFunc, spaceIndents, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.scalafmt.internal

import scala.meta.tokens.Token

import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal.Policy.NoPolicy
import org.scalafmt.util.PolicyOps
import org.scalameta.FileLine
Expand Down Expand Up @@ -142,7 +143,7 @@ case class Split(
noSyntaxNL: Boolean = false,
killOnFail: Boolean = false,
rank: Int = 0
)(implicit fileLine: FileLine): Split =
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split =
withSingleLineAndOptimal(
expire,
expire,
Expand All @@ -158,7 +159,7 @@ case class Split(
noSyntaxNL: Boolean = false,
killOnFail: Boolean = false,
rank: Int = 0
)(implicit fileLine: FileLine): Split =
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split =
expire.fold(this)(
withSingleLine(_, exclude, noSyntaxNL, killOnFail, rank)
)
Expand All @@ -170,7 +171,7 @@ case class Split(
noSyntaxNL: Boolean = false,
killOnFail: Boolean = false,
rank: Int = 0
)(implicit fileLine: FileLine): Split =
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split =
withOptimalToken(optimal, killOnFail)
.withSingleLineNoOptimal(expire, exclude, noSyntaxNL, rank)

Expand All @@ -179,7 +180,7 @@ case class Split(
exclude: => TokenRanges = TokenRanges.empty,
noSyntaxNL: Boolean = false,
rank: Int = 0
)(implicit fileLine: FileLine): Split =
)(implicit fileLine: FileLine, style: ScalafmtConfig): Split =
withPolicy(
SingleLineBlock(expire, exclude, noSyntaxNL = noSyntaxNL, rank = rank)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.scalafmt.util

import scala.meta.tokens.{Token => T}

import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal._
import org.scalameta.FileLine

Expand All @@ -16,12 +17,13 @@ object PolicyOps {
penalizeLambdas: Boolean = true,
noSyntaxNL: Boolean = false,
val rank: Int = 0
)(implicit fileLine: FileLine)
)(implicit fileLine: FileLine, style: ScalafmtConfig)
extends Policy.Clause {
override val noDequeue: Boolean = false
private val checkSyntax = noSyntaxNL || !style.newlines.ignoreInSyntax
override val f: Policy.Pf = {
case Decision(ft, s) if penalizeLambdas || !ft.left.is[T.RightArrow] =>
if (noSyntaxNL && ft.leftHasNewline) s.map(_.withPenalty(penalty))
if (checkSyntax && ft.leftHasNewline) s.map(_.withPenalty(penalty))
else s.map(x => if (x.isNL) x.withPenalty(penalty) else x)
}
override def toString: String = s"PNL:${super.toString}+$penalty"
Expand All @@ -33,7 +35,7 @@ object PolicyOps {
penalty: Int,
penalizeLambdas: Boolean = true,
noSyntaxNL: Boolean = false
)(implicit fileLine: FileLine): Policy = {
)(implicit fileLine: FileLine, style: ScalafmtConfig): Policy = {
new PenalizeAllNewlines(
Policy.End.Before(expire),
penalty,
Expand All @@ -54,15 +56,16 @@ object PolicyOps {
okSLC: Boolean = false,
noSyntaxNL: Boolean = false,
val rank: Int = 0
)(implicit fileLine: FileLine)
)(implicit fileLine: FileLine, style: ScalafmtConfig)
extends Policy.Clause {
import TokenOps.isLeftCommentThenBreak
override val noDequeue: Boolean = true
override def toString: String = "SLB:" + super.toString
private val checkSyntax = noSyntaxNL || !style.newlines.ignoreInSyntax
override val f: Policy.Pf = {
case Decision(ft, s)
if !(ft.right.is[T.EOF] || okSLC && isLeftCommentThenBreak(ft)) =>
if (noSyntaxNL && ft.leftHasNewline) Seq.empty else s.filterNot(_.isNL)
if (checkSyntax && ft.leftHasNewline) Seq.empty else s.filterNot(_.isNL)
}
}

Expand All @@ -74,7 +77,7 @@ object PolicyOps {
okSLC: Boolean = false,
noSyntaxNL: Boolean = false,
rank: Int = 0
)(implicit fileLine: FileLine): Policy =
)(implicit fileLine: FileLine, style: ScalafmtConfig): Policy =
policyWithExclude(exclude, Policy.End.On, Policy.End.After)(
Policy.End.On(expire),
new SingleLineBlock(
Expand Down
9 changes: 5 additions & 4 deletions scalafmt-tests/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,8 @@ println("""|
|""".stripMargin
)
>>>
println("""|
|A very long string
|with multiple lines
|""".stripMargin)
println(
"""|
|A very long string
|with multiple lines
|""".stripMargin)
6 changes: 4 additions & 2 deletions scalafmt-tests/src/test/resources/test/StripMargin.stat
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ final class MyClass {
}
>>>
final class MyClass {
println(s"""${1}
""".stripMargin)
println(
s"""${1}
""".stripMargin
)
}
<<< #3090 string without margin
maxColumn = 100
Expand Down

0 comments on commit 0c0dbfe

Please sign in to comment.