Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;

import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettingsConstants;

/**
* Gives access to the import rewrite configured with the settings as specified in the user interface.
* These settings are kept in JDT UI for compatibility reasons.
Expand Down Expand Up @@ -125,6 +127,14 @@
} catch (NumberFormatException e) {
// ignore
}

String keepExisting= JavaManipulation.getPreference(CodeGenerationSettingsConstants.ORGIMPORTS_KEEP_EXISTING_ONDEMAND, project);
rewrite.setKeepExistingOnDemandImports(Boolean.parseBoolean(keepExisting));

Check warning on line 132 in org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/CodeStyleConfiguration.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method setKeepExistingOnDemandImports(boolean) is undefined for the type ImportRewrite

String collapse= JavaManipulation.getPreference(CodeGenerationSettingsConstants.ORGIMPORTS_COLLAPSE_TO_ONDEMAND, project);
// Defaults to true, so a missing preference must be treated as enabled.
rewrite.setCollapseSingleImportsToOnDemand(collapse == null || Boolean.parseBoolean(collapse));

Check warning on line 136 in org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/CodeStyleConfiguration.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler

Member

ERROR: The method setCollapseSingleImportsToOnDemand(boolean) is undefined for the type ImportRewrite

return rewrite;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public class CodeGenerationSettingsConstants {
*/
public static final String ORGIMPORTS_IGNORELOWERCASE= "org.eclipse.jdt.ui.ignorelowercasenames"; //$NON-NLS-1$

/**
* A named preference that controls whether existing on-demand (".*") imports are preserved as
* on-demand imports by the "Organize Imports" operation, instead of being expanded into single
* imports.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*/
public static final String ORGIMPORTS_KEEP_EXISTING_ONDEMAND= "org.eclipse.jdt.ui.organizeimports.keepexistingondemand"; //$NON-NLS-1$

/**
* A named preference that controls whether the "Organize Imports" operation may collapse single
* imports into a new on-demand (".*") import once the on-demand threshold is reached. When
* disabled, the thresholds are ignored and no new on-demand import is created from single
* imports.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
*/
public static final String ORGIMPORTS_COLLAPSE_TO_ONDEMAND= "org.eclipse.jdt.ui.organizeimports.collapsetoondemand"; //$NON-NLS-1$

private CodeGenerationSettingsConstants() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,123 @@ static public class Test {
assertEqualString(cu.getSource(), str1);
}

