mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[merge] refactoring: move inner classes outside, move some methods, cleanup, code style
* Move Change.HighlighterHolder and Change.Side inner classes to outside since they are used a lot from the ouside of the Change class. * Move ChangeType.apply() to Change, because it is nothing to do with the ChangeType: it is the Change which is being applied. * Add some javadocs. * Cleanup.
This commit is contained in:
+42
-131
@@ -15,45 +15,70 @@
|
||||
*/
|
||||
package com.intellij.openapi.diff.impl.incrementalMerge;
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.diff.DiffBundle;
|
||||
import com.intellij.openapi.diff.impl.highlighting.FragmentSide;
|
||||
import com.intellij.openapi.diff.impl.util.GutterActionRenderer;
|
||||
import com.intellij.openapi.diff.impl.util.TextDiffType;
|
||||
import com.intellij.openapi.diff.impl.util.DocumentUtil;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
|
||||
import com.intellij.openapi.editor.markup.MarkupModel;
|
||||
import com.intellij.openapi.editor.markup.RangeHighlighter;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.ReadonlyStatusHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Represents a change in diff or merge view.
|
||||
* A change has two {@link com.intellij.openapi.diff.impl.incrementalMerge.Change.SimpleChangeSide sides} (left and right), each of them representing the text which has been changed and the original text
|
||||
* shown in the diff/merge.
|
||||
* Change can be applied, then its sides would be equal.
|
||||
*/
|
||||
public abstract class Change {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.diff.impl.incrementalMerge.Change");
|
||||
|
||||
public abstract ChangeSide getChangeSide(FragmentSide side);
|
||||
|
||||
public abstract ChangeType getType();
|
||||
|
||||
public abstract ChangeList getChangeList();
|
||||
|
||||
protected abstract void removeFromList();
|
||||
|
||||
public abstract void onRemovedFromList();
|
||||
|
||||
public abstract boolean isValid();
|
||||
|
||||
private void apply(@NotNull FragmentSide original) {
|
||||
FragmentSide targetSide = original.otherSide();
|
||||
RangeMarker originalRangeMarker = getRangeMarker(original);
|
||||
RangeMarker rangeMarker = getRangeMarker(targetSide);
|
||||
|
||||
if (originalRangeMarker != null && rangeMarker != null) {
|
||||
ChangeType.apply(getProject(), originalRangeMarker, rangeMarker);
|
||||
apply(getProject(), originalRangeMarker, rangeMarker);
|
||||
if (isValid()) {
|
||||
removeFromList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void removeFromList();
|
||||
private static void apply(@NotNull Project project, @NotNull RangeMarker original, @NotNull RangeMarker target) {
|
||||
Document document = target.getDocument();
|
||||
if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) return;
|
||||
if (DocumentUtil.isEmpty(original)) {
|
||||
int offset = target.getStartOffset();
|
||||
document.deleteString(offset, target.getEndOffset());
|
||||
}
|
||||
String text = DocumentUtil.getText(original);
|
||||
int startOffset = target.getStartOffset();
|
||||
if (DocumentUtil.isEmpty(target)) {
|
||||
document.insertString(startOffset, text);
|
||||
} else {
|
||||
document.replaceString(startOffset, target.getEndOffset(), text);
|
||||
}
|
||||
}
|
||||
|
||||
public void addMarkup(Editor[] editors) {
|
||||
LOG.assertTrue(editors.length == 2);
|
||||
@@ -71,26 +96,16 @@ public abstract class Change {
|
||||
|
||||
private Project getProject() { return getChangeList().getProject(); }
|
||||
|
||||
public abstract ChangeType.ChangeSide getChangeSide(FragmentSide side);
|
||||
|
||||
public abstract ChangeType getType();
|
||||
|
||||
public abstract ChangeList getChangeList();
|
||||
|
||||
private HighlighterHolder getHighlighterHolder(FragmentSide side) {
|
||||
private ChangeHighlighterHolder getHighlighterHolder(FragmentSide side) {
|
||||
return getChangeSide(side).getHighlighterHolder();
|
||||
}
|
||||
|
||||
private RangeMarker getRangeMarker(FragmentSide side) {
|
||||
ChangeType.ChangeSide changeSide = getChangeSide(side);
|
||||
ChangeSide changeSide = getChangeSide(side);
|
||||
LOG.assertTrue(changeSide != null);
|
||||
return changeSide.getRange();
|
||||
}
|
||||
|
||||
public abstract void onRemovedFromList();
|
||||
|
||||
public abstract boolean isValid();
|
||||
|
||||
public static void apply(final Change change, final FragmentSide fromSide) {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
public void run() {
|
||||
@@ -134,116 +149,12 @@ public abstract class Change {
|
||||
}
|
||||
}
|
||||
|
||||
protected static class HighlighterHolder implements ChangeType.MarkupHolder {
|
||||
private Editor myEditor;
|
||||
private final ArrayList<RangeHighlighter> myHighlighters = new ArrayList<RangeHighlighter>(3);
|
||||
private RangeHighlighter myMainHighlighter = null;
|
||||
private AnAction[] myActions;
|
||||
private RangeHighlighter[] myActionHighlighters = RangeHighlighter.EMPTY_ARRAY;
|
||||
|
||||
public void highlight(ChangeType.ChangeSide changeSide, Editor editor, ChangeType type) {
|
||||
LOG.assertTrue(myEditor == null || editor == myEditor);
|
||||
removeHighlighters();
|
||||
myEditor = editor;
|
||||
setHighlighter(changeSide, type);
|
||||
}
|
||||
|
||||
private MarkupModel getMarkupModel() {
|
||||
return myEditor.getMarkupModel();
|
||||
}
|
||||
|
||||
private void highlighterCreated(RangeHighlighter highlighter, TextAttributes attrs) {
|
||||
if (attrs != null) {
|
||||
highlighter.setErrorStripeMarkColor(attrs.getErrorStripeColor());
|
||||
}
|
||||
myHighlighters.add(highlighter);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public RangeHighlighter addLineHighlighter(int line, int layer, TextDiffType diffType) {
|
||||
if (myEditor.getDocument().getTextLength() == 0) return null;
|
||||
RangeHighlighter highlighter = getMarkupModel().addLineHighlighter(line, layer, null);
|
||||
highlighter.setLineSeparatorColor(diffType.getTextBackground(myEditor));
|
||||
highlighterCreated(highlighter, diffType.getTextAttributes(myEditor));
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public RangeHighlighter addRangeHighlighter(int start, int end, int layer, TextDiffType type, HighlighterTargetArea targetArea) {
|
||||
if (getMarkupModel().getDocument().getTextLength() == 0) return null;
|
||||
TextAttributes attributes = type.getTextAttributes(myEditor);
|
||||
RangeHighlighter highlighter = getMarkupModel().addRangeHighlighter(start, end, layer, attributes, targetArea);
|
||||
highlighterCreated(highlighter, attributes);
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
private void setHighlighter(ChangeType.ChangeSide changeSide, ChangeType type) {
|
||||
myMainHighlighter = type.addMarker(changeSide, this);
|
||||
updateAction();
|
||||
}
|
||||
|
||||
public Editor getEditor() {
|
||||
return myEditor;
|
||||
}
|
||||
|
||||
public void removeHighlighters() {
|
||||
if (myEditor == null) {
|
||||
LOG.assertTrue(myHighlighters.isEmpty());
|
||||
LOG.assertTrue(myMainHighlighter == null);
|
||||
return;
|
||||
}
|
||||
for (RangeHighlighter highlighter : myHighlighters) {
|
||||
highlighter.dispose();
|
||||
}
|
||||
myHighlighters.clear();
|
||||
removeActionHighlighters();
|
||||
myMainHighlighter = null;
|
||||
}
|
||||
|
||||
private void removeActionHighlighters() {
|
||||
for (RangeHighlighter actionHighlighter : myActionHighlighters) {
|
||||
actionHighlighter.dispose();
|
||||
}
|
||||
myActionHighlighters = RangeHighlighter.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
public void setActions(AnAction[] action) {
|
||||
myActions = action;
|
||||
updateAction();
|
||||
}
|
||||
|
||||
private void updateAction() {
|
||||
removeActionHighlighters();
|
||||
if (myMainHighlighter != null && myActions != null && myActions.length > 0) {
|
||||
myActionHighlighters = new RangeHighlighter[myActions.length];
|
||||
for (int i = 0; i < myActionHighlighters.length; i++) {
|
||||
RangeHighlighter highlighter = cloneMainHighlighter(myMainHighlighter);
|
||||
highlighter.setGutterIconRenderer(new GutterActionRenderer(myActions[i]));
|
||||
myActionHighlighters[i] = highlighter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RangeHighlighter cloneMainHighlighter(@NotNull RangeHighlighter mainHighlighter) {
|
||||
RangeHighlighter highlighter = myEditor.getMarkupModel().addRangeHighlighter(mainHighlighter.getStartOffset(), mainHighlighter.getEndOffset(), mainHighlighter.getLayer(),
|
||||
null, mainHighlighter.getTargetArea());
|
||||
// TODO[dyoma] copy greedyToLeft and greedyToRight
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
public void updateHighlighter(ChangeType.ChangeSide changeSide, ChangeType type) {
|
||||
LOG.assertTrue(myEditor != null);
|
||||
removeHighlighters();
|
||||
setHighlighter(changeSide, type);
|
||||
}
|
||||
}
|
||||
|
||||
protected static class Side extends ChangeType.ChangeSide {
|
||||
protected static class SimpleChangeSide extends ChangeSide {
|
||||
private final FragmentSide mySide;
|
||||
private final DiffRangeMarker myRange;
|
||||
private final HighlighterHolder myHighlighterHolder = new HighlighterHolder();
|
||||
private final ChangeHighlighterHolder myHighlighterHolder = new ChangeHighlighterHolder();
|
||||
|
||||
public Side(FragmentSide side, DiffRangeMarker rangeMarker) {
|
||||
public SimpleChangeSide(FragmentSide side, DiffRangeMarker rangeMarker) {
|
||||
mySide = side;
|
||||
myRange = rangeMarker;
|
||||
}
|
||||
@@ -256,7 +167,7 @@ public abstract class Change {
|
||||
return myRange;
|
||||
}
|
||||
|
||||
public HighlighterHolder getHighlighterHolder() {
|
||||
public ChangeHighlighterHolder getHighlighterHolder() {
|
||||
return myHighlighterHolder;
|
||||
}
|
||||
}
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.openapi.diff.impl.incrementalMerge;
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.diff.impl.util.GutterActionRenderer;
|
||||
import com.intellij.openapi.diff.impl.util.TextDiffType;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
|
||||
import com.intellij.openapi.editor.markup.MarkupModel;
|
||||
import com.intellij.openapi.editor.markup.RangeHighlighter;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Incorporates highlighting stuff of a Change.
|
||||
*
|
||||
* @author Kirill Likhodedov
|
||||
*/
|
||||
class ChangeHighlighterHolder {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance(ChangeHighlighterHolder.class);
|
||||
|
||||
private Editor myEditor;
|
||||
private final ArrayList<RangeHighlighter> myHighlighters = new ArrayList<RangeHighlighter>(3);
|
||||
private RangeHighlighter myMainHighlighter = null;
|
||||
private AnAction[] myActions;
|
||||
private RangeHighlighter[] myActionHighlighters = RangeHighlighter.EMPTY_ARRAY;
|
||||
|
||||
public void highlight(ChangeSide changeSide, Editor editor, ChangeType type) {
|
||||
LOG.assertTrue(myEditor == null || editor == myEditor);
|
||||
removeHighlighters();
|
||||
myEditor = editor;
|
||||
setHighlighter(changeSide, type);
|
||||
}
|
||||
|
||||
private MarkupModel getMarkupModel() {
|
||||
return myEditor.getMarkupModel();
|
||||
}
|
||||
|
||||
private void highlighterCreated(RangeHighlighter highlighter, TextAttributes attrs) {
|
||||
if (attrs != null) {
|
||||
highlighter.setErrorStripeMarkColor(attrs.getErrorStripeColor());
|
||||
}
|
||||
myHighlighters.add(highlighter);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public RangeHighlighter addLineHighlighter(int line, int layer, TextDiffType diffType) {
|
||||
if (myEditor.getDocument().getTextLength() == 0) return null;
|
||||
RangeHighlighter highlighter = getMarkupModel().addLineHighlighter(line, layer, null);
|
||||
highlighter.setLineSeparatorColor(diffType.getTextBackground(myEditor));
|
||||
highlighterCreated(highlighter, diffType.getTextAttributes(myEditor));
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public RangeHighlighter addRangeHighlighter(int start, int end, int layer, TextDiffType type, HighlighterTargetArea targetArea) {
|
||||
if (getMarkupModel().getDocument().getTextLength() == 0) return null;
|
||||
TextAttributes attributes = type.getTextAttributes(myEditor);
|
||||
RangeHighlighter highlighter = getMarkupModel().addRangeHighlighter(start, end, layer, attributes, targetArea);
|
||||
highlighterCreated(highlighter, attributes);
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
private void setHighlighter(ChangeSide changeSide, ChangeType type) {
|
||||
myMainHighlighter = type.addMarker(changeSide, this);
|
||||
updateAction();
|
||||
}
|
||||
|
||||
public Editor getEditor() {
|
||||
return myEditor;
|
||||
}
|
||||
|
||||
public void removeHighlighters() {
|
||||
if (myEditor == null) {
|
||||
LOG.assertTrue(myHighlighters.isEmpty());
|
||||
LOG.assertTrue(myMainHighlighter == null);
|
||||
return;
|
||||
}
|
||||
for (RangeHighlighter highlighter : myHighlighters) {
|
||||
highlighter.dispose();
|
||||
}
|
||||
myHighlighters.clear();
|
||||
removeActionHighlighters();
|
||||
myMainHighlighter = null;
|
||||
}
|
||||
|
||||
private void removeActionHighlighters() {
|
||||
for (RangeHighlighter actionHighlighter : myActionHighlighters) {
|
||||
actionHighlighter.dispose();
|
||||
}
|
||||
myActionHighlighters = RangeHighlighter.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
public void setActions(AnAction[] action) {
|
||||
myActions = action;
|
||||
updateAction();
|
||||
}
|
||||
|
||||
private void updateAction() {
|
||||
removeActionHighlighters();
|
||||
if (myMainHighlighter != null && myActions != null && myActions.length > 0) {
|
||||
myActionHighlighters = new RangeHighlighter[myActions.length];
|
||||
for (int i = 0; i < myActionHighlighters.length; i++) {
|
||||
RangeHighlighter highlighter = cloneMainHighlighter(myMainHighlighter);
|
||||
highlighter.setGutterIconRenderer(new GutterActionRenderer(myActions[i]));
|
||||
myActionHighlighters[i] = highlighter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private RangeHighlighter cloneMainHighlighter(@NotNull RangeHighlighter mainHighlighter) {
|
||||
return myEditor.getMarkupModel().addRangeHighlighter(mainHighlighter.getStartOffset(), mainHighlighter.getEndOffset(), mainHighlighter.getLayer(),
|
||||
null, mainHighlighter.getTargetArea());
|
||||
}
|
||||
|
||||
public void updateHighlighter(ChangeSide changeSide, ChangeType type) {
|
||||
LOG.assertTrue(myEditor != null);
|
||||
removeHighlighters();
|
||||
setHighlighter(changeSide, type);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.openapi.diff.impl.incrementalMerge;
|
||||
|
||||
import com.intellij.openapi.diff.impl.util.DocumentUtil;
|
||||
|
||||
/**
|
||||
* @author Kirill Likhodedov
|
||||
*/
|
||||
public abstract class ChangeSide {
|
||||
|
||||
public int getStart() {
|
||||
return getRange().getStartOffset();
|
||||
}
|
||||
|
||||
public int getStartLine() {
|
||||
return DocumentUtil.getStartLine(getRange());
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return DocumentUtil.getText(getRange());
|
||||
}
|
||||
|
||||
public int getEndLine() {
|
||||
return DocumentUtil.getEndLine(getRange());
|
||||
}
|
||||
|
||||
public abstract DiffRangeMarker getRange();
|
||||
|
||||
public abstract ChangeHighlighterHolder getHighlighterHolder();
|
||||
|
||||
public boolean contains(int offset) {
|
||||
return getStart() <= offset && offset < getEnd();
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return getRange().getEndOffset();
|
||||
}
|
||||
}
|
||||
+14
-61
@@ -34,6 +34,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.awt.*;
|
||||
|
||||
public class ChangeType {
|
||||
|
||||
private static final int LAYER = HighlighterLayer.SELECTION - 1;
|
||||
private static final ChangeType INSERT = new ChangeType(TextDiffType.INSERT);
|
||||
private static final ChangeType DELETED = new ChangeType(TextDiffType.DELETED);
|
||||
@@ -46,7 +47,7 @@ public class ChangeType {
|
||||
myDiffType = diffType;
|
||||
}
|
||||
|
||||
public RangeHighlighter addMarker(ChangeSide changeSide, MarkupHolder markup) {
|
||||
public RangeHighlighter addMarker(ChangeSide changeSide, ChangeHighlighterHolder markup) {
|
||||
String text = changeSide.getText();
|
||||
if (text != null && text.length() > 0) return addBlock(text, changeSide, markup, myDiffType);
|
||||
else return addLine(markup, changeSide.getStartLine(), myDiffType, SeparatorPlacement.TOP);
|
||||
@@ -58,16 +59,17 @@ public class ChangeType {
|
||||
|
||||
public TextDiffType getTextDiffType() { return getTypeKey(); }
|
||||
|
||||
private static RangeHighlighter addBlock(String text, ChangeSide changeSide, MarkupHolder markup, TextDiffType diffType) {
|
||||
private static RangeHighlighter addBlock(String text, ChangeSide changeSide, ChangeHighlighterHolder markup, TextDiffType diffType) {
|
||||
int length = text.length();
|
||||
int start = changeSide.getStart();
|
||||
int end = start + length;
|
||||
RangeHighlighter highlighter = markup.addRangeHighlighter(
|
||||
start, end, ChangeType.LAYER, diffType, HighlighterTargetArea.EXACT_RANGE);
|
||||
RangeHighlighter highlighter = markup.addRangeHighlighter(start, end, ChangeType.LAYER, diffType, HighlighterTargetArea.EXACT_RANGE);
|
||||
highlighter.setLineSeparatorPlacement(SeparatorPlacement.TOP);
|
||||
highlighter.setLineMarkerRenderer(LineRenderer.top());
|
||||
highlighter.setLineSeparatorColor(Color.GRAY);
|
||||
if (text.charAt(length - 1) == '\n') end--;
|
||||
if (text.charAt(length - 1) == '\n') {
|
||||
end--;
|
||||
}
|
||||
highlighter = markup.addRangeHighlighter(start, end, LAYER, TextDiffType.NONE, HighlighterTargetArea.EXACT_RANGE);
|
||||
highlighter.setLineSeparatorPlacement(SeparatorPlacement.BOTTOM);
|
||||
highlighter.setLineSeparatorColor(Color.GRAY);
|
||||
@@ -75,79 +77,30 @@ public class ChangeType {
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
private static RangeHighlighter addLine(MarkupHolder markup, int line, TextDiffType type, SeparatorPlacement placement) {
|
||||
@Nullable
|
||||
private static RangeHighlighter addLine(ChangeHighlighterHolder markup, int line, TextDiffType type, SeparatorPlacement placement) {
|
||||
RangeHighlighter highlighter = markup.addLineHighlighter(line, LAYER, type);
|
||||
if (highlighter == null) return null;
|
||||
if (highlighter == null) {
|
||||
return null;
|
||||
}
|
||||
highlighter.setLineSeparatorPlacement(placement);
|
||||
return highlighter;
|
||||
}
|
||||
|
||||
public static ChangeType fromDiffFragment(DiffFragment fragment) {
|
||||
static ChangeType fromDiffFragment(DiffFragment fragment) {
|
||||
if (fragment.getText1() == null) return INSERT;
|
||||
if (fragment.getText2() == null) return DELETED;
|
||||
return CHANGE;
|
||||
}
|
||||
|
||||
public static ChangeType fromRanges(@NotNull TextRange left, @NotNull TextRange right) {
|
||||
static ChangeType fromRanges(@NotNull TextRange left, @NotNull TextRange right) {
|
||||
if (left.getLength() == 0) return INSERT;
|
||||
if (right.getLength() == 0) return DELETED;
|
||||
return CHANGE;
|
||||
}
|
||||
|
||||
public static void apply(@NotNull Project project, @NotNull RangeMarker original, @NotNull RangeMarker target) {
|
||||
Document document = target.getDocument();
|
||||
if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) return;
|
||||
if (DocumentUtil.isEmpty(original)) {
|
||||
int offset = target.getStartOffset();
|
||||
document.deleteString(offset, target.getEndOffset());
|
||||
}
|
||||
String text = DocumentUtil.getText(original);
|
||||
int startOffset = target.getStartOffset();
|
||||
if (DocumentUtil.isEmpty(target)) {
|
||||
document.insertString(startOffset, text);
|
||||
} else {
|
||||
document.replaceString(startOffset, target.getEndOffset(), text);
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return myDiffType.getDisplayName();
|
||||
}
|
||||
|
||||
public interface MarkupHolder {
|
||||
@Nullable
|
||||
RangeHighlighter addLineHighlighter(int line, int layer, TextDiffType diffType);
|
||||
@Nullable
|
||||
RangeHighlighter addRangeHighlighter(int start, int end, int layer, TextDiffType type, HighlighterTargetArea targetArea);
|
||||
}
|
||||
|
||||
public static abstract class ChangeSide {
|
||||
public int getStart() {
|
||||
return getRange().getStartOffset();
|
||||
}
|
||||
|
||||
public int getStartLine() {
|
||||
return DocumentUtil.getStartLine(getRange());
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return DocumentUtil.getText(getRange());
|
||||
}
|
||||
|
||||
public int getEndLine() {
|
||||
return DocumentUtil.getEndLine(getRange());
|
||||
}
|
||||
|
||||
public abstract DiffRangeMarker getRange();
|
||||
|
||||
public abstract Change.HighlighterHolder getHighlighterHolder();
|
||||
|
||||
public boolean contains(int offset) {
|
||||
return getStart() <= offset && offset < getEnd();
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return getRange().getEndOffset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -25,12 +25,12 @@ import com.intellij.openapi.util.TextRange;
|
||||
*/
|
||||
class ConflictChange extends Change implements DiffRangeMarker.RangeInvalidListener {
|
||||
|
||||
private Side myOriginalSide;
|
||||
private SimpleChangeSide myOriginalSide;
|
||||
private MergeConflict myConflict;
|
||||
|
||||
public ConflictChange(MergeConflict conflict, FragmentSide mergeSide, TextRange range) {
|
||||
myConflict = conflict;
|
||||
myOriginalSide = new Side(mergeSide, new DiffRangeMarker((DocumentEx)conflict.getOriginalDocument(mergeSide), range, this));
|
||||
myOriginalSide = new SimpleChangeSide(mergeSide, new DiffRangeMarker((DocumentEx)conflict.getOriginalDocument(mergeSide), range, this));
|
||||
}
|
||||
|
||||
protected void removeFromList() {
|
||||
@@ -38,7 +38,7 @@ class ConflictChange extends Change implements DiffRangeMarker.RangeInvalidListe
|
||||
myConflict = null;
|
||||
}
|
||||
|
||||
public ChangeType.ChangeSide getChangeSide(FragmentSide side) {
|
||||
public ChangeSide getChangeSide(FragmentSide side) {
|
||||
return isBranch(side) ? myOriginalSide : myConflict;
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class ConflictChange extends Change implements DiffRangeMarker.RangeInvalidListe
|
||||
return MergeList.BRANCH_SIDE == side;
|
||||
}
|
||||
|
||||
public Change.Side getOriginalSide() {
|
||||
public SimpleChangeSide getOriginalSide() {
|
||||
return myOriginalSide;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -24,13 +24,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
/**
|
||||
* Represents a merge conflict, i.e. two {@link ConflictChange conflicting changes}, one from left, another from right.
|
||||
*/
|
||||
class MergeConflict extends ChangeType.ChangeSide implements DiffRangeMarker.RangeInvalidListener {
|
||||
class MergeConflict extends ChangeSide implements DiffRangeMarker.RangeInvalidListener {
|
||||
|
||||
@NotNull private final MergeList myMergeList;
|
||||
@NotNull private final DiffRangeMarker myCommonRange;
|
||||
@NotNull private final ConflictChange myLeftChange;
|
||||
@NotNull private final ConflictChange myRightChange;
|
||||
@NotNull private final Change.HighlighterHolder myCommonHighlighterHolder = new Change.HighlighterHolder();
|
||||
@NotNull private final ChangeHighlighterHolder myCommonHighlighterHolder = new ChangeHighlighterHolder();
|
||||
|
||||
MergeConflict(TextRange commonRange, MergeList mergeList, TextRange leftMarker, TextRange rightMarker) {
|
||||
myCommonRange = new DiffRangeMarker((DocumentEx)mergeList.getBaseDocument(),commonRange, this);
|
||||
@@ -39,7 +39,7 @@ class MergeConflict extends ChangeType.ChangeSide implements DiffRangeMarker.Ran
|
||||
myRightChange = new ConflictChange(this, FragmentSide.SIDE2, rightMarker);
|
||||
}
|
||||
|
||||
public Change.HighlighterHolder getHighlighterHolder() {
|
||||
public ChangeHighlighterHolder getHighlighterHolder() {
|
||||
return myCommonHighlighterHolder;
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -24,25 +24,25 @@ import org.jetbrains.annotations.NotNull;
|
||||
class SimpleChange extends Change implements DiffRangeMarker.RangeInvalidListener{
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.diff.impl.incrementalMerge.Change");
|
||||
private final ChangeType myType;
|
||||
private final Side[] mySides;
|
||||
private final SimpleChangeSide[] mySides;
|
||||
private final ChangeList myChangeList;
|
||||
|
||||
public SimpleChange(ChangeType type, @NotNull TextRange range1, @NotNull TextRange range2, ChangeList changeList) {
|
||||
mySides = new Side[]{createSide(changeList, range1, FragmentSide.SIDE1),
|
||||
mySides = new SimpleChangeSide[]{createSide(changeList, range1, FragmentSide.SIDE1),
|
||||
createSide(changeList, range2, FragmentSide.SIDE2)};
|
||||
myType = type;
|
||||
myChangeList = changeList;
|
||||
}
|
||||
|
||||
private Change.Side createSide(ChangeList changeList, TextRange range1, FragmentSide side) {
|
||||
return new Change.Side(side, new DiffRangeMarker((DocumentEx)changeList.getDocument(side), range1, this));
|
||||
private SimpleChangeSide createSide(ChangeList changeList, TextRange range1, FragmentSide side) {
|
||||
return new SimpleChangeSide(side, new DiffRangeMarker((DocumentEx)changeList.getDocument(side), range1, this));
|
||||
}
|
||||
|
||||
protected void removeFromList() {
|
||||
myChangeList.remove(this);
|
||||
}
|
||||
|
||||
public ChangeType.ChangeSide getChangeSide(FragmentSide side) {
|
||||
public ChangeSide getChangeSide(FragmentSide side) {
|
||||
return mySides[side.getIndex()];
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ class SimpleChange extends Change implements DiffRangeMarker.RangeInvalidListene
|
||||
|
||||
public void onRemovedFromList() {
|
||||
for (int i = 0; i < mySides.length; i++) {
|
||||
Change.Side side = mySides[i];
|
||||
SimpleChangeSide side = mySides[i];
|
||||
side.getRange().removeListener(this);
|
||||
side.getHighlighterHolder().removeHighlighters();
|
||||
mySides[i] = null;
|
||||
|
||||
Reference in New Issue
Block a user