unison icon indicating copy to clipboard operation
unison copied to clipboard

pretty-print multi-line strings with """

Open stew opened this issue 1 year ago • 9 comments

when we are pretty printing a string, if it is a multi-line string we should pretty-print it with the """ syntax so that it easier to read

stew avatar Sep 05 '23 18:09 stew

💯 I would love this.

I just hit this last night with a watch expression like:

    1 | > advancedExample
          ⧩
          "[\n  {\n    \"\130RaKpO2N1C\472472N\445368\": 53446413594,\n    \"|t2\516629\&0BzI&\": 4035084937941982746,\n    \"H7uuo\\u00161ki2\\u001fg8\": 2887500339197854320,\n    \"5\\u001a43365xx>O\": -88058632730649935\n  },\n  {\n    \"3S)28UjA7g\": -6956822617877329675,\n    \"Fr4\\u0004`DTU5L3GN7\87366QXG\": -9e+36423,\n    \"26g9eBL5yln4l\": -3861239325764.17E0,\n    \"Wn4\": 8267257252295.5,\n    \"q\\u000b\\u001cjjN2\\n9a\": -77,\n    \"9I5b74\832016\\u0007N3kF4V?\\u00157S6\": 2,\n    \"cepO1HvI0YRKO\304556\&2S2\": 62928000808213,\n    \"p1eSh4d,6t\682133x6\": -16665803.02299552\n  },\n  {\n    \"3Tr7xsav8D04o\": -39618,\n    \"\558709\&0A\\u0016r6\\u00076tW7Zy9q7TX\": 9,\n    \"CO759Zu2Nu\": 1,\n    \"UOfMy6Ib\739203\585254.5A?5x1\": -687.83077452509,\n    \"\813469\835191XGhO\": 38147073372481\n  }\n]"

It was so unreadable that I ended up creating a main method that just did printLine advancedExample. If this feature were implemented I would instead see:

    1 | > advancedExample
          ⧩
          """
          [
            {
              "RaKpO2N1CN": 53446413594,
              "|t20BzI&": 4035084937941982746,
              "H7uuo\u00161ki2\u001fg8": 2887500339197854320,
              "5\u001a43365xx>O": -88058632730649935
            },
            {
              "3S)28UjA7g": -6956822617877329675,
              "Fr4\u0004`DTU5L3GN7QXG": -9e+36423,
              "26g9eBL5yln4l": -3861239325764.17E0,
              "Wn4": 8267257252295.5,
              "q\u000b\u001cjjN2\n9a": -77,
              "9I5b74\u0007N3kF4V?\u00157S6": 2,
              "cepO1HvI0YRKO2S2": 62928000808213,
              "p1eSh4d,6tx6": -16665803.02299552
            },
            {
              "3Tr7xsav8D04o": -39618,
              "0A\u0016r6\u00076tW7Zy9q7TX": 9,
              "CO759Zu2Nu": 1,
              "UOfMy6Ib.5A?5x1": -687.83077452509,
              "XGhO": 38147073372481
            }
          ]
          """

ceedubs avatar Sep 05 '23 19:09 ceedubs

@stew or @ceedubs could you provide a reproducible test case? :)

We do try to print multi-line strings as such, for instance, try:

x = 
  """
  hello
  my
  name
  is 
  paul
  """

> x
CleanShot 2023-11-04 at 09 41 30@2x

We don't use the syntax in places where the ambient ambient precedence > 0. Example is like if it's being printed as the argument to a function, like in foo x y """some multiline string""", it won't use multiline syntax. There is trickiness I think if we wanted to relax this, since the syntax relies on looking at starting and ending columns, so if the start of the string is a random column, it might goof things up and cause round trip issues.

As a special case of this, if the watch expression is returning like a Some "some \n\n text", it won't use the syntax.

Anyway, if you've hit a case where you're expecting the syntax and it isn't being used, can you provide an example?

pchiusano avatar Nov 04 '23 14:11 pchiusano

x : Text
x = """a
򴞃
"""
> x

6 | > x
      ⧩
      "a\n\739203"

stew avatar Nov 15 '23 21:11 stew

x : Text
x = """򴞃򎸦
"""

> x

    5 | > x
          ⧩
          "\739203\585254"

Did that one actually omit the newline?

stew avatar Nov 15 '23 21:11 stew

yes, it does seem to sometimes omit newlines:

x : Text
x = """xyzzy
"""

> x
    5 | > x
          ⧩
          "xyzzy"

stew avatar Nov 15 '23 21:11 stew

ok, I really didn't understand how our multi-line strings worked, I was VERY surprised that this is true:



> let
     a = """1
"""
     b = "1"
     c = """
1"""
     d = """
         1
         """
     (a === b) && (a === c) && (a === d)

I'm guessing that cody's issue I minimized is due to his random json generator generating right to left characters, but haven't verified

stew avatar Nov 15 '23 21:11 stew

no, its not as simple as just using left to right characters

stew avatar Nov 15 '23 22:11 stew

The old algorithm would strip the first line if it was all whitespace, so you could write things like:

x = 
  """
  hello
  """

without it starting with "\n". It would do the same for the last line, and you could just add a blankline at the start or end if you wanted to start or end the raw string with a "\n".

I think @ChrisPenner tweaked the algorithm so I'm not actually sure what the new algo does (did it just tweak the indentation detection to key off the closing """, or did it also do something else)?

pchiusano avatar Apr 01 '24 15:04 pchiusano

Here's the PR and description of the algorithm as of the last time I touched it: https://github.com/unisonweb/unison/pull/4655

ChrisPenner avatar Apr 01 '24 20:04 ChrisPenner