Hyneman
Hyneman

Reputation: 61

Android Studio / IntelliJ Anonymous Class Formatting

I was wondering if it is possible to change the auto-formatting in Android Studio in such a way that braces for anonymous classes are placed on the same line while still putting braces for regular classes, methods and blocks on a new line.

Currently, IntelliJ is giving me this result:

class TestClass
{
    public void test()
    {
        FooBar foo = new FooBar(new Runnable() // I want the brace here...
        { // ...not here.
            @Override
            public void run()
            {
                //
            }
        });
    }
}

However, I would like the code to be formatted like this:

class TestClass
{
    public void test()
    {
        FooBar foo = new FooBar(new Runnable() { // <- Same Line
            @Override
            public void run()
            {
                //
            }
        });
    }
}

Everything is fine, except the one detail that I cannot get the brace to be formatted like in the second example. Is this not possible or did I just overlook the setting?

Upvotes: 6

Views: 1538

Answers (1)

hudsonb
hudsonb

Reputation: 2294

I've wanted this for a long time now as well, but unfortunately it is not possible in Intellij.

The closest you can come is setting Wrapping and Braces/Braces Placement/In class declaration to "Next line if wrapped" or "End of line" (what I use). This of course modifies the way the brace is wrapped for not only anonymous inner classes (the desired result), but also for top level and inner classes; however, methods/if/else/for/while/do-while/try/catch/finally/switch etc are unaffected.

I really wish IntelliJ would add a Wrapping and Braces/Braces Placement/In anonymous class declaration option like Eclipse has.

Upvotes: 5

Related Questions