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

Indent: add an implicit conversion from Int #4589

Merged
merged 1 commit into from
Nov 24, 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 @@ -7,7 +7,6 @@ import org.scalafmt.config.Newlines
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.ScalafmtOptimizer
import org.scalafmt.config.TrailingCommas
import org.scalafmt.internal.Length.Num
import org.scalafmt.internal.Policy.NoPolicy
import org.scalafmt.rewrite.RedundantBraces
import org.scalafmt.util.InfixApp._
Expand Down Expand Up @@ -588,13 +587,12 @@ class FormatOps(
}

private val fullIndent: Indent = assignBodyExpire match {
case Some(x) if beforeLhs =>
Indent(Num(style.indent.main), x, ExpiresOn.After)
case Some(x) if beforeLhs => Indent(style.indent.main, x, ExpiresOn.After)
case None if isLeftInfix && isAfterAssignmentOp(true) =>
Indent(Num(style.indent.main), fullExpire, ExpiresOn.After)
Indent(style.indent.main, fullExpire, ExpiresOn.After)
case _ =>
val len = style.indent.getAfterInfixSite
Indent(Num(len), fullExpire, ExpiresOn.After)
Indent(len, fullExpire, ExpiresOn.After)
}

val (nlIndent, nlPolicy) = {
Expand Down Expand Up @@ -687,7 +685,7 @@ class FormatOps(
else {
val expire = expires.head._1
val indentLen = if (skip) 0 else style.indent.main
val indent = Indent(Num(indentLen), expire, ExpiresOn.After)
val indent = Indent(indentLen, expire, ExpiresOn.After)
(expire, indent)
}
}
Expand Down Expand Up @@ -904,7 +902,7 @@ class FormatOps(
val expire = x.getExpireToken
val indent =
if (!x.isSecond) Indent.Empty
else Indent(Num(indentIfSecond), expire, ExpiresOn.After)
else Indent(indentIfSecond, expire, ExpiresOn.After)
def nlSplit(cost: Int) = Split(Newline, cost)
.withPolicy(getPolicy(expire)).withIndent(indent)
if (!style.binPack.keepParentConstructors)
Expand Down Expand Up @@ -942,7 +940,7 @@ class FormatOps(
}
val indent =
if (!isFirstCtor) Indent.Empty
else Indent(Num(indentLen), lastFt, ExpiresOn.After)
else Indent(indentLen, lastFt, ExpiresOn.After)
if (style.binPack.keepParentConstructors)
if (ft.hasBreak) Seq(Split(nlMod, 0).withIndent(indent))
else {
Expand Down Expand Up @@ -1036,8 +1034,8 @@ class FormatOps(
val nft = next(ft)
val close = matchingLeft(ft)
val beforeClose = prev(close)
val indentParam = Num(style.indent.getDefnSite(lpOwner))
val indentSep = Num((indentParam.n - 2).max(0))
val indentParam = style.indent.getDefnSite(lpOwner)
val indentSep = Indent((indentParam - 2).max(0), close, ExpiresOn.After)
val isBracket = open.is[T.LeftBracket]

def getClausesFromClauseGroup(tree: Member.ParamClauseGroup) = {
Expand Down Expand Up @@ -1094,7 +1092,7 @@ class FormatOps(
case Decision(`beforeLastParen`, _) => Seq(Split(NoSplit, 0))
// Indent separators `)(` and `](` by `indentSep`
case Decision(`beforeClose`, _) =>
Seq(Split(Newline, 0).withIndent(indentSep, close, ExpiresOn.After))
Seq(Split(Newline, 0).withIndent(indentSep))
case Decision(FT(LeftParenOrBracket(), _, m), ss)
if allParenOwners.contains(m.leftOwner) =>
ss.filter(!_.isActiveFor(SplitTag.VerticalMultilineSingleLine))
Expand All @@ -1109,8 +1107,7 @@ class FormatOps(
val policy = oneLinePerArg | paramGroupSplitter

val firstIndent =
if (r.is[T.RightParen]) // An empty param group
Indent(indentSep, close, ExpiresOn.After)
if (r.is[T.RightParen]) indentSep // An empty param group
else Indent(indentParam, close, ExpiresOn.Before)

def aboveArityThreshold = {
Expand Down Expand Up @@ -1384,22 +1381,27 @@ class FormatOps(
case _ => findXmlLastLineIndent(prev(ft))
}

def withIndentOnXmlStart(xmlEnd: => FT, splits: Seq[Split])(implicit
style: ScalafmtConfig,
): Seq[Split] =
private def withIndentOnXmlToken(
ft: => Option[FT],
xmlEnd: => FT,
splits: Seq[Split],
)(implicit style: ScalafmtConfig): Seq[Split] =
if (style.xmlLiterals.assumeFormatted) {
val end = xmlEnd
val indent = Num(findXmlLastLineIndent(prev(end)), true)
splits.map(_.withIndent(indent, end, ExpiresOn.After))
val len = findXmlLastLineIndent(prev(ft.getOrElse(end)))
val indent = Indent(Length.Num(len, reset = true), end, ExpiresOn.After)
splits.map(_.withIndent(indent))
} else splits

def withIndentOnXmlStart(xmlEnd: => FT, splits: Seq[Split])(implicit
style: ScalafmtConfig,
): Seq[Split] = withIndentOnXmlToken(None, xmlEnd, splits)

def withIndentOnXmlSpliceStart(ft: FT, splits: Seq[Split])(implicit
style: ScalafmtConfig,
): Seq[Split] = ft.left match {
case _: T.Xml.SpliceStart if style.xmlLiterals.assumeFormatted =>
val end = matchingLeft(ft)
val indent = Num(findXmlLastLineIndent(prev(ft)), true)
splits.map(_.withIndent(indent, end, ExpiresOn.After))
case _: T.Xml.SpliceStart =>
withIndentOnXmlToken(Some(ft), matchingLeft(ft), splits)
case _ => splits
}

Expand Down Expand Up @@ -1683,7 +1685,7 @@ class FormatOps(
val expire = nextNonCommentSameLine(rpOpt.fold(endFt) { rp =>
if (rp.left.end >= endFt.left.end) rp else endFt
})
nlSplit.withIndent(Num(style.indent.main), expire, ExpiresOn.After)
nlSplit.withIndent(style.indent.main, expire, ExpiresOn.After)
}

def withIndent(nlSplit: Split, body: Tree, endFt: => FT)(implicit
Expand Down Expand Up @@ -1783,7 +1785,7 @@ class FormatOps(
)(implicit style: ScalafmtConfig, ft: FT): Seq[Split] = {
val typeEnd = getLastNonTrivial(typeOwner)
val boundEnd = boundEndOpt.getOrElse(typeEnd)
def indent = Indent(Num(style.indent.main), boundEnd, ExpiresOn.After)
def indent = Indent(style.indent.main, boundEnd, ExpiresOn.After)
def unfoldPolicy = typeOwner match {
case tparam: Type.Param => Policy.onLeft(typeEnd, prefix = "VB") {
case Decision(t @ FT(_, _: T.Colon | _: T.Viewbound, _), s)
Expand Down Expand Up @@ -1885,7 +1887,7 @@ class FormatOps(
else decideNewlinesOnlyAfterToken(rank = 1, ifAny = true)(close)
}
val indentLen = indentOpt.getOrElse(style.indent.getSignificant)
val indent = Indent(Num(indentLen), close, ExpiresOn.After)
val indent = Indent(indentLen, close, ExpiresOn.After)
def nlOnly = forceNLIfTrailingStandaloneComments &&
slbExpire.right.is[T.Comment] && slbExpire.idx < close.idx
if (ft.hasBlankLine)
Expand Down Expand Up @@ -2463,7 +2465,7 @@ class FormatOps(
def policy =
if (split.isNL) decideNewlinesOnlyBeforeClose(kw)
else decideNewlinesOnlyBeforeCloseOnBreak(kw)
split.withIndent(Num(indent), kw, ExpiresOn.Before)
split.withIndent(indent, kw, ExpiresOn.Before)
.andPolicy(policy, !style.danglingParentheses.ctrlSite)
}

Expand Down Expand Up @@ -3002,8 +3004,8 @@ object FormatOps {
)(implicit style: ScalafmtConfig): Seq[Indent] =
if (style.align.closeParenSite) Seq(
Indent(Length.StateColumn, end, ExpiresOn.After),
Indent(Length.Num(1), end, ExpiresOn.Before),
Indent(Length.Num(-1), end, ExpiresOn.After),
Indent(1, end, ExpiresOn.Before),
Indent(-1, end, ExpiresOn.After),
)
else Seq(Indent(Length.StateColumn, end, ExpiresOn.Before))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalafmt.internal
import scala.meta.tokens.{Token => T}

import scala.annotation.tailrec
import scala.language.implicitConversions

sealed abstract class ExpiresOn {
def notExpiredBy(ft: FT, idxBeforeExpire: Int): Boolean
Expand Down Expand Up @@ -30,6 +31,8 @@ sealed abstract class Length {

object Length {

implicit def intToNum(len: Int): Num = Num(len)

case class Num(n: Int, reset: Boolean = false) extends Length {
override def withStateOffset(offset: Int): Int = n
override def toString: String = n.toString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.Spaces
import org.scalafmt.internal.ExpiresOn.After
import org.scalafmt.internal.ExpiresOn.Before
import org.scalafmt.internal.Length.Num
import org.scalafmt.internal.Length.StateColumn
import org.scalafmt.internal.Policy.NoPolicy
import org.scalafmt.util._
Expand All @@ -19,7 +18,6 @@ import scala.meta._
import scala.meta.tokens.{Token => T}

import scala.annotation.tailrec
import scala.language.implicitConversions

object Constants {
val ShouldBeNewline = 100000
Expand Down Expand Up @@ -176,9 +174,9 @@ class Router(formatOps: FormatOps) {
val close = matchingLeft(ft)
val alignIndents =
if (style.align.inInterpolation) Seq(
Indent(Length.StateColumn, close, ExpiresOn.After),
Indent(Length.Num(style.indent.main), close, ExpiresOn.Before),
Indent(Length.Num(-1), close, ExpiresOn.After),
Indent(StateColumn, close, ExpiresOn.After),
Indent(style.indent.main, close, ExpiresOn.Before),
Indent(-1, close, ExpiresOn.After),
)
else Nil
def spaceSplit(implicit fileLine: FileLine) =
Expand Down Expand Up @@ -1040,7 +1038,7 @@ class Router(formatOps: FormatOps) {
val indentLen =
if (anyDefnSite) style.indent.getDefnSite(leftOwner)
else style.indent.callSite
val indent = Indent(Num(indentLen), close, ExpiresOn.Before)
val indent = Indent(indentLen, close, ExpiresOn.Before)

val isBeforeOpenParen =
if (defnSite) style.newlines.isBeforeOpenParenDefnSite
Expand Down Expand Up @@ -1146,7 +1144,7 @@ class Router(formatOps: FormatOps) {
})
val extraOneArgPerLineIndent =
if (multipleArgs && style.poorMansTrailingCommasInConfigStyle)
Indent(Num(2), afterOpen, After)
Indent(2, afterOpen, After)
else Indent.Empty
val (implicitPenalty, implicitPolicy) =
if (!handleImplicit) (2, Policy.NoPolicy)
Expand Down Expand Up @@ -1422,7 +1420,7 @@ class Router(formatOps: FormatOps) {
penalizeNewlinesPolicy
val indentPolicy = Policy ? noSplitIndents.isEmpty || {
def unindentPolicy = Policy ? (isSingleArg || sjsOneline) &&
unindentAtExclude(exclude, Num(-bpIndentLen))
unindentAtExclude(exclude, -bpIndentLen)
def indentOncePolicy =
Policy ? style.binPack.indentCallSiteOnce && {
val trigger = getIndentTrigger(leftOwner)
Expand Down Expand Up @@ -2281,7 +2279,7 @@ class Router(formatOps: FormatOps) {
val pft = prevNonCommentSameLine(beforeClose)
(pft eq beforeClose) && beforeClose.left.is[T.Comment]
}
Num(if (needIndent) style.indent.main else 0)
if (needIndent) style.indent.main else 0
}
Split.opt(getNoSplitAfterOpening(ft, commentNL = null), 0)
.withIndent(indent, close, Before)
Expand Down Expand Up @@ -2721,8 +2719,6 @@ class Router(formatOps: FormatOps) {
}
}

private implicit def int2num(n: Int): Num = Num(n)

private def getSplitsDefValEquals(
body: Tree,
endFt: => FT,
Expand Down