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

Conversation

kpbochenek
Copy link
Contributor

@kpbochenek kpbochenek commented May 19, 2020

One test shows how currently worksheet summary is displayed and second one shows how it is displayed with types enabled.

Made it as an option because I was unsure of how it would look like, but I am more and more convinced it could be just default behaviour.

Also if you have any tricky statements that could be added to test to check how their summary would be rendered - shoot :)

Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

Thank you for this contribution! Just a few comments. I am in general concerned that we will start to introduce many configuration options which will make it difficult to maintain and evolve mdoc's worksheet functionality. A lot of features are easy to implement behind a configuration flag, but it gets really difficult to reason about the behavior when you support multiple configuration options

mdoc/src/main/scala/mdoc/internal/cli/Settings.scala Outdated Show resolved Hide resolved
|
|<px()> // "OK": String
|
|<{ println("P1"); List(1,2,3) }> // List(1, 2, 3): List[Int]
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the value is too large so that it gets truncated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is truncated to exactly match marginWidth (added test to show how it looks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I played with BlackWhite.tokenize as it is used for value but it only splits single chunk with full type so not helpful, we could manually cut type earlier if we encounter other char than [a-zA-Z0-9] but I am unsure it would be worth it. Especially for long type name it might make sense to show as much as possible.

Copy link
Contributor Author

@kpbochenek kpbochenek May 28, 2020

Choose a reason for hiding this comment

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

@olafurpg Let me know if you are okay with keeping it as it is for now and improve later or do you have other ideas how to improve it?

@olafurpg
Copy link
Member

I would be fine with removing the configuration option and changing the default behavior instead. Unless we have evidence that there is demand for not showing types in the summary, then I think we should be safe to always show types in the summary without an option to configure it.

|val b = a
""".stripMargin,
"""|
|<val a = Map(List(3) -> Map("v" -> Set(true, false)))> // Map(List(3) -> Map("v...
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 try and display the type at the beginning so that it doesn't get truncated for large values?

Copy link
Member

Choose a reason for hiding this comment

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

We could try the following formatting

<val a = Map(List(3) -> Map("v" -> Set(true, false)))> //: Map[List[Int], Set[Boolean]] = Map...

I don't have very strong opinions on what is the best solution here. cc/ @tpolecat any suggestions on how to format types in worksheets when the value is too large and gets truncated to fit the print width?

@tgodzik tgodzik force-pushed the types-in-worksheet branch from 4ea6a34 to e954158 Compare August 12, 2020 17:20
@tgodzik
Copy link
Contributor

tgodzik commented Aug 12, 2020

@olafurpg I rebased the changes on top of master and fixed the type to appear before the value, which was also suggested by @odersky and would be nice to have for teaching Scala 3

What do you think about how it currently looks?

@tgodzik tgodzik requested a review from olafurpg August 12, 2020 17:22
@tgodzik tgodzik force-pushed the types-in-worksheet branch from e954158 to 1772c9c Compare August 12, 2020 17:22
@tgodzik tgodzik force-pushed the types-in-worksheet branch from 1772c9c to f740846 Compare August 12, 2020 17:23
Copy link
Member

@olafurpg olafurpg left a comment

Choose a reason for hiding this comment

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

LGTM 👍 looking forward to use this!

@@ -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.

@tgodzik tgodzik merged commit 234ccc3 into scalameta:master Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants