Skip to content

Commit

Permalink
Router: implement align.beforeOpenParenXxxSite
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 11, 2022
1 parent ae58434 commit e9a6dc7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scalafmt.internal

import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.BinPack
import org.scalafmt.config.{Align, BinPack}
import org.scalafmt.config.{ImportSelectors, Newlines, ScalafmtConfig, Spaces}
import org.scalafmt.internal.ExpiresOn.{After, Before}
import org.scalafmt.internal.Length.{Num, StateColumn}
Expand Down Expand Up @@ -622,11 +622,12 @@ class Router(formatOps: FormatOps) {
val beforeDefRhs = defRhs.flatMap(tokens.tokenJustBeforeOpt)
def getSplitsBeforeOpenParen(
src: Newlines.SourceHints,
indentLen: Int
) = {
indentLen: Int,
flag: Align => Boolean
)(getArgsOpt: => Option[Seq[Tree]]) = {
val close = matching(open)
val indent = Indent(indentLen, close, ExpiresOn.After)
src match {
val splits = src match {
case Newlines.unfold =>
val slbEnd =
if (defn) beforeDefRhs.fold(getLastToken(rightOwner))(_.left)
Expand Down Expand Up @@ -678,6 +679,18 @@ class Router(formatOps: FormatOps) {
.withPolicyOpt(nlColonPolicy)
)
}
val isAlignFirstParen = flag(style.align) &&
!prevNonComment(ft).left.is[T.RightParen]
val argsOpt = if (isAlignFirstParen) getArgsOpt else None
argsOpt.map(x => tokens.tokenAfter(x).right).fold(splits) { x =>
val noSplitIndents = Seq(
Indent(StateColumn, x, ExpiresOn.Before),
Indent(-indentLen, x, ExpiresOn.Before)
)
splits.map { s =>
if (s.isNL) s else s.withIndents(noSplitIndents)
}
}
}
val beforeOpenParenSplits =
if (!open.is[T.LeftParen]) None
Expand All @@ -692,12 +705,31 @@ class Router(formatOps: FormatOps) {
style.indent.extraBeforeOpenParenDefnSite +
(if (ob) style.indent.getSignificant else style.indent.main)
}
getSplitsBeforeOpenParen(x, indent)
getSplitsBeforeOpenParen(x, indent, _.beforeOpenParenDefnSite) {
rightOwner match {
case SplitDefnIntoParts(_, _, _, args) => Some(args.last)
case _ => None
}
}
}
else if (style.dialect.allowSignificantIndentation)
style.newlines.getBeforeOpenParenCallSite.map(
getSplitsBeforeOpenParen(_, style.indent.getSignificant)
)
style.newlines.getBeforeOpenParenCallSite.map { x =>
val indent = style.indent.getSignificant
@tailrec
def findLastCallArgs(tree: Tree, ca: CallArgs): CallArgs =
tree match {
case SplitCallIntoParts(_, pca) =>
tree.parent match {
case Some(p) => findLastCallArgs(p, pca)
case _ => pca
}
case _ => ca
}
getSplitsBeforeOpenParen(x, indent, _.beforeOpenParenCallSite) {
Option(findLastCallArgs(rightOwner, null))
.map(_.fold(identity, _.last))
}
}
else None
beforeOpenParenSplits.getOrElse(Seq(Split(modification, 0)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5690,5 +5690,5 @@ def allMatching(versionString: String)
}
>>>
def allMatching(versionString: String)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
: List[String] = {}
Original file line number Diff line number Diff line change
Expand Up @@ -5449,5 +5449,5 @@ def allMatching(versionString: String)
}
>>>
def allMatching(versionString: String)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
(partialFunctions: PartialFunction[options.Common, List[String]]*)
: List[String] = {}
Original file line number Diff line number Diff line change
Expand Up @@ -5709,4 +5709,4 @@ def allMatching(versionString: String)
}
>>>
def allMatching(versionString: String)
(partialFunctions: PartialFunction[options.Common, List[String]]*): List[String] = {}
(partialFunctions: PartialFunction[options.Common, List[String]]*): List[String] = {}
Original file line number Diff line number Diff line change
Expand Up @@ -3297,4 +3297,4 @@ allMatching(versionString)
(partialFunctions)
>>>
allMatching(versionString)
(partialFunctions)
(partialFunctions)
Original file line number Diff line number Diff line change
Expand Up @@ -3140,4 +3140,4 @@ allMatching(versionString)
(partialFunctions)
>>>
allMatching(versionString)
(partialFunctions)
(partialFunctions)
Original file line number Diff line number Diff line change
Expand Up @@ -3285,4 +3285,4 @@ allMatching(versionString)
(partialFunctions)
>>>
allMatching(versionString)
(partialFunctions)
(partialFunctions)

0 comments on commit e9a6dc7

Please sign in to comment.