Skip to content

Commit

Permalink
[formatter] Align arrows on column in switch statements/expressions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-matela committed Jan 9, 2024
1 parent d513389 commit 1ddb457
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
9 changes: 8 additions & 1 deletion org.eclipse.jdt.ui/preview/formatter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2022 Mateusz Matela and others.
* Copyright (c) 2018, 2024 Mateusz Matela and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -100,6 +100,13 @@ void foo() {
object = Arrays.asList(str);
i += 2;
}

switch(i){
case 0 -> theInt++;
case 22, 33 -> theInt--;
case 1234567890 -> theInt = 0;
default -> theInt = -1;
}
}
}
//--PREVIEW--END--section-indentation-align-on-column
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -111,6 +111,7 @@ private FormatterMessages() {
public static String FormatterModifyDialog_indentation_info_blank_lines_before_field;
public static String FormatterModifyDialog_indentation_info_blank_lines_before_field_delete;
public static String FormatterModifyDialog_indentation_info_blank_lines_to_preserve;
public static String FormatterModifyDialog_indentation_pref_align_arrows_in_switch_on_columns;
public static String FormatterModifyDialog_indentation_pref_align_assignment_statements_on_columns;
public static String FormatterModifyDialog_indentation_pref_align_fields_in_columns;
public static String FormatterModifyDialog_indentation_pref_align_variable_declarations_on_columns;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2000, 2023 IBM Corporation and others.
# Copyright (c) 2000, 2024 IBM Corporation and others.
#
# This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -380,6 +380,7 @@ FormatterModifyDialog_indentation_val_indentation_default=Default for wrapped li
FormatterModifyDialog_indentation_val_indentation_preserve=Do not touch
FormatterModifyDialog_indentation_pref_indent_size=Indentation size:

FormatterModifyDialog_indentation_pref_align_arrows_in_switch_on_columns=Arrows in switch statements/expressions
FormatterModifyDialog_indentation_pref_align_assignment_statements_on_columns=Assignment statements
FormatterModifyDialog_indentation_pref_align_fields_in_columns=Field declarations
FormatterModifyDialog_indentation_pref_align_variable_declarations_on_columns=Variable declarations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -310,7 +310,7 @@ private static int getSelection(List<MenuItem> items) {
}

public static ModifyAll<ToolBar> addModifyAll(Section section, boolean withIndent, final Images images) {
return new ModifyAll<ToolBar>(section, images) {
return new ModifyAll<>(section, images) {
private LineWrapPreference fPreference;

@Override
Expand Down Expand Up @@ -919,18 +919,22 @@ public void focusLost(FocusEvent e) {
DefaultCodeFormatterConstants.FORMATTER_ALIGN_VARIABLE_DECLARATIONS_ON_COLUMNS, CheckboxPreference.FALSE_TRUE);
final CheckboxPreference alignAssignmentsPref= fTree.addCheckbox(alignSection, FormatterMessages.FormatterModifyDialog_indentation_pref_align_assignment_statements_on_columns,
DefaultCodeFormatterConstants.FORMATTER_ALIGN_ASSIGNMENT_STATEMENTS_ON_COLUMNS, CheckboxPreference.FALSE_TRUE);
final CheckboxPreference alignArrowsPref= fTree.addCheckbox(alignSection, FormatterMessages.FormatterModifyDialog_indentation_pref_align_arrows_in_switch_on_columns,
DefaultCodeFormatterConstants.FORMATTER_ALIGN_ARROWS_IN_SWITCH_ON_COLUMNS, CheckboxPreference.FALSE_TRUE);

fTree.addGap(alignSection);
final CheckboxPreference useSpacesPref= fTree.addCheckbox(alignSection, FormatterMessages.FormatterModifyDialog_indentation_pref_align_with_spaces,
DefaultCodeFormatterConstants.FORMATTER_ALIGN_WITH_SPACES, CheckboxPreference.FALSE_TRUE);
Preference<?> tabCharPref= parentSection.findChildPreference(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
Predicate<String> anyAlignChecker= v -> DefaultCodeFormatterConstants.TRUE.equals(alignFieldsPref.getValue())
|| DefaultCodeFormatterConstants.TRUE.equals(alignVariablesPref.getValue())
|| DefaultCodeFormatterConstants.TRUE.equals(alignAssignmentsPref.getValue());
|| DefaultCodeFormatterConstants.TRUE.equals(alignAssignmentsPref.getValue())
|| DefaultCodeFormatterConstants.TRUE.equals(alignArrowsPref.getValue());
Predicate<String> spacesChecker= anyAlignChecker.and(v -> !JavaCore.SPACE.equals(tabCharPref.getValue()));
alignFieldsPref.addDependant(useSpacesPref, spacesChecker);
alignVariablesPref.addDependant(useSpacesPref, spacesChecker);
alignAssignmentsPref.addDependant(useSpacesPref, spacesChecker);
alignArrowsPref.addDependant(useSpacesPref, spacesChecker);
tabCharPref.addDependant(useSpacesPref, spacesChecker);

Button checkbox = new Button(alignSection.fInnerComposite, SWT.CHECK);
Expand All @@ -945,6 +949,7 @@ public void focusLost(FocusEvent e) {
alignFieldsPref.addDependant(groupingPref, anyAlignChecker);
alignVariablesPref.addDependant(groupingPref, anyAlignChecker);
alignAssignmentsPref.addDependant(groupingPref, anyAlignChecker);
alignArrowsPref.addDependant(groupingPref, anyAlignChecker);

groupingPref.setValueValidator(value -> {
String warningMessage= null;
Expand Down

0 comments on commit 1ddb457

Please sign in to comment.