Skip to content

Commit 0945ae7

Browse files
authored
Merge pull request #106 from CodesAway/dev-0.12.0
Dev 0.12.0
2 parents 6d2cd8b + 71951c9 commit 0945ae7

File tree

16 files changed

+421
-71
lines changed

16 files changed

+421
-71
lines changed

BEXCodeCompare/pom.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>info.codesaway</groupId>
66
<artifactId>bex</artifactId>
7-
<version>0.11.0</version>
7+
<version>0.12.0</version>
88
<name>Be Enhanced Code Compare (BEϽC)</name>
99
<description>An enhanced code compare, utilities, and code matching</description>
1010
<properties>
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>info.codesaway</groupId>
2424
<artifactId>regexplus</artifactId>
25-
<version>1.2.0</version>
25+
<version>2.0.0</version>
2626
</dependency>
2727
<dependency>
2828
<groupId>com.github.spotbugs</groupId>
@@ -111,7 +111,10 @@
111111
<plugin>
112112
<groupId>org.apache.maven.plugins</groupId>
113113
<artifactId>maven-javadoc-plugin</artifactId>
114-
<version>2.9.1</version>
114+
<version>3.2.0</version>
115+
<configuration>
116+
<source>8</source>
117+
</configuration>
115118
<executions>
116119
<execution>
117120
<id>attach-javadocs</id>

