Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Eclipse align the ?: ternary operator?

I need the exact same effect as in this question but in Eclipse.

It should only do the alignment if I explcitly insert a new line before the ":" or if the second operand (the "true" expression) is too long.

Example:

a = cond ? "a veeeeeeeeeeeeeeeery loooooooooooooooooooooooooooong string"
         : "";
      // ^ put the colon here
like image 728
billc.cn Avatar asked Nov 08 '11 01:11

billc.cn


People also ask

Is ?: ternary operator?

In computer programming, ?: is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

How do you write 3 conditions in a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

How do you throw an exception in a ternary operator?

You can't throw an exception in a ternary clause. Both options must return a value, which throw new Exception(); doesn't satisfy. Solution, use if . Save this answer.

Which operators are known as ternary operator *?

The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.


1 Answers

This original answer was for Galileo which is well over a decade ago, so I'm updating it to be a little more current. Original answer remains below.

For Eclipse 2021-12 (under Window though I hope it would be the same across platforms):

  1. Go into Project, Properties on the menu.
  2. Under Java Code Style, choose Formatter.
  3. For the current project only, enable project-specific settings. For workspace settings, simply click on the Configure workspace settings. Then click the edit button.
  4. In the selection pane, under Line Wrapping, Wrapping settings, Other expressions, there is a Conditionals item that you can edit.

The options you want are:

  • wrap before operators;
  • wrap where necessary;
  • no force split; and
  • indent on column.

They give this as an example:

enter image description here



This is the dated answer for Galileo:

  1. Go into Project, Properties.
  2. Under Java Code Style, choose Formatter.
  3. For the current project only, enable project-specific settings. For workspace settings, simply click on the Configure workspace settings. Then click the edit button.
  4. On the line-wrapping tab, choose Expressions, Conditionals.
  5. Choose the relevant line wrapping policy and indentation policy.

I have "wrap only when necessary" and "indent on column" which appears to be close to what you want.

If you're not running Galileo, other versions of Eclipse should at least be close to that process. If my settings aren't quite what you want, you may need to fiddle with them a bit, but that Formatter section is pretty well the entire exposed interface so, if it can't be done with that, it can't be done (short of writing your own plug-in or finding a more configurable third-party one).

like image 111
paxdiablo Avatar answered Oct 17 '22 05:10

paxdiablo