Skip to content

Commit

Permalink
IDEA-148560 diff: allow to change separator color
Browse files Browse the repository at this point in the history
  • Loading branch information
AMPivovarov committed Aug 28, 2017
1 parent bd77343 commit f8de2a5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ private static void paintConnectorLine(@NotNull Graphics g,
// Parameters
//

private static final ColorKey BACKGROUND = ColorKey.createColorKey("DIFF_SEPARATORS_BACKGROUND");
private static final ColorKey TOP_BORDER = ColorKey.createColorKey("DIFF_SEPARATORS_TOP_BORDER");
private static final ColorKey BOTTOM_BORDER = ColorKey.createColorKey("DIFF_SEPARATORS_BOTTOM_BORDER");
public static final ColorKey BACKGROUND = ColorKey.createColorKey("DIFF_SEPARATORS_BACKGROUND");
public static final ColorKey TOP_BORDER = ColorKey.createColorKey("DIFF_SEPARATORS_TOP_BORDER");
public static final ColorKey BOTTOM_BORDER = ColorKey.createColorKey("DIFF_SEPARATORS_BOTTOM_BORDER");

private static int getStepSize(int lineHeight) {
return Math.max(lineHeight / 3, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class DiffColorSettingsTest : HeavyDiffTestCase() {
assertContainsMarkerColor(viewer, TextDiffType.INSERTED)
assertContainsMarkerColor(viewer, TextDiffType.DELETED)
assertContainsMarkerColor(viewer, TextDiffType.CONFLICT)

assertContainsFoldedFragment(viewer)
}
finally {
panel?.disposeUIResources()
Expand Down Expand Up @@ -82,4 +84,10 @@ class DiffColorSettingsTest : HeavyDiffTestCase() {
}
assertTrue(ranges.isNotEmpty())
}

private fun assertContainsFoldedFragment(viewer: SimpleThreesideDiffViewer) {
assertTrue(viewer.editors.all {
it.foldingModel.allFoldRegions.any { !it.isExpanded }
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package com.intellij.openapi.diff.impl.settings;

import com.intellij.application.options.colors.*;
import com.intellij.diff.util.DiffLineSeparatorRenderer;
import com.intellij.diff.util.TextDiffTypeFactory;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.options.OptionsBundle;
import com.intellij.openapi.options.colors.AttributesDescriptor;
import com.intellij.openapi.options.colors.ColorAndFontDescriptorsProvider;
import com.intellij.openapi.options.colors.ColorDescriptor;
Expand All @@ -27,6 +29,7 @@
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class DiffColorsPageFactory implements ColorAndFontPanelFactory, ColorAndFontDescriptorsProvider, DisplayPrioritySortable {
Expand All @@ -36,7 +39,11 @@ public class DiffColorsPageFactory implements ColorAndFontPanelFactory, ColorAnd
@NotNull
public NewColorAndFontPanel createPanel(@NotNull ColorAndFontOptions options) {
final SchemesPanel schemesPanel = new SchemesPanel(options);
final DiffColorDescriptionPanel descriptionPanel = new DiffColorDescriptionPanel(options);

CompositeColorDescriptionPanel descriptionPanel = new CompositeColorDescriptionPanel();
descriptionPanel.addDescriptionPanel(new ColorAndFontDescriptionPanel(), it -> it instanceof ColorAndFontDescription);
descriptionPanel.addDescriptionPanel(new DiffColorDescriptionPanel(options), it -> it instanceof TextAttributesDescription);

final OptionsPanelImpl optionsPanel = new OptionsPanelImpl(options, schemesPanel, DIFF_GROUP, descriptionPanel);
final DiffPreviewPanel previewPanel = new DiffPreviewPanel();

Expand All @@ -55,14 +62,22 @@ public void schemeChanged(final Object source) {
@Override
public AttributesDescriptor[] getAttributeDescriptors() {
TextDiffTypeFactory.TextDiffTypeImpl[] diffTypes = TextDiffTypeFactory.getInstance().getAllDiffTypes();
List<AttributesDescriptor> attributes = ContainerUtil.map(diffTypes, (type) -> new AttributesDescriptor(type.getName(), type.getKey()));
List<AttributesDescriptor> attributes = ContainerUtil.map(diffTypes, type -> {
return new AttributesDescriptor(OptionsBundle.message("options.general.color.descriptor.vcs.diff.type.tag.prefix") + type.getName(), type.getKey());
});
return ArrayUtil.toObjectArray(attributes, AttributesDescriptor.class);
}

@NotNull
@Override
public ColorDescriptor[] getColorDescriptors() {
return ColorDescriptor.EMPTY_ARRAY;
List<ColorDescriptor> descriptors = new ArrayList<>();

descriptors.add(new ColorDescriptor(OptionsBundle.message("options.general.color.descriptor.vcs.diff.separator.background"), DiffLineSeparatorRenderer.BACKGROUND, ColorDescriptor.Kind.BACKGROUND));
descriptors.add(new ColorDescriptor(OptionsBundle.message("options.general.color.descriptor.vcs.diff.separator.top_border"), DiffLineSeparatorRenderer.TOP_BORDER, ColorDescriptor.Kind.BACKGROUND));
descriptors.add(new ColorDescriptor(OptionsBundle.message("options.general.color.descriptor.vcs.diff.separator.bottom_border"), DiffLineSeparatorRenderer.BOTTOM_BORDER, ColorDescriptor.Kind.BACKGROUND));

return ArrayUtil.toObjectArray(descriptors, ColorDescriptor.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
import com.intellij.diff.tools.simple.SimpleThreesideDiffViewer;
import com.intellij.diff.tools.util.base.HighlightPolicy;
import com.intellij.diff.tools.util.base.IgnorePolicy;
import com.intellij.diff.util.DiffLineSeparatorRenderer;
import com.intellij.diff.util.DiffUtil;
import com.intellij.diff.util.TextDiffTypeFactory.TextDiffTypeImpl;
import com.intellij.diff.util.ThreeSide;
import com.intellij.openapi.diff.DiffBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.FoldRegion;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.*;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.project.Project;
Expand All @@ -58,17 +62,12 @@
*/
class DiffPreviewPanel implements PreviewPanel {
private final JPanel myPanel;
private final SimpleThreesideDiffViewer myViewer;
private final MyViewer myViewer;

private final EventDispatcher<ColorAndFontSettingsListener> myDispatcher = EventDispatcher.create(ColorAndFontSettingsListener.class);

public DiffPreviewPanel() {
myViewer = new SimpleThreesideDiffViewer(new SampleContext(), new SampleRequest()) {
@Override
protected boolean forceRediffSynchronously() {
return true;
}
};
myViewer = new MyViewer();
myViewer.init();

for (ThreeSide side : ThreeSide.values()) {
Expand All @@ -94,11 +93,14 @@ public void updateView() {
for (SimpleThreesideDiffChange change : changes) {
change.reinstallHighlighters();
}
myViewer.repaint();
}

public void setColorScheme(final EditorColorsScheme highlighterSettings) {
for (EditorEx editorEx : myViewer.getEditors()) {
editorEx.setColorsScheme(highlighterSettings);
editorEx.setColorsScheme(editorEx.createBoundColorSchemeDelegate(highlighterSettings));
editorEx.getColorsScheme().setAttributes(EditorColors.FOLDED_TEXT_ATTRIBUTES, null);
editorEx.reinitSettings();
}
}

Expand Down Expand Up @@ -133,6 +135,8 @@ public SampleContext() {
TextDiffSettings settings = new TextDiffSettings();
settings.setHighlightPolicy(HighlightPolicy.BY_WORD);
settings.setIgnorePolicy(IgnorePolicy.IGNORE_WHITESPACES);
settings.setContextRange(2);
settings.setExpandByDefault(false);
putUserData(TextDiffSettings.KEY, settings);
}

Expand Down Expand Up @@ -171,7 +175,10 @@ private EditorMouseListener(@NotNull ThreeSide side) {

@Override
public void mouseMoved(EditorMouseEvent e) {
if (getChange(mySide, e) != null) EditorUtil.setHandCursor(e.getEditor());
int line = getLineNumber(mySide, e);
if (getChange(mySide, line) != null || getFoldRegion(mySide, line) != null) {
EditorUtil.setHandCursor(e.getEditor());
}
}
}

Expand All @@ -184,34 +191,41 @@ private EditorClickListener(@NotNull ThreeSide side) {

@Override
public void mouseClicked(EditorMouseEvent e) {
selectChange(getChange(mySide, e));
selectColorForLine(mySide, getLineNumber(mySide, e));
}

@Override
public void caretPositionChanged(CaretEvent e) {
selectChange(getChange(mySide, e.getNewPosition().line));
selectColorForLine(mySide, e.getNewPosition().line);
}
}

private void selectChange(@Nullable SimpleThreesideDiffChange change) {
if (change == null) return;
TextDiffTypeImpl diffType = ObjectUtils.tryCast(change.getDiffType(), TextDiffTypeImpl.class);
if (diffType != null) {
myDispatcher.getMulticaster().selectionInPreviewChanged(diffType.getKey().getExternalName());
private void selectColorForLine(@NotNull ThreeSide side, int line) {
SimpleThreesideDiffChange change = getChange(side, line);
if (change != null) {
TextDiffTypeImpl diffType = ObjectUtils.tryCast(change.getDiffType(), TextDiffTypeImpl.class);
if (diffType != null) {
myDispatcher.getMulticaster().selectionInPreviewChanged(diffType.getKey().getExternalName());
}
return;
}

FoldRegion region = getFoldRegion(side, line);
if (region != null) {
myDispatcher.getMulticaster().selectionInPreviewChanged(DiffLineSeparatorRenderer.BACKGROUND.getExternalName());
return;
}
}

@Nullable
private SimpleThreesideDiffChange getChange(ThreeSide side, EditorMouseEvent e) {
private int getLineNumber(@NotNull ThreeSide side, EditorMouseEvent e) {
EditorEx editor = myViewer.getEditor(side);
LogicalPosition logicalPosition = editor.xyToLogicalPosition(e.getMouseEvent().getPoint());
int offset = editor.logicalPositionToOffset(logicalPosition);
int line = editor.getDocument().getLineNumber(offset);
return getChange(side, line);
return editor.getDocument().getLineNumber(offset);
}

@Nullable
private SimpleThreesideDiffChange getChange(ThreeSide side, int line) {
private SimpleThreesideDiffChange getChange(@NotNull ThreeSide side, int line) {
for (SimpleThreesideDiffChange change : myViewer.getChanges()) {
int startLine = change.getStartLine(side);
int endLine = change.getEndLine(side);
Expand All @@ -220,6 +234,19 @@ private SimpleThreesideDiffChange getChange(ThreeSide side, int line) {
return null;
}

@Nullable
private FoldRegion getFoldRegion(@NotNull ThreeSide side, int line) {
EditorEx editor = myViewer.getEditor(side);
DocumentEx document = editor.getDocument();
for (FoldRegion region : editor.getFoldingModel().getAllFoldRegions()) {
if (region.isExpanded()) continue;
int line1 = document.getLineNumber(region.getStartOffset());
int line2 = document.getLineNumber(region.getEndOffset());
if (line1 <= line && line <= line2) return region;
}
return null;
}

@Override
public void blinkSelectedHighlightType(final Object selected) {
}
Expand All @@ -234,4 +261,17 @@ public void disposeUIResources() {
public SimpleThreesideDiffViewer testGetViewer() {
return myViewer;
}

private static class MyViewer extends SimpleThreesideDiffViewer {
public MyViewer() {super(new SampleContext(), new SampleRequest());}

@Override
protected boolean forceRediffSynchronously() {
return true;
}

public void repaint() {
myPanel.repaint();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ private static DiffContent createContent(@NotNull String text, @NotNull FileType
" void bar() {\n" +
"\n" +
" }\n" +
"}";
"}\n" +
"\n" +
"\n";
@NonNls private static final String CENTER_TEXT = "class MyClass {\n" +
" int value;\n" +
"\n" +
Expand All @@ -85,7 +87,9 @@ private static DiffContent createContent(@NotNull String text, @NotNull FileType
" void bar() {\n" +
"\n" +
" }\n" +
"}";
"}\n" +
"\n" +
"\n";
@NonNls private static final String RIGHT_TEXT = "class MyClass {\n" +
" long value;\n" +
"\n" +
Expand All @@ -98,5 +102,7 @@ private static DiffContent createContent(@NotNull String text, @NotNull FileType
" void bar() {\n" +
" }\n" +
"\n" +
"}";
"}\n" +
"\n" +
"\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ options.general.color.descriptor.vcs.log.refs.leaf=VCS Log//Common//Leaf
options.general.color.descriptor.vcs.log.refs.branch=VCS Log//Common//Branch
options.general.color.descriptor.vcs.log.refs.branch.tag=VCS Log//Common//Branch tag
options.general.color.descriptor.vcs.log.refs.tag=VCS Log//Common//Tag
options.general.color.descriptor.vcs.diff.type.tag.prefix=Changed lines//
options.general.color.descriptor.vcs.diff.separator.background=Folded unchanged fragments//Background
options.general.color.descriptor.vcs.diff.separator.top_border=Folded unchanged fragments//Top border
options.general.color.descriptor.vcs.diff.separator.bottom_border=Folded unchanged fragments//Bottom border
options.general.color.descriptor.tearline=Editor//Tear line
options.general.color.descriptor.tearline.selected=Editor//Tear line selection
options.general.color.descriptor.separator.above=Editor//Separator line above
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public DiffContent[] createContents() {
" def bar(self, a, b)\n" +
"\n" +
" print a\n" +
" print b";
" print b\n" +
"\n" +
"\n" +
"\n";
@NonNls private static final String CENTER_TEXT = "class MyClass\n" +
" value = 123\n" +
"\n" +
Expand All @@ -57,7 +60,10 @@ public DiffContent[] createContents() {
" def bar(self, a, b)\n" +
"\n" +
" print a\n" +
" print b";
" print b\n" +
"\n" +
"\n" +
"\n";
@NonNls private static final String RIGHT_TEXT = "class MyClass\n" +
" value = -123\n" +
"\n" +
Expand All @@ -71,5 +77,8 @@ public DiffContent[] createContents() {
" def bar(self, a, b)\n" +
" print a\n" +
"\n" +
" print b";
" print b\n" +
"\n" +
"\n" +
"\n";
}

0 comments on commit f8de2a5

Please sign in to comment.