Skip to content

Commit

Permalink
bugfix: Remove additional whitespace from summary
Browse files Browse the repository at this point in the history
The recent changes caused the spaces to no longer be removed properly due to the fact that we no longer operate on an actual value when printing one line summary.

I tried solving this multiple ways but:
- there is no sensible way to just copy any value
- using pprint.Tree as itnermediate representation doesn't work as those contain Iterator and are one use only

The easiest was to just replace newline with space and then deduplicate whitespace to save space on the online summary. This also makes it consistent across Scala 2 and 3
  • Loading branch information
tgodzik committed Jul 9, 2024
1 parent c005c99 commit 3253544
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 28 deletions.
21 changes: 7 additions & 14 deletions runtime/src/main/scala-2/mdoc/internal/document/Printing.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mdoc.internal.document

import pprint.TPrintColors
import pprint.PPrinter.BlackWhite
import fansi.Str
import pprint.PPrinter
import pprint.PPrinter.BlackWhite
import pprint.TPrintColors

import Compat.TPrint

object Printing {
Expand All @@ -15,17 +17,8 @@ object Printing {
.foreach(text => out.appendAll(text.getChars))
}

def printOneLine[T](value: T, out: StringBuilder, width: Int) = {
val chunk = BlackWhite
.tokenize(value, width)
.map(_.getChars)
.filterNot(_.iterator.forall(_.isWhitespace))
.flatMap(_.iterator)
.filter {
case '\n' => false
case _ => true
}
out.appendAll(chunk)

def printOneLine(value: Str, out: StringBuilder, width: Int) = {
out.appendAll(value.toString().replace("\n", " ").replaceAll("\\s+", " ")).take(width + 1)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Printing {
}

inline def printOneLine[T](value: T, out: StringBuilder, width: Int) = {
out.append(nullableToString(value).replace("\n", ""))
out.append(nullableToString(value).replace("\n", "").replaceAll("\\s+", " "))
}

private def nullableToString[T](value: T): String = {
Expand Down
107 changes: 94 additions & 13 deletions tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class WorksheetSuite extends BaseSuite {
.load(classOf[Mdoc], this.getClass().getClassLoader())
.iterator()
.next()
.withScreenWidth(30)
.withScreenHeight(5)
.withClasspath(
CompatClassloader
Expand Down Expand Up @@ -147,6 +146,58 @@ class WorksheetSuite extends BaseSuite {
modifier = Some("reset-object")
)

checkDecorations(
"scalatags",
"""|import $dep.`com.lihaoyi::scalatags:0.13.1`
|import scalatags.Text.all._
|val htmlFile = html(
| body(
| p("This is a big paragraph of text")
| )
|)
|""".stripMargin,
"""|import $dep.`com.lihaoyi::scalatags:0.13.1`
|import scalatags.Text.all._
|<val htmlFile = html(
| body(
| p("This is a big paragraph of text")
| )
|)> // : scalatags.Text.TypedTag[String] = TypedTag( tag = "html", modifiers = List( ArraySeq( TypedTag( t...
|htmlFile: scalatags.Text.TypedTag[String] = TypedTag(
| tag = "html",
| modifiers = List(
| ArraySeq(
|...
|""".stripMargin,
compat = Map(
Compat.Scala3 ->
"""|import $dep.`com.lihaoyi::scalatags:0.13.1`
|import scalatags.Text.all._
|<val htmlFile = html(
| body(
| p("This is a big paragraph of text")
| )
|)> // : TypedTag[String] = <html><body><p>This is a big paragraph of text</p></body></html>
|htmlFile: TypedTag[String] = <html><body><p>This is a big paragraph of text</p></body></html>
|""".stripMargin,
Compat.Scala212 ->
"""|import $dep.`com.lihaoyi::scalatags:0.13.1`
|import scalatags.Text.all._
|<val htmlFile = html(
| body(
| p("This is a big paragraph of text")
| )
|)> // : scalatags.Text.TypedTag[String] = TypedTag( "html", List( WrappedArray( TypedTag( "body", List( W...
|htmlFile: scalatags.Text.TypedTag[String] = TypedTag(
| "html",
| List(
| WrappedArray(
|...
|""".stripMargin
),
width = 100
)

checkDecorations(
"multi-mods",
"""|object Foo {
Expand Down Expand Up @@ -290,21 +341,45 @@ class WorksheetSuite extends BaseSuite {
|val n = User("Susan")
|""".stripMargin,
"""|case class User(name: String)
|<val n = User("Susan")> // : User = User("Susan...
|n: User = User("Susan")
|<val n = User("Susan")> // : User = User(name =...
|n: User = User(name = "Susan")
|""".stripMargin,
compat = Map(
Compat.Scala3 -> """|case class User(name: String)
|<val n = User("Susan")> // : User = User(Susan)
|n: User = User(Susan)
|""".stripMargin,
Compat.Scala213 -> """|case class User(name: String)
|<val n = User("Susan")> // : User = User(name =...
|n: User = User(name = "Susan")
Compat.Scala212 -> """|case class User(name: String)
|<val n = User("Susan")> // : User = User("Susan...
|n: User = User("Susan")
|""".stripMargin
)
)

checkDecorations(
"whitespaces",
"""|case class User(name: String)
|val n = User("Susan Nasus")
|""".stripMargin,
"""|case class User(name: String)
|<val n = User("Susan Nasus")> // : User = User(name = "Susan Nasus")
|n: User = User(name = "Susan Nasus")
|""".stripMargin,
compat = Map(
Compat.Scala3 ->
"""|case class User(name: String)
|<val n = User("Susan Nasus")> // : User = User(Susan Nasus)
|n: User = User(Susan Nasus)
|""".stripMargin,
Compat.Scala212 ->
"""|case class User(name: String)
|<val n = User("Susan Nasus")> // : User = User("Susan Nasus")
|n: User = User("Susan Nasus")
|""".stripMargin
),
width = 500
)

checkDiagnostics(
"type-error",
"""
Expand Down Expand Up @@ -534,7 +609,7 @@ class WorksheetSuite extends BaseSuite {
)(implicit location: Location): Unit = {
test(options) {
val filename = options.name + ".scala"
val worksheet = evaluateWorksheet(filename, original, modifier)
val worksheet = evaluateWorksheet(filename, original, modifier, width = 30)
val input = Input.VirtualFile(options.name, original)
val out = new StringBuilder()
var i = 0
Expand Down Expand Up @@ -562,11 +637,12 @@ class WorksheetSuite extends BaseSuite {
original: String,
expected: String,
compat: Map[Compat.ScalaVersion, String] = Map.empty,
modifier: Option[String] = None
): Unit = {
modifier: Option[String] = None,
width: Int = 30
)(implicit loc: Location): Unit = {
test(options) {
val filename = options.name + ".scala"
val worksheet = evaluateWorksheet(filename, original, modifier)
val worksheet = evaluateWorksheet(filename, original, modifier, width)
val statements = worksheet.statements().asScala.sortBy(_.position().startLine())
val input = Input.VirtualFile(options.name, original)
val out = new StringBuilder()
Expand Down Expand Up @@ -596,12 +672,17 @@ class WorksheetSuite extends BaseSuite {
}
}

private def evaluateWorksheet(filename: String, original: String, modifier: Option[String]) = {
private def evaluateWorksheet(
filename: String,
original: String,
modifier: Option[String],
width: Int
) = {
modifier match {
case Some(mod) =>
mdoc.evaluateWorksheet(filename, original, mod)
mdoc.withScreenWidth(width).evaluateWorksheet(filename, original, mod)
case None =>
mdoc.evaluateWorksheet(filename, original)
mdoc.withScreenWidth(width).evaluateWorksheet(filename, original)
}
}
}

0 comments on commit 3253544

Please sign in to comment.