Android Studio / IntelliJ Anonymous Class Formatting
Asked Answered
P

1

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?

Percheron answered 25/1, 2014 at 3:6 Comment(9)
possibly duplicate of #21313888Youngling
@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.Percheron
Can you please include what intellij is giving and what you want both format in question ?Youngling
I updated the initial post.Percheron
sorry but the thing you want studio is doing by default for me check drive.google.com/file/d/0B-rj-rDNcJBcUWpVZHBVNTQyR1E/…. Go to File > Settings > CodeStyle > Java and choose Scheme Default if you made any changes sometime.Youngling
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.Percheron
I changed the file permission check it now.Youngling
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.Percheron
Please vote for this feature in youtrack youtrack.jetbrains.com/issue/IDEA-91929Benedicto
K
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.

Karalee answered 25/2, 2014 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.