Java Coding Style & Emacs cc-mode configuration

1.2k views Asked by At

I'm using GNU/Emacs HEAD with the included cc-mode (c-version 5.32.2) on a GNU/Linux Debian machine.

I'm trying to define a custom style to manage the Code Conventions for the Java Programming Language, Android's Code Style Guidelines for Contributors and some custom rules.

As a lisp beginner, it does not seem wise to start from scratch. As a consequence, I used google-c-style as a starting point and I managed to get the intended behavior for the most indenting rules, with an exception on nested condition (see the code snippet below).

From that post, I have defined (arglist-cont-nonempty . ++) in my custom style (full code: custom-java-style.el). Unfortunately, although most cases is indented as intended:

if ((condition1 && condition2)
        || (condition3 && condition4)
        ||!(condition5 && condition6)) {
    doSomethingAboutIt();

    someMethod(longExpression1, longExpression2, longExpression3,
            longExpression4, longExpression5);
}

Nested condition are wrongly indented:

if (!(deviceRegistred.getAddress().equalsIgnoreCase(deviceAdress)
                && deviceRegistred.getName().equalsIgnoreCase(deviceName))) {
    doSomethingAboutIt();
}

Ctrl-c Ctrl+s report Syntactic analysis: ((arglist-cont-nonempty 2447 2450) (arglist-cont-nonempty 2447 2452)) on the second line and I obviously have a 16 spaces (2 times ++) indentation instead of 8 (++).

I'd like to get the following indentation:

if (!(deviceRegistred.getAddress().equalsIgnoreCase(deviceAdress)
        && deviceRegistred.getName().equalsIgnoreCase(deviceName))) {
    doSomethingAboutIt();
}

I tried to define a (,(when (fboundp …))) condition like the one used for statement-cont but without success (my lack of lisp knowledge don't help either).

Now, the question: Is my approach right or wrong? How could/should I implement the intended behavior (i.e. detect when i'm in a nested condition to get the right indentation)?

(I don't want to use malabar-mode or JDEE, so please don't tell me to use them.)

Cheers,

Renaud

Update 2011/12/06 (reacting to the comments)

We wouldn't begin a holy war here. Those who want to use Emacs, for their own reasons, can stick to Emacs, the others will do as they want…

Saying that, I work within a team in which I'm the only one to use Emacs, the others are fond of Eclipse. Since I'm in charge of the coding rules, I have worked with my colleagues to get the right save actions and help to configure the Eclipse's formatter. All I could say is that: the Eclipse save actions and formatter are not easy to configure at all… The main difference is that you have a nice GUI with nice checkboxes but it doesn't help much to reduce the complexity.

I stick with Emacs…

1

There are 1 answers

0
Vsevolod Dyomkin On BEST ANSWER

I'd say, that Emacs does the right thing. Consider this case:

if (!(deviceRegistred.getAddress().equalsIgnoreCase(deviceAdress)
                && deviceRegistred.getName().equalsIgnoreCase(deviceName))
        || otherCondition) {
    doSomethingAboutIt();
}

If nested condition was indented 8 chars that would be a confusing variant:

if (!(deviceRegistred.getAddress().equalsIgnoreCase(deviceAdress)
        && deviceRegistred.getName().equalsIgnoreCase(deviceName))
        || otherCondition) {
    doSomethingAboutIt();
}