trixnz / lua-fmt

lua-fmt is pretty-printer for Lua code
MIT License
189 stars 25 forks source link

Strange indentation when assigning a function with a table argument to a variable #35

Open AgentOttsel opened 6 years ago

AgentOttsel commented 6 years ago

If I have a variable and assign the result of a function that receives a table to it, like this:

instance = MyClass.new({
    stuff
})

...The formatted code looks like this. It's confusing to me how MyClass.new( has the same indentation level as the table that it's receiving:

instance =
    MyClass.new(
    {
        arguments
    }
)

I believe this would be the ideal result:

instance = MyClass.new(
    {
        arguments
    }
)
Arpple commented 6 years ago

I suspect this part and after adding more check to be like this

const canBreakLine = node.init.some(n =>
    n != null &&
    n.type !== 'TableConstructorExpression' &&
    n.type !== 'FunctionDeclaration' &&
    n.type !== 'CallExpression'
);

the result is as you expect

instance = MyClass.new(
    {
        arguments
    }
)

adding checkCallExpression is just come from hard debugging and I don't understand how it's actually works 🤔

AgentOttsel commented 6 years ago

Ooh, nicely done, @Arpple! Do you wanna open a pull request for that? Since you're not sure how it works, maybe we should get @trixnz's opinion first, though.