Sublime Forum

Lua do-end block improperly indented

#1

Say you’re currently at 1 tab of indentation in a Lua file. Type do<return>end. After the , the next line is still at 1 indent instead of indenting to 2. Furthermore, after typing “end”, the line dedents to 0 tabs.

This is in contrast with for i=1,2 do<return>end which works properly.

(do-end blocks are used in Lua to create a local scope, similarly to plain { } blocks in C-like languages.)

0 Likes

#2

sounds like do should be added to the increaseIndentPattern of the indentation rules for Lua

1 Like

#3

A similar issue, the until keyword is matched by increaseIndentPattern, but it should be matched by decreaseIndentPattern. The construct is repeat...until <condition>, so until\b.* closes a block.

Another odd indentation issue I haven’t been able to figure out. If I create an empty Lua file and type:

function f(x)
end

Then Ctrl-A Delete, and type it again. Every other time, the end line will not be dedented:

function f(x)
    end

This issue is not just with function, you can also see it with while, for, etc.

1 Like

#4

Actually, you don’t need to select the text and delete it to make that second issue happen; this will do it in a brand new file:

function a(x)
end

function b(x)
    end

Although interestingly, if you were to similarly define a function c, d and so on from this point without fixing the indent on b, everything works as expected:

function a(x)
end

function b(x)
    end

    function c(x)
    end

    function d(x)
    end

On the other hand, if you do manually fix the indent on b, then it works for every second time. Almost as if the initial indent has something to do with how it indents.

I don’t know enough about how the indenting system in sublime works to know if this is due to the way the indent rules are defined or if there is something deeper going on, though.

0 Likes

#5

I suspect the indentation engine gets confused when the end is on the line immediately after the function, because function says, indent the next line, end says unindent the line the end is on, so the rules conflict and only one (the first one) is honored… I think writing it like this should work:

function a(x)

end
2 Likes

Bracket indenting issue
#6

Ah yeah, that makes sense. I had not thought of that.

From a little playing around, just simple blank lines aren’t enough to get the end to unindent. Some sort of other text like a comment or code is enough to make it work as expected, though.

0 Likes