Skip to content

Commit 0db0fdd

Browse files
committed
refactor: directly pass the time to popIfChain in the diff parser
1 parent a8065d9 commit 0db0fdd

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/main/java/org/variantsync/diffdetective/variation/diff/DiffType.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.variantsync.diffdetective.variation.diff;
22

33
import org.apache.commons.lang3.function.FailableConsumer;
4-
import org.tinylog.Logger;
4+
import org.apache.commons.lang3.function.FailableRunnable;
55

66
import java.util.Optional;
77
import java.util.Set;
8-
import java.util.function.Consumer;
98

109
/**
1110
* Type of change made to an artifact (e.g., a line of text in a text-based diff).
@@ -70,8 +69,9 @@ public static Optional<DiffType> thatExistsOnlyAtAll(final Set<Time> t) {
7069
* exists before and after the edit (DiffType = NON).
7170
* @param ifExistsBefore Procedure to run if the edited artifact existed before the edit (DiffType != ADD).
7271
* @param ifExistsAfter Procedure to run if the edited artifact exists after the edit (DiffType != REM).
72+
* @throws T iff {@code ifExistsBefore} or {@code ifExistsAfter} throws {@code E}
7373
*/
74-
public void forAllTimesOfExistence(final Runnable ifExistsBefore, final Runnable ifExistsAfter) {
74+
public <T extends Throwable> void forAllTimesOfExistence(final FailableRunnable<T> ifExistsBefore, final FailableRunnable<T> ifExistsAfter) throws T {
7575
if (this != DiffType.ADD) {
7676
ifExistsBefore.run();
7777
}
@@ -84,8 +84,9 @@ public void forAllTimesOfExistence(final Runnable ifExistsBefore, final Runnable
8484
* Runs the given procedure for any time at which elements with this diff type exist.
8585
* The consumer will be invoked at least once and at most twice.
8686
* @param t Procedure to run for each time elements with this diff type exist.
87+
* @throws T iff {@code t} throws {@code E}
8788
*/
88-
public void forAllTimesOfExistence(final Consumer<Time> t) {
89+
public <T extends Throwable> void forAllTimesOfExistence(final FailableConsumer<Time, T> t) throws T {
8990
forAllTimesOfExistence(
9091
() -> t.accept(Time.BEFORE),
9192
() -> t.accept(Time.AFTER)

src/main/java/org/variantsync/diffdetective/variation/diff/parse/VariationDiffParser.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ private void parseLine(
328328

329329
// Do not create a node for ENDIF, but update the line numbers of the closed if-chain
330330
// and remove that if-chain from the relevant stacks.
331-
diffType.forAllTimesOfExistence(beforeStack, afterStack, stack ->
332-
popIfChain(stack, fromLine, line)
331+
diffType.forAllTimesOfExistence(time ->
332+
popIfChain(time, fromLine, line)
333333
);
334334
} else if (options.collapseMultipleCodeLines()
335335
&& annotation.type() == AnnotationType.None
@@ -357,20 +357,22 @@ private void parseLine(
357357
}
358358

359359
/**
360-
* Pop {@code stack} until an IF node is popped.
360+
* Pop the stack until an IF node is popped.
361361
* If there were ELSEs or ELIFs between an IF and an ENDIF, they were placed on the stack and
362362
* have to be popped now. The {@link DiffNode#getToLine() end line numbers} are adjusted
363363
*
364-
* @param stack the stack which should be popped
364+
* @param time which stack to pop the if chain (i.e., {@link beforeStack} or {@link afterStack})
365365
* @param elseLineNumber the first line of the else which causes this IF to be popped
366366
* @param line the line containing the endif
367367
* @throws DiffParseException if {@code stack} doesn't contain an IF node
368368
*/
369369
private void popIfChain(
370-
Stack<DiffNode<DiffLinesLabel>> stack,
370+
Time time,
371371
DiffLineNumber elseLineNumber,
372372
LogicalLine line
373373
) throws DiffParseException {
374+
Stack<DiffNode<DiffLinesLabel>> stack = time.match(beforeStack, afterStack);
375+
374376
DiffLineNumber previousLineNumber = elseLineNumber;
375377
do {
376378
DiffNode<DiffLinesLabel> annotation = stack.peek();
@@ -381,19 +383,19 @@ private void popIfChain(
381383
for (int i = 0; i < line.getLines().size(); i++) {
382384
list.add(line.getLines().get(i).content());
383385
}
384-
annotation.setEndIf(list, stack == beforeStack ? Time.BEFORE : Time.AFTER);
386+
annotation.setEndIf(list, time);
385387
}
386388

387389
// Set the line number of now closed annotations to the beginning of the
388390
// following annotation.
389391
annotation.setToLine(new DiffLineNumber(
390392
Math.max(previousLineNumber.inDiff(), annotation.getToLine().inDiff()),
391-
stack == beforeStack
392-
? previousLineNumber.beforeEdit()
393-
: annotation.getToLine().beforeEdit(),
394-
stack == afterStack
395-
? previousLineNumber.afterEdit()
396-
: annotation.getToLine().afterEdit()
393+
time.match(
394+
previousLineNumber.beforeEdit(),
395+
annotation.getToLine().beforeEdit()),
396+
time.match(
397+
annotation.getToLine().afterEdit(),
398+
previousLineNumber.afterEdit())
397399
));
398400

399401
previousLineNumber = annotation.getFromLine();

0 commit comments

Comments
 (0)