From eab7049479a5cab916169aebd21377ea762d44bc Mon Sep 17 00:00:00 2001 From: "andrzej.jozwik@gmail.com" Date: Sat, 6 Sep 2014 00:12:01 +0200 Subject: [PATCH] Comments fixed #1574 --- .../scala/sbt/EvaluateConfigurations.scala | 12 ++- .../sbt/SplitExpressionsNoBlankies.scala | 57 +++++++----- .../test/scala/sbt/CheckIfParsedSpec.scala | 32 +++---- .../src/test/scala/sbt/CommentedXmlSpec.scala | 90 +++++++++---------- main/src/test/scala/sbt/NewFormatSpec.scala | 12 ++- .../scala/sbt/SplitExpressionsBehavior.scala | 4 +- 6 files changed, 110 insertions(+), 97 deletions(-) diff --git a/main/src/main/scala/sbt/EvaluateConfigurations.scala b/main/src/main/scala/sbt/EvaluateConfigurations.scala index 621a20d8f..f1c200a42 100644 --- a/main/src/main/scala/sbt/EvaluateConfigurations.scala +++ b/main/src/main/scala/sbt/EvaluateConfigurations.scala @@ -4,12 +4,10 @@ package sbt import java.io.File -import java.net.URI import compiler.{ Eval, EvalImports } import complete.DefaultParsers.validID -import Def.{ ScopedKey, Setting, SettingsDefinition } +import Def.{ ScopedKey, Setting } import Scope.GlobalScope -import scala.annotation.tailrec /** * This file is responsible for compiling the .sbt files used to configure sbt builds. @@ -61,7 +59,7 @@ object EvaluateConfigurations { * Parses a sequence of build.sbt lines into a [[ParsedFile]]. The result contains * a fragmentation of all imports, settings and definitions. * - * @param buildinImports The set of import statements to add to those parsed in the .sbt file. + * @param builtinImports The set of import statements to add to those parsed in the .sbt file. */ private[this] def parseConfiguration(file: File, lines: Seq[String], builtinImports: Seq[String], offset: Int): ParsedFile = { @@ -167,7 +165,7 @@ object EvaluateConfigurations { * @param expression The scala expression we're compiling * @param range The original position in source of the expression, for error messages. * - * @return A method that given an sbt classloader, can return the actual [[DslEntry]] defined by + * @return A method that given an sbt classloader, can return the actual [[internals.DslEntry]] defined by * the expression, and the sequence of .class files generated. */ private[sbt] def evaluateDslEntry(eval: Eval, name: String, imports: Seq[(String, Int)], expression: String, range: LineRange): TrackedEvalResult[internals.DslEntry] = { @@ -216,7 +214,7 @@ object EvaluateConfigurations { */ def splitExpressions(file: File, lines: Seq[String]): (Seq[(String, Int)], Seq[(String, LineRange)]) = { - val split = SplitExpressionsNoBlankies(null, lines) + val split = SplitExpressionsNoBlankies(file, lines) (split.imports, split.settings) } @@ -255,7 +253,7 @@ object Index { val multiMap = settings.groupBy(label) val duplicates = multiMap collect { case (k, xs) if xs.size > 1 => (k, xs.map(_.manifest)) } collect { case (k, xs) if xs.size > 1 => (k, xs) } if (duplicates.isEmpty) - multiMap.collect { case (k, v) if validID(k) => (k, v.head) } toMap; + multiMap.collect { case (k, v) if validID(k) => (k, v.head) } toMap else sys.error(duplicates map { case (k, tps) => "'" + k + "' (" + tps.mkString(", ") + ")" } mkString ("Some keys were defined with the same name but different types: ", ", ", "")) } diff --git a/main/src/main/scala/sbt/SplitExpressionsNoBlankies.scala b/main/src/main/scala/sbt/SplitExpressionsNoBlankies.scala index 02ed24419..3dc971dfa 100644 --- a/main/src/main/scala/sbt/SplitExpressionsNoBlankies.scala +++ b/main/src/main/scala/sbt/SplitExpressionsNoBlankies.scala @@ -6,7 +6,8 @@ import scala.annotation.tailrec import SplitExpressionsNoBlankies._ object SplitExpressionsNoBlankies { - val END_OF_LINE = "\n" + val END_OF_LINE_CHAR = '\n' + val END_OF_LINE = String.valueOf(END_OF_LINE_CHAR) } case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) { @@ -26,13 +27,14 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) { val indexedLines = lines.toIndexedSeq val original = indexedLines.mkString(END_OF_LINE) val merged = handleXmlContent(original) - val fileName = if (file == null) "Here should be file name" else file.getAbsolutePath + val fileName = file.getAbsolutePath val parsed = try { toolbox.parse(merged) } catch { case e: ToolBoxError => + ConsoleLogger(System.err).trace(e) val seq = toolbox.frontEnd.infos.map { i => s"""[$fileName]:${i.pos.line}: ${i.msg}""" } @@ -54,17 +56,28 @@ case class SplitExpressionsNoBlankies(file: File, lines: Seq[String]) { def convertImport(t: Tree): (String, Int) = (merged.substring(t.pos.start, t.pos.end), t.pos.line - 1) + /** + * See BugInParser + * @param t - tree + * @param originalStatement - original + * @return originalStatement or originalStatement with missing bracket + */ + def parseStatementAgain(t: Tree, originalStatement: String): String = { + val statement = util.Try(toolbox.parse(originalStatement)) match { + case util.Failure(th) => + val missingText = tryWithNextStatement(merged, t.pos.end, t.pos.line, fileName, th) + originalStatement + missingText + case _ => + originalStatement + } + statement + } + def convertStatement(t: Tree): Option[(String, LineRange)] = if (t.pos.isDefined) { val originalStatement = merged.substring(t.pos.start, t.pos.end) - val statement = util.Try(toolbox.parse(originalStatement)) match { - case util.Failure(th) => - val missingText = tryWithNextStatement(merged, t.pos.end, t.pos.line, fileName, th) - originalStatement + missingText - case _ => - originalStatement - } - val numberLines = statement.count(c => c == '\n') + val statement = parseStatementAgain(t, originalStatement) + val numberLines = statement.count(c => c == END_OF_LINE_CHAR) Some((statement, LineRange(t.pos.line - 1, t.pos.line + numberLines))) } else { None @@ -85,7 +98,7 @@ private[sbt] object BugInParser { * @param content - parsed file * @param positionEnd - from index * @param positionLine - number of start position line - * @param fileName - + * @param fileName - file name * @param th - original exception * @return */ @@ -156,7 +169,7 @@ private object XmlContent { /** * Cut file for normal text - xml - normal text - xml .... - * @param content - + * @param content - content * @param ts - import/statements * @return (text,true) - is xml (text,false) - if normal text */ @@ -181,7 +194,7 @@ private object XmlContent { /** * Cut potential xmls from content - * @param content - + * @param content - content * @return sorted by openIndex xml parts */ private def findXmlParts(content: String): Seq[(String, Int, Int)] = { @@ -283,9 +296,9 @@ private object XmlContent { /** * - * @param content - - * @param xmlParts - - * @return + * @param content - content + * @param xmlParts - xmlParts + * @return content with xml with brackets */ private def addExplicitXmlContent(content: String, xmlParts: Seq[(String, Int, Int)]): String = { val statements: Seq[(String, Boolean)] = splitFile(content, xmlParts) @@ -323,12 +336,12 @@ private object XmlContent { /** * Add to head if option is not empty - * @param acc - seq - * @param option - + * @param ts - seq + * @param option - option * @tparam T - type * @return original seq for None, add to head for Some[T] */ - private def addOptionToCollection[T](acc: Seq[T], option: Option[T]) = option.fold(acc)(el => el +: acc) + private def addOptionToCollection[T](ts: Seq[T], option: Option[T]) = option.fold(ts)(el => el +: ts) private def findNotModifiedOpeningTag(content: String, closeTagStartIndex: Int, closeTagEndIndex: Int): Option[(String, Int, Int)] = { @@ -348,8 +361,8 @@ private object XmlContent { /** * Check, if xmlPart is valid xml. If not - None is returned * @param content - file content - * @param openIndex - - * @param closeIndex - + * @param openIndex - open index + * @param closeIndex - close index * @return Some((String,Int,Int)) */ private def xmlFragmentOption(content: String, openIndex: Int, closeIndex: Int): Option[(String, Int, Int)] = { @@ -362,7 +375,7 @@ private object XmlContent { /** * If xml is in brackets - we do not need to add them - * @param statement - + * @param statement - statement * @return are brackets necessary? */ private def areBracketsNecessary(statement: String): Boolean = { diff --git a/main/src/test/scala/sbt/CheckIfParsedSpec.scala b/main/src/test/scala/sbt/CheckIfParsedSpec.scala index 45f9c5c1a..b1015f400 100644 --- a/main/src/test/scala/sbt/CheckIfParsedSpec.scala +++ b/main/src/test/scala/sbt/CheckIfParsedSpec.scala @@ -9,28 +9,24 @@ abstract class CheckIfParsedSpec(implicit val splitter: SplitExpressions.SplitEx case (content, description, nonEmptyImports, nonEmptyStatements) => println(s"""${getClass.getSimpleName}: "$description" """) val (imports, statements) = split(content) - - statements.nonEmpty must be_==(nonEmptyStatements) - // orPending(s"""$description - // |***${shouldContains(nonEmptyStatements)} statements*** - // |$content """.stripMargin) - - imports.nonEmpty must be_==(nonEmptyImports) - // orPending(s"""$description - // |***${shouldContains(nonEmptyImports)} imports*** - // |$content """.stripMargin) + statements.nonEmpty must be_==(nonEmptyStatements).setMessage(s"""$description + |***${shouldContains(nonEmptyStatements)} statements*** + |$content """.stripMargin) + imports.nonEmpty must be_==(nonEmptyImports).setMessage(s"""$description + |***${shouldContains(nonEmptyImports)} imports*** + |$content """.stripMargin) } } } - // private def shouldContains(b: Boolean) = s"""Should ${ - // if (b) { - // "contain" - // } else { - // "not contain" - // } - // }""" + private def shouldContains(b: Boolean) = s"""Should ${ + if (b) { + "contain" + } else { + "not contain" + } + }""" - protected def files: Seq[(String, String, Boolean, Boolean)] + protected val files: Seq[(String, String, Boolean, Boolean)] } diff --git a/main/src/test/scala/sbt/CommentedXmlSpec.scala b/main/src/test/scala/sbt/CommentedXmlSpec.scala index 6ddb30ed6..decf99b1c 100644 --- a/main/src/test/scala/sbt/CommentedXmlSpec.scala +++ b/main/src/test/scala/sbt/CommentedXmlSpec.scala @@ -2,7 +2,7 @@ package sbt class CommentedXmlSpec extends CheckIfParsedSpec { - override protected def files: Seq[(String, String, Boolean, Boolean)] = Seq( + override protected val files = Seq( ( s"""| |val pom = "" @@ -15,53 +15,53 @@ class CommentedXmlSpec extends CheckIfParsedSpec { | """.stripMargin, "Xml in string", false, true), (""" - |val scmpom = taskKey[xml.NodeBuffer]("Node buffer") - | - |scmpom := - | git@github.com:mohiva/play-html-compressor.git - | scm:git:git@github.com:mohiva/play-html-compressor.git - | - | - | - | akkie - | Christian Kaps - | http://mohiva.com - | - | - | // - | - | - |publishMavenStyle := true - | - """.stripMargin, "Wrong Commented xml ", false, true), + |val scmpom = taskKey[xml.NodeBuffer]("Node buffer") + | + |scmpom := + | git@github.com:mohiva/play-html-compressor.git + | scm:git:git@github.com:mohiva/play-html-compressor.git + | + | + | + | akkie + | Christian Kaps + | http://mohiva.com + | + | + | // + | + | + |publishMavenStyle := true + | + """.stripMargin, "Wrong Commented xml ", false, true), (""" - |val scmpom = taskKey[xml.NodeBuffer]("Node buffer") - | - |scmpom := - | git@github.com:mohiva/play-html-compressor.git - | scm:git:git@github.com:mohiva/play-html-compressor.git - | - | - | - | akkie - | Christian Kaps - | http://mohiva.com - | - | - | // - | // - | - |publishMavenStyle := true - | - """.stripMargin, "Commented xml ", false, true), + |val scmpom = taskKey[xml.NodeBuffer]("Node buffer") + | + |scmpom := + | git@github.com:mohiva/play-html-compressor.git + | scm:git:git@github.com:mohiva/play-html-compressor.git + | + | + | + | akkie + | Christian Kaps + | http://mohiva.com + | + | + | // + | // + | + |publishMavenStyle := true + | + """.stripMargin, "Commented xml ", false, true), (""" - |import sbt._ - | - |// - """.stripMargin, "Xml in comment2", false, false) + |// a/> + """.stripMargin, "Xml in comment2", false, false) ) } diff --git a/main/src/test/scala/sbt/NewFormatSpec.scala b/main/src/test/scala/sbt/NewFormatSpec.scala index f9f447b50..ee5a31493 100644 --- a/main/src/test/scala/sbt/NewFormatSpec.scala +++ b/main/src/test/scala/sbt/NewFormatSpec.scala @@ -1,8 +1,13 @@ package sbt import java.io.File + +import org.junit.runner.RunWith +import org.specs2.runner.JUnitRunner + import scala.io.Source +@RunWith(classOf[JUnitRunner]) class NewFormatSpec extends AbstractSpec { implicit val splitter: SplitExpressions.SplitExpression = EvaluateConfigurations.splitExpressions @@ -16,10 +21,9 @@ class NewFormatSpec extends AbstractSpec { println(s"$path") val lines = Source.fromFile(path).getLines().toList val (_, statements) = splitter(path, lines) - statements.nonEmpty must be_==(true) - // orPending(s""" - // |***should contains statements*** - // |$lines """.stripMargin) + statements.nonEmpty must be_==(true).setMessage(s""" + |***should contains statements*** + |$lines """.stripMargin) } } } diff --git a/main/src/test/scala/sbt/SplitExpressionsBehavior.scala b/main/src/test/scala/sbt/SplitExpressionsBehavior.scala index c44f182e0..246a29e11 100644 --- a/main/src/test/scala/sbt/SplitExpressionsBehavior.scala +++ b/main/src/test/scala/sbt/SplitExpressionsBehavior.scala @@ -1,9 +1,11 @@ package sbt +import java.io.File + import org.specs2.mutable.SpecificationLike trait SplitExpression { - def split(s: String)(implicit splitter: SplitExpressions.SplitExpression) = splitter(null, s.split("\n").toSeq) + def split(s: String, file: File = new File("noFile"))(implicit splitter: SplitExpressions.SplitExpression) = splitter(file, s.split("\n").toSeq) } trait SplitExpressionsBehavior extends SplitExpression {