Groovy-Emacs-Modes / groovy-emacs-modes

A groovy major mode, grails minor mode, and a groovy inferior mode.
84 stars 39 forks source link

Indentation of nested if-else statements #130

Closed djlindstr closed 5 years ago

djlindstr commented 5 years ago

The Groovy documentation contains this example:

@Immutable
class Coordinates {
    double latitude
    double longitude

    double getAt(int idx) {
        if (idx == 0) latitude
        else if (idx == 1) longitude
        else throw new Exception("Wrong coordinate index, use 0 or 1")
    }
}

The nested if-else chain is indented too much in groovy-mode. The below snippet is another shorter test case.

while (true)
    if (x) foo()
    else if (y) baz()
    else bar()

Currently indented

while (true)
    if (x) foo()
        else if (y) baz()
            else bar()

I am working on a fix for these cases.

djlindstr commented 5 years ago

Actually, let me re-phrase the issue somewhat since it's not just about the missing braces. The following examples are also mis-indented. Expected indentation:

if (x)      { foo() }
else if (y) { bar() }
else if (z) { baz() }
else        { s = 1 }
if (x) { foo() }
else   { if (y) bar() }
else   { if (z) baz() }
else   { s = 1 }

In groovy-mode the else:s are currently indented one extra level each. I will hopefully be able to fix these as well.