diff: simplify getting affected side by context

This commit is contained in:
Aleksey Pivovarov
2016-03-03 18:10:48 +03:00
parent e3b469e514
commit 3159e7ec83
6 changed files with 27 additions and 24 deletions
@@ -129,6 +129,13 @@ public enum Side {
return isLeft() ? region.first : region.second;
}
@Nullable
public static <T> Side fromValue(@NotNull List<? extends T> list, @Nullable T value) {
assert list.size() == 2;
int index = list.indexOf(value);
return index != -1 ? fromIndex(index) : null;
}
//
// Fragments
//
@@ -92,4 +92,11 @@ public enum ThreeSide {
assert list.size() == 3;
return list.get(myIndex);
}
@Nullable
public static <T> ThreeSide fromValue(@NotNull List<? extends T> list, @Nullable T value) {
assert list.size() == 3;
int index = list.indexOf(value);
return index != -1 ? fromIndex(index) : null;
}
}
@@ -597,10 +597,8 @@ public class TextMergeViewer implements MergeTool.MergeViewer {
enterBulkChangeUpdateBlock();
if (myAllMergeChanges.isEmpty()) return;
ThreeSide side = null;
if (e.getDocument() == getEditor(ThreeSide.LEFT).getDocument()) side = ThreeSide.LEFT;
if (e.getDocument() == getEditor(ThreeSide.RIGHT).getDocument()) side = ThreeSide.RIGHT;
if (e.getDocument() == getEditor(ThreeSide.BASE).getDocument()) side = ThreeSide.BASE;
List<Document> documents = ContainerUtil.map(getEditors(), Editor::getDocument);
ThreeSide side = ThreeSide.fromValue(documents, e.getDocument());
if (side == null) {
LOG.warn("Unknown document changed");
return;
@@ -47,6 +47,7 @@ import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.*;
import javax.swing.*;
@@ -57,6 +58,7 @@ import java.util.Iterator;
import java.util.List;
import static com.intellij.diff.util.DiffUtil.getLineCount;
import static com.intellij.util.ObjectUtils.assertNotNull;
public class SimpleDiffViewer extends TwosideTextDiffViewer {
public static final Logger LOG = Logger.getInstance(SimpleDiffViewer.class);
@@ -309,9 +311,8 @@ public class SimpleDiffViewer extends TwosideTextDiffViewer {
super.onBeforeDocumentChange(e);
if (myDiffChanges.isEmpty()) return;
Side side = null;
if (e.getDocument() == getEditor(Side.LEFT).getDocument()) side = Side.LEFT;
if (e.getDocument() == getEditor(Side.RIGHT).getDocument()) side = Side.RIGHT;
List<Document> documents = ContainerUtil.map(getEditors(), Editor::getDocument);
Side side = Side.fromValue(documents, e.getDocument());
if (side == null) {
LOG.warn("Unknown document changed");
return;
@@ -525,13 +526,8 @@ public class SimpleDiffViewer extends TwosideTextDiffViewer {
}
Editor editor = e.getData(CommonDataKeys.EDITOR);
if (editor != getEditor1() && editor != getEditor2()) {
e.getPresentation().setEnabledAndVisible(false);
return;
}
Side side = Side.fromLeft(editor == getEditor(Side.LEFT));
if (!isVisible(side)) {
Side side = Side.fromValue(getEditors(), editor);
if (side == null || !isVisible(side)) {
e.getPresentation().setEnabledAndVisible(false);
return;
}
@@ -551,9 +547,7 @@ public class SimpleDiffViewer extends TwosideTextDiffViewer {
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
Editor editor = e.getData(CommonDataKeys.EDITOR);
if (editor != getEditor1() && editor != getEditor2()) return;
final Side side = Side.fromLeft(editor == getEditor(Side.LEFT));
final Side side = assertNotNull(Side.fromValue(getEditors(), editor));
final List<SimpleDiffChange> selectedChanges = getSelectedChanges(side);
if (selectedChanges.isEmpty()) return;
@@ -41,6 +41,7 @@ import com.intellij.openapi.actionSystem.Separator;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
@@ -243,10 +244,8 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewerEx {
super.onBeforeDocumentChange(e);
if (myDiffChanges.isEmpty()) return;
ThreeSide side = null;
if (e.getDocument() == getEditor(ThreeSide.LEFT).getDocument()) side = ThreeSide.LEFT;
if (e.getDocument() == getEditor(ThreeSide.RIGHT).getDocument()) side = ThreeSide.RIGHT;
if (e.getDocument() == getEditor(ThreeSide.BASE).getDocument()) side = ThreeSide.BASE;
List<Document> documents = ContainerUtil.map(getEditors(), Editor::getDocument);
ThreeSide side = ThreeSide.fromValue(documents, e.getDocument());
if (side == null) {
LOG.warn("Unknown document changed");
return;
@@ -338,9 +338,7 @@ public abstract class TwosideTextDiffViewer extends TwosideDiffViewer<TextEditor
private class MyOpenInEditorWithMouseAction extends OpenInEditorWithMouseAction {
@Override
protected OpenFileDescriptor getDescriptor(@NotNull Editor editor, int line) {
Side side = null;
if (editor == getEditor(Side.LEFT)) side = Side.LEFT;
if (editor == getEditor(Side.RIGHT)) side = Side.RIGHT;
Side side = Side.fromValue(getEditors(), editor);
if (side == null) return null;
int offset = editor.logicalPositionToOffset(new LogicalPosition(line, 0));