[diff] cleanup: nullable, notnull, fix warnings

This commit is contained in:
Kirill Likhodedov
2012-10-31 15:06:56 +04:00
parent 0ef12d6db0
commit d241dfbd42
3 changed files with 71 additions and 36 deletions
@@ -35,6 +35,7 @@ import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.util.TextRange;
import com.intellij.util.diff.FilesTooBigForDiffException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
@@ -109,6 +110,7 @@ public class ChangesDiffCalculator implements Disposable {
return null;
}
@Nullable
@Override
public Document getDocument() {
return document;
@@ -123,7 +125,7 @@ public class ChangesDiffCalculator implements Disposable {
}
@Override
public void highlightText(Fragment fragment, GutterIconRenderer gutterIconRenderer) {
public void highlightText(@NotNull Fragment fragment, GutterIconRenderer gutterIconRenderer) {
}
@@ -143,7 +145,7 @@ public class ChangesDiffCalculator implements Disposable {
}
@Override
public void highlightText(Fragment fragment, GutterIconRenderer gutterIconRenderer) {
public void highlightText(@NotNull Fragment fragment, GutterIconRenderer gutterIconRenderer) {
TextRange currentRange = fragment.getRange(FragmentSide.SIDE2);
if (INTERESTED_DIFF_TYPES.contains(fragment.getType())) {
ranges.add(currentRange);
@@ -29,6 +29,7 @@ import com.intellij.openapi.diff.impl.util.TextDiffTypeEnum;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.markup.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
@@ -36,6 +37,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.util.Consumer;
import com.intellij.util.containers.HashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.util.ArrayList;
@@ -43,42 +45,49 @@ import java.util.Collection;
import java.util.List;
public abstract class DiffMarkup implements EditorSource, Disposable {
private static final Logger LOG = Logger.getInstance(
"#com.intellij.openapi.diff.impl.highlighting.EditorTextAppender");
private static final Logger LOG = Logger.getInstance(DiffMarkup.class);
private static final int LAYER = HighlighterLayer.SELECTION - 1;
private final ArrayList<RangeHighlighter> myExtraHighLighters = new ArrayList<RangeHighlighter>();
private final ArrayList<RangeHighlighter> myHighLighters = new ArrayList<RangeHighlighter>();
private final HashSet<RangeHighlighter> myActionHighlighters = new HashSet<RangeHighlighter>();
private final Project myProject;
@Nullable private final Project myProject;
private final List<Disposable> myDisposables = new ArrayList<Disposable>();
private boolean myDisposed = false;
protected DiffMarkup(Project project, @NotNull Disposable parentDisposable) {
protected DiffMarkup(@Nullable Project project, @NotNull Disposable parentDisposable) {
myProject = project;
Disposer.register(parentDisposable, this);
}
@Nullable
private MarkupModel getMarkupModel() {
Editor editor = getEditor();
return editor == null ? null : editor.getMarkupModel();
}
public void highlightText(Fragment fragment, GutterIconRenderer gutterIconRenderer) {
final TextDiffTypeEnum diffTypeEnum = fragment.getType();
if (diffTypeEnum == null) return;
public void highlightText(@NotNull Fragment fragment, @Nullable GutterIconRenderer gutterIconRenderer) {
MarkupModel markupModel = getMarkupModel();
TextDiffTypeEnum diffTypeEnum = fragment.getType();
if (diffTypeEnum == null || markupModel == null) {
return;
}
TextDiffType type = TextDiffType.create(diffTypeEnum);
if (type == null) return;
TextRange range = fragment.getRange(getSide());
TextAttributes attributes = type.getTextAttributes(getEditor());
if (attributes == null) {
return;
}
RangeHighlighter rangeMarker;
if (range.getLength() == 0) {
TextAttributes textAttributes = new TextAttributes(null, null, attributes.getBackgroundColor(), EffectType.BOXED, Font.PLAIN);
rangeMarker = getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getStartOffset(), LAYER, textAttributes, HighlighterTargetArea.EXACT_RANGE);
rangeMarker = markupModel
.addRangeHighlighter(range.getStartOffset(), range.getStartOffset(), LAYER, textAttributes, HighlighterTargetArea.EXACT_RANGE);
}
else {
rangeMarker = getMarkupModel().addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), LAYER,
attributes, HighlighterTargetArea.EXACT_RANGE);
rangeMarker = markupModel.addRangeHighlighter(range.getStartOffset(), range.getEndOffset(), LAYER,
attributes, HighlighterTargetArea.EXACT_RANGE);
}
if (gutterIconRenderer != null) {
rangeMarker.setGutterIconRenderer(gutterIconRenderer);
@@ -94,29 +103,41 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
saveHighlighter(rangeMarker);
}
public void addLineMarker(int line, TextAttributesKey type) {
public void addLineMarker(int line, @Nullable TextAttributesKey type) {
RangeHighlighter marker = createLineMarker(type, line);
if (marker == null) return;
saveHighlighter(marker);
}
void setSeparatorMarker(int line, Consumer<Integer> consumer) {
RangeHighlighter marker = getMarkupModel().addLineHighlighter(line, LAYER, null);
EditorEx editor = getEditor();
MarkupModel markupModel = getMarkupModel();
if (editor == null || markupModel == null) {
return;
}
RangeHighlighter marker = markupModel.addLineHighlighter(line, LAYER, null);
marker.setLineSeparatorPlacement(SeparatorPlacement.TOP);
final FragmentBoundRenderer renderer = new FragmentBoundRenderer(getEditor().getLineHeight(), getEditor(), consumer);
final FragmentBoundRenderer renderer = new FragmentBoundRenderer(editor.getLineHeight(), editor, consumer);
marker.setLineSeparatorColor(renderer.getColor());
marker.setLineSeparatorRenderer(renderer);
marker.setLineMarkerRenderer(renderer);
myExtraHighLighters.add(marker);
}
private RangeHighlighter createLineMarker(TextAttributesKey type, int line) {
@Nullable
private RangeHighlighter createLineMarker(@Nullable TextAttributesKey type, int line) {
MarkupModel markupModel = getMarkupModel();
Document document = getDocument();
if (markupModel == null || document == null) {
return null;
}
Color color = getLineSeparatorColorForType(type);
RangeHighlighter lastHighlighter = getLastHighlighter();
if (lastHighlighter != null &&
lastHighlighter.getTargetArea() == HighlighterTargetArea.LINES_IN_RANGE &&
SeparatorPlacement.BOTTOM == lastHighlighter.getLineSeparatorPlacement()) {
int lastLine = getDocument().getLineNumber(lastHighlighter.getStartOffset());
int lastLine = document.getLineNumber(lastHighlighter.getStartOffset());
LOG.assertTrue(lastLine <= line);
if (lastLine == line) {
Color lastLineColor = lastHighlighter.getLineSeparatorColor();
@@ -128,27 +149,27 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
}
}
}
RangeHighlighter marker = getMarkupModel().addLineHighlighter(line, LAYER, null);
// saveHighlighter(marker);
RangeHighlighter marker = markupModel.addLineHighlighter(line, LAYER, null);
marker.setLineSeparatorColor(color);
marker.setLineSeparatorPlacement(SeparatorPlacement.BOTTOM);
// TODO[dyoma] type should be policy
if (type == DiffColors.DIFF_DELETED) marker.setErrorStripeMarkColor(color);
return marker;
}
private void removeHighlighter(RangeHighlighter highlighter) {
private void removeHighlighter(@NotNull RangeHighlighter highlighter) {
highlighter.dispose();
myHighLighters.remove(highlighter);
myActionHighlighters.remove(highlighter);
}
private Color getLineSeparatorColorForType(TextAttributesKey type) {
@Nullable
private Color getLineSeparatorColorForType(@Nullable TextAttributesKey type) {
LOG.assertTrue(type == DiffColors.DIFF_DELETED || type == DiffColors.DIFF_MODIFIED || type == null);
if (type == null || type == DiffColors.DIFF_MODIFIED) return Color.GRAY;
return TextDiffType.DELETED.getTextBackground(getEditor());
}
@Nullable
private RangeHighlighter getLastHighlighter() {
int size = myHighLighters.size();
return size > 0 ? myHighLighters.get(size - 1) : null;
@@ -158,24 +179,28 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
myHighLighters.add(marker);
}
@Nullable
public Document getDocument() {
return getEditor().getDocument();
EditorEx editor = getEditor();
return editor == null ? null : editor.getDocument();
}
public void addAction(final MergeOperations.Operation operation, int lineStartOffset) {
public void addAction(@Nullable MergeOperations.Operation operation, int lineStartOffset) {
RangeHighlighter highlighter = createAction(operation, lineStartOffset);
if (highlighter != null) {
myActionHighlighters.add(highlighter);
}
}
private RangeHighlighter createAction(final MergeOperations.Operation operation, int lineStartOffset) {
if (operation == null) return null;
RangeHighlighter highlighter =
getMarkupModel().addRangeHighlighter(lineStartOffset, lineStartOffset,
HighlighterLayer.ADDITIONAL_SYNTAX,
new TextAttributes(null, null, null, null, Font.PLAIN),
HighlighterTargetArea.LINES_IN_RANGE);
@Nullable
private RangeHighlighter createAction(@Nullable MergeOperations.Operation operation, int lineStartOffset) {
MarkupModel markupModel = getMarkupModel();
if (operation == null || markupModel == null) {
return null;
}
RangeHighlighter highlighter = markupModel.addRangeHighlighter(lineStartOffset, lineStartOffset, HighlighterLayer.ADDITIONAL_SYNTAX,
new TextAttributes(null, null, null, null, Font.PLAIN),
HighlighterTargetArea.LINES_IN_RANGE);
final MergeActionGroup.OperationAction action = new MergeActionGroup.OperationAction(operation);
highlighter.setGutterIconRenderer(new GutterActionRenderer(action));
return highlighter;
@@ -190,7 +215,7 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
myExtraHighLighters.clear();
}
private void removeHighlighters(Collection<RangeHighlighter> highlighters) {
private void removeHighlighters(@NotNull Collection<RangeHighlighter> highlighters) {
MarkupModel markupModel = getMarkupModel();
if (markupModel != null) {
for (RangeHighlighter highlighter : highlighters) {
@@ -200,6 +225,7 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
highlighters.clear();
}
@Nullable
protected Project getProject() {
return myProject;
}
@@ -217,14 +243,20 @@ public abstract class DiffMarkup implements EditorSource, Disposable {
myDisposables.add(disposable);
}
@Nullable
public String getText() {
return getDocument().getText();
Document document = getDocument();
return document == null ? null : document.getText();
}
protected final boolean isDisposed() { return myDisposed; }
protected final boolean isDisposed() {
return myDisposed;
}
public final void dispose() {
if (isDisposed()) return;
if (isDisposed()) {
return;
}
onDisposed();
myDisposed = true;
}
@@ -56,6 +56,7 @@ public class TextDiffType implements DiffStatusBar.LegendTypeDescriptor {
return myApplied;
}
@NotNull
public static TextDiffType create(@Nullable final TextDiffTypeEnum type) {
if (TextDiffTypeEnum.INSERT.equals(type)) {
return INSERT;