From cc1138523ee00eed31b1ee54fa5f8da529b5c94c Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Thu, 4 Apr 2019 13:21:44 +0200 Subject: [PATCH] Correctly parse stream signatures (#26659) (#26669) That were spread over multiple lines since switching to scalafmt --- project/ParadoxSupport.scala | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/project/ParadoxSupport.scala b/project/ParadoxSupport.scala index 665569b4cb..b3fdd21318 100644 --- a/project/ParadoxSupport.scala +++ b/project/ParadoxSupport.scala @@ -13,6 +13,7 @@ import org.pegdown.ast._ import sbt.Keys._ import sbt._ +import scala.annotation.tailrec import scala.io.{Codec, Source} import scala.collection.JavaConverters._ @@ -52,9 +53,9 @@ object ParadoxSupport { } else new File(page.file.getParentFile, source) val Signature = """\s*((def|val|type) (\w+)(?=[:(\[]).*)(\s+\=.*)""".r // stupid approximation to match a signature - //println(s"Looking for signature regex '$Signature'") + val text = - Source.fromFile(file)(Codec.UTF8).getLines.collect { + getDefs(file).collect { case line@Signature(signature, kind, l, definition) if labels contains l.replaceAll("Mat$", "").toLowerCase() => //println(s"Found label '$l' with sig '$full' in line $line") if (kind == "type") signature + definition @@ -62,11 +63,9 @@ object ParadoxSupport { }.mkString("\n") if (text.trim.isEmpty) { - logWarn( - s"Did not find any signatures with one of those names [${labels.mkString(", ")}] in ${node.source} " + + throw new IllegalArgumentException( + s"Did not find any signatures with one of those names [${labels.mkString(", ")}] in $source " + s"(was referenced from [${page.path}])") - - new HtmlBlockNode(s"""
[Broken signature inclusion [${labels.mkString(", ")}] to [${node.source}]
""").accept(visitor) } else { val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file)) new VerbatimNode(text, lang).accept(visitor) @@ -77,4 +76,19 @@ object ParadoxSupport { } } + def getDefs(file: File): Seq[String] = { + val Indented = "(\\s*)(.*)".r + + @tailrec + def rec(lines: Iterator[String], currentDef: Option[String], defIndent: Integer, soFar: Seq[String]): Seq[String] = { + if (!lines.hasNext) soFar ++ currentDef + else lines.next() match { + case Indented(indent, line) => + if (line.startsWith("def")) rec(lines, Some(line), indent.length, soFar ++ currentDef) + else if (indent.length == defIndent + 4) rec(lines, currentDef.map(_ ++ line), defIndent, soFar) + else rec(lines, None, 0, soFar ++ currentDef) + } + } + rec(Source.fromFile(file)(Codec.UTF8).getLines, None, 0, Nil) + } }