11package info .codesaway .bex .diff ;
22
3+ import static info .codesaway .bex .BEXPairs .bexPair ;
34import static info .codesaway .bex .BEXPairs .mapGet ;
45import static info .codesaway .bex .BEXSide .BEX_SIDES ;
56import static info .codesaway .bex .BEXSide .LEFT ;
67import static info .codesaway .bex .BEXSide .RIGHT ;
8+ import static info .codesaway .bex .IntBEXRange .closed ;
79import static info .codesaway .bex .diff .BasicDiffType .REPLACEMENT_BLOCK ;
810import static info .codesaway .bex .util .BEXUtilities .firstNonNull ;
911import static info .codesaway .bex .util .BEXUtilities .not ;
3840import info .codesaway .bex .BEXPairValue ;
3941import info .codesaway .bex .BEXSide ;
4042import info .codesaway .bex .IntPair ;
43+ import info .codesaway .bex .IntRange ;
4144import info .codesaway .bex .diff .patience .FrequencyCount ;
4245import info .codesaway .bex .diff .patience .PatienceMatch ;
4346import 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 *
0 commit comments