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

Add option to display types in worksheets #335

Merged
merged 1 commit into from
Aug 13, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,15 @@ class WorksheetProvider(settings: Settings) {
out
.append(if (out.isEmpty) "" else ", ")
.append(binder.name)
.append("=")
.append(": ")
.append(binder.tpeString)
.append(" = ")
}
if (isSingle)
out
.append(": ")
.append(binder.tpeString)
.append(" = ")
val chunk = BlackWhite
.tokenize(binder.value, width = margin - out.length)
.map(_.getChars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WorksheetSuite extends BaseSuite {
|val x = 1.to(4).toVector
|""".stripMargin,
"""|
|<val x = 1.to(4).toVector> // Vector(1, 2, 3, 4)
|<val x = 1.to(4).toVector> // : Vector[Int] = Vect...
|x: Vector[Int] = Vector(1, 2, 3, 4)
|""".stripMargin
)
Expand All @@ -54,7 +54,7 @@ class WorksheetSuite extends BaseSuite {
|val y = 1.0
|""".stripMargin,
"""|lazy val x = 0.0
|<val y = 1.0> // 1.0
|<val y = 1.0> // : Double = 1.0
|y: Double = 1.0
|""".stripMargin
)
Expand All @@ -65,7 +65,7 @@ class WorksheetSuite extends BaseSuite {
|val List(x, y) = List(1, 2)
|""".stripMargin,
"""|
|<val List(x, y) = List(1, 2)> // x=1, y=2
|<val List(x, y) = List(1, 2)> // x: Int = 1, y: Int =...
|x: Int = 1
|y: Int = 2
|""".stripMargin
Expand All @@ -77,7 +77,7 @@ class WorksheetSuite extends BaseSuite {
|Stream.from(10)
|""".stripMargin,
"""|
|<Stream.from(10)> // Stream(10,11,12,13,1...
|<Stream.from(10)> // : Stream[Int] = Stre...
|res0: Stream[Int] = Stream(
| 10,
| 11,
Expand Down Expand Up @@ -111,7 +111,7 @@ class WorksheetSuite extends BaseSuite {
|<val x = {
| println("hello")
| 42
|}> // 42
|}> // : Int = 42
|x: Int = 42
|// hello
|""".stripMargin
Expand All @@ -125,11 +125,11 @@ class WorksheetSuite extends BaseSuite {
|val m = n * 10
|""".stripMargin,
"""|
|<val n = 10> // 10
|<val n = 10> // : Int = 10
|n: Int = 10
|<println(n)> // 10
|// 10
|<val m = n * 10> // 100
|<val m = n * 10> // : Int = 100
|m: Int = 100
|""".stripMargin
)
Expand All @@ -140,15 +140,15 @@ class WorksheetSuite extends BaseSuite {
|val n = Future.successful(10)
|""".stripMargin,
"""|import scala.concurrent.Future
|<val n = Future.successful(10)> // Future(Success(10))
|<val n = Future.successful(10)> // : Future[Int] = Futu...
|n: Future[Int] = Future(Success(10))
|""".stripMargin
)

// From 2.13 we get `name =` part
val definitionCompat =
"""|case class User(name: String)
|<val n = User("Susan")> // User(name = "Susan")
|<val n = User("Susan")> // : User = User(name =...
|n: User = User(name = "Susan")
|""".stripMargin

Expand All @@ -158,7 +158,7 @@ class WorksheetSuite extends BaseSuite {
|val n = User("Susan")
|""".stripMargin,
"""|case class User(name: String)
|<val n = User("Susan")> // User("Susan")
|<val n = User("Susan")> // : User = User("Susan...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to special case this scenario and drop the type when the value starts with the same syntax as the type? Feel free to ignore, just an idea

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to be consistent, otherwise it might be surprising to users. And it might be quite fragile I think.

It would need to be something like:

            if (isSingle && !chunk.startsWith(binder.tpeString + "("))
              out
                .append(": ")
                .append(binder.tpeString)
                .append(" = ")
            out.append(chunk)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get back to it if current behavior proves problematic.

|n: User = User("Susan")
|""".stripMargin,
compat = Map(
Expand Down Expand Up @@ -224,7 +224,7 @@ class WorksheetSuite extends BaseSuite {
|val y = "foobar".stripSuffix("bar")
|""".stripMargin,
"""|
|<val x = "foobar".stripSuffix("bar")> // "foo"
|<val x = "foobar".stripSuffix("bar")> // : String = "foo"
|x: String = "foo"
|""".stripMargin
)
Expand Down Expand Up @@ -260,17 +260,17 @@ class WorksheetSuite extends BaseSuite {
"""|case class Circle(x: Double, y: Double, radius: Double)
|extension (c: Circle)
| def circumference: Double = c.radius * math.Pi * 2
|<val circle = Circle(0.0, 0.0, 2.0)> // Circle(x = 0.0,y = 0...
|<val circle = Circle(0.0, 0.0, 2.0)> // : Circle = Circle(x ...
|circle: Circle = Circle(
| x = 0.0,
| y = 0.0,
| radius = 2.0
|)
|<circle.circumference> // 12.566370614359172
|<circle.circumference> // : Double = 12.566370...
|res0: Double = 12.566370614359172
|extension [T](xs: List[T])
| def second = xs.tail.head
|<List(1,2,3).second> // 2
|<List(1,2,3).second> // : Int = 2
|res1: Int = 2
|""".stripMargin
)
Expand All @@ -285,7 +285,7 @@ class WorksheetSuite extends BaseSuite {
"""|import $dep.`org.json4s:json4s-native_2.13:3.6.9`
|import org.json4s._
|import org.json4s.native.JsonMethods._
|<parse("{ \"numbers\" : [1, 2, 3, 4] }")> // JObject(obj = List((...
|<parse("{ \"numbers\" : [1, 2, 3, 4] }")> // : JValue = JObject(o...
|res0: JValue = JObject(
| obj = List(
| (
Expand Down