From 27d71a5779ac28276e8081e152b41323dccc4d84 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Wed, 1 Jun 2016 16:30:57 +0300 Subject: [PATCH] diff: inline unnecessary helper classes --- .../intellij/diff/merge/MergeModelBase.java | 83 +++++++------------ .../src/com/intellij/diff/util/DiffUtil.java | 79 +++++++----------- 2 files changed, 59 insertions(+), 103 deletions(-) diff --git a/platform/diff-impl/src/com/intellij/diff/merge/MergeModelBase.java b/platform/diff-impl/src/com/intellij/diff/merge/MergeModelBase.java index 16674ebeef4e..fbcfd091a987 100644 --- a/platform/diff-impl/src/com/intellij/diff/merge/MergeModelBase.java +++ b/platform/diff-impl/src/com/intellij/diff/merge/MergeModelBase.java @@ -52,7 +52,7 @@ public abstract class MergeModelBase implements @NotNull private final TIntHashSet myChangesToUpdate = new TIntHashSet(); private int myBulkChangeUpdateDepth; - @Nullable private MyCommandAction myCurrentCommand; + private boolean myInsideCommand; private boolean myDisposed; @@ -104,7 +104,7 @@ public abstract class MergeModelBase implements @CalledInAwt public boolean isInsideCommand() { - return myCurrentCommand != null; + return myInsideCommand; } private void setLineStart(int index, int line) { @@ -201,7 +201,7 @@ public abstract class MergeModelBase implements if (oldState == null) continue; invalidateHighlighters(index); - if (myCurrentCommand == null) corruptedStates.add(oldState); + if (!isInsideCommand()) corruptedStates.add(oldState); } if (myUndoManager != null && !corruptedStates.isEmpty()) { @@ -224,67 +224,46 @@ public abstract class MergeModelBase implements boolean underBulkUpdate, @Nullable TIntArrayList affectedChanges, @NotNull Runnable task) { - new MyCommandAction(myProject, myDocument, commandName, commandGroupId, confirmationPolicy, underBulkUpdate, affectedChanges, task).run(); - } - - private class MyCommandAction extends DiffUtil.DiffCommandAction { - @Nullable private final TIntArrayList myAffectedChanges; - @NotNull private final Runnable myTask; - - public MyCommandAction(@Nullable Project project, - @NotNull Document document, - @Nullable String commandName, - @Nullable String commandGroupId, - @NotNull UndoConfirmationPolicy confirmationPolicy, - boolean underBulkUpdate, - @Nullable TIntArrayList affectedChanges, - @NotNull Runnable task) { - super(project, document, commandName, commandGroupId, confirmationPolicy, underBulkUpdate); - myAffectedChanges = affectedChanges != null ? collectAffectedChanges(affectedChanges) : null; - myTask = task; - } - - @Override - @CalledWithWriteLock - protected final void execute() { - LOG.assertTrue(myCurrentCommand == null); + TIntArrayList allAffectedChanges = affectedChanges != null ? collectAffectedChanges(affectedChanges) : null; + DiffUtil.executeWriteCommand(myProject, myDocument, commandName, commandGroupId, confirmationPolicy, underBulkUpdate, () -> { + LOG.assertTrue(!myInsideCommand); // We should restore states after changes in document (by DocumentUndoProvider) to avoid corruption by our onBeforeDocumentChange() // Undo actions are performed in backward order, while redo actions are performed in forward order. // Thus we should register two UndoableActions. - myCurrentCommand = this; + myInsideCommand = true; enterBulkChangeUpdateBlock(); - registerUndoRedo(true); + registerUndoRedo(true, allAffectedChanges); try { - myTask.run(); + task.run(); } finally { - registerUndoRedo(false); + registerUndoRedo(false, allAffectedChanges); exitBulkChangeUpdateBlock(); - myCurrentCommand = null; + myInsideCommand = false; + } + }); + } + + private void registerUndoRedo(boolean undo, @Nullable TIntArrayList affectedChanges) { + if (myUndoManager == null) return; + + List states; + if (affectedChanges != null) { + states = new ArrayList<>(affectedChanges.size()); + affectedChanges.forEach((index) -> { + states.add(storeChangeState(index)); + return true; + }); + } + else { + states = new ArrayList<>(getChangesCount()); + for (int index = 0; index < getChangesCount(); index++) { + states.add(storeChangeState(index)); } } - - private void registerUndoRedo(final boolean undo) { - if (myUndoManager == null) return; - - List states; - if (myAffectedChanges != null) { - states = new ArrayList<>(myAffectedChanges.size()); - myAffectedChanges.forEach((index) -> { - states.add(storeChangeState(index)); - return true; - }); - } - else { - states = new ArrayList<>(getChangesCount()); - for (int index = 0; index < getChangesCount(); index++) { - states.add(storeChangeState(index)); - } - } - myUndoManager.undoableActionPerformed(new MyUndoableAction(MergeModelBase.this, states, undo)); - } + myUndoManager.undoableActionPerformed(new MyUndoableAction(this, states, undo)); } private static class MyUndoableAction extends BasicUndoableAction { diff --git a/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java b/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java index 70e4e8982efa..066557c70312 100644 --- a/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java +++ b/platform/diff-impl/src/com/intellij/diff/util/DiffUtil.java @@ -100,7 +100,10 @@ import com.intellij.util.ui.JBInsets; import com.intellij.util.ui.JBUI; import com.intellij.util.ui.UIUtil; import gnu.trove.Equality; -import org.jetbrains.annotations.*; +import org.jetbrains.annotations.CalledInAwt; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.swing.*; import javax.swing.border.Border; @@ -1059,64 +1062,38 @@ public class DiffUtil { // Writable // - public static abstract class DiffCommandAction implements Runnable { - @Nullable protected final Project myProject; - @NotNull protected final Document myDocument; - @Nullable private final String myCommandName; - @Nullable private final String myCommandGroupId; - @NotNull private final UndoConfirmationPolicy myConfirmationPolicy; - private final boolean myUnderBulkUpdate; - - public DiffCommandAction(@Nullable Project project, - @NotNull Document document, - @Nullable String commandName, - @Nullable String commandGroupId, - @NotNull UndoConfirmationPolicy confirmationPolicy, - boolean underBulkUpdate) { - myDocument = document; - myProject = project; - myCommandName = commandName; - myCommandGroupId = commandGroupId; - myConfirmationPolicy = confirmationPolicy; - myUnderBulkUpdate = underBulkUpdate; + @CalledInAwt + public static void executeWriteCommand(@Nullable Project project, + @NotNull Document document, + @Nullable String commandName, + @Nullable String commandGroupId, + @NotNull UndoConfirmationPolicy confirmationPolicy, + boolean underBulkUpdate, + @NotNull Runnable task) { + if (!makeWritable(project, document)) { + VirtualFile file = FileDocumentManager.getInstance().getFile(document); + LOG.warn("Document is read-only" + (file != null ? ": " + file.getPresentableName() : "")); + return; } - @Override - @CalledInAwt - public final void run() { - if (!makeWritable(myProject, myDocument)) { - VirtualFile file = FileDocumentManager.getInstance().getFile(myDocument); - LOG.warn("Document is read-only" + (file != null ? ": " + file.getPresentableName() : "")); - return; - } - - ApplicationManager.getApplication().runWriteAction(() -> { - CommandProcessor.getInstance().executeCommand(myProject, () -> { - if (myUnderBulkUpdate) { - DocumentUtil.executeInBulk(myDocument, true, this::execute); - } - else { - execute(); - } - }, myCommandName, myCommandGroupId, myConfirmationPolicy, myDocument); - }); - } - - @CalledWithWriteLock - protected abstract void execute(); + ApplicationManager.getApplication().runWriteAction(() -> { + CommandProcessor.getInstance().executeCommand(project, () -> { + if (underBulkUpdate) { + DocumentUtil.executeInBulk(document, true, task); + } + else { + task.run(); + } + }, commandName, commandGroupId, confirmationPolicy, document); + }); } @CalledInAwt public static void executeWriteCommand(@NotNull final Document document, @Nullable final Project project, - @Nullable final String name, + @Nullable final String commandName, @NotNull final Runnable task) { - new DiffCommandAction(project, document, name, null, UndoConfirmationPolicy.DEFAULT, false) { - @Override - protected void execute() { - task.run(); - } - }.run(); + executeWriteCommand(project, document, commandName, null, UndoConfirmationPolicy.DEFAULT, false, task); } public static boolean isEditable(@NotNull Editor editor) {