Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update scalafmt-core to 2.6.2 #113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions src/main/scala/play/doc/PageIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
228 changes: 117 additions & 111 deletions src/main/scala/play/doc/PlayDoc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/play/doc/PlayDocTemplates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ trait PlayDocTemplates {
def sidebar(hierarchy: List[Toc]): String

/**
*
* @param hierarchy The hierarchy to render in the breadcrumbs.
* @return
*/
Expand Down
11 changes: 6 additions & 5 deletions src/test/scala/play/doc/FileRepositorySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/test/scala/play/doc/PlayDocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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_== "<p>The current Play version is 2.1.3</p>"
oldRenderer.render(
"The current Play version is %PLAY_VERSION%"
) must_== "<p>The current Play version is 2.1.3</p>"
}
"work in verbatim blocks" in {
oldRenderer.render("""
Expand Down