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: Remove additional whitespace from summary #872

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

tgodzik
Copy link
Contributor

@tgodzik tgodzik commented Jun 19, 2024

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 intermediate representation doesn't work as those contain Iterator and are one use only

The easiest approach was to just replace newline with space and then deduplicate whitespace to save space on the oneline summary. This also makes it consistent across Scala 2 and 3

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
@tgodzik tgodzik changed the title bugfix: Remove all whitespace from summary bugfix: Remove additional whitespace from summary Jun 20, 2024
@tgodzik tgodzik marked this pull request as ready for review June 20, 2024 10:58
@tgodzik tgodzik requested a review from keynmol June 20, 2024 11:00
@keynmol
Copy link
Collaborator

keynmol commented Jul 3, 2024

@tgodzik I'm struggling to figure out

  • What problem was the original code solving
  • What broke with recent release
  • What is the result you expect visually

Do you have a screenshot perhaps of something broken and/or working well?

@tgodzik
Copy link
Contributor Author

tgodzik commented Jul 3, 2024

What problem was the original code solving

When evaluating mutable collections we would always get the last result for everything, because values were evaluated only when printing. I switched to printing them right away to always get the current result.

What broke with recent release

Printing one line stopped removing additional spaces, so if the printed value had indentation it would all stay in the oneline summary and be quite unreadable.

What is the result you expect visually

One line summary should be as compact as possible without losing the visibility, so I decided to just squash any whitespace into a single space.

Do you have a screenshot perhaps of something broken and/or working well?

This broke in Metals scalameta/metals#6523 (and I think it's the only place the one line summary is being used)

image

@tgodzik
Copy link
Contributor Author

tgodzik commented Jul 8, 2024

@keynmol what do you think? Or do you see a possibility to improve this approach?

out.appendAll(chunk)

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

Choose a reason for hiding this comment

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

Hm, I feel like this should also collapse the possible spaces inside a value, e.g. as part of a string constant for example?

But I struggle to reproduce it in tests.

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 might, but we already collapse it even if it contains a newline, so we are already changing the string value. We can't show one line summary otherwise. Maybe we should instead always do ... on newline then and keep spaces in the values?

This is tricky to actually get right since every solution is losing information

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I don't see a good way of doing it without changing how binding works in general..
That said I'm not deeply familiar with worksheets, will take some time to read it eventually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We would need a nice intermediate representation of the values, which we can print later on. I almost got it to work with pprint trees, but then pprint uses iterators and I could write things only once. So in the end we would need to reuse pprint trees by either inlining and changing them or by doing a fix upstream. That said, for Scala 2 we don't use pprint, just toString and I am afraid to change it since Martin was mad at us for not doing toString.

All that makes me think that making it work proper currently is not really worth the effort and current approach is good enough 😅

@tgodzik tgodzik merged commit 3253544 into scalameta:main Jul 9, 2024
14 checks passed
@tgodzik tgodzik deleted the fix-whitespace branch July 9, 2024 14:58
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.

2 participants