Correctly parse stream signatures (#26659) (#26669)

That were spread over multiple lines since switching to scalafmt
This commit is contained in:
Arnout Engelen 2019-04-04 13:21:44 +02:00 committed by Johan Andrén
parent 9a19a73f9a
commit cc1138523e

View file

@ -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"""<div style="color: red;">[Broken signature inclusion [${labels.mkString(", ")}] to [${node.source}]</div>""").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)
}
}