julia-vscode / DocumentFormat.jl

Auto-formatter for Julia
Other
62 stars 18 forks source link

Indent function-call arguments #103

Closed non-Jedi closed 2 years ago

non-Jedi commented 4 years ago

Prompted by https://github.com/julia-vscode/LanguageServer.jl/pull/739.

EDIT: See comment below for actual problem description. This is probably technically an "enhancement" at this point rather than a "bug", but I'm going to leave it classified as "bug" since the behavior without the enhancement is so broken.

function (x)
for i in x
println(i)
end
end

should be indented as:

function (x)
    for i in x
        println(x)
    end
end

not

function (x)
for i in x
    println(x)
end
end
non-Jedi commented 4 years ago

Indentation works fine actually for these anonymous functions at the top-level. The problem in julia-vscode/LanguageServer.jl#739 must be something to do with the nesting.

non-Jedi commented 4 years ago

This looks to be because DocumentFormat currently doesn't attempt to correctly indent function-call arguments and keeps track of an absolute level of indentation for everything. So since the function call SymbolServer.getstore doesn't add level of indentation, technically the calculated indentation for the function (i) line is 1 less than it's actual indentation. So when you add a level of indentation to the function-block's contents their actual indentation becomes even with the actual indentation of function (i).

The best way of fixing this is to teach the formatter to indent function-call arguments correctly. This will require a bit of rearchitecting if we want to have indents that look like below since the second line is indented according to the length of the function name rather than at a multiple of FormatOptions.indent.

functioncall(firstarg,
             secondarg)