IDEA-19061 Integrate the Rearranger-plugin into core-IDE

1. Default arrangement rules are defined for java;
2. Refactoring;
This commit is contained in:
Denis.Zhdanov
2012-08-27 22:53:37 +04:00
parent 4540917c0b
commit 9c04c890b4
6 changed files with 116 additions and 31 deletions
@@ -68,6 +68,43 @@ public class JavaRearranger implements Rearranger<JavaElementArrangementEntry>,
MODIFIERS_BY_TYPE.put(FIELD, concat(commonModifiers, TRANSIENT, VOLATILE));
}
private static final List<ArrangementRule<StdArrangementEntryMatcher>> DEFAULT_RULES =
new ArrayList<ArrangementRule<StdArrangementEntryMatcher>>();
static {
ArrangementModifier[] visibility = { PUBLIC, PROTECTED, PACKAGE_PRIVATE, PRIVATE };
for (ArrangementModifier modifier : visibility) {
and(FIELD, STATIC, FINAL, modifier);
}
for (ArrangementModifier modifier : visibility) {
and(FIELD, STATIC, modifier);
}
for (ArrangementModifier modifier : visibility) {
and(FIELD, FINAL, modifier);
}
for (ArrangementModifier modifier : visibility) {
and(FIELD, modifier);
}
and(METHOD);
and(ENUM);
and(INTERFACE);
and(CLASS);
}
private static void and(@NotNull Object ... conditions) {
if (conditions.length == 1) {
DEFAULT_RULES.add(new ArrangementRule<StdArrangementEntryMatcher>(new StdArrangementEntryMatcher(new ArrangementAtomMatchCondition(
ArrangementUtil.parseType(conditions[0]), conditions[0]
))));
return;
}
ArrangementCompositeMatchCondition composite = new ArrangementCompositeMatchCondition(ArrangementOperator.AND);
for (Object condition : conditions) {
composite.addOperand(new ArrangementAtomMatchCondition(ArrangementUtil.parseType(condition), condition));
}
DEFAULT_RULES.add(new ArrangementRule<StdArrangementEntryMatcher>(new StdArrangementEntryMatcher(composite)));
}
@NotNull
private static Set<ArrangementModifier> concat(@NotNull Set<ArrangementModifier> base, ArrangementModifier... modifiers) {
EnumSet<ArrangementModifier> result = EnumSet.copyOf(base);
@@ -174,7 +211,6 @@ public class JavaRearranger implements Rearranger<JavaElementArrangementEntry>,
@Nullable
@Override
public List<ArrangementRule<StdArrangementEntryMatcher>> getDefaultRules() {
// TODO den implement
return null;
return DEFAULT_RULES;
}
}
@@ -18,10 +18,9 @@ package com.intellij.psi.codeStyle.arrangement;
import com.intellij.lang.Language;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementAtomMatchCondition;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementCompositeMatchCondition;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementMatchCondition;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementMatchConditionVisitor;
import com.intellij.psi.codeStyle.arrangement.match.ArrangementEntryType;
import com.intellij.psi.codeStyle.arrangement.match.ArrangementModifier;
import com.intellij.psi.codeStyle.arrangement.model.*;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -211,4 +210,19 @@ public class ArrangementUtil {
return TextRange.create(startOffsetToUse, endOffsetToUse);
}
//endregion
@NotNull
public static ArrangementSettingType parseType(@NotNull Object condition) throws IllegalArgumentException {
if (condition instanceof ArrangementEntryType) {
return ArrangementSettingType.TYPE;
}
else if (condition instanceof ArrangementModifier) {
return ArrangementSettingType.MODIFIER;
}
else {
throw new IllegalArgumentException(String.format(
"Can't parse type for the given condition of class '%s': %s", condition.getClass(), condition
));
}
}
}
@@ -57,6 +57,29 @@ public class StdArrangementEntryMatcher implements ArrangementEntryMatcher {
return myDelegate.isMatched(entry);
}
@Override
public int hashCode() {
return myCondition.hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StdArrangementEntryMatcher matcher = (StdArrangementEntryMatcher)o;
return myCondition.equals(matcher.myCondition);
}
@Override
public String toString() {
return myCondition.toString();
}
@NotNull
private static ArrangementEntryMatcher doBuildMatcher(@NotNull ArrangementMatchCondition condition) {
MyVisitor visitor = new MyVisitor();
@@ -44,8 +44,8 @@ public class ArrangementCompositeMatchCondition implements ArrangementMatchCondi
return myOperands;
}
public ArrangementCompositeMatchCondition addOperand(@NotNull ArrangementMatchCondition node) {
myOperands.add(node);
public ArrangementCompositeMatchCondition addOperand(@NotNull ArrangementMatchCondition condition) {
myOperands.add(condition);
return this;
}
@@ -181,22 +181,10 @@ public class ArrangementRuleTree {
}
});
map(rules);
expandAll(myTree, new TreePath(myRoot));
setRules(rules);
myTree.setShowsRootHandles(false);
myTree.setCellRenderer(new MyCellRenderer());
if (ArrangementConstants.LOG_RULE_MODIFICATION) {
LOG.info("Arrangement tree is constructed. Models:");
myModels.forEachValue(new TObjectProcedure<ArrangementRuleEditingModelImpl>() {
@Override
public boolean execute(ArrangementRuleEditingModelImpl model) {
LOG.info(String.format(" row %d, model '%s'", model.getRow(), model.getRule()));
return true;
}
});
}
}
private void selectPreviousRule() {
@@ -377,6 +365,30 @@ public class ArrangementRuleTree {
}
return result;
}
public void setRules(@NotNull List<ArrangementRule<StdArrangementEntryMatcher>> rules) {
myRenderers.clear();
myModels.clear();
while (myRoot.getChildCount() > 0)
myTreeModel.removeNodeFromParent(myRoot.getFirstChild());
map(rules);
expandAll(myTree, new TreePath(myRoot));
if (ArrangementConstants.LOG_RULE_MODIFICATION) {
LOG.info("Arrangement tree is refreshed. Given rules:");
for (ArrangementRule<StdArrangementEntryMatcher> rule : rules) {
LOG.info(" " + rule.toString());
}
LOG.info("Following models have been built:");
myModels.forEachValue(new TObjectProcedure<ArrangementRuleEditingModelImpl>() {
@Override
public boolean execute(ArrangementRuleEditingModelImpl model) {
LOG.info(String.format(" row %d, model '%s'", model.getRow(), model.getRule()));
return true;
}
});
}
}
@NotNull
private ArrangementNodeComponent getNodeComponentAt(int row, @NotNull ArrangementMatchCondition condition) {
@@ -188,24 +188,24 @@ public abstract class ArrangementSettingsPanel extends CodeStyleAbstractPanel {
@SuppressWarnings("unchecked")
@NotNull
private List<ArrangementRule<StdArrangementEntryMatcher>> getRules(@NotNull CodeStyleSettings settings) {
List<ArrangementRule<StdArrangementEntryMatcher>> result = Collections.emptyList();
List<ArrangementRule<?>> storedRules = settings.getCommonSettings(myLanguage).getArrangementRules();
if (storedRules.isEmpty()) {
List<ArrangementRule<StdArrangementEntryMatcher>> defaultRules = mySettingsAware.getDefaultRules();
if (defaultRules != null) {
result = defaultRules;
return defaultRules;
}
}
else {
// We use unchecked cast here in assumption that current rearranger is based on standard settings if it uses standard
// settings-based rule editor.
// Note: unchecked cast for the whole collection doesn't work here (compiler error).
List<ArrangementRule<StdArrangementEntryMatcher>> result = new ArrayList<ArrangementRule<StdArrangementEntryMatcher>>();
for (ArrangementRule<?> rule : storedRules) {
result.add((ArrangementRule<StdArrangementEntryMatcher>)rule);
}
return result;
}
return result;
return Collections.emptyList();
}
private static void setupKeyboardActions(@NotNull ActionManager actionManager, @NotNull Tree treeComponent) {
@@ -231,18 +231,18 @@ public abstract class ArrangementSettingsPanel extends CodeStyleAbstractPanel {
}
@Override
public boolean isModified(CodeStyleSettings settings) {
return getRules(settings).equals(myRuleTree.getRules());
public boolean isModified(@NotNull CodeStyleSettings settings) {
return !getRules(settings).equals(myRuleTree.getRules());
}
@Override
public void apply(CodeStyleSettings settings) {
public void apply(@NotNull CodeStyleSettings settings) {
settings.getCommonSettings(myLanguage).setArrangementRules(myRuleTree.getRules());
}
@Override
protected void resetImpl(CodeStyleSettings settings) {
// TODO den implement
protected void resetImpl(@NotNull CodeStyleSettings settings) {
myRuleTree.setRules(getRules(settings));
}
@Override