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

FormatOps: remove getOptimalTokenFor #4489

Merged
merged 1 commit into from
Oct 28, 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 @@ -368,11 +368,6 @@ class FormatOps(
case _ => res
}

def getOptimalTokenFor(token: T): T = getOptimalTokenFor(tokens(token))

def getOptimalTokenFor(ft: FormatToken): T =
if (isAttachedCommentThenBreak(ft)) ft.right else ft.left

def insideInfixSplit(
app: Member.Infix,
)(implicit style: ScalafmtConfig, ft: FormatToken): Seq[Split] = {
Expand Down Expand Up @@ -1989,7 +1984,8 @@ class FormatOps(
): Option[OptionalBracesRegion] = {
def funcSplit(arg: Term.FunctionTerm)(implicit fl: FileLine) = {
val end = getLast(arg)
val opt = getOptimalTokenFor(getFuncArrow(arg).getOrElse(end))
val opt = nextNonCommentSameLine(getFuncArrow(arg).getOrElse(end))
.left
Split(Space, 0).withSingleLine(opt)
.andPolicy(decideNewlinesOnlyAfterToken(opt))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class Router(formatOps: FormatOps) {
val noLambdaSplit = style.newlines.keepBreak(newlines) ||
lambdaArrow == null || !lambdaNLOnly.contains(false)
val lambdaExpire =
if (noLambdaSplit) null else getOptimalTokenFor(lambdaArrow)
if (noLambdaSplit) null else nextNonCommentSameLine(lambdaArrow).left

def getSingleLinePolicy = {
val needBreak = closeFT.right match {
Expand Down Expand Up @@ -461,7 +461,8 @@ class Router(formatOps: FormatOps) {
case _ if afterCurlySpace && {
style.newlines.fold || !rightOwner.is[Defn]
} =>
val exp = getOptimalTokenFor(getLastNonTrivial(leftFunc.body).left)
val exp = nextNonCommentSameLine(getLastNonTrivial(leftFunc.body))
.left
spaceSplitBase.withSingleLine(exp, noSyntaxNL = true)
case _ => Split.ignored
}
Expand Down Expand Up @@ -535,7 +536,7 @@ class Router(formatOps: FormatOps) {
Split(Space, 0)
.withIndent(indent, endOfFunction, expiresOn, hasBlock)
.withOptimalToken(
getOptimalTokenFor(next(nonComment)),
nextNonCommentSameLineAfter(nonComment).left,
killOnFail = false,
)
Seq(noSplit, newlineSplit(1 + nestedApplies(leftOwner)))
Expand Down Expand Up @@ -847,8 +848,9 @@ class Router(formatOps: FormatOps) {

val arrowFt = getFuncArrow(lambda).get
val lambdaIsABlock = lambdaLeft.contains(arrowFt.right)
val lambdaToken =
getOptimalTokenFor(if (lambdaIsABlock) next(arrowFt) else arrowFt)
val lambdaToken = nextNonCommentSameLine(
if (lambdaIsABlock) next(arrowFt) else arrowFt,
).left

val spacePolicy = SingleLineBlock(lambdaToken) ==> {
Policy ? lambdaIsABlock || delayedBreakPolicy(
Expand Down Expand Up @@ -989,10 +991,10 @@ class Router(formatOps: FormatOps) {
val splitsForAssign =
if (defnSite || isBracket || keepConfigStyleSplit) None
else getAssignAtSingleArgCallSite(args).map { assign =>
val breakToken = getOptimalTokenFor(assign.rhs match {
val breakToken = nextNonCommentSameLine(assign.rhs match {
case b: Term.Block if isEnclosedInBraces(b) => getHead(b)
case x => tokenBefore(x)
})
}).left
val newlineAfterAssignDecision = Policy ? newlinePolicy.isEmpty ||
decideNewlinesOnlyAfterToken(breakToken)
Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1488002, "total explored")
assertEquals(explored, 1487998, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down
Loading