Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jdt.core/formatter')
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java14
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java12
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java48
3 files changed, 67 insertions, 7 deletions
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
index 4dde0c415e..e2f4edd280 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java
@@ -324,11 +324,25 @@ public class DefaultCodeFormatterConstants {
* - default: createAlignmentValue(false, WRAP_ONE_PER_LINE, INDENT_DEFAULT)
* </pre>
* @see #createAlignmentValue(boolean, int, int)
+ * @see #FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION_CHAIN
* @since 3.0
*/
public static final String FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION = JavaCore.PLUGIN_ID + ".formatter.alignment_for_conditional_expression"; //$NON-NLS-1$
/**
* <pre>
+ * FORMATTER / Option for alignment of conditional expression chains. If disabled, chains are not recognized
+ * and only {@link #FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION} policy is used instead.
+ * - option id: "org.eclipse.jdt.core.formatter.alignment_for_conditional_expression_chain"
+ * - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
+ * - default: createAlignmentValue(false, WRAP_NO_SPLIT, INDENT_DEFAULT)
+ * </pre>
+ * @see #createAlignmentValue(boolean, int, int)
+ * @see #FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION
+ * @since 3.17
+ */
+ public static final String FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION_CHAIN = JavaCore.PLUGIN_ID + ".formatter.alignment_for_conditional_expression_chain"; //$NON-NLS-1$
+ /**
+ * <pre>
* FORMATTER / Option for alignment of enum constants
* - option id: "org.eclipse.jdt.core.formatter.alignment_for_enum_constants"
* - possible values: values returned by <code>createAlignmentValue(boolean, int, int)</code> call
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java
index 12b0b2f2f0..257f5c53fa 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java
@@ -134,6 +134,7 @@ public class DefaultCodeFormatterOptions {
public int alignment_for_compact_if;
public int alignment_for_compact_loop;
public int alignment_for_conditional_expression;
+ public int alignment_for_conditional_expression_chain;
public int alignment_for_enum_constants;
public int alignment_for_expressions_in_array_initializer;
public int alignment_for_expressions_in_for_loop_header;
@@ -518,6 +519,7 @@ public class DefaultCodeFormatterOptions {
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_COMPACT_IF, getAlignment(this.alignment_for_compact_if));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_COMPACT_LOOP, getAlignment(this.alignment_for_compact_loop));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION, getAlignment(this.alignment_for_conditional_expression));
+ options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION_CHAIN, getAlignment(this.alignment_for_conditional_expression_chain));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, getAlignment(this.alignment_for_enum_constants));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, getAlignment(this.alignment_for_expressions_in_array_initializer));
options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_FOR_LOOP_HEADER, getAlignment(this.alignment_for_expressions_in_for_loop_header));
@@ -965,6 +967,8 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_conditional_expression = Alignment.M_ONE_PER_LINE_SPLIT;
}
}
+ setInt(settings, DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION_CHAIN,
+ v -> this.alignment_for_conditional_expression_chain = v);
final Object alignmentForEnumConstantsOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS);
if (alignmentForEnumConstantsOption != null) {
try {
@@ -2521,6 +2525,12 @@ public class DefaultCodeFormatterOptions {
* This method is used to handle deprecated preferences which might be replaced by
* one or more preferences.
* Depending on deprecated option handling policy, set the new formatting option(s).
+ * <p>
+ * Note: Also add deprecated preference keys in {@link org.eclipse.jdt.internal.core.JavaCorePreferenceInitializer#initializeDeprecatedOptions}
+ * so that the formatter recognizes those deprecated options when used with project specific formatter profiles.
+ * (see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=544776">Bug 544776</a>)
+ * </p>
+ *
* @param settings the given map
* @deprecated
*/
@@ -2778,6 +2788,7 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_compact_if = Alignment.M_ONE_PER_LINE_SPLIT | Alignment.M_INDENT_BY_ONE;
this.alignment_for_compact_loop = Alignment.M_ONE_PER_LINE_SPLIT | Alignment.M_INDENT_BY_ONE;
this.alignment_for_conditional_expression = Alignment.M_ONE_PER_LINE_SPLIT;
+ this.alignment_for_conditional_expression_chain = Alignment.M_NO_ALIGNMENT;
this.alignment_for_enum_constants = Alignment.M_NO_ALIGNMENT;
this.alignment_for_expressions_in_array_initializer = Alignment.M_COMPACT_SPLIT;
this.alignment_for_expressions_in_for_loop_header = Alignment.M_NO_ALIGNMENT;
@@ -3127,6 +3138,7 @@ public class DefaultCodeFormatterOptions {
this.alignment_for_compact_if = Alignment.M_COMPACT_SPLIT;
this.alignment_for_compact_loop = Alignment.M_COMPACT_SPLIT;
this.alignment_for_conditional_expression = Alignment.M_NEXT_PER_LINE_SPLIT;
+ this.alignment_for_conditional_expression_chain = Alignment.M_NO_ALIGNMENT;
this.alignment_for_enum_constants = Alignment.M_COMPACT_SPLIT;
this.alignment_for_expressions_in_array_initializer = Alignment.M_COMPACT_SPLIT;
this.alignment_for_expressions_in_for_loop_header = Alignment.M_NO_ALIGNMENT;
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
index 0d39d3e9ed..5c12e80886 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapPreparator.java
@@ -682,16 +682,50 @@ public class WrapPreparator extends ASTVisitor {
@Override
public boolean visit(ConditionalExpression node) {
+ boolean chainsMatter = (this.options.alignment_for_conditional_expression_chain
+ & Alignment.SPLIT_MASK) != Alignment.M_NO_ALIGNMENT;
+ boolean isNextInChain = node.getParent() instanceof ConditionalExpression
+ && node == ((ConditionalExpression) node.getParent()).getElseExpression();
+ boolean isFirstInChain = node.getElseExpression() instanceof ConditionalExpression && !isNextInChain;
boolean wrapBefore = this.options.wrap_before_conditional_operator;
List<Integer> before = wrapBefore ? this.wrapIndexes : this.secondaryWrapIndexes;
List<Integer> after = wrapBefore ? this.secondaryWrapIndexes : this.wrapIndexes;
- before.add(this.tm.firstIndexAfter(node.getExpression(), TokenNameQUESTION));
- before.add(this.tm.firstIndexAfter(node.getThenExpression(), TokenNameCOLON));
- after.add(this.tm.firstIndexIn(node.getThenExpression(), -1));
- after.add(this.tm.firstIndexIn(node.getElseExpression(), -1));
- this.wrapParentIndex = this.tm.lastIndexIn(node.getExpression(), -1);
- this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
- handleWrap(this.options.alignment_for_conditional_expression);
+ if (!chainsMatter || (!isFirstInChain && !isNextInChain)) {
+ before.add(this.tm.firstIndexAfter(node.getExpression(), TokenNameQUESTION));
+ before.add(this.tm.firstIndexAfter(node.getThenExpression(), TokenNameCOLON));
+ after.add(this.tm.firstIndexIn(node.getThenExpression(), -1));
+ after.add(this.tm.firstIndexIn(node.getElseExpression(), -1));
+ this.wrapParentIndex = this.tm.lastIndexIn(node.getExpression(), -1);
+ this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
+ handleWrap(this.options.alignment_for_conditional_expression);
+
+ } else if (isFirstInChain) {
+ List<ConditionalExpression> chain = new ArrayList<>();
+ chain.add(node);
+ ConditionalExpression next = node;
+ while (next.getElseExpression() instanceof ConditionalExpression) {
+ next = (ConditionalExpression) next.getElseExpression();
+ chain.add(next);
+ }
+
+ for (ConditionalExpression conditional : chain) {
+ before.add(this.tm.firstIndexAfter(conditional.getThenExpression(), TokenNameCOLON));
+ after.add(this.tm.firstIndexIn(conditional.getElseExpression(), -1));
+ }
+ this.wrapParentIndex = this.tm.firstIndexIn(node.getExpression(), -1);
+ this.wrapGroupEnd = this.tm.lastIndexIn(node, -1);
+ handleWrap(this.options.alignment_for_conditional_expression_chain);
+
+ this.currentDepth++;
+ for (ConditionalExpression conditional : chain) {
+ before.add(this.tm.firstIndexAfter(conditional.getExpression(), TokenNameQUESTION));
+ after.add(this.tm.firstIndexIn(conditional.getThenExpression(), -1));
+ this.wrapParentIndex = this.tm.firstIndexIn(conditional.getExpression(), -1);
+ this.wrapGroupEnd = this.tm.lastIndexIn(conditional.getThenExpression(), -1);
+ handleWrap(this.options.alignment_for_conditional_expression);
+ }
+ this.currentDepth--;
+ }
return true;
}

Back to the top