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

Format function types. #1288

Merged
merged 3 commits into from
Oct 20, 2023
Merged

Format function types. #1288

merged 3 commits into from
Oct 20, 2023

Conversation

munificent
Copy link
Member

This involves formatting parameter lists which is a big piece of work. They work similar to argument lists and collection literals... except for the special syntax around [ ... ] and { ... } for optional parameters.

Getting that working right involved adding some more functionality to DelimitedListBuilder and ListPiece.

Getting comments working with that syntax was particularly difficult. I ended up deciding that the formatter can move comments before or after the optional parameter delimiters if it needs to. Doing so produces better looking output, as in:

// Before:
f(// weird comment location
[param] // another
);

// After:
f([
  // weird comment location
  param, // another
]);

It also, I think is cleaner to implement than trying to laboriously keep the comment where the user authored it.

In the process of reworking how comments are handled in lists, I also tweaked how inline block comments are handled before commas. Previously, they would get moved after the comma, like:

// Before:
[element /* comment */, another];

// After:
[element, /* comment */ another];

Now, they stay where they were, which is I think what users want. A common use case for block comments in argument/collection lists is describing the meaning of a preceding null argument and this keeps that.

Line after an element but before the comma continue to be moved after the comma:

// Before:
f(param // comment
,);

// After:
f(
  param, // comment
);

Also:

  • Added tests for the loosened behavior of equalIgnoringWhitespace().
  • Added a test for a corner case of variable splitting that I ran into with parameters, which use the same VariablePiece.

This involves formatting parameter lists which is a big piece of work.
They work similar to argument lists and collection literals... except
for the special syntax around `[ ... ]` and `{ ... }` for optional
parameters.

Getting that working right involved adding some more functionality to
DelimitedListBuilder and ListPiece.

Getting comments working with that syntax was particularly difficult.
I ended up deciding that the formatter can move comments before or after
the optional parameter delimiters if it needs to. Doing so produces
better looking output, as in:

```
// Before:
f(// weird comment location
[param] // another
);

// After:
f([
  // weird comment location
  param, // another
]);
```

It also, I think is cleaner to implement than trying to laboriously
keep the comment where the user authored it.

In the process of reworking how comments are handled in lists, I also
tweaked how inline block comments are handled before commas. Previously,
they would get moved after the comma, like:

```
// Before:
[element /* comment */, another];

// After:
[element, /* comment */ another];
```

Now, they stay where they were, which is I think what users want. A
common use case for block comments in argument/collection lists is
describing the meaning of a preceding null argument and this keeps that.

Line after an element but before the comma continue to be moved after
the comma:

```
// Before:
f(param // comment
,);

// After:
f(
  param, // comment
);
```

Also:

- Added tests for the loosened behavior of equalIgnoringWhitespace().
- Added a test for a corner case of variable splitting that I ran into
  with parameters, which use the same VariablePiece.
lib/src/front_end/piece_factory.dart Outdated Show resolved Hide resolved
lib/src/front_end/comment_writer.dart Show resolved Hide resolved
lib/src/front_end/delimited_list_builder.dart Show resolved Hide resolved
@@ -0,0 +1,202 @@
40 columns |
>>> Block comment before first parameter.
Copy link
Member

Choose a reason for hiding this comment

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

Do we have tests for inline block comments?
(And less exciting, but doc comments also)

Copy link
Member Author

@munificent munificent Oct 20, 2023

Choose a reason for hiding this comment

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

These ones are all inline block comments. Updated the descriptions to match and added tests of non-inline block and doc comments.

Deciding how comprehensive to be in tests is always a challenge. In this case, you were exactly right. These tests turned up some bugs. Thanks!

lib/src/piece/list.dart Show resolved Hide resolved
@kallentu
Copy link
Member

Forgot to say :D
Thank you for the comments and documentation everywhere. It's the only way I can understand all of this right now lol 🚀

lib/src/back_end/code_writer.dart Show resolved Hide resolved
lib/src/front_end/ast_node_visitor.dart Outdated Show resolved Hide resolved
lib/src/front_end/delimited_list_builder.dart Outdated Show resolved Hide resolved
@munificent munificent merged commit ee3dd8a into main Oct 20, 2023
7 checks passed
@munificent munificent deleted the format-function-types branch October 20, 2023 18:40
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.

4 participants