Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird multi-line string indentation #501

Open
pfitzseb opened this issue Nov 8, 2021 · 14 comments
Open

Weird multi-line string indentation #501

pfitzseb opened this issue Nov 8, 2021 · 14 comments

Comments

@pfitzseb
Copy link
Contributor

pfitzseb commented Nov 8, 2021

julia> print(JuliaFormatter.format_text("""
       write("", \"\"\"
           second
           third
           \"\"\")
       """))
write(
    "",
    """
second
third
""",
)

Not strictly an issue since it parses correctly, but does look weird.

Originally reported at julia-vscode/julia-vscode#2545.

@domluna
Copy link
Owner

domluna commented Nov 8, 2021

this looks correct to me, if you look at how the text is placed in relation to the opening """ in the original text it's the same in the formatted output. It can lead to weird text in some cases, but you can indent the words more as one 1 way to get around it. This is done intentionally so that strings are left intact.

This nests because the current rule is that if a string is multiline in a function call, the function call is automatically nested.

@pfitzseb
Copy link
Contributor Author

pfitzseb commented Nov 8, 2021

Well, that's not the invariant I expected :)

write(
    "",
    """
    second
    third
    """,
)

parses the same and looks nicer, imho.

@domluna
Copy link
Owner

domluna commented Nov 8, 2021

true, would this backfire in other cases though?

@pfitzseb
Copy link
Contributor Author

pfitzseb commented Nov 8, 2021

Well, Julia disregards

  • a leading newline
  • the whitespace all lines have in common

Sooo, sure, there are case where indentation isn't possible.

@domluna
Copy link
Owner

domluna commented Nov 9, 2021

would it be bad to align the lines to the first " if they currently appear prior to it? If it's a good idea it wouldn't be difficult to do (i think)

@mortenpi
Copy link

mortenpi commented Sep 17, 2023

Not 100% sure if it's related, but this

function myfunction_foo(io::IO)
    write(io, """
    """)
end

goes into

function myfunction_foo(io::IO)
    write(
        io,
        """
""",
    )
end

which does seem pretty weird to me.

@domluna
Copy link
Owner

domluna commented Oct 4, 2023

that's intentional. the distance between the start and end quotes stays consistent.

@mortenpi
Copy link

mortenpi commented Oct 4, 2023

That doesn't seem necessary? The string wouldn't change? So it could just go into

function myfunction_foo(io::IO)
    write(
        io,
        """
        """,
    )
end

Or if we do insist on keeping the distance, then it could go into

function myfunction_foo(io::IO)
    write(
        io,
            """
        """,
    )
end

which would still be a bit weird, but slightly better? Basically, it should be aligning the ending """, not the starting one. There may well be an edge case here that I'm not thinking of right now though.

@pfitzseb
Copy link
Contributor Author

pfitzseb commented Oct 4, 2023

Yeah, that's not required. Julia doesn't care about leading whitespace common to all lines inside of the string, cf

julia> x = """
       """
""

julia> x = """
           """
""

julia> x = """

           """
"\n"

julia> x = """

       """
"\n"

julia> x = """
           ab
           """
"ab\n"

julia> x = """
           ab
       """
"    ab\n"

julia> x = """
        ab
       """
" ab\n"

@domluna
Copy link
Owner

domluna commented Oct 4, 2023

julia> x = """
           ab
       """
"    ab\n"

julia> x = """
           ab
        """
"   ab\n"

these are different based on the positioning of the quotations though, which is why it's maintained.

@pfitzseb
Copy link
Contributor Author

pfitzseb commented Oct 4, 2023

They are only different based on the distance between ab and the closing quotes. The formatter needs to keep the string content the same without taking common leading whitespace into account.

@domluna
Copy link
Owner

domluna commented Oct 5, 2023

if the other way is more aesthetically pleasing I would be open to it. Not sure when i would get around to doing a PR atm

@pfitzseb
Copy link
Contributor Author

pfitzseb commented Oct 5, 2023

Imho we should never indent multi-line strings by less than the surrounding code. IIRC CSTParser handles multi-line strings with common leading whitespace fairly well, so maybe it'll be easy to implement this here.

@domluna
Copy link
Owner

domluna commented Oct 5, 2023

Imho we should never indent multi-line strings by less than the surrounding code. IIRC CSTParser handles multi-line strings with common leading whitespace fairly well, so maybe it'll be easy to implement this here.

makes sense. I'd be assist with a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants