int permutedStart = statementRanges.start(j);
int permutedEnd = statementRanges.end(j);
int originalStart = originalStatementRanges.start(reorderedIndices[j]);
int originalEnd = originalStatementRanges.end(reorderedIndices[j]);
statementShifts.put(new Range(originalStart, originalEnd),
new Range(permutedStart, permutedEnd));
}
Range[] oldStatementRanges = statementShifts.keySet().toArray(new Range[0]);
Arrays.sort(oldStatementRanges, Range.SOURCE_ORDER_COMPARATOR);
Range[] oldExpressionRanges = sourceInfoMap.keySet().toArray(new Range[0]);
Arrays.sort(oldExpressionRanges, Range.SOURCE_ORDER_COMPARATOR);
// iterate over expression ranges and shift
Map<Range, SourceInfo> updatedInfoMap = new HashMap<Range, SourceInfo>();
Range entireProgram =
new Range(0, oldStatementRanges[oldStatementRanges.length - 1].getEnd());
for (int i = 0, j = 0; j < oldExpressionRanges.length; j++) {
Range oldExpression = oldExpressionRanges[j];
if (oldExpression.equals(entireProgram)) {
updatedInfoMap.put(oldExpression, sourceInfoMap.get(oldExpression));
continue;
}
if (!oldStatementRanges[i].contains(oldExpressionRanges[j])) {
// expression should fall in the next statement
i++;
assert oldStatementRanges[i].contains(oldExpressionRanges[j]);
}
Range oldStatement = oldStatementRanges[i];
Range newStatement = statementShifts.get(oldStatement);
int shift = newStatement.getStart() - oldStatement.getStart();
Range oldExpressionRange = oldExpressionRanges[j];
Range newExpressionRange = new Range(oldExpressionRange.getStart() + shift,
oldExpressionRange.getEnd() + shift);
updatedInfoMap.put(newExpressionRange, sourceInfoMap.get(oldExpressionRange));
}
sourceInfoMap = new JsSourceMap(updatedInfoMap);