domluna / JuliaFormatter.jl

An opinionated code formatter for Julia. Plot twist - the opinion is your own.
https://domluna.github.io/JuliaFormatter.jl/dev/
MIT License
573 stars 67 forks source link

Multi-line strings with keyword and pair arguments #560

Open tkf opened 2 years ago

tkf commented 2 years ago

I couldn't find the issue (maybe #501 is the closest) but I find this behavior strange

julia> code = """
       f(
       a = \"\"\"
       string
       \"\"\",
       b = \"\"\"
       string
       \"\"\",
       )
       """;

julia> print(code)
f(
a = """
string
""",
b = """
string
""",
)

julia> print(format_text(code))
f(a = """
  string
  """, b = """
       string
       """)

Similar with =>:

julia> code = """
       f(
       "a" => \"\"\"
       string
       \"\"\",
       "b" => \"\"\"
       string
       \"\"\",
       )
       """;

julia> print(code)
f(
"a" => """
string
""",
"b" => """
string
""",
)

julia> print(format_text(code))
f("a" => """
  string
  """, "b" => """
       string
       """)

I checked this with

(jl_XRYQ8s) pkg> st -m
      Status `/tmp/jl_XRYQ8s/Manifest.toml`
  [00ebfdb7] CSTParser v3.3.3
  [a80b9123] CommonMark v0.8.6
  [34da2185] Compat v3.42.0
  [a8cc5b0e] Crayons v4.1.1
  [864edb3b] DataStructures v0.18.11
  [682c06a0] JSON v0.21.3
  [98e50ef6] JuliaFormatter v0.22.7
  [bac558e1] OrderedCollections v1.4.1
  [69de0a69] Parsers v2.2.3
  [0796e94c] Tokenize v0.5.21
  [5c2747f8] URIs v1.3.0
  ...
domluna commented 2 years ago

hmmm so what's going on is it thinks the width of the code < the maximum width so it joins the lines which in this case ends up with a strange format.

If you just some 2 multiline strings (no binary op) then it doesn't do this right?

tkf commented 2 years ago

Yeah, I think this is expected:

julia> code = """
       f(
       \"\"\"
       string
       \"\"\",
       \"\"\"
       string
       \"\"\",
       )
       """;

julia> print(code)
f(
"""
string
""",
"""
string
""",
)

julia> print(format_text(code))
f(
    """
    string
    """,
    """
    string
    """,
)
tkf commented 2 years ago

My workaround ATM is to put some comments inside:

julia> code = """
       f(
       # workaround
       "a" => \"\"\"
       string
       \"\"\",
       "b" => \"\"\"
       string
       \"\"\",
       )
       """;

julia> print(code)
f(
# workaround
"a" => """
string
""",
"b" => """
string
""",
)

julia> print(format_text(code))
f(
    # workaround
    "a" => """
    string
    """,
    "b" => """
    string
    """,
)