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
576 stars 69 forks source link

Consistently use indent instead of aligning things for VS Code #592

Open davidanthoff opened 2 years ago

davidanthoff commented 2 years ago

Here is an example where the formatter seems to fall back on aligning things. It turns

        @map({
            name = _[2]["name"],
            uuid = UUID(_[1]),
            path = _[2]["path"]
        }) |>
        @mutate(
            versions = (Pkg.TOML.parsefile(joinpath(registry_folder_path, _.path, "Versions.toml")) |>
                @map(i->{version=VersionNumber(i[1]), treehash=i[2]["git-tree-sha1"]}) |> 
                @orderby_descending(i->i.version) |>
                @take(max_versions) |>
                collect)
        ) |>
        collect

into

               @map({
                   name = _[2]["name"],
                   uuid = UUID(_[1]),
                   path = _[2]["path"]
               }) |>
               @mutate(
                   versions = (Pkg.TOML.parsefile(joinpath(registry_folder_path, _.path, "Versions.toml")) |>
                               @map(i -> {version = VersionNumber(i[1]), treehash = i[2]["git-tree-sha1"]}) |>
                               @orderby_descending(i -> i.version) |>
                               @take(max_versions) |>
                               collect)
               ) |>
               collect

which it shouldn't do.

domluna commented 2 years ago

btw with an indent of 4 rule then

        @map({
            name = _[2]["name"],
            uuid = UUID(_[1]),
            path = _[2]["path"]
        }) |>
        @mutate(
            versions = (Pkg.TOML.parsefile(joinpath(registry_folder_path, _.path, "Versions.toml")) |>
                @map(i->{version=VersionNumber(i[1]), treehash=i[2]["git-tree-sha1"]}) |> 
                @orderby_descending(i->i.version) |>
                @take(max_versions) |>
                collect)
        ) |>
        collect

would be

        @map({
            name = _[2]["name"],
            uuid = UUID(_[1]),
            path = _[2]["path"]
        }) |>
            @mutate(
                versions = (Pkg.TOML.parsefile(joinpath(registry_folder_path, _.path, "Versions.toml")) |>
                    @map(i->{version=VersionNumber(i[1]), treehash=i[2]["git-tree-sha1"]}) |> 
                    @orderby_descending(i->i.version) |>
                    @take(max_versions) |>
                    collect)
            ) |>
            collect

is that really what we want? IMO when it lines up it's a bit more pleasant.

domluna commented 2 years ago

it would also apply generally to all binary ops

a +
b

would be

a +
    b

which is why for these operations in the other styles we don't abide strictly to the "indent of x" rule.

davidanthoff commented 2 years ago

Yeah, I think that would still make sense in a strictly indent based story, right? CC @pfitzseb

I think it should in the end look like this:

@map({
    name = _[2]["name"],
    uuid = UUID(_[1]),
    path = _[2]["path"]
}) |>
    @mutate(
        versions = (Pkg.TOML.parsefile(joinpath(registry_folder_path, _.path, "Versions.toml")) |>
            @map(i->{version=VersionNumber(i[1]), treehash=i[2]["git-tree-sha1"]}) |> 
            @orderby_descending(i->i.version) |>
            @take(max_versions) |>
            collect)
    ) |>
    collect