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

bugfix: Calculate string value eagerly #870

Merged
merged 1 commit into from
Jun 17, 2024
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
13 changes: 7 additions & 6 deletions mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package mdoc.internal.markdown

import java.io.ByteArrayOutputStream
import java.io.PrintStream
import mdoc.Reporter
import mdoc.Variable
import mdoc.document.CompileResult
import mdoc.document.CrashResult
import mdoc.document.CrashResult.Crashed
import mdoc.document.RangePosition
import mdoc.internal.cli.Context
import mdoc.internal.cli.InputFile
import mdoc.internal.cli.Settings
import mdoc.internal.document.FailSection
import mdoc.internal.document.MdocExceptions
import mdoc.internal.document.Printing
import mdoc.internal.pos.PositionSyntax._
import mdoc.internal.pos.TokenEditDistance

import java.io.ByteArrayOutputStream
import java.io.PrintStream
import scala.meta._
import scala.meta.inputs.Position
import mdoc.internal.cli.InputFile
import mdoc.internal.cli.Settings
import mdoc.internal.cli.Context

object Renderer {

Expand Down Expand Up @@ -172,7 +173,7 @@ object Renderer {
}
appendFreshMultiline(sb, compiled)
case _ =>
val obtained = Printing.stringValue(binder.value)
val obtained = binder.stringValue
throw new IllegalArgumentException(
s"Expected FailSection. Obtained $obtained"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package mdoc.internal.worksheets

import scala.meta._
import mdoc.internal.pos.PositionSyntax._
import mdoc.internal.cli.Context
import mdoc.internal.markdown.SectionInput
import mdoc.internal.markdown.Modifier
import mdoc.internal.markdown.Instrumenter
import mdoc.internal.markdown.MarkdownBuilder
import mdoc.internal.document.Printing
import mdoc.document.Statement
import mdoc.document.RangePosition
import mdoc.document.Statement
import mdoc.internal.cli.Context
import mdoc.internal.cli.InputFile
import mdoc.internal.cli.Settings
import mdoc.internal.document.Printing
import mdoc.internal.io.StoreReporter
import mdoc.internal.markdown.Instrumenter
import mdoc.internal.markdown.MarkdownBuilder
import mdoc.internal.markdown.Modifier
import mdoc.internal.markdown.SectionInput
import mdoc.internal.pos.PositionSyntax._
import mdoc.{interfaces => i}

import java.{util => ju}
import mdoc.internal.cli.InputFile
import scala.meta._

class WorksheetProvider(settings: Settings) {

Expand Down Expand Up @@ -93,7 +94,7 @@ class WorksheetProvider(settings: Settings) {
.append(": ")
.append(binder.tpeString)
.append(" = ")
Printing.print(binder.value, out, settings.screenWidth, settings.screenHeight)
Printing.print(binder.stringValue, out, settings.screenWidth, settings.screenHeight)
}
}
statement.out.linesIterator.foreach { line =>
Expand Down Expand Up @@ -138,7 +139,7 @@ class WorksheetProvider(settings: Settings) {
.append(binder.tpeString)
.append(" = ")

Printing.printOneLine(binder.value, out, width = margin - out.length)
Printing.printOneLine(binder.stringValue, out, width = margin - out.length)
out.length > margin
}
}
Expand Down
7 changes: 5 additions & 2 deletions runtime/src/main/scala/mdoc/document/Binder.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package mdoc.document

import mdoc.internal.sourcecode.SourceStatement
import mdoc.internal.document.Printing
import mdoc.internal.document.Compat.TPrint
import mdoc.internal.document.Printing
import mdoc.internal.sourcecode.SourceStatement

final class Binder[T](val value: T, val name: String, val tpe: TPrint[T], val pos: RangePosition) {

val stringValue = Printing.stringValue(value)

override def toString: String = {
val valueString = Printing.stringValue(value)
s"""Binder($valueString, "$name", "$tpeString")"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ class WorksheetSuite extends BaseSuite {
|""".stripMargin
)

checkDecorations(
"updates",
"""
|import scala.collection.mutable.ArrayBuffer
|val b = ArrayBuffer[Int]()
|b += 1
|b ++= ArrayBuffer(5, 8, 12)
|b.dropRight(3)
|""".stripMargin,
"""|import scala.collection.mutable.ArrayBuffer
|<val b = ArrayBuffer[Int]()> // : ArrayBuffer[Int] =...
|b: ArrayBuffer[Int] = ArrayBuffer()
|<b += 1> // : ArrayBuffer[Int] = Arr...
|res0: ArrayBuffer[Int] = ArrayBuffer(1)
|<b ++= ArrayBuffer(5, 8, 12)> // : ArrayBuffer[Int] =...
|res1: ArrayBuffer[Int] = ArrayBuffer(1, 5, 8, 12)
|<b.dropRight(3)> // : ArrayBuffer[Int] =...
|res2: ArrayBuffer[Int] = ArrayBuffer(1)
|""".stripMargin
)

checkDecorations(
"infix",
"""|import scala.language.postfixOps
Expand Down