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

Handle a situation when the root is not shown
This commit is contained in:
Denis.Zhdanov
2012-08-20 22:26:09 +04:00
parent 67d4bbf702
commit 9899440429
4 changed files with 55 additions and 8 deletions
@@ -21,6 +21,7 @@ import gnu.trove.TObjectProcedure;
import org.junit.Test;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import static com.intellij.psi.codeStyle.arrangement.ArrangementUtil.and;
import static com.intellij.psi.codeStyle.arrangement.match.ArrangementEntryType.FIELD;
@@ -135,7 +136,7 @@ public class ArrangementRuleEditingModelImplTest extends AbstractArrangementRule
}
@Test
public void removeLastRowConditionFromMultiChildrenParent() {
public void removeFirstRowConditionFromMultiChildrenParent() {
configure(and(atom(FIELD), atom(PUBLIC)));
configure(and(atom(FIELD), atom(STATIC)));
@@ -159,7 +160,7 @@ public class ArrangementRuleEditingModelImplTest extends AbstractArrangementRule
assertNotNull(atomFieldNode);
assertEquals(atom(FIELD), atomFieldNode.getUserObject());
DefaultMutableTreeNode layeredFieldNode = atomFieldNode.getNextNode();
DefaultMutableTreeNode layeredFieldNode = atomFieldNode.getNextSibling();
assertNotNull(atomFieldNode);
assertEquals(atom(FIELD), atomFieldNode.getUserObject());
@@ -169,6 +170,42 @@ public class ArrangementRuleEditingModelImplTest extends AbstractArrangementRule
checkTreeNodesConsistency();
}
@Test
public void removeLastRowConditionFromMultiChildrenParent() {
configure(and(atom(FIELD), atom(PUBLIC)));
configure(and(atom(FIELD), atom(STATIC)));
ArrangementRuleEditingModel siblingModel = myRowMappings.get(2);
assertNotNull(siblingModel);
ArrangementRuleEditingModel modelToChange = myRowMappings.get(3);
assertNotNull(modelToChange);
assertEquals(2, myRowMappings.size());
modelToChange.removeAndCondition(atom(STATIC));
assertEquals(2, myRowMappings.size());
assertSame(siblingModel, myRowMappings.get(2));
assertEquals(and(atom(FIELD), atom(PUBLIC)), siblingModel.getSettingsNode());
assertSame(modelToChange, myRowMappings.get(3));
assertEquals(atom(FIELD), modelToChange.getSettingsNode());
DefaultMutableTreeNode compositeFieldNode = (DefaultMutableTreeNode)myRoot.getFirstChild();
assertNotNull(compositeFieldNode);
assertEquals(atom(FIELD), compositeFieldNode.getUserObject());
DefaultMutableTreeNode publicNode = (DefaultMutableTreeNode)compositeFieldNode.getFirstChild();
assertNotNull(publicNode);
assertEquals(atom(PUBLIC), publicNode.getUserObject());
DefaultMutableTreeNode atomFieldNode = compositeFieldNode.getNextSibling();
assertNotNull(atomFieldNode);
assertEquals(atom(FIELD), atomFieldNode.getUserObject());
checkTreeNodesConsistency();
}
private void checkTreeNodesConsistency() {
final Ref<DefaultMutableTreeNode> rootRef = new Ref<DefaultMutableTreeNode>();
@@ -479,7 +479,12 @@ public class ArrangementConfigUtil {
parent.add(child);
return false;
}
if (child.getChildCount() <= 0) {
// Don't merge the last child.
parent.insert(child, index);
}
boolean anchorAbove = false;
DefaultMutableTreeNode mergeCandidate = null;
if (index > 0) {
@@ -96,7 +96,7 @@ public class ArrangementRuleEditingModelBuilder {
DefaultMutableTreeNode topMostNode = (DefaultMutableTreeNode)ArrangementConfigUtil.getLastBefore(pair.first, root);
int row = initialInsertRow + pair.second - 1;
ArrangementRuleEditingModelImpl model
= new ArrangementRuleEditingModelImpl(setting, topMostNode, pair.first, grouper, rowMappings, row);
= new ArrangementRuleEditingModelImpl(setting, topMostNode, pair.first, grouper, rowMappings, row, tree.isRootVisible() ? 0 : -1);
rowMappings.put(row, model);
}
@@ -43,6 +43,7 @@ public class ArrangementRuleEditingModelImpl implements ArrangementRuleEditingMo
@NotNull private final TIntObjectHashMap<ArrangementRuleEditingModelImpl> myRowMappings;
@NotNull private final ArrangementSettingsGrouper myGrouper;
private final int myRowShift;
@NotNull private DefaultMutableTreeNode myTopMost;
@NotNull private DefaultMutableTreeNode myBottomMost;
@@ -60,13 +61,16 @@ public class ArrangementRuleEditingModelImpl implements ArrangementRuleEditingMo
* @param grouper strategy that encapsulates information on how settings node should be displayed
* @param mappings {@code 'row -> model'} mappings
* @param row row number for which current model is registered at the given model mappings
* @param shift specifies a shift to be applied to the node rows on model modification. Primary intention is to handle
* a situation when tree root is not shown (a shift is '-1' then)
*/
public ArrangementRuleEditingModelImpl(@NotNull ArrangementSettingsNode node,
@NotNull DefaultMutableTreeNode topMost,
@NotNull DefaultMutableTreeNode bottomMost,
@NotNull ArrangementSettingsGrouper grouper,
@NotNull TIntObjectHashMap<ArrangementRuleEditingModelImpl> mappings,
int row)
int row,
int shift)
{
mySettingsNode = node;
myTopMost = topMost;
@@ -74,6 +78,7 @@ public class ArrangementRuleEditingModelImpl implements ArrangementRuleEditingMo
myGrouper = grouper;
myRowMappings = mappings;
myRow = row;
myRowShift = shift;
refreshConditions();
}
@@ -188,8 +193,8 @@ public class ArrangementRuleEditingModelImpl implements ArrangementRuleEditingMo
if (row == myRow) {
return true;
}
if (rowChanges.containsKey(row)) {
newMappings.put(rowChanges.get(row), model);
if (rowChanges.containsKey(row - myRowShift)) {
newMappings.put(rowChanges.get(row - myRowShift) + myRowShift, model);
}
else {
newMappings.put(row, model);
@@ -198,7 +203,7 @@ public class ArrangementRuleEditingModelImpl implements ArrangementRuleEditingMo
return true;
}
});
myRow = ArrangementConfigUtil.getRow(myBottomMost);
myRow = ArrangementConfigUtil.getRow(myBottomMost) + myRowShift;
newMappings.put(myRow, this);
myRowMappings.clear();