From f96baafa71592fa3585d98da5074b9e88f8207c3 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 12 Jan 2016 18:14:15 +0300 Subject: [PATCH] vcs: Show History for Selection - more consistent Block logic greedy expand range on damage --- .../diff/SelectedBlockHistoryTest.java | 11 ++++++ .../vcs-impl/src/com/intellij/diff/Block.java | 35 +++++++------------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/diff/SelectedBlockHistoryTest.java b/platform/platform-tests/testSrc/com/intellij/diff/SelectedBlockHistoryTest.java index 1cc8ed0e75b7..2bf94ce765bf 100644 --- a/platform/platform-tests/testSrc/com/intellij/diff/SelectedBlockHistoryTest.java +++ b/platform/platform-tests/testSrc/com/intellij/diff/SelectedBlockHistoryTest.java @@ -182,6 +182,17 @@ public class SelectedBlockHistoryTest extends TestCase { ); } + public void test15() throws FilesTooBigForDiffException { + doTest( + new String[]{"1"}, + new String[]{"0"}, + new String[]{"3"}, + + new String[]{"1", "4"}, + new String[]{"5", "0"}, + new String[]{"3"} + ); + } public void testContent(){ Block block = new Block("0\n1\n2\n3\n4\n5\n6", 3, 7); diff --git a/platform/vcs-impl/src/com/intellij/diff/Block.java b/platform/vcs-impl/src/com/intellij/diff/Block.java index c4574b5383ea..1ad63e247cea 100644 --- a/platform/vcs-impl/src/com/intellij/diff/Block.java +++ b/platform/vcs-impl/src/com/intellij/diff/Block.java @@ -46,8 +46,9 @@ public class Block { @NotNull public Block createPreviousBlock(@NotNull String[] prevContent) { - int startLine = myStart; - int endLine = myEnd; + int start = -1; + int end = -1; + int shift = 0; Diff.Change change = Diff.buildChangesSomehow(prevContent, getSource()); while (change != null) { @@ -56,31 +57,21 @@ public class Block { int endLine1 = startLine1 + change.deleted; int endLine2 = startLine2 + change.inserted; - int shiftStart = startLine2 - startLine1; - int shiftEnd = endLine2 - endLine1; - - if (startLine2 <= myStart) { - startLine = myStart - shiftStart; - } - - if (endLine2 <= myStart) { - startLine = myStart - shiftEnd; - } - - if (startLine2 < myEnd) { - endLine = myEnd - shiftEnd; + if (Math.max(myStart, startLine2) < Math.min(myEnd, endLine2)) { + // ranges intersect + if (startLine2 <= myStart) start = startLine1; + if (endLine2 > myEnd) end = endLine1; } + if (start == -1 && startLine2 > myStart) start = myStart - shift; + if (end == -1 && startLine2 >= myEnd) end = myEnd - shift; + shift += change.inserted - change.deleted; change = change.link; } + if (start == -1) start = myStart - shift; + if (end == -1) end = myEnd - shift; - if (endLine > prevContent.length) { - endLine = prevContent.length; - } - if (startLine < 0) startLine = 0; - if (endLine < startLine) endLine = startLine; - - return new Block(prevContent, startLine, endLine); + return new Block(prevContent, start, end); } @NotNull