BEXCodeCompare/src/main/java/info/codesaway/bex/ImmutableIntRangeMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ private int getIndex(final int key) {
170170
*
171171
* @param key the value to be searched for
172172
* @return index of the search key, if it is contained in the array;
173-
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
173+
* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
174174
* <i>insertion point</i> is defined as the point at which the
175175
* key would be inserted into the array: the index of the first
176-
* element greater than the key, or <tt>a.length</tt> if all
176+
* element greater than the key, or <code>a.length</code> if all
177177
* elements in the array are less than the specified key. Note
178178
* that this guarantees that the return value will be &gt;= 0 if
179179
* and only if the key is found.

BEXCodeCompare/src/main/java/info/codesaway/bex/IntBEXRange.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public static IntBEXRange closedOpen(final int start, final int end) {
8080
return new IntBEXRange(start, end, false);
8181
}
8282

83+
/**
84+
* Closed range containing a single value
85+
* @param value the value
86+
* @return a closed range containing a single value
87+
* @since 0.12
88+
* @see #closed(int, int)
89+
*/
90+
public static IntBEXRange singleton(final int value) {
91+
return closed(value, value);
92+
}
93+
8394
@Override
8495
public boolean hasInclusiveStart() {
8596
return this.hasInclusiveStart;

BEXCodeCompare/src/main/java/info/codesaway/bex/diff/DiffHelper.java

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package info.codesaway.bex.diff;
22

3+
import static info.codesaway.bex.BEXPairs.bexPair;
34
import static info.codesaway.bex.BEXPairs.mapGet;
45
import static info.codesaway.bex.BEXSide.BEX_SIDES;
56
import static info.codesaway.bex.BEXSide.LEFT;
67
import static info.codesaway.bex.BEXSide.RIGHT;
8+
import static info.codesaway.bex.IntBEXRange.closed;
79
import static info.codesaway.bex.diff.BasicDiffType.REPLACEMENT_BLOCK;
810
import static info.codesaway.bex.util.BEXUtilities.firstNonNull;
911
import static info.codesaway.bex.util.BEXUtilities.not;
@@ -38,6 +40,7 @@
3840
import info.codesaway.bex.BEXPairValue;
3941
import info.codesaway.bex.BEXSide;
4042
import info.codesaway.bex.IntPair;
43+
import info.codesaway.bex.IntRange;
4144
import info.codesaway.bex.diff.patience.FrequencyCount;
4245
import info.codesaway.bex.diff.patience.PatienceMatch;
4346
import info.codesaway.bex.diff.substitution.RefactoringDiffType;
@@ -268,35 +271,35 @@ public static List<DiffEdit> handleImports(final List<DiffEdit> diff) {
268271
.filter(DiffWithIndex::isInsertOrDelete)
269272
.filter(d -> d.getDiffEdit().getText().trim().startsWith("import"))
270273
.collect(toList());
271-
274+
272275
// Group imports by classname
273276
Map<String, List<DiffWithIndex>> importsByClassName = new HashMap<>();
274277
Map<DiffWithIndex, MatchResult> matchResults = new HashMap<>();
275-
278+
276279
for (DiffWithIndex possibleImport : possibleImports) {
277280
Matcher matcher = IMPORT_MATCHER.get().reset(possibleImport.getDiffEdit().getText());
278-
281+
279282
if (!matcher.find()) {
280283
continue;
281284
}
282-
285+
283286
matchResults.put(possibleImport, matcher.toMatchResult());
284287
importsByClassName.computeIfAbsent(matcher.group("class"), k -> new ArrayList<>()).add(possibleImport);
285288
}
286-
289+
287290
// Check results
288291
for (Entry<String, List<DiffWithIndex>> entry : importsByClassName.entrySet()) {
289292
List<DiffWithIndex> list = entry.getValue();
290293
if (list.size() != 2) {
291294
continue;
292295
}
293-
296+
294297
DiffWithIndex firstDiff = list.get(0);
295298
DiffWithIndex secondDiff = list.get(1);
296-
299+
297300
DiffWithIndex left = null;
298301
DiffWithIndex right = null;
299-
302+
300303
if (firstDiff.hasLeftLine()) {
301304
if (secondDiff.hasRightLine()) {
302305
left = firstDiff;
@@ -306,21 +309,21 @@ public static List<DiffEdit> handleImports(final List<DiffEdit> diff) {
306309
left = secondDiff;
307310
right = firstDiff;
308311
}
309-
312+
310313
if (left != null && right != null) {
311314
ImportSameClassnameDiffType diffType = determineImportSameClassnameDiffType(matchResults.get(left),
312315
matchResults.get(right), true);
313-
316+
314317
// System.out.println(entry);
315-
318+
316319
if (diffType != null) {
317320
DiffEdit diffEdit = new DiffEdit(diffType, left.getLeftLine(), right.getRightLine());
318321
diff.set(left.getIndex(), diffEdit);
319322
diff.set(right.getIndex(), diffEdit);
320323
}
321324
}
322325
}
323-
326+
324327
return diff;
325328
}*/
326329

@@ -904,6 +907,58 @@ private static List<DiffEdit> sort(final List<DiffEdit> diff) {
904907
return results;
905908
}
906909

910+
/**
911+
* Determines the range which encloses all the lines of the specified DiffUnit
912+
*
913+
* <p>If the DiffUnit does not consist of sequential lines, the range will enclose all the lines,
914+
* even though each value in the range will not have a corresponding line in the DiffUnit</p>
915+
*
916+
* @param diffUnit the DiffUnit
917+
* @return the range of lines (left / right) in the specified DiffUnit
918+
* @since 0.12
919+
*/
920+
public static BEXPair<IntRange> determineEnclosedRange(final DiffUnit diffUnit) {
921+
return determineEnclosedRange(diffUnit.getEdits());
922+
}
923+
924+
/**
925+
* Determines the range which encloses all the lines of the specified edits
926+
*
927+
* <p>If the edits do not consist of sequential lines, the range will enclose all the lines,
928+
* even though each value in the range will not have a corresponding line in the edits</p>
929+
*
930+
* @param diffEdits the edits
931+
* @return the range of lines (left / right) in the specified edits
932+
* @since 0.12
933+
*/
934+
public static BEXPair<IntRange> determineEnclosedRange(final Collection<DiffEdit> diffEdits) {
935+
int minLeft = diffEdits.stream()
936+
.filter(DiffEdit::hasLeftLine)
937+
.mapToInt(DiffEdit::getLeftLineNumber)
938+
.min()
939+
.orElse(-1);
940+
941+
int maxLeft = diffEdits.stream()
942+
.filter(DiffEdit::hasLeftLine)
943+
.mapToInt(DiffEdit::getLeftLineNumber)
944+
.max()
945+
.orElse(-1);
946+
947+
int minRight = diffEdits.stream()
948+
.filter(DiffEdit::hasRightLine)
949+
.mapToInt(DiffEdit::getRightLineNumber)
950+
.min()
951+
.orElse(-1);
952+
953+
int maxRight = diffEdits.stream()
954+
.filter(DiffEdit::hasRightLine)
955+
.mapToInt(DiffEdit::getRightLineNumber)
956+
.max()
957+
.orElse(-1);
958+
959+
return bexPair(closed(minLeft, maxLeft), closed(minRight, maxRight));
960+
}
961+
907962
/**
908963
* Combines consecutive DiffEdit to form DiffBlock when possible
909964
*

BEXCodeCompare/src/main/java/info/codesaway/bex/matching/BEXMatcher.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,10 @@ public String replaceAll(final Function<BEXMatchResult, String> replacementFunct
852852
* replaced in the result by the replacement string. The replacement string
853853
* may contain references to captured subsequences as in the {@link #appendReplacement appendReplacement} method.</p>
854854
*
855-
* <p>Given the pattern <tt>dog</tt>, the input
856-
* <tt>"zzzdogzzzdogzzz"</tt>, and the replacement string <tt>"cat"</tt>, an
855+
* <p>Given the pattern <code>dog</code>, the input
856+
* <code>"zzzdogzzzdogzzz"</code>, and the replacement string <code>"cat"</code>, an
857857
* invocation of this method on a matcher for that expression would yield
858-
* the string <tt>"zzzcatzzzdogzzz"</tt>.</p>
858+
* the string <code>"zzzcatzzzdogzzz"</code>.</p>
859859
*
860860
* <p>Invoking this method changes this matcher's state. If the matcher
861861
* is to be used in further matching operations then it should first be
@@ -977,7 +977,7 @@ && isWordCharacter(nextChar(replacement, cursor + 1))) {
977977
* <p>It reads characters from the input sequence, starting at the append
978978
* position, and appends them to the given string buffer. It stops after
979979
* reading the last character preceding the previous match, that is, the
980-
* character at index {@link #start()}&nbsp;<tt>-</tt>&nbsp;<tt>1</tt>.</p>
980+
* character at index {@link #start()}&nbsp;<code>-</code>&nbsp;<code>1</code>.</p>
981981
* </li>
982982
*
983983
* <li>
@@ -996,8 +996,8 @@ && isWordCharacter(nextChar(replacement, cursor + 1))) {
996996
*
997997
* <p>This method is intended to be used in a loop together with the {@link #appendTail appendTail} and
998998
* {@link #find find} methods. The
999-
* following code, for example, writes <tt>one dog two dogs in the
1000-
* yard</tt> to the standard-outputSyntax stream:</p>
999+
* following code, for example, writes <code>one dog two dogs in the
1000+
* yard</code> to the standard-outputSyntax stream:</p>
10011001
*
10021002
* <blockquote>
10031003
*

BEXCodeCompare/src/main/java/info/codesaway/bex/matching/BEXMatchingLanguage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public enum BEXMatchingLanguage implements MatchingLanguage {
2929
* SQL Matching language
3030
* @since 0.11
3131
*/
32-
SQL(BEXMatchingUtilities::parseSQLTextStates, "@", true, bexPair("BEGIN", "END")),
32+
SQL(BEXMatchingUtilities::parseSQLTextStates, "@#$", true, bexPair("BEGIN", "END")),
3333

3434
// End of enum
3535
;
@@ -92,11 +92,11 @@ public MatchingDelimiterState findEndDelimiter(final BEXPair<String> lastDelimit
9292

9393
BiPredicate<String, String> equals = this.hasCaseInsensitiveDelimiters
9494
? String::equalsIgnoreCase
95-
: String::equals;
95+
: String::equals;
9696

9797
MatchingDelimiterResult result = lastDelimiter != null && equals.test(s, lastDelimiter.getRight())
9898
? MatchingDelimiterResult.FOUND
99-
: MatchingDelimiterResult.MISMATCHED;
99+
: MatchingDelimiterResult.MISMATCHED;
100100

101101
return new MatchingDelimiterState(result, s);
102102
}

BEXCodeCompare/src/test/java/info/codesaway/bex/diff/DiffHelperTests.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package info.codesaway.bex.diff;
22

33
import static com.google.common.collect.Maps.immutableEntry;
4+
import static info.codesaway.bex.BEXPairs.bexPair;
5+
import static info.codesaway.bex.IntBEXRange.closed;
46
import static info.codesaway.bex.diff.BasicDiffType.DELETE;
57
import static info.codesaway.bex.diff.BasicDiffType.EQUAL;
68
import static info.codesaway.bex.diff.BasicDiffType.INSERT;
79
import static info.codesaway.bex.diff.BasicDiffType.MOVE_LEFT;
810
import static info.codesaway.bex.diff.BasicDiffType.MOVE_RIGHT;
11+
import static org.assertj.core.api.Assertions.assertThat;
912
import static org.junit.jupiter.api.Assertions.assertEquals;
1013

1114
import java.util.Arrays;
1215
import java.util.List;
1316
import java.util.Map.Entry;
1417

18+
import org.junit.jupiter.api.Disabled;
1519
import org.junit.jupiter.api.Test;
1620
import org.junit.jupiter.params.ParameterizedTest;
1721
import org.junit.jupiter.params.provider.MethodSource;
@@ -20,6 +24,9 @@
2024
import com.google.common.collect.Streams;
2125
import com.google.common.primitives.ImmutableIntArray;
2226

27+
import info.codesaway.bex.BEXPair;
28+
import info.codesaway.bex.IntBEXPair;
29+
import info.codesaway.bex.IntRange;
2330
import info.codesaway.bex.diff.myers.MyersLinearDiff;
2431
import info.codesaway.bex.diff.patience.PatienceDiff;
2532
import info.codesaway.bex.diff.patience.PatienceMatch;
@@ -119,4 +126,38 @@ public void testCombineToDiffBlocks() {
119126

120127
assertEquals(expected, diffUnits);
121128
}
129+
130+
@Test
131+
public void testDetermineEnclosedRange() {
132+
List<DiffLine> leftLines = ImmutableList.of(new DiffLine(0, "a"), new DiffLine(1, "b"), new DiffLine(2, "c"));
133+
List<DiffLine> rightLines = ImmutableList.of(new DiffLine(3, "a"), new DiffLine(4, "b"), new DiffLine(5, "c"));
134+
135+
List<DiffEdit> diff = ImmutableList.of(
136+
new DiffEdit(MOVE_LEFT, leftLines.get(0), rightLines.get(0)),
137+
new DiffEdit(MOVE_LEFT, leftLines.get(1), rightLines.get(1)),
138+
new DiffEdit(MOVE_LEFT, leftLines.get(2), rightLines.get(2)));
139+
140+
List<DiffUnit> diffUnits = DiffHelper.combineToDiffBlocks(diff);
141+
142+
DiffUnit diffUnit = diffUnits.get(0);
143+
144+
BEXPair<IntRange> enclosedRange = DiffHelper.determineEnclosedRange(diffUnit);
145+
BEXPair<IntRange> expectedRange = bexPair(closed(0, 2), closed(3, 5));
146+
assertThat(enclosedRange).isEqualTo(expectedRange);
147+
}
148+
149+
@Test
150+
@Disabled("Not sure if I plan or need to add functionality for consecutive ranges")
151+
public void testHasConsecutiveRanges() {
152+
IntRange left1 = closed(0, 2);
153+
IntRange left2 = closed(3, 5);
154+
155+
IntRange right1 = closed(1, 5);
156+
IntRange right2 = closed(6, 10);
157+
158+
IntBEXPair line1 = IntBEXPair.of(left1.getEnd(), right1.getEnd());
159+
IntBEXPair line2 = IntBEXPair.of(left2.getStart(), right2.getStart());
160+
161+
System.out.println(DiffHelper.isConsecutive(line1, line2, false));
162+
}
122163
}

0 commit comments

Comments
 (0)