Add indentation to if-then-else expressions

As a Roblox developer, it is currently too hard to write automatically beautiful code using long if-then-else expressions.

If Roblox is able to address this issue, it would improve my development experience because beautiful code increases productivity.

How it currently auto-formats the code:

currentTrack =
	if timeType then
	musicPlaylists[playlistSwitchTable[timeType]]
	:GetChildren()[math.random(1, #musicPlaylists[playlistSwitchTable[timeType]]:GetChildren())]
	else
	musicPlaylists[fallbackPlaylist]
	:GetChildren()[math.random(1, #musicPlaylists[fallbackPlaylist]:GetChildren())]

How it should format it:

currentTrack =
	if timeType then
		musicPlaylists[playlistSwitchTable[timeType]]
			:GetChildren()[math.random(1, #musicPlaylists[playlistSwitchTable[timeType]]:GetChildren())]
	else
		musicPlaylists[fallbackPlaylist]
			:GetChildren()[math.random(1, #musicPlaylists[fallbackPlaylist]:GetChildren())]

I think it’s a bug because the :GetChildren()[...] line after musicPlaylists[fallbackPlaylist] isn’t indented either, but for some reason I can’t post in #bug-reports even though I got accepted into it so I guess I’ll just pretend it’s an unimplemented feature.

1 Like

Everything is already formatted accordingly to the way Lua formats their code.

How you should write if .. then expressions:

if conditionIsMet then
	StartFunction()
else
	EndFunction()
end

It only indents after an expression which continues, such as if and for for example.
If the expression returns, such as else or end, it moves back.

1 Like

OP is referring to if-then-else expressions, not if-then-else statements. Luau can use if-then-else as an assignment that doesn’t require a closing end, while if-then-else statements still behave (and indent) as expected with vanilla Lua.

3 Likes

Thank you for the explanation.
I should’ve known there was more than met the eye.

I heavily disagree on it putting “then” on a new line. However, I absolutely agree on it adding tablature to the inner contents.

I would heavily prefer if the auto-indentation (not even through just the formatting option) did this:

currentTrack = (
    if timeType then
        ...
    else
        ...
)

Currently it puts the inner-contents on the same tablature and that’s my biggest gripe, because when I copy/paste code around it’ll ruin the formatting I had previously.

1 Like

Oh, that was an oversight caused by fatigue, I’ll fix this now.