Handle xml content in session save

This commit is contained in:
andrzej.jozwik@gmail.com 2014-09-16 11:25:33 +02:00 committed by Eugene Yokota
parent c720a973a6
commit 232c28ecd1
12 changed files with 86 additions and 26 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
target/ target/
__pycache__ __pycache__
.idea
.idea_modules

View File

@ -18,16 +18,16 @@ object SessionSettingsNoBlankies {
oldLinesToNew(newContent, lineMap.tail) oldLinesToNew(newContent, lineMap.tail)
} }
private def toNewContent(content: List[String], tuple: (Int, List[(Int, List[String])])): List[String] = { private def toNewContent(content: List[String], setCommands: (Int, List[(Int, List[String])])): List[String] = {
val (from, newSettingSeq) = tuple val (from, newSettings) = setCommands
val newTreeStringSeqMap = newSettingSeq.seq.map { val newTreeStringsMap = newSettings.map {
case (_, lines) => toTreeStringMap(lines) case (_, lines) => toTreeStringMap(lines)
} }
val to = newSettingSeq.map(_._1).max val to = newSettings.map(_._1).max
val originalLine = content.slice(from - 1, to - 1) val originalLine = content.slice(from - 1, to - 1)
val operations = newTreeStringSeqMap.flatMap { val operations = newTreeStringsMap.flatMap {
map => map =>
map.flatMap { map.flatMap {
case (name, (startIndex, statement)) => case (name, (startIndex, statement)) =>
@ -39,7 +39,7 @@ object SessionSettingsNoBlankies {
} }
} }
} }
val statements = originalLine.mkString("\n") val statements = XmlContent.handleXmlContent(originalLine.mkString("\n"))
val sortedOperations = operations.sortBy(_._1)(REVERSE_ORDERING_INT) val sortedOperations = operations.sortBy(_._1)(REVERSE_ORDERING_INT)
val newContent = sortedOperations.foldLeft(statements) { val newContent = sortedOperations.foldLeft(statements) {
case (acc, (startIndex, old, newStatement)) => case (acc, (startIndex, old, newStatement)) =>
@ -58,8 +58,8 @@ object SessionSettingsNoBlankies {
} }
private def toTreeStringMap(lines: List[String]) = { private def toTreeStringMap(lines: List[String]) = {
val split = SplitExpressionsNoBlankies(new File("fake"), lines)
val trees = SplitExpressionsNoBlankies(new File("fake"), lines).settingsTrees val trees = split.settingsTrees
val seq = trees.map { val seq = trees.map {
case (statement, tree) => case (statement, tree) =>
(extractSettingName(tree), (tree.pos.start, statement)) (extractSettingName(tree), (tree.pos.start, statement))

View File

@ -28,12 +28,12 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) {
val toolbox = mirror.mkToolBox(options = "-Yrangepos") val toolbox = mirror.mkToolBox(options = "-Yrangepos")
val indexedLines = lines.toIndexedSeq val indexedLines = lines.toIndexedSeq
val original = indexedLines.mkString(END_OF_LINE) val original = indexedLines.mkString(END_OF_LINE)
val merged = handleXmlContent(original) val modifiedContent = handleXmlContent(original)
val fileName = file.getAbsolutePath val fileName = file.getAbsolutePath
val parsed = val parsed =
try { try {
toolbox.parse(merged) toolbox.parse(modifiedContent)
} catch { } catch {
case e: ToolBoxError => case e: ToolBoxError =>
val seq = toolbox.frontEnd.infos.map { i => val seq = toolbox.frontEnd.infos.map { i =>
@ -41,7 +41,7 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) {
} }
throw new MessageOnlyException( throw new MessageOnlyException(
s"""====== s"""======
|$merged |$modifiedContent
|====== |======
|${seq.mkString(EOL)}""".stripMargin) |${seq.mkString(EOL)}""".stripMargin)
} }
@ -58,7 +58,7 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) {
} }
def convertImport(t: Tree): (String, Int) = def convertImport(t: Tree): (String, Int) =
(merged.substring(t.pos.start, t.pos.end), t.pos.line - 1) (modifiedContent.substring(t.pos.start, t.pos.end), t.pos.line - 1)
/** /**
* See BugInParser * See BugInParser
@ -69,7 +69,7 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) {
def parseStatementAgain(t: Tree, originalStatement: String): String = { def parseStatementAgain(t: Tree, originalStatement: String): String = {
val statement = util.Try(toolbox.parse(originalStatement)) match { val statement = util.Try(toolbox.parse(originalStatement)) match {
case util.Failure(th) => case util.Failure(th) =>
val missingText = tryWithNextStatement(merged, t.pos.end, t.pos.line, fileName, th) val missingText = tryWithNextStatement(modifiedContent, t.pos.end, t.pos.line, fileName, th)
originalStatement + missingText originalStatement + missingText
case _ => case _ =>
originalStatement originalStatement
@ -79,7 +79,7 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) {
def convertStatement(t: Tree): Option[(String, Tree, LineRange)] = def convertStatement(t: Tree): Option[(String, Tree, LineRange)] =
if (t.pos.isDefined) { if (t.pos.isDefined) {
val originalStatement = merged.substring(t.pos.start, t.pos.end) val originalStatement = modifiedContent.substring(t.pos.start, t.pos.end)
val statement = parseStatementAgain(t, originalStatement) val statement = parseStatementAgain(t, originalStatement)
val numberLines = statement.count(c => c == END_OF_LINE_CHAR) val numberLines = statement.count(c => c == END_OF_LINE_CHAR)
Some((statement, t, LineRange(t.pos.line - 1, t.pos.line + numberLines))) Some((statement, t, LineRange(t.pos.line - 1, t.pos.line + numberLines)))

View File

@ -2,6 +2,6 @@
<setting> <setting>
<start>1</start> <start>1</start>
<end>2</end> <end>2</end>
<set>name := "alaMaKota"</set> <set><![CDATA[name := "alaMaKota"]]></set>
</setting> </setting>
</settings> </settings>

View File

@ -2,11 +2,11 @@
<setting> <setting>
<start>1</start> <start>1</start>
<end>2</end> <end>2</end>
<set>name := "alaMaKota"</set> <set><![CDATA[name := "alaMaKota"]]></set>
</setting> </setting>
<setting> <setting>
<start>25</start> <start>25</start>
<end>26</end> <end>26</end>
<set>organization := "scalania"</set> <set><![CDATA[organization := "scalania"]]></set>
</setting> </setting>
</settings> </settings>

View File

@ -0,0 +1,7 @@
<settings>
<setting>
<start>4</start>
<end>11</end>
<set><![CDATA[checkPom := "OK"]]></set>
</setting>
</settings>

View File

@ -0,0 +1,21 @@
name := "newName"
libraryDependencies := Seq("org.scala-sbt" %% "sbinary" % "0.4.1")
lazy val checkPom = taskKey[Unit]("check pom to ensure no <type> sections are generated"); checkPom := "OK";scalacOptions := Seq("-deprecation")
val b = ( <b/>)
val a = <aaa>
</aaa>
/*
*/
organization := "jozwikr" // OK
scalaVersion := "2.9.2"
organization := "ololol"

View File

@ -2,6 +2,6 @@
<setting> <setting>
<start>1</start> <start>1</start>
<end>2</end> <end>2</end>
<set>name := "alaMaKota"</set> <set><![CDATA[name := "alaMaKota"]]></set>
</setting> </setting>
</settings> </settings>

View File

@ -0,0 +1,16 @@
import sbt._
val scmpom = taskKey[xml.NodeBuffer]("Node buffer")
scmpom := <scm>
<url>git@github.com:mohiva/play-html-compressor.git</url>
<connection>scm:git:git@github.com:mohiva/play-html-compressor.git</connection>
</scm>
<developers>
<developer>
<id>akkie</id>
<name>Christian Kaps</name>
<url>http://mohiva.com</url>
</developer>
</developers>
<a></a>

View File

@ -0,0 +1,7 @@
<settings>
<setting>
<start>5</start>
<end>18</end>
<set><![CDATA[scmpom := <a/><b a="rt">OK</b>]]></set>
</setting>
</settings>

View File

@ -0,0 +1,5 @@
import sbt._
val scmpom = taskKey[xml.NodeBuffer]("Node buffer")
scmpom := ( <a/><b a="rt">OK</b> )

View File

@ -9,14 +9,14 @@ import scala.collection.immutable.{ SortedMap, TreeMap }
import scala.io.Source import scala.io.Source
import scala.xml.XML import scala.xml.XML
abstract class AbstractSessionSettingsSpec(folder: String) extends AbstractSpec { abstract class AbstractSessionSettingsSpec(folder: String, printDetails: Boolean = false) extends AbstractSpec {
protected val rootPath = getClass.getResource("").getPath + folder protected val rootPath = getClass.getResource("").getPath + folder
println(s"Reading files from: $rootPath") println(s"Reading files from: $rootPath")
protected val rootDir = new File(rootPath) protected val rootDir = new File(rootPath)
"SessionSettings " should { "SessionSettings " should {
"Be identical for empty map " in { "Be identical for empty map " in {
def unit(f: File) = Seq((Source.fromFile(f).getLines().toSeq, SortedMap.empty[Int, List[(Int, List[String])]])) def unit(f: File) = Seq((Source.fromFile(f).getLines().toList, SortedMap.empty[Int, List[(Int, List[String])]]))
runTestOnFiles(unit) runTestOnFiles(unit)
} }
@ -25,7 +25,7 @@ abstract class AbstractSessionSettingsSpec(folder: String) extends AbstractSpec
} }
} }
private def runTestOnFiles(expectedResultAndMap: File => Seq[(Seq[String], SortedMap[Int, List[(Int, List[String])]])]): MatchResult[GenTraversableOnce[File]] = { private def runTestOnFiles(expectedResultAndMap: File => Seq[(List[String], SortedMap[Int, List[(Int, List[String])]])]): MatchResult[GenTraversableOnce[File]] = {
val allFiles = rootDir.listFiles(new FilenameFilter() { val allFiles = rootDir.listFiles(new FilenameFilter() {
def accept(dir: File, name: String) = name.endsWith(".sbt.txt") def accept(dir: File, name: String) = name.endsWith(".sbt.txt")
@ -34,9 +34,11 @@ abstract class AbstractSessionSettingsSpec(folder: String) extends AbstractSpec
file => file =>
val originalLines = Source.fromFile(file).getLines().toList val originalLines = Source.fromFile(file).getLines().toList
foreach(expectedResultAndMap(file)) { foreach(expectedResultAndMap(file)) {
case (expectedResult, map) => case (expectedResultList, map) =>
val result = SessionSettingsNoBlankies.oldLinesToNew(originalLines, map) val resultList = SessionSettingsNoBlankies.oldLinesToNew(originalLines, map)
expectedResult === result val expected = SplitExpressionsNoBlankies(file, expectedResultList)
val result = SplitExpressionsNoBlankies(file, resultList)
result.settings must_== expected.settings
} }
} }
} }
@ -55,7 +57,7 @@ abstract class AbstractSessionSettingsSpec(folder: String) extends AbstractSpec
}) })
files.map { xmlFile => files.map { xmlFile =>
val xml = XML.loadFile(xmlFile) val xml = XML.loadFile(xmlFile)
val result = Source.fromFile(xmlFile.getAbsolutePath + ".result").getLines().toSeq val result = Source.fromFile(xmlFile.getAbsolutePath + ".result").getLines().toList
val tupleCollection = (xml \\ "settings" \\ "setting").map { val tupleCollection = (xml \\ "settings" \\ "setting").map {
node => node =>
val set = (node \\ "set").text val set = (node \\ "set").text