mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
diff: cleanup
* add contract for select() * use selectNotNull only for explicit assertions
This commit is contained in:
@@ -18,6 +18,7 @@ package com.intellij.diff.util;
|
||||
import com.intellij.diff.fragments.DiffFragment;
|
||||
import com.intellij.diff.fragments.LineFragment;
|
||||
import com.intellij.openapi.util.Couple;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -65,6 +66,7 @@ public enum Side {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Contract("!null, !null -> !null; null, null -> null")
|
||||
public <T> T select(@Nullable T left, @Nullable T right) {
|
||||
return isLeft() ? left : right;
|
||||
}
|
||||
@@ -79,7 +81,6 @@ public enum Side {
|
||||
return array[myIndex];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@NotNull T[] array) {
|
||||
assert array.length == 2;
|
||||
return array[myIndex];
|
||||
@@ -91,7 +92,6 @@ public enum Side {
|
||||
return array[myIndex];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@NotNull List<T> list) {
|
||||
assert list.size() == 2;
|
||||
return list.get(myIndex);
|
||||
@@ -103,7 +103,6 @@ public enum Side {
|
||||
return list.get(myIndex);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@NotNull Couple<T> region) {
|
||||
return isLeft() ? region.first : region.second;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.diff.util;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -40,10 +41,12 @@ public enum ThreeSide {
|
||||
//
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@Nullable T left, @NotNull T base, @Nullable T right) {
|
||||
@Contract("!null, !null, !null -> !null; null, null, null -> null")
|
||||
public <T> T select(@Nullable T left, @Nullable T base, @Nullable T right) {
|
||||
if (myIndex == 0) return left;
|
||||
if (myIndex == 1) return base;
|
||||
if (myIndex == 2) return right;
|
||||
//noinspection Contract
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
@@ -60,7 +63,6 @@ public enum ThreeSide {
|
||||
return array[myIndex];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@NotNull T[] array) {
|
||||
assert array.length == 3;
|
||||
return array[myIndex];
|
||||
@@ -72,7 +74,6 @@ public enum ThreeSide {
|
||||
return array[myIndex];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public <T> T select(@NotNull List<T> list) {
|
||||
assert list.size() == 3;
|
||||
return list.get(myIndex);
|
||||
|
||||
@@ -241,7 +241,7 @@ public class MergeUtil {
|
||||
|
||||
@NotNull
|
||||
private Interval getRange(@NotNull ThreeSide side) {
|
||||
return side.selectNotNull(myLeft, myBase, myRight);
|
||||
return side.select(myLeft, myBase, myRight);
|
||||
}
|
||||
|
||||
public int getStartLine(@NotNull ThreeSide side) {
|
||||
|
||||
+1
-1
@@ -69,7 +69,7 @@ public class OnesideEditorHighlighter implements EditorHighlighter {
|
||||
offset = base.getStartOffset();
|
||||
}
|
||||
|
||||
HighlighterIterator it = range.getSide().selectNotNull(it1, it2);
|
||||
HighlighterIterator it = range.getSide().select(it1, it2);
|
||||
while (!it.atEnd() && changed.getStartOffset() >= it.getEnd()) {
|
||||
it.advance();
|
||||
}
|
||||
|
||||
+1
-1
@@ -145,7 +145,7 @@ public class OnesideFragmentBuilder {
|
||||
}
|
||||
|
||||
private void appendText(@NotNull Side side, int offset1, int offset2, int lines, int startLine1, int startLine2) {
|
||||
Document document = side.selectNotNull(myDocument1, myDocument2);
|
||||
Document document = side.select(myDocument1, myDocument2);
|
||||
|
||||
int newline = document.getTextLength() > offset2 + 1 ? 1 : 0;
|
||||
TextRange base = new TextRange(myBuilder.length(), myBuilder.length() + offset2 - offset1 + newline);
|
||||
|
||||
@@ -231,9 +231,9 @@ public class SimpleDiffChange {
|
||||
final Document document1 = myEditor1.getDocument();
|
||||
final Document document2 = myEditor2.getDocument();
|
||||
|
||||
DiffUtil.applyModification(sourceSide.other().selectNotNull(document1, document2),
|
||||
DiffUtil.applyModification(sourceSide.other().select(document1, document2),
|
||||
getStartLine(sourceSide.other()), getEndLine(sourceSide.other()),
|
||||
sourceSide.selectNotNull(document1, document2),
|
||||
sourceSide.select(document1, document2),
|
||||
getStartLine(sourceSide), getEndLine(sourceSide));
|
||||
|
||||
destroyHighlighter();
|
||||
@@ -249,9 +249,9 @@ public class SimpleDiffChange {
|
||||
final Document document1 = myEditor1.getDocument();
|
||||
final Document document2 = myEditor2.getDocument();
|
||||
|
||||
DiffUtil.applyModification(sourceSide.other().selectNotNull(document1, document2),
|
||||
DiffUtil.applyModification(sourceSide.other().select(document1, document2),
|
||||
getEndLine(sourceSide.other()), getEndLine(sourceSide.other()),
|
||||
sourceSide.selectNotNull(document1, document2),
|
||||
sourceSide.select(document1, document2),
|
||||
getStartLine(sourceSide), getEndLine(sourceSide));
|
||||
|
||||
destroyHighlighter();
|
||||
@@ -265,7 +265,7 @@ public class SimpleDiffChange {
|
||||
private MyGutterOperation createOperation(@NotNull Side side) {
|
||||
assert myEditor1 != null && myEditor2 != null;
|
||||
int offset = side.getStartOffset(myFragment);
|
||||
EditorEx editor = side.selectNotNull(myEditor1, myEditor2);
|
||||
EditorEx editor = side.select(myEditor1, myEditor2);
|
||||
RangeHighlighter highlighter = editor.getMarkupModel().addRangeHighlighter(offset, offset,
|
||||
HighlighterLayer.ADDITIONAL_SYNTAX,
|
||||
null,
|
||||
@@ -310,8 +310,8 @@ public class SimpleDiffChange {
|
||||
myCtrlPressed = myViewer.getModifierProvider().isCtrlPressed();
|
||||
myShiftPressed = myViewer.getModifierProvider().isShiftPressed();
|
||||
|
||||
boolean isEditable = DiffUtil.isEditable(mySide.selectNotNull(myEditor1, myEditor2));
|
||||
boolean isOtherEditable = DiffUtil.isEditable(mySide.other().selectNotNull(myEditor1, myEditor2));
|
||||
boolean isEditable = DiffUtil.isEditable(mySide.select(myEditor1, myEditor2));
|
||||
boolean isOtherEditable = DiffUtil.isEditable(mySide.other().select(myEditor1, myEditor2));
|
||||
|
||||
if (myCtrlPressed && myShiftPressed) return null;
|
||||
if ((myShiftPressed || !isOtherEditable) && isEditable) {
|
||||
@@ -357,7 +357,7 @@ public class SimpleDiffChange {
|
||||
@Nullable
|
||||
private GutterIconRenderer createIconRenderer(@NotNull final Side sourceSide, @NotNull final Icon icon, @NotNull final Runnable perform) {
|
||||
assert myEditor1 != null && myEditor2 != null;
|
||||
if (!DiffUtil.isEditable(sourceSide.other().selectNotNull(myEditor1, myEditor2))) return null;
|
||||
if (!DiffUtil.isEditable(sourceSide.other().select(myEditor1, myEditor2))) return null;
|
||||
return new GutterIconRenderer() {
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -381,7 +381,7 @@ public class SimpleDiffChange {
|
||||
|
||||
if (!myIsValid) return;
|
||||
|
||||
DiffUtil.executeWriteCommand(sourceSide.other().selectNotNull(document1, document2), project, "Replace change", new Runnable() {
|
||||
DiffUtil.executeWriteCommand(sourceSide.other().select(document1, document2), project, "Replace change", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
perform.run();
|
||||
|
||||
@@ -586,7 +586,7 @@ public class SimpleDiffViewer extends TwosideTextDiffViewer {
|
||||
return;
|
||||
}
|
||||
|
||||
Editor modifiedEditor = side.other(myModifyOpposite).selectNotNull(myEditor1, myEditor2);
|
||||
Editor modifiedEditor = side.other(myModifyOpposite).select(myEditor1, myEditor2);
|
||||
if (!DiffUtil.isEditable(modifiedEditor)) {
|
||||
e.getPresentation().setEnabledAndVisible(false);
|
||||
return;
|
||||
@@ -604,7 +604,7 @@ public class SimpleDiffViewer extends TwosideTextDiffViewer {
|
||||
final Side side = Side.fromLeft(editor == myEditor1);
|
||||
final List<SimpleDiffChange> selectedChanges = getSelectedChanges(side);
|
||||
|
||||
Editor modifiedEditor = side.other(myModifyOpposite).selectNotNull(myEditor1, myEditor2);
|
||||
Editor modifiedEditor = side.other(myModifyOpposite).select(myEditor1, myEditor2);
|
||||
String title = e.getPresentation().getText() + " selected changes";
|
||||
DiffUtil.executeWriteCommand(modifiedEditor.getDocument(), e.getProject(), title, new Runnable() {
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ public class SimpleThreesideDiffChange {
|
||||
//
|
||||
|
||||
private void createHighlighter(@NotNull ThreeSide side) {
|
||||
Editor editor = side.selectNotNull(myEditors);
|
||||
Editor editor = side.select(myEditors);
|
||||
Document document = editor.getDocument();
|
||||
|
||||
TextDiffType type = getDiffType();
|
||||
@@ -208,7 +208,7 @@ public class SimpleThreesideDiffChange {
|
||||
private static CharSequence getRangeContent(@NotNull MergeLineFragment fragment,
|
||||
@NotNull List<EditorEx> editors,
|
||||
@NotNull ThreeSide side) {
|
||||
DocumentEx document = side.selectNotNull(editors).getDocument();
|
||||
DocumentEx document = side.select(editors).getDocument();
|
||||
int line1 = fragment.getStartLine(side);
|
||||
int line2 = fragment.getEndLine(side);
|
||||
return DiffUtil.getLinesContent(document, line1, line2);
|
||||
|
||||
+9
-14
@@ -15,9 +15,6 @@
|
||||
*/
|
||||
package com.intellij.diff.tools.simple;
|
||||
|
||||
import com.intellij.codeInsight.hint.HintManager;
|
||||
import com.intellij.codeInsight.hint.HintManagerImpl;
|
||||
import com.intellij.codeInsight.hint.HintUtil;
|
||||
import com.intellij.diff.DiffContext;
|
||||
import com.intellij.diff.comparison.ByLine;
|
||||
import com.intellij.diff.comparison.ComparisonPolicy;
|
||||
@@ -51,7 +48,6 @@ import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.UserDataHolder;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.ui.LightweightHint;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.ui.ButtonlessScrollBarUI;
|
||||
@@ -380,7 +376,7 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewer {
|
||||
@NotNull
|
||||
@Override
|
||||
protected SyncScrollSupport.SyncScrollable getSyncScrollable(@NotNull Side side) {
|
||||
return side.selectNotNull(mySyncScrollable1, mySyncScrollable2);
|
||||
return side.select(mySyncScrollable1, mySyncScrollable2);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -527,16 +523,15 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewer {
|
||||
|
||||
@Override
|
||||
protected void processHelper(@NotNull ScrollHelper helper) {
|
||||
ThreeSide left = mySide.selectNotNull(ThreeSide.LEFT, ThreeSide.BASE);
|
||||
ThreeSide right = mySide.selectNotNull(ThreeSide.BASE, ThreeSide.RIGHT);
|
||||
ThreeSide left = mySide.select(ThreeSide.LEFT, ThreeSide.BASE);
|
||||
ThreeSide right = mySide.select(ThreeSide.BASE, ThreeSide.RIGHT);
|
||||
|
||||
if (!helper.process(0, 0)) return;
|
||||
for (SimpleThreesideDiffChange diffChange : myDiffChanges) {
|
||||
if (!helper.process(diffChange.getStartLine(left), diffChange.getStartLine(right))) return;
|
||||
if (!helper.process(diffChange.getEndLine(left), diffChange.getEndLine(right))) return;
|
||||
}
|
||||
helper
|
||||
.process(left.selectNotNull(myEditors).getDocument().getLineCount(), right.selectNotNull(myEditors).getDocument().getLineCount());
|
||||
helper.process(left.select(myEditors).getDocument().getLineCount(), right.select(myEditors).getDocument().getLineCount());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -549,8 +544,8 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewer {
|
||||
|
||||
@Override
|
||||
public void process(@NotNull Handler handler) {
|
||||
ThreeSide left = mySide.selectNotNull(ThreeSide.LEFT, ThreeSide.BASE);
|
||||
ThreeSide right = mySide.selectNotNull(ThreeSide.BASE, ThreeSide.RIGHT);
|
||||
ThreeSide left = mySide.select(ThreeSide.LEFT, ThreeSide.BASE);
|
||||
ThreeSide right = mySide.select(ThreeSide.BASE, ThreeSide.RIGHT);
|
||||
|
||||
for (SimpleThreesideDiffChange diffChange : myDiffChanges) {
|
||||
if (!diffChange.getType().isChange(mySide)) continue;
|
||||
@@ -576,8 +571,8 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewer {
|
||||
public void paint(@NotNull Graphics g, @NotNull Component divider) {
|
||||
Graphics2D gg = getDividerGraphics(g, divider);
|
||||
|
||||
Editor editor1 = mySide.selectNotNull(myEditors.get(0), myEditors.get(1));
|
||||
Editor editor2 = mySide.selectNotNull(myEditors.get(1), myEditors.get(2));
|
||||
Editor editor1 = mySide.select(myEditors.get(0), myEditors.get(1));
|
||||
Editor editor2 = mySide.select(myEditors.get(1), myEditors.get(2));
|
||||
|
||||
//DividerPolygonUtil.paintSimplePolygons(gg, divider.getWidth(), editor1, editor2, myPaintable);
|
||||
DiffDividerDrawUtil.paintPolygons(gg, divider.getWidth(), editor1, editor2, myPaintable);
|
||||
@@ -659,7 +654,7 @@ public class SimpleThreesideDiffViewer extends ThreesideTextDiffViewer {
|
||||
}
|
||||
|
||||
public void paintOnDivider(@NotNull Graphics2D gg, @NotNull Component divider, @NotNull Side side) {
|
||||
MyPaintable paintable = side.selectNotNull(myPaintable1, myPaintable2);
|
||||
MyPaintable paintable = side.select(myPaintable1, myPaintable2);
|
||||
paintable.paintOnDivider(gg, divider);
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -256,7 +256,7 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
|
||||
|
||||
@NotNull
|
||||
public EditorEx getCurrentEditor() {
|
||||
return myCurrentSide.selectNotNull(myEditors);
|
||||
return myCurrentSide.select(myEditors);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -276,7 +276,7 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
|
||||
|
||||
@CalledInAwt
|
||||
protected void scrollToLine(@NotNull ThreeSide side, int line) {
|
||||
Editor editor = side.selectNotNull(myEditors);
|
||||
Editor editor = side.select(myEditors);
|
||||
DiffUtil.scrollEditor(editor, line);
|
||||
myCurrentSide = side;
|
||||
}
|
||||
@@ -303,7 +303,7 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
|
||||
protected OpenFileDescriptor getOpenFileDescriptor() {
|
||||
EditorEx editor = getCurrentEditor();
|
||||
|
||||
DocumentContent content = getCurrentSide().selectNotNull(myActualContents);
|
||||
DocumentContent content = getCurrentSide().select(myActualContents);
|
||||
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
return content.getOpenFileDescriptor(offset);
|
||||
@@ -365,8 +365,9 @@ public abstract class ThreesideTextDiffViewer extends TextDiffViewerBase {
|
||||
List<DiffContent> contents = myRequest.getContents();
|
||||
List<String> titles = myRequest.getContentTitles();
|
||||
|
||||
DiffRequest request = new SimpleDiffRequest(myRequest.getTitle(), mySide1.selectNotNull(contents), mySide2.selectNotNull(contents),
|
||||
mySide1.selectNotNull(titles), mySide1.selectNotNull(titles));
|
||||
DiffRequest request = new SimpleDiffRequest(myRequest.getTitle(),
|
||||
mySide1.select(contents), mySide2.select(contents),
|
||||
mySide1.select(titles), mySide1.select(titles));
|
||||
DiffManager.getInstance().showDiff(myProject, request, new DiffDialogHints(null, myPanel));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user