*/
public Location getOriginalTextPositionFor(int plainTextPosition) {
if (plainTextPosition < 1) {
throw new RuntimeException("plainTextPosition must be > 0 - its value starts at 1");
}
final Location origPosition = mapping.get(plainTextPosition);
if (origPosition != null) {
//System.out.println("mapping " + plainTextPosition + " to " + origPosition + " [direct]");
return origPosition;
}
int minDiff = Integer.MAX_VALUE;
Location bestMatch = null;
//Integer bestMaybeClosePosition = null;
// algorithm: find the closest lower position
for (Map.Entry<Integer, Location> entry : mapping.entrySet()) {
int maybeClosePosition = entry.getKey();
if (plainTextPosition > maybeClosePosition) {
int diff = plainTextPosition - maybeClosePosition;
if (diff >= 0 && diff < minDiff) {
bestMatch = entry.getValue();
//bestMaybeClosePosition = maybeClosePosition;
minDiff = diff;
}
}
}
if (bestMatch == null) {
throw new RuntimeException("Could not map " + plainTextPosition + " to original position. Mapping: " + mapping);
}
// we assume that when we have found the closest match there's a one-to-one mapping
// in this region, thus we can add 'minDiff' to get the exact position:
//System.out.println("mapping " + plainTextPosition + " to line " + bestMatch.line + ", column " +
// bestMatch.column + "+" + minDiff + ", bestMatch was: " + bestMaybeClosePosition +"=>"+ bestMatch);
return new Location(bestMatch.file, bestMatch.line, bestMatch.column + minDiff);
}