@Test
public void testKeepExistingOnDemandImport() throws Exception {
IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");

IPackageFragment pack1= sourceFolder.createPackageFragment("pack1", false, null);
String str= """
package pack1;

import java.util.*;

public class C {
Map<String, String> m;
Set<String> s;
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("C.java", str, false, null);

String[] order= new String[] { "", "#" };
IChooseImportQuery query= createQuery("C", new String[] {}, new int[] {});

OrganizeImportsOperation op= createOperation(cu, order, 99, false, true, true, query);
setKeepExistingAndCollapse(cu.getJavaProject(), true, true);
op.run(null);

// The existing on-demand import is preserved instead of being expanded into single imports.
String expected= """
package pack1;

import java.util.*;

public class C {
Map<String, String> m;
Set<String> s;
}
""";
assertEqualString(cu.getSource(), expected);
}

@Test
public void testExistingOnDemandImportExpandedByDefault() throws Exception {
IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");

IPackageFragment pack1= sourceFolder.createPackageFragment("pack1", false, null);
String str= """
package pack1;

import java.util.*;

public class C {
Map<String, String> m;
Set<String> s;
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("C.java", str, false, null);

String[] order= new String[] { "", "#" };
IChooseImportQuery query= createQuery("C", new String[] {}, new int[] {});

OrganizeImportsOperation op= createOperation(cu, order, 99, false, true, true, query);
setKeepExistingAndCollapse(cu.getJavaProject(), false, true);
op.run(null);

// With the default settings the on-demand import is expanded into single imports.
String expected= """
package pack1;

import java.util.Map;
import java.util.Set;

public class C {
Map<String, String> m;
Set<String> s;
}
""";
assertEqualString(cu.getSource(), expected);
}

@Test
public void testDoNotCollapseSingleImportsToOnDemand() throws Exception {
IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");

IPackageFragment pack1= sourceFolder.createPackageFragment("pack1", false, null);
String str= """
package pack1;

public class C {
Collection<String> c;
Map<String, String> m;
Set<String> s;
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("C.java", str, false, null);

String[] order= new String[] { "", "#" };
IChooseImportQuery query= createQuery("C", new String[] {}, new int[] {});

// A threshold of 2 would normally collapse the three java.util types into an on-demand import.
OrganizeImportsOperation op= createOperation(cu, order, 2, false, true, true, query);
setKeepExistingAndCollapse(cu.getJavaProject(), false, false);
op.run(null);

String expected= """
package pack1;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class C {
Collection<String> c;
Map<String, String> m;
Set<String> s;
}
""";
assertEqualString(cu.getSource(), expected);
}

@Test
public void test_PackageInfoBug157541a() throws Exception {
IPackageFragmentRoot sourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
Expand Down Expand Up @@ -4085,4 +4202,10 @@ protected void setOrganizeImportSettings(String[] order, int threshold, int stat
}
}

protected void setKeepExistingAndCollapse(IJavaProject project, boolean keepExistingOnDemand, boolean collapseToOnDemand) {
IEclipsePreferences scope= new ProjectScope(project.getProject()).getNode(JavaUI.ID_PLUGIN);
scope.put(PreferenceConstants.ORGIMPORTS_KEEP_EXISTING_ONDEMAND, String.valueOf(keepExistingOnDemand));
scope.put(PreferenceConstants.ORGIMPORTS_COLLAPSE_TO_ONDEMAND, String.valueOf(collapseToOnDemand));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ public class ImportOrganizeConfigurationBlock extends OptionsConfigurationBlock
private static final Key PREF_ONDEMANDTHRESHOLD= getJDTUIKey(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD);
private static final Key PREF_IGNORELOWERCASE= getJDTUIKey(PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE);
private static final Key PREF_STATICONDEMANDTHRESHOLD= getJDTUIKey(PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD);
private static final Key PREF_KEEP_EXISTING_ONDEMAND= getJDTUIKey(PreferenceConstants.ORGIMPORTS_KEEP_EXISTING_ONDEMAND);
private static final Key PREF_COLLAPSE_TO_ONDEMAND= getJDTUIKey(PreferenceConstants.ORGIMPORTS_COLLAPSE_TO_ONDEMAND);

private static final String DIALOGSETTING_LASTLOADPATH= JavaUI.ID_PLUGIN + ".importorder.loadpath"; //$NON-NLS-1$
private static final String DIALOGSETTING_LASTSAVEPATH= JavaUI.ID_PLUGIN + ".importorder.savepath"; //$NON-NLS-1$

private static Key[] getAllKeys() {
return new Key[] {
PREF_IMPORTORDER, PREF_ONDEMANDTHRESHOLD, PREF_STATICONDEMANDTHRESHOLD, PREF_IGNORELOWERCASE
PREF_IMPORTORDER, PREF_ONDEMANDTHRESHOLD, PREF_STATICONDEMANDTHRESHOLD, PREF_IGNORELOWERCASE,
PREF_KEEP_EXISTING_ONDEMAND, PREF_COLLAPSE_TO_ONDEMAND
};
}

Expand Down Expand Up @@ -177,6 +180,8 @@ public void doubleClicked(ListDialogField<ImportOrderEntry> field) {
private StringDialogField fThresholdField;
private StringDialogField fStaticThresholdField;
private SelectionButtonDialogField fIgnoreLowerCaseTypesField;
private SelectionButtonDialogField fKeepExistingOnDemandField;
private SelectionButtonDialogField fCollapseToOnDemandField;
private SelectionButtonDialogField fExportButton;
private SelectionButtonDialogField fImportButton;

Expand Down Expand Up @@ -226,6 +231,14 @@ public ImportOrganizeConfigurationBlock(IStatusChangeListener context, IProject
fIgnoreLowerCaseTypesField.setDialogFieldListener(adapter);
fIgnoreLowerCaseTypesField.setLabelText(PreferencesMessages.ImportOrganizeConfigurationBlock_ignoreLowerCase_label);

fKeepExistingOnDemandField= new SelectionButtonDialogField(SWT.CHECK);
fKeepExistingOnDemandField.setDialogFieldListener(adapter);
fKeepExistingOnDemandField.setLabelText(PreferencesMessages.ImportOrganizeConfigurationBlock_keepExistingOnDemand_label);

fCollapseToOnDemandField= new SelectionButtonDialogField(SWT.CHECK);
fCollapseToOnDemandField.setDialogFieldListener(adapter);
fCollapseToOnDemandField.setLabelText(PreferencesMessages.ImportOrganizeConfigurationBlock_collapseToOnDemand_label);

updateControls();
}

Expand Down Expand Up @@ -265,6 +278,8 @@ protected Control createContents(Composite parent) {
fThresholdField.doFillIntoGrid(composite, 2);
((GridData) fThresholdField.getTextControl(null).getLayoutData()).grabExcessHorizontalSpace= false;
fStaticThresholdField.doFillIntoGrid(composite, 2);
fCollapseToOnDemandField.doFillIntoGrid(composite, 2);
fKeepExistingOnDemandField.doFillIntoGrid(composite, 2);
fIgnoreLowerCaseTypesField.doFillIntoGrid(composite, 2);

Dialog.applyDialogFont(composite);
Expand Down Expand Up @@ -418,6 +433,8 @@ protected void updateControls() {
int threshold= getImportNumberThreshold(PREF_ONDEMANDTHRESHOLD);
int staticThreshold= getImportNumberThreshold(PREF_STATICONDEMANDTHRESHOLD);
boolean ignoreLowerCase= Boolean.parseBoolean(getValue(PREF_IGNORELOWERCASE));
boolean keepExistingOnDemand= Boolean.parseBoolean(getValue(PREF_KEEP_EXISTING_ONDEMAND));
boolean collapseToOnDemand= Boolean.parseBoolean(getValue(PREF_COLLAPSE_TO_ONDEMAND));

fOrderListField.removeAllElements();
for (ImportOrderEntry i : importOrder) {
Expand All @@ -426,6 +443,8 @@ protected void updateControls() {
fThresholdField.setText(String.valueOf(threshold));
fStaticThresholdField.setText(String.valueOf(staticThreshold));
fIgnoreLowerCaseTypesField.setSelection(ignoreLowerCase);
fKeepExistingOnDemandField.setSelection(keepExistingOnDemand);
fCollapseToOnDemandField.setSelection(collapseToOnDemand);
}


Expand All @@ -443,6 +462,10 @@ protected final void doDialogFieldChanged(DialogField field) {
}
} else if (field == fIgnoreLowerCaseTypesField) {
setValue(PREF_IGNORELOWERCASE, fIgnoreLowerCaseTypesField.isSelected());
} else if (field == fKeepExistingOnDemandField) {
setValue(PREF_KEEP_EXISTING_ONDEMAND, fKeepExistingOnDemandField.isSelected());
} else if (field == fCollapseToOnDemandField) {
setValue(PREF_COLLAPSE_TO_ONDEMAND, fCollapseToOnDemandField.isSelected());
} else if (field == fImportButton) {
List<ImportOrderEntry> order= loadImportOrder();
if (order != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ private PreferencesMessages() {
public static String ImportOrganizeConfigurationBlock_order_load_button;
public static String ImportOrganizeConfigurationBlock_order_save_button;
public static String ImportOrganizeConfigurationBlock_ignoreLowerCase_label;
public static String ImportOrganizeConfigurationBlock_keepExistingOnDemand_label;
public static String ImportOrganizeConfigurationBlock_collapseToOnDemand_label;
public static String ImportOrganizeConfigurationBlock_threshold_label;
public static String ImportOrganizeConfigurationBlock_error_invalidthreshold;
public static String ImportOrganizeConfigurationBlock_loadDialog_title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ ImportOrganizeConfigurationBlock_order_add_static_button=New Stati&c...
ImportOrganizeConfigurationBlock_order_load_button=I&mport...
ImportOrganizeConfigurationBlock_order_save_button=E&xport...
ImportOrganizeConfigurationBlock_ignoreLowerCase_label=Do not create imports for &types starting with a lowercase letter
ImportOrganizeConfigurationBlock_keepExistingOnDemand_label=&Keep existing star (.*) imports
ImportOrganizeConfigurationBlock_collapseToOnDemand_label=&Collapse single imports into star (.*) imports when the threshold is reached
ImportOrganizeConfigurationBlock_threshold_label=Number of &imports needed for .* (e.g. 'org.eclipse.*'):
ImportOrganizeConfigurationBlock_error_invalidthreshold=Invalid import number.
ImportOrganizeConfigurationBlock_loadDialog_title=Load Import Order from File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ private PreferenceConstants() {
* <p>
* Value is of type <code>Boolean</code>.
* </p>
* @since 3.36
* @since 3.39
*/
public static final String CODEGEN_USE_MARKDOWN= CodeGenerationSettingsConstants.CODEGEN_USE_MARKDOWN;

Expand Down Expand Up @@ -456,6 +456,27 @@ private PreferenceConstants() {
*/
public static final String ORGIMPORTS_IGNORELOWERCASE= CodeGenerationSettingsConstants.ORGIMPORTS_IGNORELOWERCASE;

/**
* A named preference that controls whether existing on-demand (".*") imports are preserved as
* on-demand imports by the "Organize Imports" operation, instead of being expanded into single
* imports.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
* @since 3.39
*/
public static final String ORGIMPORTS_KEEP_EXISTING_ONDEMAND= CodeGenerationSettingsConstants.ORGIMPORTS_KEEP_EXISTING_ONDEMAND;

/**
* A named preference that controls whether the "Organize Imports" operation may collapse single
* imports into a new on-demand (".*") import once the on-demand threshold is reached.
* <p>
* Value is of type <code>Boolean</code>.
* </p>
* @since 3.39
*/
public static final String ORGIMPORTS_COLLAPSE_TO_ONDEMAND= CodeGenerationSettingsConstants.ORGIMPORTS_COLLAPSE_TO_ONDEMAND;

/**
* A named preference that specifies whether children of a compilation unit are shown in the package explorer.
* <p>
Expand Down Expand Up @@ -4140,6 +4161,8 @@ public static void initializeDefaultValues(IPreferenceStore store) {
store.setDefault(PreferenceConstants.ORGIMPORTS_ONDEMANDTHRESHOLD, 99);
store.setDefault(PreferenceConstants.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD, 99);
store.setDefault(PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE, true);
store.setDefault(PreferenceConstants.ORGIMPORTS_KEEP_EXISTING_ONDEMAND, false);
store.setDefault(PreferenceConstants.ORGIMPORTS_COLLAPSE_TO_ONDEMAND, true);

// TypeFilterPreferencePage
store.setDefault(PreferenceConstants.TYPEFILTER_ENABLED, ""); //$NON-NLS-1$
Expand Down
Loading