Ternary operation alignment

This what Idea alignes
return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ? new AppMenuGroupItem(strAppIds.get(
                item.getAppId()), item.getItemType(), item.getTitle(), null, item.getIcon()) : new AppMenuItem(strAppIds
                                                                                                                       .get(item.getAppId()),
                                                                                                               item.getStrId(),
                                                                                                               item.getItemType(),
                                                                                                               item.getTitle(),
                                                                                                               item.getWindowClass(),
                                                                                                               item.getJsniEvent(),
                                                                                                               item.getIcon(),
                                                                                                               item.getUrl());



and this what I would expect (or something similar)

  return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ?
          new AppMenuGroupItem(strAppIds.get(item.getAppId()),
                                              item.getItemType(),
                                              item.getTitle(),
                                              null,
                                              item.getIcon()) :
          new AppMenuItem(strAppIds.get(item.getAppId()),
                                      item.getStrId(),
                                      item.getItemType(),
                                      item.getTitle(),
                                      item.getWindowClass(),
                                      item.getJsniEvent(),
                                      item.getIcon(),
                                      item.getUrl());



Is there anything can be done, thanks
9 comments
Comment actions Permalink

Hi Vladimir,

Please try the following:

  • 'Project Settings - Code Style - Wrapping and Braces - Ternary operation' - 'Wrap always';


That produces the following result:

return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ?                     
        new AppMenuGroupItem(strAppIds.get(                                                   
                item.getAppId()), item.getItemType(), item.getTitle(), null, item.getIcon()) :
        new AppMenuItem(strAppIds                                                             
                .get(item.getAppId()),                                                        
                item.getStrId(),                                                              
                item.getItemType(),                                                           
                item.getTitle(),                                                              
                item.getWindowClass(),                                                        
                item.getJsniEvent(),                                                          
                item.getIcon(),                                                               
                item.getUrl());                                                               



Denis

0
Comment actions Permalink

I want chop :) not wrap and result is inconsistent as I can tell.

0
Comment actions Permalink

I'm afraid I don't quite understand what do you want to achieve - 'wrap ternary operation' makes the second and third operands to be located on new lines; 'chop method call arguments if long' makes method call arguments to 'chop' if they exceed right margin.

I get the following result by formatting your code written in one line:

Before   

public class BrokenAlignment {
    public void foo() {
        return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ? new AppMenuGroupItem(strAppIds.get(item.getAppId()), item.getItemType(), item.getTitle(), null, item.getIcon()) : new AppMenuItem(strAppIds.get( item.getAppId()), item.getStrId(), item.getItemType(), item.getTitle(),item.getWindowClass(), item.getJsniEvent(), item.getIcon(),item.getUrl());
    }
}


After (right margin is set to 120)    

public class BrokenAlignment {
    public void foo() {
        return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ?
                new AppMenuGroupItem(strAppIds.get(item.getAppId()),
                        item.getItemType(),
                        item.getTitle(),
                        null,
                        item.getIcon()) :
                new AppMenuItem(strAppIds.get(item.getAppId()),
                        item.getStrId(),
                        item.getItemType(),
                        item.getTitle(),
                        item.getWindowClass(),
                        item.getJsniEvent(),
                        item.getIcon(),
                        item.getUrl());
    }
}


After (right margin is set to 160)             

public class BrokenAlignment {
    public void foo() {
        return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ?
                new AppMenuGroupItem(strAppIds.get(item.getAppId()), item.getItemType(), item.getTitle(), null, item.getIcon()) :
                new AppMenuItem(strAppIds.get(item.getAppId()),
                        item.getStrId(),
                        item.getItemType(),
                        item.getTitle(),
                        item.getWindowClass(),
                        item.getJsniEvent(),
                        item.getIcon(),
                        item.getUrl());
    }
}




You can see that both operands are chopped when right margin is set to 120 (because they both exceed it) but only the second operand exceeds 160 visual columns, hence, it is the only one chopped at the second case.

Please clarify desired formatter's behavior if the current one doesn't suit your needs.

Denis

0
Comment actions Permalink

Setting: Chop down when long, align when multiple
I have a right margin set to 120 and would extect alignment as in the second part of the original post

  return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ?
          new AppMenuGroupItem(strAppIds.get(item.getAppId()),
                                              item.getItemType(),
                                              item.getTitle(),
                                              null,
                                              item.getIcon()) :
          new AppMenuItem(strAppIds.get(item.getAppId()),
                                      item.getStrId(),
                                      item.getItemType(),
                                      item.getTitle(),
                                      item.getWindowClass() + (url != null ? "#" + url : ""),
                                      item.getWindowClass(),
                                      item.getJsniEvent(),
                                      item.getIcon(),
                                      item.getUrl());

And I'm getting below

        return MenuItemType.GROUP_ITEM.getTypeCode().equals(item.getItemType()) ? new AppMenuGroupItem(strAppIds.get(
                item.getAppId()), item.getItemType(), item.getTitle(), null, item.getIcon()) : new AppMenuItem(strAppIds
                                                                                                                       .get(item.getAppId()),
                                                                                                               item.getStrId(),
                                                                                                               item.getItemType(),
                                                                                                               item.getTitle(),
                                                                                                               item.getWindowClass() +
                                                                                                               (url !=
                                                                                                                null ?
                                                                                                                "#" +
                                                                                                                url :
                                                                                                                ""),
                                                                                                               item.getJsniEvent(),
                                                                                                               item.getIcon(),
                                                                                                               url);



0
Comment actions Permalink

What is your original text before formatting? Is it the one where the whole ternary operator is writtenn in one line?

Please provide original text and your code style settings.

Denis

0
Comment actions Permalink

Yes, everything was one line, setting.jar is attached



Attachment(s):
x-settings.jar
0
Comment actions Permalink

Just uncheck the following property and you're done - 'Wrapping and Braces - Method call arguments - Take priority over call chain wrapping'

Denis

0
Comment actions Permalink

Thanks, but I'm not sure I want to do this, I want to take priority over call chain but at the same time I want to a?b:c operation to be properly aligned as well, not a big deal but nice to have.

Thanks

0
Comment actions Permalink

Hi Vladimir,

I'm afraid it's not possible to achieve that with the current formatting facilities.

Looks like you need the feature discussed here.

Denis

0

Please sign in to leave a comment.