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

[WIP] [Feedback] Inlining function call arguments #21

Closed
wants to merge 5 commits into from
Closed

[WIP] [Feedback] Inlining function call arguments #21

wants to merge 5 commits into from

Conversation

ThaisRobba
Copy link

@ThaisRobba ThaisRobba commented Mar 14, 2018

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 :)

@trixnz
Copy link
Owner

trixnz commented Mar 23, 2018

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.

@qaisjp
Copy link

qaisjp commented Jul 29, 2018

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.

@ThaisRobba
Copy link
Author

@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 :-)

@trixnz
Copy link
Owner

trixnz commented Jul 31, 2018

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.

@ThaisRobba ThaisRobba closed this Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants