6

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Hyneman
  • 61
  • 3
  • possibly duplicate of http://stackoverflow.com/questions/21313388/stop-intellij-android-studio-preview-adding-newlines-when-formatting-anonymous – Piyush Agarwal Jan 25 '14 at 07:34
  • @pyus13 I saw this thread before I posted and it is not what I want. I want IntelliJ to stop inserting a newline before the opening brace, not before the closing parenthesis. – Hyneman Jan 25 '14 at 07:53
  • Can you please include what intellij is giving and what you want both format in question ? – Piyush Agarwal Jan 25 '14 at 08:25
  • I updated the initial post. – Hyneman Jan 25 '14 at 09:11
  • sorry but the thing you want studio is doing by default for me check https://drive.google.com/file/d/0B-rj-rDNcJBcUWpVZHBVNTQyR1E/edit?usp=sharing. Go to File > Settings > CodeStyle > Java and choose Scheme Default if you made any changes sometime. – Piyush Agarwal Jan 25 '14 at 09:20
  • Thank you for answering on this. I cannot access the file (permission denied). You said you use the default settings, so I assume your braces are on the same line (Java standard). However, I **only** want to have braces for anonymous classes on the same line. This is possible with eclipse, so I'm wondering if it is also supported by IntelliJ. – Hyneman Jan 25 '14 at 09:58
  • I changed the file permission check it now. – Piyush Agarwal Jan 25 '14 at 10:21
  • Thank you, but that's what I'm saying in my previous comment. It works when all braces are being placed on the same line, but that's exactly what I do not want. – Hyneman Jan 25 '14 at 10:26
  • Please vote for this feature in youtrack https://youtrack.jetbrains.com/issue/IDEA-91929 – Ali Apr 07 '15 at 14:01

1 Answers1

5

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.

hudsonb
  • 2,214
  • 19
  • 20