Skip to content

Commit

Permalink
Improve debugging, disable files troubled by #140.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed May 23, 2016
1 parent de9213e commit e5c56b6
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 37 deletions.
2 changes: 2 additions & 0 deletions core/src/main/scala/org/scalafmt/ScalafmtOptimizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ object ScalafmtOptimizer {
disableOptimizationsInsideSensitiveAreas = true,
pruneSlowStates = true,
recurseOnBlocks = true

)

val noOptimizations = default.copy(
dequeueOnNewStatements = false,
escapeInPathologicalCases = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ class BestFirstSearch(
}

def getBestPath: SearchResult = {
var state = shortestPath(State.start, tree.tokens.last)
runner.eventCallback(CompleteFormat(explored, state, tokens))
val state = shortestPath(State.start, tree.tokens.last)
if (state.splits.length == tokens.length) {
runner.eventCallback(CompleteFormat(explored, state, tokens))
SearchResult(state.splits, reachedEOF = true)
} else {
val nextSplits = routes(deepestYet.splits.length)
Expand All @@ -279,7 +279,7 @@ class BestFirstSearch(
|splitsAfterPolicy=$splitsAfterPolicy""".stripMargin
logger.error(s"""Failed to format
|$msg""".stripMargin)
state = deepestYet
runner.eventCallback(CompleteFormat(explored, deepestYet, tokens))
SearchResult(deepestYet.splits, reachedEOF = false)
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/scalafmt/internal/Decision.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Decision(formatToken: FormatToken, splits: Seq[Split]) {
def noNewlines: Decision =
Decision(formatToken, splits.filter(_.modification.isNewline))

def safeNoNewlines(implicit line: sourcecode.Line): Decision = {
def onlyNewlines(implicit line: sourcecode.Line): Decision = {
val filtered = splits.filter(_.modification.isNewline)
if (filtered.nonEmpty) Decision(formatToken, filtered)
else Decision(formatToken, Seq(Split(Newline, 0)))
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/scalafmt/internal/FormatOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class FormatOps(val tree: Tree,
val expire = matchingParentheses(hash(open))
Policy({
// Newline on every comma.
case Decision(t @ FormatToken(comma: `,`, right, between), splits)
case d@Decision(t @ FormatToken(comma: `,`, right, between), splits)
if owners(open) == owners(comma) &&
// TODO(olafur) what the right { decides to be single line?
!right.isInstanceOf[`{`] &&
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/org/scalafmt/internal/FormatWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FormatWriter(formatOps: FormatOps) {
val sb = new StringBuilder()
var lastState =
State.start // used to calculate start of formatToken.right.
reconstructPath(tokens, splits, style) {
reconstructPath(tokens, splits, style, debug = false) {
case (state, formatToken, whitespace) =>
formatToken.left match {
case c: Comment if c.code.startsWith("/*") =>
Expand Down Expand Up @@ -80,7 +80,7 @@ class FormatWriter(formatOps: FormatOps) {
currState = State.next(currState, style, split, tok)
statesBuilder += FormatLocation(tok, split, currState)
// TIP. Use the following line to debug origin of splits.
if (debug && toks.length < 1000) {
if (debug && tokens.length < 1000) {
val left = cleanup(tok.left).slice(0, 15)
logger.debug(
f"$left%-15s $split ${currState.indentation} ${currState.column}")
Expand All @@ -96,7 +96,7 @@ class FormatWriter(formatOps: FormatOps) {
def reconstructPath(toks: Array[FormatToken],
splits: Vector[Split],
style: ScalafmtStyle,
debug: Boolean = false)(
debug: Boolean)(
callback: (State, FormatToken, String) => Unit): Unit = {
require(toks.length >= splits.length, "splits !=")
val locations = getFormatLocations(toks, splits, style, debug)
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ class Router(formatOps: FormatOps) {
val blockSize = close.start - open.end
val ignore = blockSize > style.maxColumn || isInlineComment(right)
val newlineBeforeClosingCurly = Policy({
case Decision(t @ FormatToken(_, `close`, _), s) =>
case d@Decision(t @ FormatToken(_, `close`, _), s) =>
Decision(t, Seq(Split(Newline, 0)))
d.onlyNewlines
}, close.end)

val (startsLambda, lambdaPolicy, lambdaArrow, lambdaIndent) =
Expand Down Expand Up @@ -810,7 +811,7 @@ class Router(formatOps: FormatOps) {
val breakOnlyBeforeElse = Policy({
case d @ Decision(t, s)
if elses.contains(t.right) && !t.left.isInstanceOf[`}`] =>
d.safeNoNewlines
d.onlyNewlines
}, expire.end)
Seq(
Split(Space, 0)
Expand Down Expand Up @@ -1000,7 +1001,7 @@ class Router(formatOps: FormatOps) {
.withPolicy(SingleLineBlock(expire)),
Split(Space, 1)
.withPolicy(Policy({
case Decision(t @ FormatToken(`arrow`, right, between), s)
case d@Decision(t @ FormatToken(`arrow`, right, between), s)
// TODO(olafur) any other corner cases?
if !right.isInstanceOf[`{`] &&
!isAttachedComment(right, between) =>
Expand Down
4 changes: 3 additions & 1 deletion core/src/test/resources/Test.manual
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ SKIP https://raw.githubusercontent.com/akka/akka/3698928fbdaa8fef8e2037fbc51887a
SKIP https://raw.githubusercontent.com/twitter/scalding/7759d9c399896fc4c54be0f2406e047e030d8586/scalding-core/src/main/scala/com/twitter/scalding/typed/Grouped.scala
SKIP https://raw.githubusercontent.com/JetBrains/intellij-scala/45c86930977278eb445b6aba79ac7011bc418490/src/org/jetbrains/plugins/scala/lang/psi/ScalaPsiUtil.scala
SKIP https://raw.githubusercontent.com/JetBrains/intellij-scala/45c86930977278eb445b6aba79ac7011bc418490/src/org/jetbrains/plugins/scala/codeInspection/collections/MethodRepr.scala
ONLY https://raw.githubusercontent.com/JetBrains/intellij-scala/45c86930977278eb445b6aba79ac7011bc418490/src/org/jetbrains/plugins/scala/lang/psi/types/ScProjectionType.scala
SKIP https://raw.githubusercontent.com/JetBrains/intellij-scala/45c86930977278eb445b6aba79ac7011bc418490/src/org/jetbrains/plugins/scala/lang/psi/types/ScProjectionType.scala
SKIP https://raw.githubusercontent.com/apache/spark/2c5b18fb0fdeabd378dd97e91f72d1eac4e21cc7/sql/core/src/main/scala/org/apache/spark/sql/execution/stat/FrequentItems.scala
SKIP https://raw.githubusercontent.com/apache/spark/2c5b18fb0fdeabd378dd97e91f72d1eac4e21cc7/project/SparkBuild.scala
27 changes: 27 additions & 0 deletions core/src/test/resources/default/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,30 @@ foo.fold(err => {
}
}
}
<<< SKIP freqItems
{
def singlePassFreqItems( ): DataFrame = {

val freqItems = df.select(cols.map(Column(_)) : _*).rdd.aggregate(countMaps)(
seqOp = (counts, row) => {
var i = 0
while (i < numCols) {
val key = row.get(i)
thisMap.add(key, 1L)
i += 1
}
counts
},
combOp = (baseCounts, counts) => {
var i = 0
while (i < numCols) {
baseCounts(i).merge(counts(i))
i += 1
}
baseCounts
}
)
}
}
>>>
x
46 changes: 21 additions & 25 deletions core/src/test/resources/default/Lambda.stat
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,33 @@ def withDedicatedVar: Unit = {
val end = res withOp Nrt.call(Nrt.end_catch)
end withValue res.value
})
<<< ONLY intellij bug

/**
* This type means type projection:
* SomeType#member
* member can be class or type alias
*/
class ScProjectionType private (val projected: ScType, val element: PsiNamedElement,
val superReference: Boolean /* todo: find a way to remove it*/) extends ValueType {
def copy(superReference: Boolean): ScProjectionType = new ScProjectionType(projected, element, superReference)

override protected def isAliasTypeInner: Option[AliasType] = {
if (actualElement.isInstanceOf[ScTypeAlias]) {
actualElement match {
<<< SKIP intellij bug
class ScProjectionType private () {
def isAliasTypeInner = {
x match {
case ta: ScTypeAlias => //higher kind case
val args: ArrayBuffer[ScExistentialArgument] = new ArrayBuffer[ScExistentialArgument]()
val genericSubst = ScalaPsiUtil.
typesCallSubstitutor(ta.typeParameters.map(tp => (tp.name, ScalaPsiUtil.getPsiElementId(tp))),
typesCallSubstitutor(
ta.typeParameters.map(tp => (tp.name, ScalaPsiUtil.getPsiElementId(tp))),
ta.typeParameters.map(tp => {
val name = tp.name + "$$"
args += new ScExistentialArgument(name, Nil, types.Nothing, types.Any)
args = ScExistentialArgument()
ScTypeVariable(name)
}))
val s = actualSubst.followed(genericSubst)
Some(AliasType(ta, ta.lowerBound.map(scType => ScExistentialType(s.subst(scType), args.toList)),
ta.upperBound.map(scType => ScExistentialType(s.subst(scType), args.toList))))
case _ => None
}
}
}
}

>>>
x
class ScProjectionType private () {
def isAliasTypeInner = {
x match {
case ta: ScTypeAlias => //higher kind case
typesCallSubstitutor(
ta.typeParameters.map(tp =>
(tp.name, ScalaPsiUtil.getPsiElementId(tp))),
ta.typeParameters.map(tp => {
args += new ScExistentialArgument()
ScTypeVariable(name)
}))
}
}
}
4 changes: 4 additions & 0 deletions core/src/test/scala/org/scalafmt/FormatExperiment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ trait FormatExperiment extends ScalaProjectsExperiment with FormatAssertions {
Seq(
// TODO(olafur) include this file after fixing #140
"project/MimaExcludes.scala",
// These works if escapeInPathologicalCases = false
"project/SparkBuild.scala",
"src/org/jetbrains/plugins/scala/lang/psi/types/ScProjectionType.scala",
"sql/core/src/main/scala/org/apache/spark/sql/execution/stat/FrequentItems.scala",
// These format fine when run individually, but hog when run together with other files.
"core/src/main/scala/org/apache/spark/deploy/SparkSubmit.scala",
"sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/WindowQuerySuite.scala",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ class ReflectiveDynamicAccess(val classLoader: ClassLoader) {
getClassFor(fqcn) flatMap { c
createInstanceFor(c, args)
}

}

0 comments on commit e5c56b6

Please sign in to comment.