mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-19061 Integrate the Rearranger-plugin into core-IDE
'move rule(s) up' action
This commit is contained in:
-2
@@ -48,8 +48,6 @@ public class ArrangementConstants {
|
||||
|
||||
public static final boolean LOG_RULE_MODIFICATION = Boolean.parseBoolean(System.getProperty("log.arrangement.rule.modification"));
|
||||
|
||||
public static final DataKey<NotNullFunction<Boolean/* move up? */, Boolean/* is enabled */>> UPDATE_MOVE_RULE_FUNCTION_KEY
|
||||
= DataKey.create("Arrangement.Rule.Function.Update.Move");
|
||||
public static final DataKey<Consumer<Boolean/* move up? */>> MOVE_RULE_FUNCTION_KEY = DataKey.create("Arrangement.Rule.Function.Move");
|
||||
|
||||
private ArrangementConstants() {
|
||||
|
||||
+1
-2
@@ -36,8 +36,7 @@ public class MoveArrangementRuleDownAction extends AnAction implements DumbAware
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
Function<Boolean,Boolean> function = ArrangementConstants.UPDATE_MOVE_RULE_FUNCTION_KEY.getData(e.getDataContext());
|
||||
e.getPresentation().setEnabled(function != null && function.fun(false));
|
||||
// TODO den implement
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+85
-7
@@ -16,12 +16,17 @@
|
||||
package com.intellij.application.options.codeStyle.arrangement.action;
|
||||
|
||||
import com.intellij.application.options.codeStyle.arrangement.ArrangementConstants;
|
||||
import com.intellij.application.options.codeStyle.arrangement.match.ArrangementMatchingRulesControl;
|
||||
import com.intellij.application.options.codeStyle.arrangement.match.ArrangementMatchingRulesModel;
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.application.ApplicationBundle;
|
||||
import com.intellij.openapi.project.DumbAware;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.Function;
|
||||
import gnu.trove.TIntArrayList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Denis Zhdanov
|
||||
@@ -36,15 +41,88 @@ public class MoveArrangementRuleUpAction extends AnAction implements DumbAware {
|
||||
|
||||
@Override
|
||||
public void update(AnActionEvent e) {
|
||||
Function<Boolean,Boolean> function = ArrangementConstants.UPDATE_MOVE_RULE_FUNCTION_KEY.getData(e.getDataContext());
|
||||
e.getPresentation().setEnabled(function != null && function.fun(true));
|
||||
ArrangementMatchingRulesControl control = ArrangementConstants.MATCHING_RULES_CONTROL_KEY.getData(e.getDataContext());
|
||||
if (control == null) {
|
||||
e.getPresentation().setEnabled(false);
|
||||
return;
|
||||
}
|
||||
|
||||
TIntArrayList rows = control.getSelectedModelRows();
|
||||
int top = -1;
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
int row = rows.get(i);
|
||||
if (row == top + 1) {
|
||||
top++;
|
||||
}
|
||||
else {
|
||||
e.getPresentation().setEnabled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
e.getPresentation().setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(AnActionEvent e) {
|
||||
Consumer<Boolean> function = ArrangementConstants.MOVE_RULE_FUNCTION_KEY.getData(e.getDataContext());
|
||||
if (function != null) {
|
||||
function.consume(true);
|
||||
final ArrangementMatchingRulesControl control = ArrangementConstants.MATCHING_RULES_CONTROL_KEY.getData(e.getDataContext());
|
||||
if (control == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int editing = control.getEditingRow() - 1;
|
||||
|
||||
control.runOperationIgnoreSelectionChange(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
control.hideEditor();
|
||||
final List<int[]> mappings = new ArrayList<int[]>();
|
||||
TIntArrayList rows = control.getSelectedModelRows();
|
||||
rows.reverse();
|
||||
int top = -1;
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
int row = rows.get(i);
|
||||
if (row == top + 1) {
|
||||
mappings.add(new int[] { row, row });
|
||||
top++;
|
||||
}
|
||||
else {
|
||||
mappings.add(new int[]{ row, row - 1 });
|
||||
}
|
||||
}
|
||||
|
||||
if (mappings.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int newRowToEdit = editing;
|
||||
ArrangementMatchingRulesModel model = control.getModel();
|
||||
Object value;
|
||||
int from;
|
||||
int to;
|
||||
for (int[] pair : mappings) {
|
||||
from = pair[0];
|
||||
to = pair[1];
|
||||
if (from != to) {
|
||||
value = model.getElementAt(from);
|
||||
model.removeRow(from);
|
||||
model.insert(to, value);
|
||||
if (newRowToEdit == from) {
|
||||
newRowToEdit = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ListSelectionModel selectionModel = control.getSelectionModel();
|
||||
for (int[] pair : mappings) {
|
||||
selectionModel.addSelectionInterval(pair[1], pair[1]);
|
||||
}
|
||||
|
||||
|
||||
if (newRowToEdit >= 0) {
|
||||
control.showEditor(newRowToEdit);
|
||||
}
|
||||
}
|
||||
});
|
||||
control.repaintRows(0, control.getModel().getSize() - 1, true);
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -357,6 +357,9 @@ public class ArrangementMatchingRulesControl extends JBTable {
|
||||
return new Rectangle(firstRect.x, firstRect.y, lastRect.width, lastRect.y + lastRect.height - firstRect.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return selected model rows sorted in descending order
|
||||
*/
|
||||
@NotNull
|
||||
public TIntArrayList getSelectedModelRows() {
|
||||
mySelectedRows.clear();
|
||||
|
||||
+4
@@ -48,6 +48,10 @@ public class ArrangementMatchingRulesModel extends DefaultTableModel {
|
||||
public void set(int row, Object value) {
|
||||
setValueAt(value, row, 0);
|
||||
}
|
||||
|
||||
public void insert(int row, Object value) {
|
||||
insertRow(row, new Object[] { value });
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object aValue, int row, int column) {
|
||||
|
||||
Reference in New Issue
Block a user