Split to small methods

This commit is contained in:
andrzej.jozwik@gmail.com 2014-09-30 18:04:30 +02:00 committed by Eugene Yokota
parent 963e75d182
commit dac3edb546
1 changed files with 37 additions and 26 deletions

View File

@ -1,48 +1,60 @@
package sbt.internals.parser
package sbt
package internals
package parser
import scala.reflect.runtime.universe._
private[sbt] object SbtRefactorings {
import sbt.internals.parser.SplitExpressionsNoBlankies.{ END_OF_LINE, FAKE_FILE }
val EMPTY_STRING = ""
val REVERSE_ORDERING_INT = Ordering[Int].reverse
def applyStatements(lines: List[String], setCommands: List[List[String]]): List[String] = {
def applyStatements(lines: List[String], commands: List[List[String]]): List[String] = {
val split = SplitExpressionsNoBlankies(FAKE_FILE, lines)
val recordedCommand = setCommands.flatMap {
command =>
val map = toTreeStringMap(command)
map.flatMap {
case (name, statement) =>
split.settingsTrees.foldLeft(Seq.empty[(Int, String, String)]) {
case (acc, (st, tree)) =>
val treeName = extractSettingName(tree)
if (name == treeName) {
val replacement = if (acc.isEmpty) {
command.mkString(END_OF_LINE)
} else {
EMPTY_STRING
}
(tree.pos.start, st, replacement) +: acc
} else {
acc
}
}
}
}
val recordedCommand = recordCommands(commands, split)
val sortedRecordedCommand = recordedCommand.sortBy(_._1)(REVERSE_ORDERING_INT)
val newContent = sortedRecordedCommand.foldLeft(split.modifiedContent) {
case (acc, (from, old, replacement)) =>
val before = acc.substring(0, from)
val after = acc.substring(from + old.length, acc.length)
val afterLast = if (after.trim.isEmpty) after.trim else after
val afterLast = emptyStringForEmptyStatement(after)
before + replacement + afterLast
}
newContent.lines.toList
}
def emptyStringForEmptyStatement(after: String) =
if (after.trim.isEmpty) EMPTY_STRING else after
private def recordCommands(commands: List[List[String]], split: SplitExpressionsNoBlankies) =
commands.flatMap {
command =>
val map = toTreeStringMap(command)
map.flatMap {
case (name, statement) =>
treesToReplacements(split, name, command)
}
}
private def treesToReplacements(split: SplitExpressionsNoBlankies, name: String, command: List[String]) =
split.settingsTrees.foldLeft(Seq.empty[(Int, String, String)]) {
case (acc, (st, tree)) =>
val treeName = extractSettingName(tree)
if (name == treeName) {
val replacement = if (acc.isEmpty) {
command.mkString(END_OF_LINE)
} else {
EMPTY_STRING
}
(tree.pos.start, st, replacement) +: acc
} else {
acc
}
}
private def toTreeStringMap(command: List[String]) = {
val split = SplitExpressionsNoBlankies(FAKE_FILE, command)
val trees = split.settingsTrees
@ -53,13 +65,12 @@ private[sbt] object SbtRefactorings {
seq.toMap
}
private def extractSettingName(tree: Tree): String = {
private def extractSettingName(tree: Tree): String =
tree.children match {
case h :: _ =>
extractSettingName(h)
case _ =>
tree.toString()
}
}
}