prettier-plugin-motoko icon indicating copy to clipboard operation
prettier-plugin-motoko copied to clipboard

Generics with comments are fun :)

Open ByronBecker opened this issue 3 years ago • 4 comments

canisters : HashMap.HashMap<
  // The principal of the canister.
  Principal, 
  // The configuration of the canister.
  CanisterConfig
>;

becomes

canisters : HashMap.HashMap</* The principal of the canister. */ Principal, /* The configuration of the canister. */ CanisterConfig>;`

ByronBecker avatar Dec 09 '22 23:12 ByronBecker

This was originally the intended behavior, but I'll see if there's a way to support opt-in wrapping for generics (since this seems like a reasonable use case for it).

rvanasa avatar Dec 10 '22 19:12 rvanasa

Since it might be a few weeks until I get around to this, just wanted to mention that you can deactivate the formatter for any declaration, expression, field, etc. using a // prettier-ignore comment.

Motoko's lexer has some finicky rules for whitespace around type bindings. For example, a slight variation of the above code snippet results in a syntax error. My understanding is that this has something to do with disambiguating the "greater than" binary operator and the "close angle bracket" for type bindings, so it's rather deeply rooted in the language's syntax. I'll eventually do another pass on this (since it would be nice to have a way opt into the multi-line formatting), but the current approach seems to be the most consistent and reliable behavior given this parser limitation.

rvanasa avatar Dec 15 '22 20:12 rvanasa

No problem Ryan - we actually just moved the comment to directly above the generics. You can label this as a super low priority issue.

Enjoy the holidays!

ByronBecker avatar Dec 15 '22 21:12 ByronBecker

Sounds good. You as well!

rvanasa avatar Dec 15 '22 22:12 rvanasa