use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.
the class AutoRefactorFix method createCleanUp.
/**
* Create the clean up.
*
* @param compilationUnit compilation unit
* @param enabled enabled
* @param fOptions options
* @return Clean up fix
*/
public static ICleanUpFix createCleanUp(final CompilationUnit compilationUnit, final boolean enabled, final CleanUpOptions fOptions) {
boolean hasChanges = false;
final ICompilationUnit iCompilationUnit = (ICompilationUnit) compilationUnit.getJavaElement();
// $NON-NLS-1$
final CleanUpChange cleanUpChange = new CleanUpChange("AutoRefactor", iCompilationUnit);
TextEdit allEdits = null;
if (enabled) {
final IJavaProject javaProject = PrepareApplyRefactoringsJob.getIJavaProject(iCompilationUnit);
final JavaProjectOptions options = new JavaProjectOptionsImpl(javaProject.getOptions(true));
final Environment environment = AutoRefactorPlugin.getEnvironment();
final List<RefactoringRule> refactoringRules = getConfiguredRefactoringRules(fOptions);
final SubMonitor loopMonitor = SubMonitor.convert(null, 1);
final Queue<RefactoringUnit> refactoringUnits = new ConcurrentLinkedQueue<>();
refactoringUnits.add(new RefactoringUnit(iCompilationUnit, options));
final ApplyRefactoringsJob applyRefactoringsJob = new ApplyRefactoringsJob(refactoringUnits, refactoringRules, environment);
final AggregateASTVisitor visitor = new AggregateASTVisitor(refactoringRules);
try {
List<TextEdit> textEdits = applyRefactoringsJob.applyRefactoring(iCompilationUnit, visitor, options, loopMonitor, false);
for (TextEdit textEdit : textEdits) {
if (hasChanges) {
allEdits = TextEditUtil.merge(allEdits, textEdit);
} else {
hasChanges = true;
allEdits = textEdit;
}
}
} catch (Exception e) {
if (!hasChanges) {
return null;
}
}
}
if (!hasChanges) {
return null;
}
cleanUpChange.setEdit(allEdits);
AutoRefactorFix autoRefactorFix = new AutoRefactorFix();
autoRefactorFix.cleanUpChange = cleanUpChange;
return autoRefactorFix;
}
use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.
the class AutoRefactorFix method getConfiguredRefactoringRules.
/**
* Returns the cleanup rules which have been enabled from the Eclipse
* preferences.
*
* @param options the options
* @return the cleanup rules which have been enabled from the Eclipse
* preferences
*/
public static List<RefactoringRule> getConfiguredRefactoringRules(final CleanUpOptions options) {
final List<RefactoringRule> refactorings = AllCleanUpRules.getAllCleanUpRules();
for (final Iterator<RefactoringRule> iter = refactorings.iterator(); iter.hasNext(); ) {
final RefactoringRule refactoring = iter.next();
final String cleanupPropertyName = AutoRefactorTabPage.getCleanupPropertyName(refactoring);
if (!AutoRefactorPlugin.getDefault().getPreferenceStore().contains(cleanupPropertyName) || !CleanUpOptions.TRUE.equals(AutoRefactorPlugin.getDefault().getPreferenceStore().getString(cleanupPropertyName))) {
iter.remove();
}
}
return refactorings;
}
use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.
the class AutoRefactorTabPage method createContents.
/**
* Create the contents.
*
* @param parent parent
* @return Composite
*/
@Override
public Composite createContents(Composite parent) {
final List<RefactoringRule> allRefactoringRules = AllCleanUpRules.getAllCleanUpRules();
Collections.sort(allRefactoringRules, Comparator.comparing(RefactoringRule::getName));
final Group ruleGroup = createControls(parent, allRefactoringRules);
initialize();
invalidateToggleRules(ruleGroup);
return fieldEditorParent;
}
use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.
the class AutoRefactorTabPage method createControls.
private Group createControls(final Composite parent, final List<RefactoringRule> allRefactoringRules) {
fieldEditorParent = new Composite(parent, SWT.FILL);
fieldEditorParent.setLayout(new FillLayout());
ScrolledComposite scrolledComposite = new ScrolledComposite(fieldEditorParent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
initFields(allRefactoringRules);
Composite composite = new Composite(scrolledComposite, SWT.FILL);
final Group ruleGroup = new Group(composite, SWT.FILL);
// $NON-NLS-1$
ruleGroup.setText("Rules by default");
// All rule checkbox
toggleAllRules = new Button(ruleGroup, SWT.CHECK | SWT.LEFT);
toggleAllRules.setFont(ruleGroup.getFont());
// $NON-NLS-1$
toggleAllRules.setText("Toggle all the rules");
toggleAllRules.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
boolean isSelected = AutoRefactorTabPage.this.toggleAllRules.getSelection();
for (BooleanFieldEditor rule : AutoRefactorTabPage.this.rules) {
((Button) rule.getDescriptionControl(ruleGroup)).setSelection(isSelected);
rule.getPreferenceStore().setValue(rule.getPreferenceName(), isSelected);
}
AutoRefactorPlugin.getDefault().getPreferenceStore().needsSaving();
}
});
// Add a space
Composite spacer = new Composite(ruleGroup, SWT.NULL);
spacer.setLayoutData(new GridData(0, 5));
rules = new ArrayList<>(allRefactoringRules.size());
for (RefactoringRule refactoringRule : allRefactoringRules) {
final BooleanFieldEditor booleanFieldEditor = new BooleanFieldEditor(getPropertyName(refactoringRule), refactoringRule.getName(), SWT.WRAP, ruleGroup);
booleanFieldEditor.getDescriptionControl(ruleGroup).setToolTipText(refactoringRule.getDescription());
((Button) booleanFieldEditor.getDescriptionControl(ruleGroup)).addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final String isPreferenceEnabled = booleanFieldEditor.getBooleanValue() ? CleanUpOptions.TRUE : CleanUpOptions.FALSE;
booleanFieldEditor.getPreferenceStore().setValue(booleanFieldEditor.getPreferenceName(), isPreferenceEnabled);
booleanFieldEditor.getPreferenceStore().needsSaving();
invalidateToggleRules(ruleGroup);
}
});
rules.add(booleanFieldEditor);
}
fields.addAll(rules);
scrolledComposite.setContent(composite);
ruleGroup.pack();
composite.pack();
return ruleGroup;
}
use of org.autorefactor.jdt.internal.corext.dom.RefactoringRule in project AutoRefactor by JnRouvignac.
the class RefactoringRulesTest method testRefactoring0.
private void testRefactoring0() throws Exception {
// $NON-NLS-1$
String sampleName = testName + "Sample.java";
// $NON-NLS-1$
File sampleIn = new File(SAMPLES_BASE_DIR, "samples_in/" + sampleName);
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(testName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
// $NON-NLS-1$
File sampleOut = new File(SAMPLES_BASE_DIR, "samples_out/" + sampleName);
// $NON-NLS-1$ //$NON-NLS-2$
assertTrue(testName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
// $NON-NLS-1$
String refactoringClassname = testName + "CleanUp";
RefactoringRule refactoring = getRefactoringClass(refactoringClassname);
assertNotNull(// $NON-NLS-1$ //$NON-NLS-2$
testName + ": refactoring class " + refactoringClassname + " should exist.\n" + "Make sure you added it to the method getAllCleanUpRules() " + "of the " + // $NON-NLS-1$ //$NON-NLS-2$
AllCleanUpRules.class + ".", // $NON-NLS-1$
refactoring);
String sampleInSource = readAll(sampleIn);
String sampleOutSource = readAll(sampleOut);
given(sampleInSource, sampleOutSource);
IDocument doc = when(sampleName, refactoring, sampleInSource);
then(sampleOutSource, doc);
}
Aggregations