11package org .variantsync .vevos .extraction ;
22
3+ import org .tinylog .Logger ;
34import org .variantsync .diffdetective .util .Assert ;
45import org .variantsync .diffdetective .util .LineRange ;
56
1213public class FileGT implements Iterable <LineAnnotation >, Serializable {
1314 // The name of the file (its relative path from the root of the repo)
1415 protected final String file ;
15- // List of annotation for each line
16- private final ArrayList <LineAnnotation > annotations ;
1716 // A matching of this file's lines to counterparts associated with the same commit
1817 // i.e., the matching line numbers before or after the changes have been applied
1918 // a match is -1 if there is no counterpart
2019 protected final ArrayList <Integer > matching ;
21- // The set of variables occurring in the annotations of this file
22- private final Set <String > variables ;
2320 // Set of annotation block starts and ends
2421 protected final HashSet <Integer > blockEnds ;
2522 protected final HashSet <Integer > blockStarts ;
23+ // List of annotation for each line
24+ private final ArrayList <LineAnnotation > annotations ;
25+ // The set of variables occurring in the annotations of this file
26+ private final Set <String > variables ;
2627 // We can only use the before mapping until its being mutated
2728 protected boolean consumed ;
2829
@@ -83,7 +84,8 @@ protected LineAnnotation insert(int index, LineAnnotation annotation) {
8384
8485 /**
8586 * Set the match for the given line number.
86- * @param lineNumber The line number in the current version of the file
87+ *
88+ * @param lineNumber The line number in the current version of the file
8789 * @param matchedLine The matching line number in the counterpart version of the file
8890 */
8991 protected void setMatching (int lineNumber , int matchedLine ) throws MatchingException {
@@ -92,7 +94,10 @@ protected void setMatching(int lineNumber, int matchedLine) throws MatchingExcep
9294 return ;
9395 }
9496 if (this .matching .get (lineNumber ) != -1 && this .matching .get (lineNumber ) != matchedLine ) {
95- throw new MatchingException ("line number mismatch for " + this .file + " -- " + lineNumber
97+ // TODO: Handle this case
98+ // throw new MatchingException("line number mismatch for " + this.file + " -- " + lineNumber
99+ // + " : (" + this.matching.get(lineNumber) + " vs. " + matchedLine + ")");
100+ Logger .error ("line number mismatch for " + this .file + " -- " + lineNumber
96101 + " : (" + this .matching .get (lineNumber ) + " vs. " + matchedLine + ")" );
97102 }
98103 this .matching .set (lineNumber , matchedLine );
@@ -161,7 +166,8 @@ public Mutable insert(LineAnnotation line) {
161166
162167 /**
163168 * Set the matching of line numbers between current file version and counterpart file version.
164- * @param currentRange The range of lines in the current file version
169+ *
170+ * @param currentRange The range of lines in the current file version
165171 * @param counterpartRange The range of lines in the counterpart file version
166172 */
167173 public void setMatching (LineRange currentRange , LineRange counterpartRange ) throws MatchingException {
@@ -174,9 +180,9 @@ public void setMatching(LineRange currentRange, LineRange counterpartRange) thro
174180
175181 if (matchedLine != -1 ) {
176182 // If there is a match, the matched regions must have the same size
177- if (endNumber - lineNumber != matchEnd - matchedLine ) {
183+ if (endNumber - lineNumber != matchEnd - matchedLine ) {
178184 throw new MatchingException ("line number mismatch for file" + this .file + " -- ; " +
179- "ranges have different size " + (endNumber - lineNumber ) + " : " + (matchEnd - matchedLine ));
185+ "ranges have different size " + (endNumber - lineNumber ) + " : " + (matchEnd - matchedLine ));
180186 }
181187 // Set the matches
182188 for (; lineNumber < currentRange .toExclusive (); lineNumber ++, matchedLine ++) {
@@ -273,7 +279,7 @@ private static String csvMatchingLines(Complete complete) {
273279 private static ArrayList <BlockAnnotation > aggregateBlocks (Complete complete ) {
274280 ArrayList <BlockAnnotation > blocks = new ArrayList <>();
275281 // The root annotation is always true and covers all lines
276- BlockAnnotation rootBlock = new BlockAnnotation (1 , complete .size (), "True" , "True" );
282+ BlockAnnotation rootBlock = new BlockAnnotation (1 , complete .size (), new FeatureMapping ( "True" ), new PresenceCondition ( "True" ), new FeatureMapping ( "True" ), new PresenceCondition ( "True" ) );
277283
278284 LinkedList <BlockAnnotation > blockStack = new LinkedList <>();
279285 blockStack .push (rootBlock );
@@ -282,24 +288,36 @@ private static ArrayList<BlockAnnotation> aggregateBlocks(Complete complete) {
282288
283289 if (blockStack .isEmpty ()) {
284290 // Push a new block onto the stack
285- blockStack .push (new BlockAnnotation (line .lineNumber (), line .lineNumber (), line .featureMapping (), line .presenceCondition ()));
286- continue ;
291+ // blockStack.push(new BlockAnnotation(line.lineNumber(), line.lineNumber(), line.featureMappingBefore(),
292+ // line.presenceConditionBefore(), line.featureMappingAfter(), line.presenceConditionAfter()));
293+ // continue;
294+ throw new IllegalStateException ();
287295 }
288296
297+ // Does the next line have a different annotation than the current block?
298+ boolean annotationChange = !blockStack .peek ().annotationEquals (line ) && !blockStack .peek ().equals (rootBlock );
299+
289300 // new block, we have to unwind the stack in reverse order to find all completed blocks
290- if (complete . blockEnds . contains ( line . lineNumber ()) ) {
301+ if (annotationChange ) {
291302 // The current line is nested in retrieved block
292303 // Collect a completed block
293304 BlockAnnotation block = blockStack .pop ();
294305 block .setLineEndInclusive (line .lineNumber () - 1 );
295306 blocks .add (block );
296307 }
297308
309+ if (blockStack .peek () == null ) {
310+ continue ;
311+ }
312+
313+ // Does the next line have a different annotation than the current block?
314+ annotationChange = !blockStack .peek ().annotationEquals (line );
315+
298316 Assert .assertTrue (blockStack .peekFirst () != null , () -> "%s\n Problem while processing line %d of file %s" .formatted (complete .toString (), line .lineNumber (), complete .file ));
299317 // If the current line is in a new block
300- if (complete . blockStarts . contains ( line . lineNumber ()) ) {
318+ if (annotationChange ) {
301319 // Push a new block onto the stack
302- blockStack .push (new BlockAnnotation (line .lineNumber (), line .lineNumber (), line .featureMapping (), line .presenceCondition ()));
320+ blockStack .push (new BlockAnnotation (line .lineNumber (), line .lineNumber (), line .featureMappingBefore (), line .presenceConditionBefore (), line . featureMappingAfter (), line . presenceConditionAfter ()));
303321 }
304322 }
305323 // Unwind the stack fully
@@ -335,7 +353,9 @@ public String csvPCLines() {
335353 *
336354 * @return A String with the line matchings in csv format
337355 */
338- public String csvMatchingLines () { return this .csvMatchingText ; }
356+ public String csvMatchingLines () {
357+ return this .csvMatchingText ;
358+ }
339359
340360 /**
341361 * @return The list of block annotations for this file.
0 commit comments