dfinity / prettier-plugin-motoko

A code formatter for the Motoko smart contract language.
Apache License 2.0
28 stars 1 forks source link

Generics with comments are fun :) #69

Open ByronBecker opened 1 year ago

ByronBecker commented 1 year ago
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>;`
rvanasa commented 1 year ago

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 commented 1 year ago

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.

ByronBecker commented 1 year ago

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!

rvanasa commented 1 year ago

Sounds good. You as well!