[WIP] [Feedback] Inlining function call arguments
Hi there!
I wanted to be able to have inline anonymous function expressions, like so:
local ok, err = pcall(function()
-- logic
end)
I've added a new configuration param inlineFunctionCalls, defaulting to true due to Lua's oficial documentation and common patterns (but can be persuaded to change it to default to false!) :-)
Examples:
-- input
local result = filter({1, 2, 3}, function(value) return 1 % 2 == 0 end)
-- output with inlineFunctionCalls: true
local result = filter({1, 2, 3}, function(value)
return 1 % 2 == 0
end)
-- output with inlineFunctionCalls: false
local result = filter(
{1, 2, 3},
function(value)
return 1 % 2 == 0
end
)
-- desired (but not working yet) output with inlineFunctionCalls: true
local result = filter(
{
some_really_long_variable_name,
some_even_larger_variable_name_I_mean_really_really_really_larger,
this_feels_normal_in_comparison,
tiny_1
},
my_func
)
-- desired (but not working yet) output with inlineFunctionCalls: true
local result = filter(
{
some_really_long_variable_name,
some_even_larger_variable_name_I_mean_really_really_really_larger,
this_feels_normal_in_comparison,
tiny_1
},
function(value)
return 1 % 2 == 0
end
)
-- desired (but not working yet) output with inlineFunctionCalls: true
local result = test(
{1, 2, 3},
"helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
"hi",
true,
nil,
function(value)
return 1 % 2 == 0
end
)
-- Cannot be formatted yet
local result = test(
{1, 2, 3}
-- a comment
)
-- results in
local result = test({1, 2, 3}-- a comment)
-- which breaks tests and is clearly not correct
I'm marking this PR as [WIP] because it is not ready to be merged yet. I'd love to have @trixnz feedback on it :)
Apologies for the radio silence @OttoRobba.
I like it! I'd be in favour of keeping it true by default.
With regards to the last example with the comment, I think the only option would be to keep the call broken onto multiple lines if there is a comment attached to any of the parameter nodes, unless there is a more idiomatic way of moving the comment.
I think something like this:
-- Cannot be formatted yet
local result = test(
{1, 2, 3},
-- true because true
true
)
should be reformatted as:
-- Cannot be formatted yet
local result = test(
{1, 2, 3},
+
-- true because true
true
)
I am not sure if there should be whitespace or not, but there should be a (configurable) newline IMO.
Thanks for this pull request @ThaisRobba, it's exactly what we're looking for here. And thank you @trixnz for this fantastic project.
I'll try and see if I can source some effort on this (either by myself or other developers), if you're not working on it @ThaisRobba.
@qaisjp I'm currently not working on this (no longer with the company I was working with), so feel free to tackle it and reuse any of my suggested changes :-)
I like the linebreak before the comment, good idea :-)
I'd love to see an option for newlines, I think forcing it on users by default could be a little intrusive. We could begin with a command line option, although once #3 lands, I'd like to open a lot more of the format to customizations. Feel free to make issues for specific modifications you would like to see.
Comment support is in a really bad place right now, and I'm working (when time allows) on writing my own Lua parser to better expose the AST to allow the formatter to make better decisions.