Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does try-with-resources break indentation in Emacs?"

Emacs 24 Java mode does not indent the new try-with-resource construct properly.

class X{
    void foo() {
        try {
            check(); // ((statement-block-intro 35))
        }
    }

    void bar() {
        try (Lock l = bar()) {
                check(); // ((substatement 140) (statement-block-intro 140))
            }
    }
}

As seen above, statements inside such a try block are indented by an extra 4 spaces, value of c-basic-indent, including closing brace.

One difference between try and try-with-resource that I identified is the syntactic-information for the first statement in the block, the latter having an extra (substatement) at the head. Could this be the reason it breaks?

If I manually delete 4 spaces from the first line then the rest of the lines indent correctly, except for the closing brace, which continues to be indented with extra spaces.

This is driving me nuts. If solving it my changing c-guess-basic-syntax is non-trivial, perhaps a workaround using advice?

like image 776
Miserable Variable Avatar asked May 14 '13 23:05

Miserable Variable


2 Answers

This is well known bug in emacs and there is thread on list.gnu.org about this issue with patch, which fixes this bug: https://lists.gnu.org/archive/html/bug-gnu-emacs/2013-07/msg00577.html

Futhermore, according to this: http://osdir.com/ml/emacs-diffs-gnu/2013-07/msg00350.html it seems, that this bug was fixed already on 2013-07-27.

like image 77
Andremoniy Avatar answered Nov 17 '22 01:11

Andremoniy


Although the http://osdir.com/ml/emacs-diffs-gnu/2013-07/msg00350.html thread does indeed have a fix, it is still not in the latest cc-mode. I grabbed the latest cc-mode and installed it, then grabbed the diff and applied it (it didn't apply cleanly any more, but it was short and easy to do by hand). Correct indentation! Way more annoying than it should be, but I have properly indented try blocks, so I'm happy.

like image 35
John Jannotti Avatar answered Nov 17 '22 03:11

John Jannotti