From 9667bca8876dca85d1916dd28699f7ea7f7c2403 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Sun, 5 Jul 2020 19:14:28 +0200 Subject: [PATCH 1/2] Update scalafmt-core to 2.6.2 --- .scalafmt.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 868b548..53d7892 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -8,4 +8,4 @@ rewrite.rules = [ AvoidInfix, ExpandImportSelectors, RedundantParens, SortModifi rewrite.sortModifiers.order = [ "private", "protected", "final", "sealed", "abstract", "implicit", "override", "lazy" ] spaces.inImportCurlyBraces = true # more idiomatic to include whitepsace in import x.{ yyy } trailingCommas = preserve -version = 2.4.2 +version = 2.6.2 From e72ab44a4446774ca4256e5d1ab83b6281d99ae8 Mon Sep 17 00:00:00 2001 From: Enno Runne <458526+ennru@users.noreply.github.com> Date: Mon, 6 Jul 2020 07:59:15 +0200 Subject: [PATCH 2/2] scalafmtAll & scalafmtSbt --- build.sbt | 6 +- src/main/scala/play/doc/PageIndex.scala | 13 +- src/main/scala/play/doc/PlayDoc.scala | 228 +++++++++--------- .../scala/play/doc/PlayDocTemplates.scala | 1 - .../scala/play/doc/FileRepositorySpec.scala | 11 +- src/test/scala/play/doc/PlayDocSpec.scala | 4 +- 6 files changed, 136 insertions(+), 127 deletions(-) diff --git a/build.sbt b/build.sbt index 4a8b88d..a3f4a00 100644 --- a/build.sbt +++ b/build.sbt @@ -34,9 +34,9 @@ lazy val `play-doc` = (project in file(".")) ) libraryDependencies ++= Seq( - "org.pegdown" % "pegdown" % "1.6.0", - "commons-io" % "commons-io" % "2.7", - "org.specs2" %% "specs2-core" % "4.10.0" % Test + "org.pegdown" % "pegdown" % "1.6.0", + "commons-io" % "commons-io" % "2.7", + "org.specs2" %% "specs2-core" % "4.10.0" % Test ) javacOptions ++= Seq( diff --git a/src/main/scala/play/doc/PageIndex.scala b/src/main/scala/play/doc/PageIndex.scala index 0032f18..4992b18 100644 --- a/src/main/scala/play/doc/PageIndex.scala +++ b/src/main/scala/play/doc/PageIndex.scala @@ -51,12 +51,13 @@ case class TocPage(page: String, title: String, next: Option[List[String]]) exte class PageIndex(val toc: Toc, path: Option[String] = None) { private val byPage: Map[String, Page] = { // First, create a by name index - def indexByName(node: TocTree): List[(String, TocTree)] = node match { - case Toc(name, _, nodes, _) => - (name -> node) :: nodes.map(_._2).flatMap(indexByName) - case TocPage(name, _, _) => - List(name -> node) - } + def indexByName(node: TocTree): List[(String, TocTree)] = + node match { + case Toc(name, _, nodes, _) => + (name -> node) :: nodes.map(_._2).flatMap(indexByName) + case TocPage(name, _, _) => + List(name -> node) + } val byNameMap = indexByName(toc).toMap def indexPages(path: Option[String], nav: List[Toc], toc: Toc): List[Page] = { diff --git a/src/main/scala/play/doc/PlayDoc.scala b/src/main/scala/play/doc/PlayDoc.scala index c386c36..3ec1894 100644 --- a/src/main/scala/play/doc/PlayDoc.scala +++ b/src/main/scala/play/doc/PlayDoc.scala @@ -160,19 +160,21 @@ class PlayDoc( } // Recursively search for Sidebar - def findSideBar(file: Option[File]): Option[String] = file match { - case None => None - case Some(parent) => - val sidebar = render(parent.getPath + "/_Sidebar.md", headerIds = false) - sidebar.orElse(findSideBar(Option(parent.getParentFile))) - } + def findSideBar(file: Option[File]): Option[String] = + file match { + case None => None + case Some(parent) => + val sidebar = render(parent.getPath + "/_Sidebar.md", headerIds = false) + sidebar.orElse(findSideBar(Option(parent.getParentFile))) + } - def findBreadcrumbs(file: Option[File]): Option[String] = file match { - case None => None - case Some(parent) => - val breadcrumbs = render(parent.getPath + "/_Breadcrumbs.md", headerIds = false) - breadcrumbs.orElse(findBreadcrumbs(Option(parent.getParentFile))) - } + def findBreadcrumbs(file: Option[File]): Option[String] = + file match { + case None => None + case Some(parent) => + val breadcrumbs = render(parent.getPath + "/_Breadcrumbs.md", headerIds = false) + breadcrumbs.orElse(findBreadcrumbs(Option(parent.getParentFile))) + } // Render both the markdown and the sidebar render(pagePath).map { markdown => @@ -333,113 +335,116 @@ class PlayDoc( codeRepository.loadFile(path) { is => IOUtils.readLines(is).asScala.toSeq } } - def visit(node: Node, visitor: Visitor, printer: Printer) = node match { - case code: CodeReferenceNode => { - // Label is after the #, or if no #, then is the link label - val (source, label) = code.getSource.split("#", 2) match { - case Array(source, label) => (source, label) - case Array(source) => (source, code.getLabel) - } + def visit(node: Node, visitor: Visitor, printer: Printer) = + node match { + case code: CodeReferenceNode => { + // Label is after the #, or if no #, then is the link label + val (source, label) = code.getSource.split("#", 2) match { + case Array(source, label) => (source, label) + case Array(source) => (source, code.getLabel) + } - // The file is either relative to current page or absolute, under the root - val sourceFile = if (source.startsWith("/")) { - repo(source.drop(1)) - } else { - repo(pagePath + source) - } + // The file is either relative to current page or absolute, under the root + val sourceFile = if (source.startsWith("/")) { + repo(source.drop(1)) + } else { + repo(pagePath + source) + } - val segment = if (label.nonEmpty) { - val labelPattern = ("""\s*#\Q""" + label + """\E(\s|\z)""").r - sourceFile.flatMap { sourceCode => - val notLabel = (s: String) => labelPattern.findFirstIn(s).isEmpty - val segment = sourceCode.dropWhile(notLabel).drop(1).takeWhile(notLabel) - if (segment.isEmpty) { - None - } else { - Some(segment) + val segment = if (label.nonEmpty) { + val labelPattern = ("""\s*#\Q""" + label + """\E(\s|\z)""").r + sourceFile.flatMap { sourceCode => + val notLabel = (s: String) => labelPattern.findFirstIn(s).isEmpty + val segment = sourceCode.dropWhile(notLabel).drop(1).takeWhile(notLabel) + if (segment.isEmpty) { + None + } else { + Some(segment) + } } + } else { + sourceFile } - } else { - sourceFile - } - segment - .map { segment => - // Calculate the indent, which is equal to the smallest indent of any line, excluding lines that only consist - // of space characters - val indent = segment - .map { line => if (!line.exists(_ != ' ')) None else Some(line.indexWhere(_ != ' ')) } - .reduce((i1, i2) => - (i1, i2) match { - case (None, None) => None - case (i, None) => i - case (None, i) => i - case (Some(i1), Some(i2)) => Some(math.min(i1, i2)) + segment + .map { segment => + // Calculate the indent, which is equal to the smallest indent of any line, excluding lines that only consist + // of space characters + val indent = segment + .map { line => if (!line.exists(_ != ' ')) None else Some(line.indexWhere(_ != ' ')) } + .reduce((i1, i2) => + (i1, i2) match { + case (None, None) => None + case (i, None) => i + case (None, i) => i + case (Some(i1), Some(i2)) => Some(math.min(i1, i2)) + } + ) + .getOrElse(0) + + // Process directives in segment + case class State(buffer: StringBuilder = new StringBuilder, skip: Option[Int] = None) { + def dropIndentAndAppendLine(s: String): State = { + buffer.append(s.drop(indent)).append("\n") + this + } + def appendLine(s: String): State = { + buffer.append(s).append("\n") + this } - ) - .getOrElse(0) - - // Process directives in segment - case class State(buffer: StringBuilder = new StringBuilder, skip: Option[Int] = None) { - def dropIndentAndAppendLine(s: String): State = { - buffer.append(s.drop(indent)).append("\n") - this - } - def appendLine(s: String): State = { - buffer.append(s).append("\n") - this } - } - val compiledSegment = segment - .foldLeft(State()) { (state, line) => - state.skip match { - case Some(n) if (n > 1) => state.copy(skip = Some(n - 1)) - case Some(n) => state.copy(skip = None) - case None => - line match { - case Insert(code) => state.appendLine(code) - case SkipN(n) => state.copy(skip = Some(n.toInt)) - case Skip() => state - case ReplaceNext(code) => state.appendLine(code).copy(skip = Some(1)) - case _ => state.dropIndentAndAppendLine(line) - } + val compiledSegment = segment + .foldLeft(State()) { (state, line) => + state.skip match { + case Some(n) if n > 1 => state.copy(skip = Some(n - 1)) + case Some(n) => state.copy(skip = None) + case None => + line match { + case Insert(code) => state.appendLine(code) + case SkipN(n) => state.copy(skip = Some(n.toInt)) + case Skip() => state + case ReplaceNext(code) => state.appendLine(code).copy(skip = Some(1)) + case _ => state.dropIndentAndAppendLine(line) + } + } } + .buffer /* Drop last newline */ + .dropRight(1) + .toString() + + // Guess the type of the file + val fileType = source.split("\\.") match { + case withExtension if withExtension.length > 1 => Some(withExtension.last) + case _ => None } - .buffer /* Drop last newline */ - .dropRight(1) - .toString() - - // Guess the type of the file - val fileType = source.split("\\.") match { - case withExtension if (withExtension.length > 1) => Some(withExtension.last) - case _ => None - } - // And visit it - fileType - .map(t => new VerbatimNode(compiledSegment, t)) - .getOrElse(new VerbatimNode(compiledSegment)) - .accept(visitor) + // And visit it + fileType + .map(t => new VerbatimNode(compiledSegment, t)) + .getOrElse(new VerbatimNode(compiledSegment)) + .accept(visitor) - true - } - .getOrElse { - printer.print("Unable to find label " + label + " in source file " + source) - true - } + true + } + .getOrElse { + printer.print("Unable to find label " + label + " in source file " + source) + true + } + } + case _ => false } - case _ => false - } } private class VariableSerializer(variables: Map[String, String]) extends ToHtmlSerializerPlugin { - def visit(node: Node, visitor: Visitor, printer: Printer) = node match { - case variable: VariableNode => { - new TextNode(variables.get(variable.getName).getOrElse("Unknown variable: " + variable.getName)).accept(visitor) - true + def visit(node: Node, visitor: Visitor, printer: Printer) = + node match { + case variable: VariableNode => { + new TextNode(variables.get(variable.getName).getOrElse("Unknown variable: " + variable.getName)) + .accept(visitor) + true + } + case _ => false } - case _ => false - } } private class VerbatimSerializerWrapper(wrapped: VerbatimSerializer) extends VerbatimSerializer { @@ -450,14 +455,15 @@ class PlayDoc( } private class TocSerializer(maybeToc: Option[Toc]) extends ToHtmlSerializerPlugin { - def visit(node: Node, visitor: Visitor, printer: Printer) = node match { - case tocNode: TocNode => - maybeToc.fold(false) { t => - printer.print(templates.toc(t)) - true - } - case _ => false - } + def visit(node: Node, visitor: Visitor, printer: Printer) = + node match { + case tocNode: TocNode => + maybeToc.fold(false) { t => + printer.print(templates.toc(t)) + true + } + case _ => false + } } private val inputStreamToString: InputStream => String = IOUtils.toString(_, "utf-8") diff --git a/src/main/scala/play/doc/PlayDocTemplates.scala b/src/main/scala/play/doc/PlayDocTemplates.scala index 9b02a3b..9f61e33 100644 --- a/src/main/scala/play/doc/PlayDocTemplates.scala +++ b/src/main/scala/play/doc/PlayDocTemplates.scala @@ -30,7 +30,6 @@ trait PlayDocTemplates { def sidebar(hierarchy: List[Toc]): String /** - * * @param hierarchy The hierarchy to render in the breadcrumbs. * @return */ diff --git a/src/test/scala/play/doc/FileRepositorySpec.scala b/src/test/scala/play/doc/FileRepositorySpec.scala index a8d46b5..b6ba676 100644 --- a/src/test/scala/play/doc/FileRepositorySpec.scala +++ b/src/test/scala/play/doc/FileRepositorySpec.scala @@ -8,11 +8,12 @@ import java.util.jar.JarFile class FileRepositorySpec extends Specification { def fileFromClasspath(name: String) = new File(Thread.currentThread.getContextClassLoader.getResource(name).toURI) def loadFileFromRepo(repo: FileRepository, path: String) = repo.loadFile(path)(IOUtils.toString(_, "utf-8")) - def handleFileFromRepo(repo: FileRepository, path: String) = repo.handleFile(path) { handle => - val result = (handle.name, handle.size, IOUtils.toString(handle.is, "utf-8")) - handle.close() - result - } + def handleFileFromRepo(repo: FileRepository, path: String) = + repo.handleFile(path) { handle => + val result = (handle.name, handle.size, IOUtils.toString(handle.is, "utf-8")) + handle.close() + result + } "FilesystemRepository" should { val repo = new FilesystemRepository(fileFromClasspath("file-placeholder").getParentFile) diff --git a/src/test/scala/play/doc/PlayDocSpec.scala b/src/test/scala/play/doc/PlayDocSpec.scala index 88f7bfa..8a83045 100644 --- a/src/test/scala/play/doc/PlayDocSpec.scala +++ b/src/test/scala/play/doc/PlayDocSpec.scala @@ -94,7 +94,9 @@ class PlayDocSpec extends Specification { "play version variables" should { "be substituted with the Play version" in { - oldRenderer.render("The current Play version is %PLAY_VERSION%") must_== "

The current Play version is 2.1.3

" + oldRenderer.render( + "The current Play version is %PLAY_VERSION%" + ) must_== "

The current Play version is 2.1.3

" } "work in verbatim blocks" in { oldRenderer.render("""