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

complex indenting example with odd behaviour

Open borovan opened this issue 2 years ago • 4 comments

This was motoko code generated by Go. I try and make the generator as close to the prettified output as possible.

image

so then I put a newline after update(, and Ctrl-S

image

and then finally one after do { on line 81, and Ctrl-S

image

I've found myself just trying to enter random newlines to make it format correctly, and I wasn't sure this was the desired behaviour.

borovan avatar Oct 05 '22 21:10 borovan

This looks like a possible regression from #56. I'll see if there's a way to make this work in the way you're expecting.

If you get a chance, would you mind sending this code snippet (or anything with similar behavior) in plain text so I can play around with it?

rvanasa avatar Oct 05 '22 22:10 rvanasa

here we go

      // punch
      switch (update(
        M.content_Ability, "punch", [("iconId", #text(do { switch (TextMap.get(pks, itemKey("iconId", "xxx"))) { case (?pk) { pk }; case (null) { "ERR" } } }))])) {
        case (#ok res) { buf.add(res) };
        case (#err e) { return #err(e) };
      };

      // punch
      switch (
        update(
          M.content_Ability,
          "punch",
          [("iconId", #text(do { switch (TextMap.get(pks, itemKey("iconId", "xxx"))) { case (?pk) { pk }; case (null) { "ERR" } } }))],
        ),
      ) {
        case (#ok res) { buf.add(res) };
        case (#err e) { return #err(e) };
      };

      // punch
      switch (
        update(
          M.content_Ability,
          "punch",
          [(
            "iconId",
            #text(
              do {
                switch (TextMap.get(pks, itemKey("iconId", "xxx"))) {
                  case (?pk) { pk };
                  case (null) { "ERR" };
                };
              },
            ),
          )],
        ),
      ) {
        case (#ok res) { buf.add(res) };
        case (#err e) { return #err(e) };
      };

borovan avatar Oct 06 '22 06:10 borovan

At the moment, the formatter is very selective about adding line breaks within parentheses. You might be able to get around this by adding newlines in expression blocks. For example, would it be possible to output do {\n ... }, switch {\n ...}, etc. in your code generation? This should work if you're expecting to run the formatter directly afterwards. Otherwise, let me know and I'll increase the priority of fixing this issue.

rvanasa avatar Oct 07 '22 16:10 rvanasa

thats fine, no rush :) we've got less of a focus on this I just wondered if it was some low hanging thing to fix.

borovan avatar Oct 10 '22 10:10 borovan