Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 944
Hyneman Avatar asked Oct 02 '22 18:10

Hyneman


1 Answers

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.

like image 99
hudsonb Avatar answered Oct 06 '22 00:10

hudsonb