StyLua icon indicating copy to clipboard operation
StyLua copied to clipboard

If an argument in a function call is multi-lined, break up all the arguments

Open archiif opened this issue 4 years ago • 2 comments

If an argument within a function call is going to be broken up into multiple lines, the entire series of arguments should also be broken up. So instead of this:

function_call(arg_1, more_args, function()
    more_function_call(arg)
end)

function_call({
        key = value,
        key = value,
        key = value,
        key = value,
        key = value,
}, arg_1, more_args)

I think it's more readable like this:

function_call(
    arg_1,
    more_args,
    function()
        more_function_call(arg)
    end
)

function_call(
    {
        key = value,
        key = value,
        key = value,
        key = value,
        key = value,
    },
    arg_1,
    more_args
)

Again, the styling is referenced from black. Sorry for the combo of issues, just got a bit excited knowing that there's at least one good Lua formatter out there :)

archiif avatar Oct 07 '21 02:10 archiif

This is something that was present in the initial implementation of StyLua. I agree, in some cases, that this is quite nice: it's also what's used in prettier and black, as you mention.

The main reason we don't have this anymore is because a specific case that cropped up often in a lot of projects that use StyLua:

Roact.createElement(Name, {
    prop = true
}, {
    child = ...
})

StyLua is originally catered for the Roblox community, which uses this pattern often, and this is why this was enforced. Another, more general example, was something like

table.sort(items, function(a,b)
    return a > b
end)

which (arguably) looks nicer collapsed.

Indeed though, if we just expanded in all case, the code implementation would also be much nicer (currently, the code for formatting function arguments is pretty horrendous 😅)

I'm happy to look into more cases where we should expand though, I know @mattharget had a case too which I've forgotten to look into...

Again, the styling is referenced from black. Sorry for the combo of issues, just got a bit excited knowing that there's at least one good Lua formatter out there :)

Don't worry about it, I'm always happy to hear about other formatting changes and style ideas :)

JohnnyMorganz avatar Oct 07 '21 16:10 JohnnyMorganz

An example from #606 where its probably better to keep expanded:

vim.keymap.set(
  "n",
  "<Leader>e",
  function()
    require("trouble").toggle({ "document_diagnostics", group = false })
  end,
  { desc = "Show LSP errors/diagnostics" }
)

... gets formatted as ...

vim.keymap.set("n", "<Leader>e", function()
  require("trouble").toggle({ "document_diagnostics", group = false })
end, { desc = "Show LSP errors/diagnostics" })

JohnnyMorganz avatar Jun 14 '23 14:06 JohnnyMorganz