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

Replace Term::deep_repr by the pretty printer #1262

Merged
merged 2 commits into from
Jun 9, 2023
Merged

Conversation

vkleen
Copy link
Contributor

@vkleen vkleen commented Apr 18, 2023

This PR gets rid of Term::deep_repr and replaces all calls to it by invocations of the pretty printer. Incidentally, it ensures that quotes and newlines in strings are correctly escaped when printing evaluation results.

As a bonus, the pretty printer formats records with many fields on multiple lines in the REPL if they don't fit into 80 columns.

Closes #1257

@github-actions github-actions bot temporarily deployed to pull request April 18, 2023 13:31 Inactive
@github-actions github-actions bot temporarily deployed to pull request April 18, 2023 14:02 Inactive
@github-actions github-actions bot temporarily deployed to pull request June 2, 2023 14:41 Inactive
@vkleen vkleen marked this pull request as ready for review June 6, 2023 11:12
@vkleen vkleen requested a review from yannham June 6, 2023 11:12
Copy link
Member

@yannham yannham left a comment

Choose a reason for hiding this comment

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

Sounds better, but I think we had a discussion back then about what to do about e.g. functions. Where you're defining a function in the REPL, it's customary (as it, in other languages) to not re-print again the whole code of the function. Might be the same for documentation and metadata in general (I guess in the REPL you really want to print values, not the fuss around).

IIRC, one proposal was to implement Pretty on a more elaborated type, basically isomoprhic to (Term, PrettyConfig), with PrettyConfig controls the options of the pretty printer. That being said, we can consider that in a second step.

@@ -1734,7 +1701,6 @@ impl From<Term> for RichTerm {
impl std::fmt::Display for RichTerm {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use crate::pretty::*;
use pretty::BoxAllocator;
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to move this specific use from the local function to the top-level? We don't really have consistent rules right now about where to place use statements, but I vaguely intuit that it makes more sense to keep it local if it's very specific to the current function, and doesn't have much to do with the rest of the module, which seems to be the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a very good question. It might have happened during a previous iteration, but right now there's no good reason. I think I'll revert this part.

@github-actions github-actions bot temporarily deployed to pull request June 9, 2023 07:21 Inactive
@vkleen vkleen added this pull request to the merge queue Jun 9, 2023
Merged via the queue into master with commit ce9501d Jun 9, 2023
@vkleen vkleen deleted the pretty_deep_repr branch June 9, 2023 08:10
@vkleen
Copy link
Contributor Author

vkleen commented Jun 9, 2023

I've made a tracking issue for making the pretty printer configurable at #1349.

vkleen added a commit that referenced this pull request Aug 9, 2023
We previously used a bespoke formatting algorithm for `Type`. I replaced
the analogous code for `Term`s by the pretty printer in #1262 but we
were worried about some questionable code for contract error reporting
before doing the same for types. Namely, at some point it relied on hard
coded string offsets for pointing at parts of types that were inferred
by Nickel and consequently had no `TermPos`. In #1229 we ripped out
that code and replaced it by reparsing the pretty printer output when
necessary.

Incidentally, this change also fixes some terms being truncated when
formatted. For example, previously

```
SomeUserDefinedContract "that" "takes" "many" "arguments"
```

would be printed as `SomeUserDefinedContract "that" …`. This is somewhat
useful to prevent huge screenfuls of error messages sometimes, but it
makes the `Display` implementation useless for other natural purposes.
github-merge-queue bot pushed a commit that referenced this pull request Aug 10, 2023
)

* Use the pretty printer in the  `Display` implementation for `Type`

We previously used a bespoke formatting algorithm for `Type`. I replaced
the analogous code for `Term`s by the pretty printer in #1262 but we
were worried about some questionable code for contract error reporting
before doing the same for types. Namely, at some point it relied on hard
coded string offsets for pointing at parts of types that were inferred
by Nickel and consequently had no `TermPos`. In #1229 we ripped out
that code and replaced it by reparsing the pretty printer output when
necessary.

Incidentally, this change also fixes some terms being truncated when
formatted. For example, previously

```
SomeUserDefinedContract "that" "takes" "many" "arguments"
```

would be printed as `SomeUserDefinedContract "that" …`. This is somewhat
useful to prevent huge screenfuls of error messages sometimes, but it
makes the `Display` implementation useless for other natural purposes.

* Revert a useless change to snapshot test inputs

* Factor out calling the pretty printer for `Display`
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.

Control characters and quotes in strings are not escaped when printed
2 participants